0% found this document useful (0 votes)
29 views63 pages

Linked List DSA

Uploaded by

chandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views63 pages

Linked List DSA

Uploaded by

chandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

1.

Singular linked liSt


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void create(struct node * );
void display(struct node * );
void insertAtBeg(struct node * );
void insertAtEnd(struct node *);
void insertAtPosition(struct node *);
void insertAfterPosition(struct node *);
void insertBeforePosition(struct node *);
void deleteAtBeg(struct node *);
void deleteAtEnd(struct node *);
void deleteAtPosition(struct node *);

int main()
{
int ch;
while(1)
{
printf("\nPress 1:Create 2:Display 0:Exit 3:Insert_At_Begenning
4:insertAtEnd 5:insertAtPosition 6:insertAfterPosition
7:insertBeforePosition 8:deleteAtBeg 9:deleteAtEnd
10:deleteAtPosition\n ");
printf("enter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1: create(head); break;
case 2: display(head);break;
case 3: insertAtBeg(head);break;
case 4: insertAtEnd(head);break;
case 5: insertAtPosition(head);break;
case 6: insertAfterPosition(head);break;
case 7: insertBeforePosition(head);break;
case 8: deleteAtBeg(head);break;
case 9: deleteAtEnd(head);break;
case 10:deleteAtPosition(head);break;
case 0: exit(0);
default: printf("Wrong choice....use only above option ");
}
}
return 0;
}
void create(struct node *ptr)
{
struct node *temp;
int x;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
if(head==NULL)
head=temp;
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}

void display(struct node *ptr)


{
if(ptr==NULL)
printf("List Underflow....");
else
{

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

while(ptr->next!=NULL)
{
printf("%d->",ptr->data);
ptr=ptr->next;
}
printf("%d",ptr->data);
}
}

void insertAtBeg(struct node *ptr)


{

struct node *temp;


int x;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data which you insert at begenning:");
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
temp->next=ptr;
head=temp;
printf("Node inserted successfully....");

void insertAtEnd(struct node *ptr)


{
struct node *temp;
int x;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data which you insert at End:");
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
if(head==NULL)
head=temp;
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
printf("Node inserted successfully....");
}

void insertAtPosition(struct node *ptr)


{
struct node *temp;
int x,position,i;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data which you insert at End:");
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
printf("Enter position at which you want to insert:");
scanf("%d",&position);
i=1;
while(i<position-1)
{
ptr=ptr->next;
i++;
}
temp->next=ptr->next;
ptr->next=temp;
printf("Node inserted successfully....");
}

void insertAfterPosition(struct node *ptr)


{
struct node *temp;
int x,position,i;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data which you insert at End:");
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
printf("Enter position at which you want to insert:");
scanf("%d",&position);
i=1;
while(i<=position-1)
{
ptr=ptr->next;
i++;
}
temp->next=ptr->next;
ptr->next=temp;
printf("Node inserted successfully....");
}

void insertBeforePosition(struct node *ptr)


{
struct node *temp;
int x,position,i;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data which you insert at End:");
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
printf("Enter position at which you want to insert:");
scanf("%d",&position);
if(position==1)
{
temp->next=ptr;
head=temp;
}
else
{
i=1;
while(i<=position)
{
ptr=ptr->next;
i++;
}
temp->next=ptr->next;
ptr->next=temp;
}
printf("Node inserted successfully....");
}

void deleteAtBeg(struct node *ptr)


{

if(ptr==NULL)
printf("No element remains for deletion...Linked list underflow...");
else

{
head=ptr->next;
free(ptr);
printf("Node deleted successfully....");
}
}
void deleteAtEnd(struct node *ptr)
{
struct node *preptr;
if(ptr==NULL)
printf("List Underflow...");
else if(ptr->next==NULL)
{

head=NULL;
printf("Node Deleted....");
free(ptr);
}
else
{
while(ptr->next!=NULL)
{
preptr=ptr;
ptr=ptr->next;
}
preptr->next=NULL;
printf("Node Deleted....");
free(ptr);
}

}
void deleteAtPosition(struct node *ptr)
{
struct node *preptr;
int x,position,i;
if(ptr==NULL)
printf("List Underflow...");
else
{
printf("Enter position from which you want to delete:");
scanf("%d",&position);
if(position==1)
{
head=ptr->next;
free(ptr);
}
else
{
i=1;
while(i<position)
{
preptr=ptr;
ptr=ptr->next;
i++;
}
preptr->next=ptr->next;
printf("\nNode Deleted....\n");
free(ptr);
}
}
}
2. uSe local variable aS a head pointer
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};

struct node *create(struct node *);


void display(struct node *);

int main()
{
int ch;
struct node *head = NULL; // Local head pointer

while(1)
{
printf("\n1:create 2:display 0:exit\n");
printf("Enter Choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1: head = create(head); break; // Update and assign head
case 2: display(head); break; // Pass head to display
case 0: exit(1); break;
default: printf("Wrong choice...\n");
}
}
return 0;
}

struct node *create(struct node *head) // Return updated head


{
struct node *temp, *ptr;
int data;

// Allocate memory for new node


temp = (struct node *)malloc(sizeof(struct node));
printf("Enter Data: ");
scanf("%d", &data);

// Assign data to new node


temp->info = data;
temp->next = NULL;

// If list is empty, make temp the head


if(head == NULL)
{
head = temp;
}
else
{
// Traverse to the end of the list and add the new node
ptr = head;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
return head; // Return the updated head pointer
}

void display(struct node *head) // Display the list


{
struct node *ptr = head;

// Traverse and print the list


while(ptr != NULL)
{
printf("%d ", ptr->info);
ptr = ptr->next;
}
}
3. Stack aS a linked liSt
#include<stdio.h>
#include<stdlib.h>

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

struct stack *push(struct stack *); // Function to push an element onto


the stack
struct stack *pop(struct stack *); // Function to pop an element from the
stack
void display(struct stack *); // Function to display the stack

int main()
{
int ch;
struct stack *top = NULL; // Top pointer initialized to NULL

while(1)
{
printf("\n1:Push 2:Pop 3:Display 0:Exit\n");
printf("Enter Choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1: top = push(top); break; // Push operation updates top
case 2: top = pop(top); break; // Pop operation updates top
case 3: display(top); break; // Display the stack
case 0: exit(0); break;
default: printf("Wrong choice...\n");
}
}
return 0;
}

// Function to push an element onto the stack


struct stack *push(struct stack *top)
{
struct stack *temp;
int value;

// Allocate memory for the new node


temp = (struct stack *)malloc(sizeof(struct stack));
if(temp == NULL)
{
printf("Memory not allocated\n");
return top;
}

printf("Enter data: ");


scanf("%d", &value);
// Assign data to the new node
temp->data = value;
temp->next = top; // Point the new node to the current top
top = temp; // Update the top to the new node

return top; // Return the updated top pointer


}

// Function to pop an element from the stack


struct stack *pop(struct stack *top)
{
struct stack *temp;

if(top == NULL)
{
printf("Stack is empty\n");
return top;
}

temp = top; // Hold the current top node


printf("Popped element: %d\n", temp->data);
top = top->next; // Update the top to the next node
free(temp); // Free the memory of the popped node

return top; // Return the updated top pointer


}
// Function to display the stack
void display(struct stack *top)
{
struct stack *ptr = top;

if(top == NULL)
{
printf("Stack is empty\n");
return;
}

printf("Stack elements:\n");
// Traverse the stack and print elements
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}
4. Queue aS a linked liSt
#include<stdio.h>
#include<stdlib.h>

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

struct queue *enqueue(struct queue *, struct queue **);


struct queue *dequeue(struct queue *, struct queue **);
void display(struct queue *);
int main()
{
int ch;
struct queue *front = NULL, *rear = NULL;

while(1)
{
printf("\n1:Enqueue 2:Dequeue 3:Display 0:Exit\n");
printf("Enter Choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1: front = enqueue(front, &rear); break; // Enqueue updates
front/rear
case 2: front = dequeue(front, &rear); break; // Dequeue updates
front/rear
case 3: display(front); break; // Display the queue
case 0: exit(0); break;
default: printf("Wrong choice...\n");
}
}
return 0;
}

// Function to enqueue an element into the queue


struct queue *enqueue(struct queue *front, struct queue **rear)
{
struct queue *temp;
int value;

// Allocate memory for the new node


temp = (struct queue *)malloc(sizeof(struct queue));
if(temp == NULL)
{
printf("Memory not allocated\n");
return front;
}

printf("Enter data: ");


scanf("%d", &value);
// Assign data and set next to NULL
temp->data = value;
temp->next = NULL;

// If queue is empty, both front and rear will point to the new node
if(front == NULL)
{
front = temp;
*rear = temp;
}
else
{
// Link the new node at the end of the queue and update rear
(*rear)->next = temp;
*rear = temp;
}

return front; // Return the updated front pointer


}

// Function to dequeue an element from the queue


struct queue *dequeue(struct queue *front, struct queue **rear)
{
struct queue *temp;

if(front == NULL)
{
printf("Queue is empty\n");
return front;
}

temp = front; // Hold the current front node


printf("Dequeued element: %d\n", temp->data);
front = front->next; // Update front to the next node
if(front == NULL) // If queue becomes empty, set rear to NULL
{
*rear = NULL;
}
free(temp); // Free the memory of the dequeued node

return front; // Return the updated front pointer


}

// Function to display the queue


void display(struct queue *front)
{
struct queue *ptr = front;

if(front == NULL)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements:\n");
// Traverse the queue and print elements
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}

5. input aS a Stack and output aS a Queue


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

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

struct node *push(struct node *); // Stack push operation


struct node *pop(struct node *); // Stack pop operation
void display_as_queue(struct node *); // Display as queue
int main()
{
int ch;
struct node *top = NULL; // Top pointer initialized to NULL

while(1)
{
printf("\n1:Push 2:Display as Queue 0:Exit\n");
printf("Enter Choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1: top = push(top); break; // Push to stack
case 2: display_as_queue(top); break; // Display in queue order
case 0: exit(0); break;
default: printf("Wrong choice...\n");
}
}
return 0;
}

// Function to push an element onto the stack


struct node *push(struct node *top)
{
struct node *temp;
int value;

// Allocate memory for the new node


temp = (struct node *)malloc(sizeof(struct node));
if(temp == NULL)
{
printf("Memory not allocated\n");
return top;
}

printf("Enter data: ");


scanf("%d", &value);

// Assign data to the new node


temp->data = value;
temp->next = top; // Point new node to the current top
top = temp; // Update top to the new node

return top; // Return the updated top pointer


}

// Function to display the linked list as a queue (FIFO)


void display_as_queue(struct node *top)
{
struct node *ptr, *new_top = NULL, *temp;

// Reverse the stack to simulate queue order


while(top != NULL)
{
// Pop from original stack
temp = (struct node *)malloc(sizeof(struct node));
temp->data = top->data;
temp->next = new_top; // Push to new stack (reversing the order)
new_top = temp;
top = top->next;
}

// Now display the reversed stack, which is in queue order


ptr = new_top;
if(ptr == NULL)
{
printf("Queue is empty\n");
return;
}

printf("Queue elements:\n");
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");

// Free the new stack (to avoid memory leaks)


while(new_top != NULL)
{
temp = new_top;
new_top = new_top->next;
free(temp);
}
}

6. double pointer Sl linkedliSt


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

struct node
{
int info;
struct node *next;
};

void create(struct node **head); // Double pointer for head


void display(struct node *head); // Pass head as parameter
int main()
{
int ch;
struct node *head = NULL; // Local head pointer

while(1)
{
printf("\n1:create 2:display 0:exit\n");
printf("Enter Choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1: create(&head); break; // Pass address of head
case 2: display(head); break; // Pass head to display
case 0: exit(1); break;
default: printf("Wrong choice...\n");
}
}
return 0;
}

void create(struct node **head) // Double pointer to modify head


{
struct node *temp, *ptr;
int data;

// Allocate memory for new node


temp = (struct node *)malloc(sizeof(struct node));
printf("Enter Data: ");
scanf("%d", &data);

// Assign data to new node


temp->info = data;
temp->next = NULL;

// If list is empty, make temp the head


if(*head == NULL)
{
*head = temp;
}
else
{
// Traverse to the end of the list and add the new node
ptr = *head;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}

void display(struct node *head) // Display the list


{
struct node *ptr = head;

// Traverse and print the list


while(ptr != NULL)
{
printf("%d ", ptr->info);
ptr = ptr->next;
}
printf("\n"); // Newline for better output formatting
}

7. circular linked liSt


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void create(struct node * );
void display(struct node * );

int main()
{
int ch;
while(1)
{
printf("\nPress 1:Create 2:Display 0:Exit \n ");
printf("enter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1: create(head); break;
case 2: display(head);break;

case 0: exit(0);
default: printf("Wrong choice....use only above option ");
}
}
return 0;
}
void create(struct node *ptr)
{
struct node *temp;
int x;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
if(head==NULL)
{

head=temp;
temp->next=head;
}
else
{
ptr=head;
while(ptr->next!=head)
{
ptr=ptr->next;
}
ptr->next=temp;
temp->next=head;
}
}

void display(struct node *ptr)


{
if(ptr==NULL)
printf("No Node in this circular Linked List...");
else
{

printf("\ncircular Linked List is :\n");

while(ptr->next!=head)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("%d ",ptr->data);
}
}

8. doubly linked liSt


#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head=NULL;
void create(struct node * );
void display(struct node * );

int main()
{
int ch;
while(1)
{
printf("\nPress 1:Create 2:Display 0:Exit \n ");
printf("enter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1: create(head); break;
case 2: display(head);break;

case 0: exit(0);
default: printf("Wrong choice....use only above option ");
}
}
return 0;
}
void create(struct node *ptr)
{
struct node *temp;
int x;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
if(head==NULL)
{

head=temp;
temp->next=NULL;
temp->prev=NULL;
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
temp->prev=ptr;
temp->next=NULL;
}
}

void display(struct node *ptr)


{
if(ptr==NULL)
printf("No Node in this circular Linked List...");
else
{

printf("\ndoubly Linked List is :\n");

while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}

}
}
9. create polynomial
#include<stdio.h>
#include<stdlib.h>
struct node
{
int pow;
int coeff;
struct node *next;
};
struct node *head=NULL;
void create(struct node * );
void display(struct node * );

int main()
{
int ch;
while(1)
{
printf("\nPress 1:Create 2:Display 0:Exit \n ");
printf("enter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1: create(head); break;
case 2: display(head);break;
case 0: exit(0);
default: printf("Wrong choice....use only above option ");
}
}
return 0;
}
void create(struct node *ptr)
{
struct node *temp;
int x;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter power and coefficient of polynomial term:");
scanf("%d%d",&(temp->pow),&(temp->coeff));
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}

void display(struct node *ptr)


{
if(ptr==NULL)
printf("No Node in this Linked List...");
else
{

printf("\nPolynomial List is :\n");

while(ptr->next!=NULL)
{
printf("%dx^%d+",ptr->coeff,ptr->pow);
ptr=ptr->next;
}
printf("%dx^%d",ptr->coeff,ptr->pow);
}
}

10. add polynomial


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

struct node {
int coeff;
int pow;
struct node *next;
};
struct node *poly_add(struct node *, struct node *);
struct node *enter(struct node *);
struct node *insert(struct node *, int, int);
void display(struct node *);

int main() {
struct node *head1, *head2, *head3;
head1 = NULL;
head2 = NULL;
head3 = NULL;

printf("In Polynomial 1 :\n");


head1 = enter(head1);

printf("In Polynomial 2 :\n");


head2 = enter(head2);

head3 = poly_add(head1, head2);

printf("Polynomial 1 is : ");
display(head1);

printf("Polynomial 2 is : ");
display(head2);

printf("Sum of above two polynomials is : ");


display(head3);

return 0;
}

struct node *enter(struct node *start) {


int i, n, ex;
int co;
printf("How many terms you want: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("Enter coefficient for term %d : ", i);
scanf("%d", &co);
printf("Enter exponent for term %d : ", i);
scanf("%d", &ex);
start = insert(start, co, ex);
}
return start;
}

struct node *insert(struct node *start, int co, int ex) {


struct node *ptr, *tmp;
tmp = (struct node *)malloc(sizeof(struct node));
tmp->coeff = co;
tmp->pow = ex;

if (start == NULL || ex > start->pow) {


tmp->next = start;
start = tmp;
} else {
ptr = start;
while (ptr->next != NULL && ptr->next->pow > ex)
ptr = ptr->next;
tmp->next = ptr->next;
ptr->next = tmp;
}
return start;
}

struct node *poly_add(struct node *ptr1, struct node *ptr2) {


struct node *head3, *ptr3, *tmp;
head3 = NULL;

if (ptr1 == NULL && ptr2 == NULL)


return head3;

while (ptr1 != NULL && ptr2 != NULL) {


tmp = (struct node *)malloc(sizeof(struct node));
tmp->next = NULL; // Important to initialize this!

if (head3 == NULL) {
head3 = tmp;
ptr3 = head3;
} else {
ptr3->next = tmp;
ptr3 = ptr3->next;
}

if (ptr1->pow == ptr2->pow) {
tmp->coeff = ptr1->coeff + ptr2->coeff;
tmp->pow = ptr1->pow;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
} else if (ptr1->pow > ptr2->pow) {
tmp->coeff = ptr1->coeff;
tmp->pow = ptr1->pow;
ptr1 = ptr1->next;
} else {
tmp->coeff = ptr2->coeff;
tmp->pow = ptr2->pow;
ptr2 = ptr2->next;
}
}

// Append remaining nodes from ptr1 or ptr2


while (ptr1 != NULL) {
ptr3->next = ptr1;
ptr3 = ptr3->next;
ptr1 = ptr1->next;
}
while (ptr2 != NULL) {
ptr3->next = ptr2;
ptr3 = ptr3->next;
ptr2 = ptr2->next;
}

ptr3->next = NULL;
return head3;
}

void display(struct node *ptr) {


if (ptr == NULL) {
printf("Empty\n");
return;
}

while (ptr != NULL) {


printf("(%dx^%d) ", ptr->coeff, ptr->pow);
ptr = ptr->next;
if (ptr != NULL) {
printf("+ ");
}
}
printf("\n");
}
11. Sort linked liSt
#include<stdio.h>
#include<stdlib.h>

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

struct node *create(struct node *); // Function to create the list


void display(struct node *); // Function to display the list
struct node *sort(struct node *); // Function to sort the list

int main()
{
int ch;
struct node *head = NULL; // Head pointer initialized to NULL

while(1)
{
printf("\n1:Create 2:Display 3:Sort 0:Exit\n");
printf("Enter Choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1: head = create(head); break; // Create the linked list
case 2: display(head); break; // Display the linked list
case 3: head = sort(head); break; // Sort the linked list
case 0: exit(0); break;
default: printf("Wrong choice...\n");
}
}
return 0;
}

// Function to create the linked list


struct node *create(struct node *head)
{
struct node *temp, *ptr;
int data;

// Allocate memory for new node


temp = (struct node *)malloc(sizeof(struct node));
printf("Enter Data: ");
scanf("%d", &data);

// Assign data and set next to NULL


temp->data = data;
temp->next = NULL;

// If list is empty, make the new node the head


if(head == NULL)
{
head = temp;
}
else
{
// Traverse to the end and add the new node
ptr = head;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}

return head; // Return the updated head pointer


}

// Function to display the linked list


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

if(head == NULL)
{
printf("List is empty\n");
return;
}
printf("Linked list elements:\n");
// Traverse and print the list
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}

// Function to sort the linked list using Bubble Sort


struct node *sort(struct node *head)
{
struct node *ptr1, *ptr2;
int temp;

if(head == NULL)
{
printf("List is empty, nothing to sort\n");
return head;
}

// Bubble sort logic to sort the linked list


for(ptr1 = head; ptr1 != NULL; ptr1 = ptr1->next)
{
for(ptr2 = ptr1->next; ptr2 != NULL; ptr2 = ptr2->next)
{
if(ptr1->data > ptr2->data)
{
// Swap the data of the nodes
temp = ptr1->data;
ptr1->data = ptr2->data;
ptr2->data = temp;
}
}
}

printf("List sorted successfully\n");


return head; // Return the sorted head pointer
}

12. to remove duplicateS from the liSt

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

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

struct node *create(struct node *); // Function to create the list


void display(struct node *); // Function to display the list
void removeDuplicates(struct node *); // Function to remove
duplicates from the list

int main()
{
int ch;
struct node *head = NULL; // Head pointer initialized to NULL

while(1)
{
printf("\n1:Create 2:Display 3:Remove Duplicates 0:Exit\n");
printf("Enter Choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1: head = create(head); break; // Create the linked list
case 2: display(head); break; // Display the linked list
case 3: removeDuplicates(head); break; // Remove duplicates
from the linked list
case 0: exit(0); break;
default: printf("Wrong choice...\n");
}
}
return 0;
}
// Function to create the linked list
struct node *create(struct node *head)
{
struct node *temp, *ptr;
int data;

// Allocate memory for new node


temp = (struct node *)malloc(sizeof(struct node));
printf("Enter Data: ");
scanf("%d", &data);

// Assign data and set next to NULL


temp->data = data;
temp->next = NULL;

// If list is empty, make the new node the head


if(head == NULL)
{
head = temp;
}
else
{
// Traverse to the end and add the new node
ptr = head;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}

return head; // Return the updated head pointer


}

// Function to display the linked list


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

if(head == NULL)
{
printf("List is empty\n");
return;
}

printf("Linked list elements:\n");


// Traverse and print the list
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}
// Function to remove duplicates from the linked list
void removeDuplicates(struct node *head)
{
struct node *current = head, *ptr, *prev, *dup;

if(head == NULL)
{
printf("List is empty\n");
return;
}

// Traverse the list


while(current != NULL && current->next != NULL)
{
prev = current;
ptr = current->next;

// Check for duplicates of the current node


while(ptr != NULL)
{
if(current->data == ptr->data) // Duplicate found
{
// Remove the duplicate node
prev->next = ptr->next;
dup = ptr;
free(dup); // Free memory of the duplicate node
ptr = prev->next; // Move to the next node
}
else
{
prev = ptr;
ptr = ptr->next;
}
}
current = current->next; // Move to the next node
}

printf("Duplicates removed successfully\n");


}

13. to inSert and delete at alternate


poSitionS

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

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

struct node *create(struct node *); // Function to create the list


void display(struct node *); // Function to display the list
void insertAtAlternate(struct node *); // Function to insert at
alternate positions
void deleteAtAlternate(struct node *); // Function to delete at
alternate positions

int main()
{
int ch;
struct node *head = NULL; // Head pointer initialized to NULL

while(1)
{
printf("\n1:Create 2:Display 3:Insert at Alternate 4:Delete at
Alternate 0:Exit\n");
printf("Enter Choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1: head = create(head); break; // Create the linked list
case 2: display(head); break; // Display the linked list
case 3: insertAtAlternate(head); break; // Insert at alternate
positions
case 4: deleteAtAlternate(head); break; // Delete at alternate
positions
case 0: exit(0); break;
default: printf("Wrong choice...\n");
}
}
return 0;
}

// Function to create the linked list


struct node *create(struct node *head)
{
struct node *temp, *ptr;
int data;

// Allocate memory for new node


temp = (struct node *)malloc(sizeof(struct node));
printf("Enter Data: ");
scanf("%d", &data);

// Assign data and set next to NULL


temp->data = data;
temp->next = NULL;

// If list is empty, make the new node the head


if(head == NULL)
{
head = temp;
}
else
{
// Traverse to the end and add the new node
ptr = head;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}

return head; // Return the updated head pointer


}

// Function to display the linked list


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

if(head == NULL)
{
printf("List is empty\n");
return;
}

printf("Linked list elements:\n");


// Traverse and print the list
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}

// Function to insert nodes at alternate positions


void insertAtAlternate(struct node *head)
{
if (head == NULL)
{
printf("List is empty\n");
return;
}

struct node *ptr = head;


int count = 1, data;

while(ptr != NULL && ptr->next != NULL)


{
struct node *newNode = (struct node *)malloc(sizeof(struct node));

printf("Enter data to insert at position %d: ", count + 1);


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

newNode->next = ptr->next; // Point the new node to the next of


current node
ptr->next = newNode; // Insert the new node after current node
ptr = newNode->next; // Move two nodes ahead to skip
alternate nodes
count += 2;
}

printf("Nodes inserted at alternate positions.\n");


}

// Function to delete nodes at alternate positions


void deleteAtAlternate(struct node *head)
{
if (head == NULL)
{
printf("List is empty\n");
return;
}

struct node *ptr = head, *temp;


int count = 1;

while(ptr != NULL && ptr->next != NULL)


{
temp = ptr->next; // Temp holds the node to be deleted
ptr->next = temp->next; // Bypass the node to be deleted
free(temp); // Free the memory of the deleted node
ptr = ptr->next; // Move two nodes ahead to skip alternate
nodes
count += 2;
}

printf("Nodes deleted at alternate positions.\n");


}

14. the liSt iS a palindrome

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

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

struct node *create(struct node *); // Function to create the list


void display(struct node *); // Function to display the list
int isPalindrome(struct node *); // Function to check if the list is a
palindrome
struct node *reverse(struct node *); // Function to reverse the linked
list

int main()
{
int ch;
struct node *head = NULL; // Head pointer initialized to NULL
while(1)
{
printf("\n1:Create 2:Display 3:Check Palindrome 0:Exit\n");
printf("Enter Choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1: head = create(head); break; // Create the linked list
case 2: display(head); break; // Display the linked list
case 3:
if(isPalindrome(head)) // Check if the list is a
palindrome
printf("Linked list is a palindrome\n");
else
printf("Linked list is not a palindrome\n");
break;
case 0: exit(0); break;
default: printf("Wrong choice...\n");
}
}
return 0;
}

// Function to create the linked list


struct node *create(struct node *head)
{
struct node *temp, *ptr;
int data;

// Allocate memory for new node


temp = (struct node *)malloc(sizeof(struct node));
printf("Enter Data: ");
scanf("%d", &data);

// Assign data and set next to NULL


temp->data = data;
temp->next = NULL;

// If list is empty, make the new node the head


if(head == NULL)
{
head = temp;
}
else
{
// Traverse to the end and add the new node
ptr = head;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
return head; // Return the updated head pointer
}

// Function to display the linked list


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

if(head == NULL)
{
printf("List is empty\n");
return;
}

printf("Linked list elements:\n");


// Traverse and print the list
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}

// Function to reverse a linked list and return the new head


struct node *reverse(struct node *head)
{
struct node *prev = NULL, *current = head, *next = NULL;

while(current != NULL)
{
next = current->next; // Store the next node
current->next = prev; // Reverse the current node's pointer
prev = current; // Move pointers ahead
current = next;
}

return prev; // Return the new head


}

// Function to check if the linked list is a palindrome


int isPalindrome(struct node *head)
{
if(head == NULL || head->next == NULL) // If the list is empty or has
only one element
return 1;

// Step 1: Find the middle of the linked list


struct node *slow = head, *fast = head;
while(fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
}
// Step 2: Reverse the second half of the list
struct node *second_half = reverse(slow);
struct node *first_half = head;

// Step 3: Compare the first half and the reversed second half
struct node *temp_second_half = second_half; // Save for restoring
later
while(second_half != NULL)
{
if(first_half->data != second_half->data)
{
reverse(temp_second_half); // Restore the original list (optional)
return 0; // Not a palindrome
}
first_half = first_half->next;
second_half = second_half->next;
}

// Step 4: Restore the original list by reversing the second half back
reverse(temp_second_half);

return 1; // The list is a palindrome


}

You might also like