Circular Linked List

Algorithm:Circular Linked-List Operation Let us consider a circular linked list pointed by header.We have to Perform insertion,deletion,searching,traversal,sorting,inversion And merging operations in this linked list. Insert_begining(header,item) { In this case We have to insert an element item at the beginning of the list such that it become the first node. t=getnode() //getnode is a function which create a new node (node has two part i) data ii) link of the linked list and return the address of node. t->data=item if(header->link=header) { header->link=t } else { t->link=header->link header->link=t } } Insert_end(header,item) { In this case we want to insert an element item at the end of the list such that it become the last node. t=getnode() t->data=item if(header->link=header) { header->link=t } else { trav=header->link //trav points to 1st node while(trav->link≠header) //until trav reaches the last node { trav=trav->link //trav moves to next node } trav->link=t //the node,t is added at the last } position of the list } Insert_any(header,key,item) { In this case we have toinsert an element,item in this list after a node whose data is key. t=getnode() t->data=item trav=header->link //trav points to 1st node while(trav->data≠key and trav≠header) //until key not found and { the trav becomes header trav=trav->link //trav moves to next node } if(trav=header) //checks if key not found { Print "Insertion impossible" } else { t->link=trav->link //t points to the node just after trav trav->link=t //trav points to t } } Traversal(header) { trav=header->link if(trav=header) print "List empty" else { while(trav!=header) //until trav becomes header { Print "trav->data” trav=trav->link //trav moves to next node } } } Delete_begining(header) { In this case we have to delete the 1st node of the list. if(header->link=header) print "List empty" else { x=header->link //x points to the 1st node in the list header->link=x->link //the link part of header points to the } 2nd node } Delete_end(header) { In this case we have to delete the last node of the list. if(header->link=header) print "List empty" else { ptr=header->link //ptr points to the 1st node in the list if(ptr->link=header) /checks if there is only one node header->link=header present in the list else { trav=ptr->link //trav points to the 2nd node while(trav->link≠header) //until trav reaches the last node { ptr=trav //ptr points to trav trav=trav->link //trav moves to next node } ptr->link=header //the link part of the node just before } the last node is made header } } Delete_any(header,key) { In this case we have to delete a node from the list containing the value,key. if(header->link=header) print "List empty" else { ptr=header //ptr points to header node trav=ptr->link //trav points to the 1st node in the list while(trav->data≠key and trav≠header) //until the key not found { or trav becomes header ptr=trav //ptr points to trav trav=trav->link //trav moves to next node } if(trav=header) //checks if the key not found { Print "Deletion impossible" } else { ptr->link=trav->link //the link part of ptr points to the } node just after trav } } Search(header,item) { In this case we have to search a node in the list containing the value,item. if(header->link=header) Print “List empty" else { trav=header->link //trav points to the 1st node j=0 while(trav≠header) //until trav becomes header { if(trav->data=item) //checks if the item is found { Print "Element found in position j” Return } trav=trav->link //if item not found,trav moves to j=j+1 next node } if(trav=header) Print "Element not found" } } Inversion(header) { In this case we have to inverse the linked list such that the header node of the list points to the last node. if(header->link=header) printf "List empty" else { p=header q=header->link //q points to the 1st node while(q≠header) //until q becomes header { r=q->link //r points to the node just after q q->link=p p=q //p points to q q=r //q points to r } header->link=p //link part of header points to p } } Sorting(header) { In this case we have to sort the nodes of the list accoeding to the value of the nodes in ascending or descending order. if(header->link=header) Print "List empty" else { for(ptr=header->link;ptr->link≠header;ptr=ptr->link) { for(trav=ptr->link;trav≠header;trav=trav->link) { if(option=ascending) { if(ptr->data>trav->data) { t=ptr->data ptr->data=trav->data trav->data=t } } else if(option=descending) { if(ptr->datadata) { t=ptr->data; ptr->data=trav->data trav->data=t } } } } } } Merge_unsorted(h1,h2) { In this case we have to merge two unsorted linked lists pointed by h1 and h2. trav=h1->link //trav points to the 1st node of 1st list ptr=h2->link //ptr points to the 1st node of 2nd list if(trav=header && ptr=header) print "Merging not possible" else { while(trav->link≠header) //until trav reaches the last node { of 1st linked-list trav=trav->link //trav moves to next node } trav->link=h2->link //now trav points to the 1st node } of 2nd linked-list } Merge_sorted(h1, h2, h3) { In this case we have to merge two linked lists in sorted order pointed by headers h1 and h2. p1=h1->link //p1 points to the 1st node of 1st list p2=h2->link //p2 points to the 1st node of 2nd list while(p1≠h1 && p2≠h2) { t=getnode() if(p1->datadata) { t->data=p1->data p1=p1->link //p1 moves to next node } else { t->data=p2->data p2=p2->link //p2 moves to next node } if(h3->link=h3) { h3->link=t trav=t } else { trav->link=t trav=trav->link } } while(p1≠h1) { t=getnode() t->data=p1->data p1=p1->link trav->link=t trav=trav->link } while(p2≠h2) { t=getnode() t->data=p2->data p2=p2->link trav->link=t trav=trav->link } } C Program For Circular Linked List Source Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { int data; struct node *link; }; struct node *header; struct node * getnode() { struct node *t; int item; t=(struct node *)malloc(sizeof(struct node)); t->link=t; return t; } void traversal() { struct node *trav; trav=header->link; if(trav==header) printf("\nList empty."); else { printf("\nLINK LIST :\n\n"); while(trav!=header) { printf(" %d",trav->data); trav=trav->link; } } printf("\n"); } void insert_beg(int item) { struct node *t; t=getnode(); t->data=item; if(header->link==header) { header->link=t; t->link=header; } else { t->link=header->link; header->link=t; } } void insert_end(int item) { struct node *trav,*t; t=getnode(); t->data=item; if(header->link==header) { header->link=t; t->link=header; } else { trav=header->link; while(trav->link!=header) { trav=trav->link; } trav->link=t; t->link=header; } } void insert_any(int key,int item) { struct node *trav,*t; trav=header->link; while(trav->data!=key && trav!=header) { trav=trav->link; } if(trav==header) { printf("\nInsertion impossible\n"); } else { t=getnode(); t->data=item; t->link=trav->link; trav->link=t; } } void delete_beg() { struct node *x; if(header->link==header) { printf("\nList empty.\n"); } else { x=header->link; header->link=x->link; } } void delete_end() { struct node *trav,*ptr; if(header->link==header) { printf("\nList empty.\n"); } else { ptr=header->link; if(ptr->link==header) { header->link=header; } else { while(ptr->link!=header) { ptr=ptr->link; } trav=ptr->link; while(trav->link!=ptr) { trav=trav->link; } trav->link=ptr->link; } } } void delete_any(int key) { struct node *trav,*ptr; if(header->link==header) { printf("\nList empty.\n"); } else { ptr=header->link; while(ptr->data!=key && ptr!=header) { ptr=ptr->link; } if(ptr==header) { printf("\nDeletion impossible.\n"); } else { trav=ptr->link; while(trav->link!=ptr) { trav=trav->link; } trav->link=ptr->link; } } } void search(int item) { int j; struct node *trav; if(header->link==header) { printf("\nList empty"); } else { trav=header->link; j=1; while(trav!=header && trav->data!=item) { trav=trav->link; j++; } if(trav==header) { printf("\nElement not found."); } else { printf("\nElement found in position:%d",j); } } } void sorting(int s) { struct node *trav,*ptr; int t; if(header->link==header) { printf("nList empty"); } else { for(ptr=header->link;ptr->link!=header;ptr=ptr->link) { for(trav=ptr->link;trav!=header;trav=trav->link) { if(s==1) { if(ptr->data>trav->data) { t=ptr->data; ptr->data=trav->data; trav->data=t; } } else if(s==2) { if(ptr->datadata) { t=ptr->data; ptr->data=trav->data; trav->data=t; } } } } } } void inversion() { struct node *p,*q,*r; if(header->link==header) { printf("\nList empty"); } else { p=header; q=header->link; while(q!=header) { r=q->link; q->link=p; p=q; q=r; } header->link=p; traversal(); } } void merge_u(struct node *h1,struct node *h2) { struct node *trav,*ptr; trav=h1->link; ptr=h2->link; if(trav==h1 && ptr==h2) printf("\nMerging not possible."); else { while(trav->link!=h1) { trav=trav->link; } trav->link=ptr; while(ptr->link!=h2) { ptr=ptr->link; } ptr->link=h1; h2->link=h2; } } void merge_sort(struct node *h1,struct node *h2,struct node *h3) { struct node *p1,*p2,*trav,*t; p1=h1->link; p2=h2->link; while(p1!=h1 && p2!=h2) { t=getnode(); if(p1->datadata) { t->data=p1->data; p1=p1->link; } else { t->data=p2->data; p2=p2->link; } if(h3->link==h3) { h3->link=t; trav=t; } else { trav->link=t; trav=trav->link; } } while(p1!=h1) { t=getnode(); t->data=p1->data; p1=p1->link; trav->link=t; trav=trav->link; } while(p2!=h2) { t=getnode(); t->data=p2->data; p2=p2->link; trav->link=t; trav=trav->link; } trav->link=h3; } void main() { int ch,ch1,ch2,item,key,ch3,s,ch4,ch5; struct node *h1,*h2,*h3; h1=getnode(); h1->link=h1; h2=getnode(); h2->link=h2; h3=getnode(); h3->link=h3; clrscr(); while(1) { printf("\nLinked List Operations:"); printf("\n1.Linked list-1\n2.Linked list-2\n3.Merging\n4.Exit"); printf("\nEnter your choice:"); scanf("%d",&ch4); switch(ch4) { case 1: header=h1; while(1) { start : printf("\nLINKED LIST-1 OPERATION :"); printf("\n1.Insertion 2.Deletion 3.Traversal 4.Search\n5.Sorting 6.Inversion 7.Exit from LL-1"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nINSERTION:"); printf("\n1.Insert begining 2.Insert end 3.Insert any pos 4.Break"); printf("\nEnter your choice:"); scanf("%d",&ch1); switch(ch1) { case 1: printf("\nEnter the item to be inserted:"); scanf("%d",&item); insert_beg(item); break; case 2: printf("\nEnter the item to be inserted:"); scanf("%d",&item); insert_end(item); break; case 3: printf("\nEnter the item to be inserted:"); scanf("%d",&item); printf("\nEnter the key:"); scanf("%d",&key); insert_any(key,item); break; case 4: break; } goto start; case 2: printf("\nDELETION:"); printf("\n1.Delete begining 2.Delete end 3.Delete any pos 4.Break"); printf("\nEnter your choice:"); scanf("%d",&ch2); switch(ch2) { case 1: delete_beg(); break; case 2: delete_end(); break; case 3: printf("\nEnter the data of the node to be deleted:"); scanf("%d",&key); delete_any(key); break; case 4: break; } goto start; case 3: traversal(); goto start; case 4: printf("\nSEARCHING:"); printf("\nEnter the element you want to search:"); scanf("%d",&item); search(item); goto start; case 5: printf("\nSORTING:"); printf("\nSorting-> 1.Ascending order 2.Decending order"); printf("\nEnter your choice:"); scanf("%d",&ch3); switch(ch3) { case 1: s=1; sorting(s); traversal(); break; case 2: s=2; sorting(s); traversal(); break; } goto start; case 6: printf("\nINVERSION:"); inversion(); goto start; case 7: goto end; } end : break; } break; case 2: header=h2; while(1) { start2 : printf("\nLINKED LIST-2 OPERATION :"); printf("\n1.Insertion 2.Deletion 3.Traversal 4.Search\n5.Sorting 6.Inversion 7.Exit from LL-2"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nINSERTION:"); printf("\n1.Insert begining 2.Insert end 3.Insert any pos 4.Break"); printf("\nEnter your choice:"); scanf("%d",&ch1); switch(ch1) { case 1: printf("\nEnter the item to be inserted:"); scanf("%d",&item); insert_beg(item); break; case 2: printf("\nEnter the item to be inserted:"); scanf("%d",&item); insert_end(item); break; case 3: printf("\nEnter the item to be inserted:"); scanf("%d",&item); printf("\nEnter the key:"); scanf("%d",&key); insert_any(key,item); break; case 4: break; } goto start2; case 2: printf("\nDELETION:"); printf("\n1.Delete begining 2.Delete end 3.Delete any pos 4.Break"); printf("\nEnter your choice:"); scanf("%d",&ch2); switch(ch2) { case 1: delete_beg(); break; case 2: delete_end(); break; case 3: printf("\nEnter the data of the node to be deleted:"); scanf("%d",&key); delete_any(key); break; case 4: break; } goto start2; case 3: traversal(); goto start2; case 4: printf("\nSEARCHING:"); printf("\nEnter the element you want to search:"); scanf("%d",&item); search(item); goto start2; case 5: printf("\nSORTING:"); printf("\nSorting-> 1.Ascending order 2.Decending order"); printf("\nEnter your choice:"); scanf("%d",&ch3); switch(ch3) { case 1: s=1; sorting(s); traversal(); break; case 2: s=2; sorting(s); traversal(); break; } goto start2; case 6: printf("\nINVERSION:"); inversion(); goto start2; case 7: goto end2; } end2 : break; } break; case 3: printf("\nMERGING:=>"); printf("1.Unsorted 2.Sorted list"); printf("\nEnter your choice:"); scanf("%d",&ch5); switch(ch5) { case 1: header=h1; merge_u(h1,h2); traversal(); break; case 2: merge_sort(h1,h2,h3); header=h3; traversal(); break; } break; case 4: exit(0); } } getch(); } OUTPUT : Linked List Operations: 1.Linked list-1 2.Linked list-2 3.Merging 4.Exit Enter your choice:1 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:1 INSERTION: 1.Insert begining 2.Insert end 3.Insert any pos 4.Break Enter your choice:1 Enter the item to be inserted:22 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:1 INSERTION: 1.Insert begining 2.Insert end 3.Insert any pos 4.Break Enter your choice:2 Enter the item to be inserted:44 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:1 INSERTION: 1.Insert begining 2.Insert end 3.Insert any pos 4.Break Enter your choice:2 Enter the item to be inserted:66 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:1 INSERTION: 1.Insert begining 2.Insert end 3.Insert any pos 4.Break Enter your choice:3 Enter the item to be inserted:88 Enter the key:44 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:3 LINK LIST : 22 44 88 66 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:4 SEARCHING: Enter the element you want to search:88 Element found in position:3 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:6 INVERSION: LINK LIST : 66 88 44 22 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:5 SORTING: Sorting-> 1.Ascending order 2.Decending order Enter your choice:1 LINK LIST : 22 44 66 88 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:5 SORTING: Sorting-> 1.Ascending order 2.Decending order Enter your choice:2 LINK LIST : 88 66 44 22 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:2 DELETION: 1.Delete begining 2.Delete end 3.Delete any pos 4.Break Enter your choice:1 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:3 LINK LIST : 66 44 22 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:2 DELETION: 1.Delete begining 2.Delete end 3.Delete any pos 4.Break Enter your choice:2 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:3 LINK LIST : 66 44 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:1 INSERTION: 1.Insert begining 2.Insert end 3.Insert any pos 4.Break Enter your choice:2 Enter the item to be inserted:22 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:1 INSERTION: 1.Insert begining 2.Insert end 3.Insert any pos 4.Break Enter your choice:2 Enter the item to be inserted:88 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:3 LINK LIST : 66 44 22 88 LINKED LIST-1 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-1 Enter your choice:7 Linked List Operations: 1.Linked list-1 2.Linked list-2 3.Merging 4.Exit Enter your choice:2 LINKED LIST-2 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-2 Enter your choice:1 INSERTION: 1.Insert begining 2.Insert end 3.Insert any pos 4.Break Enter your choice:1 Enter the item to be inserted:33 LINKED LIST-2 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-2 Enter your choice:1 INSERTION: 1.Insert begining 2.Insert end 3.Insert any pos 4.Break Enter your choice:2 Enter the item to be inserted:11 LINKED LIST-2 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-2 Enter your choice:1 INSERTION: 1.Insert begining 2.Insert end 3.Insert any pos 4.Break Enter your choice:2 Enter the item to be inserted:55 LINKED LIST-2 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-2 Enter your choice:3 LINK LIST : 33 11 55 LINKED LIST-2 OPERATION : 1.Insertion 2.Deletion 3.Traversal 4.Search 5.Sorting 6.Inversion 7.Exit from LL-2 Enter your choice:7 Linked List Operations: 1.Linked list-1 2.Linked list-2 3.Merging 4.Exit Enter your choice:3 MERGING:=>1.Unsorted 2.Sorted list Enter your choice:2 LINK LIST : 11 22 33 44 55 66 88 Linked List Operations: 1.Linked list-1 2.Linked list-2 3.Merging 4.Exit Enter your choice:3 MERGING:=>1.Unsorted 2.Sorted list Enter your choice:1 LINK LIST : 66 44 22 88 33 11 55

Share:

0 comments