You are on page 1of 11

Q.

1 Find maximum and minimum element in a linked list


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void append()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));

printf("enter node data:");


scanf("%d",&temp->data);

if(head==NULL)
{
head=temp;
}
else
{
struct node *p;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
}
}
void display()
{
struct node *temp;
temp=head;

if(temp==NULL)
{
printf("empty list!!");
}
else
{
while(temp!=NULL)
{
printf("%d",temp->data);
printf("\n");
temp=temp->next;
}
}
}
void maxelement()
{
int max=head->data;
while(head!=NULL)
{
if(head->data>max)
{
max=head->data;
}
head=head->next;
}
printf("max element is: %d ",max);
}
void minelement()
{
int min=0;
while(head!=NULL)
{
if(min>head->data)
{
min=head->data;
}
head=head->next;
}
printf("\nmin element is: %d",min);
}
void main()
{

clrscr();
int size;
printf("size:");
scanf("%d",&size);
for(int i=0;i<size;i++)
{
append();
}
printf("\nlist is:\n");
display();
maxelement();
minelement();
getch();
}
Q4. Write a function to delete a given list.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void append()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));

printf("enter node data:");


scanf("%d",&temp->data);

if(head==NULL)
{
head=temp;
}
else
{
struct node *p;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
}
}
void del(int s)
{
struct node *temp,*p,*q;
temp=(struct node*)malloc(sizeof(struct node));
int loc;
printf("enter location to be deleted");
scanf("%d",&loc);

if(loc>s)
{
printf("invalid location");
}
else
if(loc==1)
{
temp=head;
head=temp->next;
temp->next=NULL;
free(temp);
}
else
{
p=head;
int i=1;
while(i<loc-1)
{
p=p->next;
i++;
}

q=p->next;
p->next=q->next;
q->next=NULL;
free(q);
}

void display()
{
struct node *temp;
temp=head;

if(temp==NULL)
{
printf("empty list!!");
}
else
{
while(temp!=NULL)
{
printf("%d",temp->data);
printf("\n");
temp=temp->next;
}
}
}
void main()
{

clrscr();
int size;
printf("size:");
scanf("%d",&size);
for(int i=0;i<size;i++)
{
append();
}
del(size);
printf("\nnew list:\n");
display();
getch();
}
Q5. Write a program to reverse a linked list with iterative and recursive solution.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct Node
{
int data;
struct Node* next;
};
static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL)
{
// Store next
next = current->next;

// Reverse current node's pointer


current->next = prev;

// Move pointers one position ahead.


prev = current;
current = next;
}
*head_ref = prev;
}
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
void recursiveReverse(struct Node** head_ref)
{
struct Node* first;
struct Node* rest;
if (*head_ref == NULL)
return;

first = *head_ref;
rest = first->next;
if (rest == NULL)
return;

recursiveReverse(&rest);
first->next->next = first;
first->next = NULL;
*head_ref = rest;
}

void main()
{
clrscr();
struct Node* head = NULL;

push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);

printf("Given linked list\n");


printList(head);
reverse(&head);
printf("\nReversed Linked list \n");
printList(head);

struct Node* head2 = NULL;

push(&head2, 16);
push(&head2, 34);
push(&head2, 25);
push(&head2, 7);
printf("\n\nGiven linked list\n");
printList(head2);
recursiveReverse(&head2);
printf("\nReversed Linked list \n");
printList(head2);

getche();
}

Q6. Write a program to reverse a linked list using atmost two extra pointers.
#include <stdio.h>
#include <conio.h>

struct Node {
int data;
struct Node* next;
};

void reverse(struct Node** head_ref)


{
struct Node* current = *head_ref;
struct Node* next;
while (current->next != NULL) {
next = current->next;
current->next = next->next;
next->next = (*head_ref);
*head_ref = next;
}
}

void push(struct Node** head_ref, int new_data)


{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

void printList(struct Node* head)


{
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}

void main()
{
clrscr();
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);

printf("Given linked list\n");


printList(head);
reverse(&head);
printf("\nReversed Linked list \n");
printList(head);
getche();

}
Q7. Write a program given a linked list and two integers M and N, deleting N nodes after
M nodes.

#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};

void push(struct Node ** head_ref, int new_data)


{

struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));


new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

void printList(struct Node *head)


{
struct Node *temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

void skipMdeleteN(struct Node *head, int M, int N)


{
struct Node *curr = head, *t;
int count;
while (curr)
{

for (count = 1; count<M && curr!= NULL; count++)


curr = curr->next;

if (curr == NULL)
return;
t = curr->next;
for (count = 1; count<=N && t!= NULL; count++)
{
struct node *temp = t;
t = t->next;
free(temp);
}
curr->next = t;
curr = t;
}
}

void main()
{

struct Node* head = NULL;


int M=2, N=3;
push(&head, 10);
push(&head, 9);
push(&head, 8);
push(&head, 7);
push(&head, 6);
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);

printf("M = %d, N = %d \nGiven Linked list is :\n", M, N);


printList(head);

skipMdeleteN(head, M, N);

printf("\nLinked list after deletion is :\n");


printList(head);

}
Q8. Write a program given a linked list, add 1 to it and create a new linked list.
#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
Node* next;
};
Node *newNode(int data)
{
Node *new_node = new Node;
new_node->data = data;
new_node->next = NULL;
return new_node;
}

int addWithCarry(Node *head)


{

if (head == NULL)
return 1;

int res = head->data + addWithCarry(head->next);

head->data = (res) % 10;


return (res) / 10;
}
Node* addOne(Node *head)
{

int carry = addWithCarry(head);


if (carry)
{
Node *newNode = new Node;
newNode->data = carry;
newNode->next = head;
return newNode;
}

return head;
}
void printList(Node *node)
{
while (node != NULL)
{
printf("%d", node->data);
node = node->next;
}
printf("\n");
}

int main(void)
{
Node *head = newNode(1);
head->next = newNode(9);
head->next->next = newNode(9);
head->next->next->next = newNode(9);

printf("List is ");
printList(head);

head = addOne(head);

printf("\nResultant list is ");


printList(head);

return 0;
}

You might also like