Hello Friends,
Output:
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; struct node *start=NULL; void display(struct node *t) { int i=0; struct node *temp; temp=t; if(temp==NULL) { printf("List is empty!\n"); } else { printf("\nThe Linked list elements are : \n"); do { printf("%d\t",temp->data); temp=temp->next; }while(temp!=t); printf("\n"); } } struct node *getnode() { return((struct node *)malloc(sizeof(struct node))); } void freenode(struct node *ptr) { free(ptr); } void search() { struct node *temp; int d,c=0,k; printf("\nEnter the number to be searched:\t"); scanf("%d",&k); temp=start; if (temp==NULL) { printf("List is empty!\n"); } else { do { c++; if(temp->data==k) { d=c; break; } temp=temp->next; }while(temp!=start); if(d==1) { printf("\nElement is present at position %d",d); } else if(temp==start) { printf("\nElement not found\n"); } else { printf("\nElement is present at position %d",d); } } } int count() { struct node *temp; int c=0; temp=start; if (temp==NULL) { printf("List is empty!\n"); return 0; } else { do { c++; temp=temp->next; }while(temp!=start); return(c); } } void delbeg() { struct node *temp,*t; temp=start; if(start== NULL) { printf("\nSorry,List is empty!!!!!\n"); } else { printf("\nFirst Element %d successfully deleted\n",temp->data); if(start->next!=start) { start=start->next; t=start; while(t->next!=temp) { t=t->next; } t->next=start; freenode(temp); display(start); } else { start=NULL; display(start); } } } void delend() { int x; struct node *temp,*ptr; temp=start; if(start== NULL) { printf("\nSorry,List is empty!!!!!\n"); } else { if(temp->next!=start) { while(temp->next!=start) { ptr=temp; temp=temp->next; } x=temp->data; ptr->next=start; } else { x=temp->data; start=NULL; } } printf("\nLast Element %d successfully deleted\n",x); freenode(temp); display(start); } void delloc() { int x,p,i; struct node *t,*del,*newnode; t=start; do { printf("\nEnter the location in which data is to be deleted\n"); scanf("%d",&p); if(p<1) printf("\nInvalid position\n"); }while(p<1); if(start== NULL) { printf("\nSorry,List is empty!!!!!\n"); } else if(p==1) delbeg(); else { for(i=1;i<p-1;i++) { if(t->next==start) { printf("\nThere are less than %d elements in the list\n",p); break; } t=t->next; } if(i==p-1) { if(t->next!=start) { x=(t->next)->data; del=t->next; t->next=(t->next)->next; freenode(del); printf("Element %d successfully deleted at the desired node\n",x); display(start); } else { printf("\nThere are less than %d elements in the list",p); } } } } void insertbeg() { struct node *newnode,*temp; int n; printf("\nEnter the data to be inserted\n"); scanf("%d",&n); newnode=getnode(); newnode->data=n; if(start!=NULL) { temp=start; do { temp=temp->next; }while(temp->next!=start); temp->next=newnode; newnode->next=start; printf("\n%d Successfully inserted",n); start=newnode; display(start); } else { start=newnode; newnode->next=start; display(start); } } void insertend() { struct node *temp,*newnode; int n; printf("\nEnter the data to be inserted\n"); scanf("%d",&n); temp=start; if(temp==NULL) { newnode=getnode(); start=newnode; newnode->data=n; newnode->next=start; display(start); } else { do { temp=temp->next; }while(temp->next!=start); newnode=getnode(); newnode->data=n; newnode->next=start; temp->next=newnode; display(start); } } void insertloc() { struct node *temp,*newnode; int n,i,p; printf("\nEnter the position in which element is to be inserted\n"); scanf("%d",&p); temp=start; if(p==1) { insertbeg(); } else if(start==NULL) { printf("\nSorry,List is empty!!!!!\n"); } else { for(i=1;i<p-1;i++) { if(temp->next==start) { printf("\nThe no. of Elements are less than %d\n",p); break; } temp=temp->next; } if(i==p-1) { printf("\nEnter the no to be inserted\n"); scanf("%d",&n); newnode=getnode(); newnode->data=n; newnode->next=temp->next; temp->next=newnode; display(start); } } } struct node *create() { struct node *newnode,*temp; int num; printf("\nEnter a LIST\n"); printf("\nPlease enter data (Press -1 to end)\n"); scanf("%d",&num); start=NULL; temp=start; while(num!=-1) { if(temp==NULL) { newnode=getnode(); newnode->data=num; start=newnode; newnode->next=start; temp=start; } else { newnode=getnode(); newnode->data=num; newnode->next=start; temp->next=newnode; temp=temp->next; } printf("\nPlease enter data(Press -1 to end)\n"); scanf("%d",&num); } display(start); return(start); } void concatenate () { struct node *newnode,*temp,*list1,*list2; printf("\n List1 : "); list1 = start; display(list1); printf("\nEnter List2 : "); list2=create(); temp=list1; while(temp->next!=list1) { temp=temp->next; } temp->next=list2; do { temp=temp->next; }while(temp->next!=list2); temp->next=list1; display(list1); } void split() { struct node *list1,*list2,*temp,*t; int k,c=0,i,m; list1 = start; m=count(); do { printf(" Enter the no of elements in list1 after spliting : \n"); printf(" Remember that you have %d elements in your list \n",m); scanf("%d",&k); if(k>m) { printf(" Invalid number\n"); break; } temp=list1; for(i=1;i<k;i++) { temp=temp->next; } if(temp->next!=list1) { list2=temp->next; t=temp->next; do { t=t->next; }while(t->next!=list1); t->next=list2; temp->next=list1; } else { list2=NULL; } printf("\n\nLIST 1\n\n"); display(list1); printf("\n\nLIST 2\n\n"); display(list2); }while(0); } void reverse() { struct node *temp,*newnode,*list2; temp = start; list2=NULL; if (temp==NULL) { printf(" Empty list\n"); } else { do { newnode=getnode(); newnode->data=temp->data; newnode->next=list2; list2=newnode; temp=temp->next; }while(temp!=start); temp=list2; while(temp->next!=NULL) { temp=temp->next; } temp->next=list2; display(list2); start = list2; } } void copy() { struct node *list1,*list2; list1 = start; printf("List1 : \n"); display(list1); list2= list1; printf("List2 : \n"); display(list2); } void main() { int ch,k; do { printf("~~~ MENU ~~~\n"); printf("1. Create List \n"); printf("2. Insert At Beginning \n"); printf("3. Insert At End \n"); printf("4. Insert At Location \n"); printf("5. Delete At Beginning\n"); printf("6. Delete At End \n"); printf("7. Delete At Location\n"); printf("8. Display \n"); printf("9. Search\n"); printf("10. Count\n"); printf("11. Copy \n"); printf("12. Concatenate \n"); printf("13. Split \n"); printf("14. Reverse \n"); printf("15. Quit \n"); printf("\nEnter your choice\n"); scanf("%d",&ch); switch(ch) { case 1: start=create(); break; case 2: insertbeg(); break; case 3: insertend(); break; case 4: insertloc(); break; case 5: delbeg(); break; case 6: delend(); break; case 7: delloc(); break; case 8: display(start); break; case 9: search(); break; case 10: k=count(); printf("\n Count = %d\n",k); break; case 11: copy(); break; case 12: concatenate (); break; case 13: split(); break; case 14: reverse(); break; case 15: printf("\nThank you !!!\n"); break; default: printf("\nInvalid input!!!\n"); } }while(ch!=15); }
Output:
Create Circular Link List |
Insert At Beginning |
Insert at End |
Insert At Location |
Delete at Beginning |