Write a program to implement single link list.
Implement
- Add node at the beginning of the linked list
- Add node at the end of the linked list
- Add node at the middle(sort it) of the linked list
- Delete the Node at given position
- Delete the Node from beginning
- Delete the Node at from end
- Delete the data from given linked list
- Delete all nodes
- Check whether linked list is palindrome or not
- Print Nodes
- Print Nodes in reverse
- Print Nodes using recursive function
- Print Nodes in reverse order using recursive function
- Reverse the linked list
- Reverse the data of the nodes
- Count nodes of the linked list
Structure, header file and main function used is
#include<stdio.h> #include<stdlib.h> typedef struct SingleLinkList { int num; struct SingleLinkList *next; }SLL; int main() { int op,n; SLL *hp = 0; printf("\nEnter Option:\n1] Add_at_begin 2] Print 3] Count 4] Add_at_end 5] Add_at_Middle\n6] Reverse_Printing\ 7] Reverse 8] Recursion Reverse Print 9] Recursion print 10] Delete 11] Delete all 12] Reverse data 0] Exit \n"); scanf("%d",&op); while(op != 0) { switch(op) { case 1: printf("Enter Number to add = "); scanf("%d",&n); add_begin(n,&hp); break; case 2: print(hp); break; case 3: printf("count = %d\n",count(hp)); break; case 4: printf("Enter Number to add = "); scanf("%d",&n); add_end(n,&hp); break; case 5: printf("Enter Number to add = "); scanf("%d",&n); add_middle(n,&hp); break; case 6: reverse_print(hp); break; case 7: reverse(&hp); break; case 8: rec_reverse_print(hp); break; case 9: rec_print(hp); break; case 10: printf("Enter Number to delete = "); scanf("%d",&n); delete(n,&hp); break; case 11: delete_all(&hp); break; case 12: reverse_data(hp); break; default: printf("Invalid Option.... \n"); } printf("\nEnter Option:\n1] Add_at_begin 2] Print 3] Count 4] Add_at_end 5] Add_at_Middle\n6] Reverse_Printing\ 7] Reverse 8] Recusrsion Reverse Print 9] Recursion print 10] Delete 11] Delete all 12] Reverse data 0] Exit \n"); scanf("%d",&op); } return 0; }
Function definitions are given below.
//Add node at the beginning of the linked list void add_begin(int n,SLL **ptr) { SLL *p; p = (SLL*)malloc(sizeof(SLL)); p->num = n; p->next = *ptr; *ptr = p; } //Add node at the middle(sort it) of the linked list void add_middle(int n,SLL **ptr) { SLL *p1,*p2; p1 = (SLL*)malloc(sizeof(SLL)); p1->num = n; if(*ptr == 0 || p1->num < (*ptr)->num) { p1->next = *ptr; *ptr = p1; } else { p2 = *ptr; while(p2) { if(p2->next == 0 || p1->num < p2->next->num) { p1->next = p2->next; p2->next = p1; break; } p2 = p2->next; } } } //Add node at the end of the linked list void add_end(int n,SLL** ptr) { SLL *p1,*p2; p1 = (SLL*)malloc(sizeof(SLL)); p1->num = n; if(*ptr == 0) { p1->next = *ptr; *ptr = p1; } else { p2 = *ptr; while(p2->next) p2 = p2->next; p1->next = p2->next; p2->next = p1; } } //Delete the Node at given position void delete_pos(int n,SLL**p) { if(*p == 0) { printf("Empty List\n"); return; } if(n < 1 || n > count(*p)) { printf("Invalid Choice\n"); return; } else if(n == 1) { SLL *p1; p1 = *p; (*p)= p1->next; free(p1); return; } else { SLL *p1,*p2; int i; p1 = p2 = *p; for(i = 1;i < n;i++) { p2 = p1; p1 = p1->next; } p2->next = p1->next; free(p1); } } //Delete the Node from beginning void delete_begin(SLL **p) { if(*p == 0) { printf("Empty List\n"); return; } else if((*p)->next == 0) { free(*p); *p = 0; return; } else { SLL *p1; p1 = *p; (*p) = p1->next; free(p1); } } //Delete the Node at from end void delete_end(SLL **p) { if(*p == 0) { printf("Empty List\n"); return; } else if((*p)->next == 0) { free((*p)); *p = 0; return; } else { SLL *p1,*p2; p1 = p2 = *p; while(p1->next) { p2 = p1; p1 = p1->next; } p2->next = NULL; free(p1); } } //Delete the data from given linked list void delete_data(int n,SLL**p) { SLL *p1,*p2; p1 = p2 = *p; while(p1) { if(p1->num == n) { if(p1 == *p) { *p = p1->next; free(p1); return; } else { p2->next = p1->next; free(p1); return; } } p2 = p1; p1 = p1->next; } printf("Not Found....\n"); } //Delete all nodes void delete_all(SLL**p) { if(*p == 0) { printf("Empty List\n"); return; } else if((*p)->next == 0) { free(*p); *p = 0; return; } else { SLL *p1; p1 = *p; while(p1) { *p = p1->next; free(p1); p1 = *p; } } } //Check whether linked list is palindrome or not int palin_check(SLL *p) { int i,j,c; SLL *p1,*p2; c = count(p); p1 = p; for(i = 0;i < (c/2);i++) { p2 = p; for(j=0;j<c-i-1;j++) { p2 = p2->next; } if(p1->num != p2->num) return 0; p1= p1->next; } if(i == (c/2))//or i == (c - j) return 1;//it`s palindrome else return 0; } //Print Nodes void print(SLL* p) { while(p) { printf("%d ----> ",p->num); p = p->next; } printf("\n"); } //Print Nodes in reverse void reverse_print(SLL *p) { SLL *p1; int c,i,j; c = count(p); for(i = 0; i < c;i++) { p1 = p; for(j = 0;j < c-i-1; j++) { p1 = p1->next; } printf("%d ",p1->num); } printf("\n"); } //Print Nodes using recursive function void rec_print(SLL*p) { if(p) { printf("%d ",p->num); rec_print(p->next); } } //Print Nodes in reverse order using recursive function void rec_reverse_print(SLL*p) { if(p) { rec_reverse_print(p->next); printf("%d ",p->num); } } //Reverse the linked list void reverse_link(SLL **ptr) { SLL *p,*q,*r; p = *ptr; q = 0; while(p) { r = q; q = p; p = p->next; q->next = r; } *ptr = q; } //Reverse the data of the nodes void reverse_data(SLL *p) { int i,j,c,t2; SLL *p1,*p2,*t; c = count(p); p1 = p; for(i = 0;i < c/2;i++) { p2 = p; for(j=0;j<c-i-1;j++) { p2=p2->next; } t2 = p1->num; p1->num = p2->num; p2->num = t2; p1=p1->next; } } //Count nodes of the linked list unsigned int count(SLL *p) { unsigned int count = 0; while(p) { count++; p = p->next; } return count; }