Linked List DSA
Linked List DSA
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;
}
}
while(ptr->next!=NULL)
{
printf("%d->",ptr->data);
ptr=ptr->next;
}
printf("%d",ptr->data);
}
}
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;
};
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 stack
{
int data;
struct stack *next;
};
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;
}
if(top == NULL)
{
printf("Stack is empty\n");
return 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;
};
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;
}
// 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;
}
if(front == NULL)
{
printf("Queue is empty\n");
return 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");
}
struct node
{
int data;
struct node *next;
};
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;
}
printf("Queue elements:\n");
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
struct node
{
int info;
struct node *next;
};
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;
}
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;
}
}
while(ptr->next!=head)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("%d ",ptr->data);
}
}
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;
}
}
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;
}
}
while(ptr->next!=NULL)
{
printf("%dx^%d+",ptr->coeff,ptr->pow);
ptr=ptr->next;
}
printf("%dx^%d",ptr->coeff,ptr->pow);
}
}
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("Polynomial 1 is : ");
display(head1);
printf("Polynomial 2 is : ");
display(head2);
return 0;
}
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;
}
}
ptr3->next = NULL;
return head3;
}
struct node
{
int data;
struct node *next;
};
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;
}
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");
}
if(head == NULL)
{
printf("List is empty, nothing to sort\n");
return head;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
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;
if(head == NULL)
{
printf("List is empty\n");
return;
}
if(head == NULL)
{
printf("List is empty\n");
return;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
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;
}
if(head == NULL)
{
printf("List is empty\n");
return;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
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;
}
if(head == NULL)
{
printf("List is empty\n");
return;
}
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;
}
// 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);