/* http://native-code.blogspot.com */ #include<stdio.h> #include<conio.h> #include<alloc.h> #define NULL 0 struct node { int data; struct node *next; }; void create(int x,struct node **h1); void display(struct node **h1); void insertatbeg(struct node **h1,int item); void insertatmid(struct node **h1,int element); void insertatend(struct node **h1,int item); void deleteatbeg(struct node **h1); void deleteatmid(struct node **h1); void deleteatend(struct node **h1); void main() { struct node *ptr,*new1,**h1; int x,item,element,choice; h1=NULL; _setcursortype(_NOCURSOR); do { clrscr(); printf("\n\t\t /* http://native-code.blogspot.com */ \n"); printf("\n\t\t ......MENU...... \n"); printf("\n\n\t\t(1) Create A Circular Linked List"); printf("\n\n\t\t(2) Insert Value At Begining Of The List"); printf("\n\n\t\t(3) Insert Value At Middle Of The List"); printf("\n\n\t\t(4) Insert Value At End Of The List"); printf("\n\n\t\t(5) Delete An Item From Begining Of The List"); printf("\n\n\t\t(6) Delete An Item From Middle Of The List"); printf("\n\n\t\t(7) Delete An Item From End Of The List"); printf("\n\n\t\t(8) Display The List"); printf("\n\n\t\t(9) Exit"); printf("\n\n\n\t\t Enter Your Choice : "); scanf("%d",&choice); clrscr(); switch(choice) { case 1: h1=NULL; printf("\n\n\t\t (1) CREATE A CIRCULAR LInKED List.\n\n"); printf("\n\t\t ENTER A NUMBER (0 TO Stop) :: "); scanf("%d",&x); while(x!=0) { create(x,&h1); printf("\n\t\t ENTER A NUMBER (0 TO Stop) :: "); scanf("%d",&x); } break; case 2: printf("\n\n\t\t (2) Insert An Element In The Begining OF The List.\n\n"); printf("\n\t\t Insert An Element (0 TO Stop) :: "); scanf("%d",&item); while(item!=0) { insertatbeg(&h1,item); printf("\n\t\t Insert An Element (0 TO Stop) :: "); scanf("%d",&item); } break; case 3: printf("\n\n\t\t (3) Insert An Element In The Middle OF The List.\n\n"); printf("\n\n\t\t ENTER The PREDECCESSOR Element :: "); scanf("%d",&element); insertatmid(&h1,element); break; case 4: printf("\n\n\t\t (4) Insert AT The End OF The List.\n\n"); printf("\n\t\t ENTER The Element (0 TO Stop) :: "); scanf("%d",&item); while(item!=0) { insertatend(&h1,item); printf("\n\t\t ENTER The Element (0 TO Stop) :: "); scanf("%d",&item); } break; case 5: printf("\n\n\t\t (5) Delete From Begining OF The List.\n\n"); if(h1==NULL) printf("\n\n\t\t No Data Available !!!"); else deleteatbeg(&h1); getch(); break; case 6: printf("\n\n\t\t (6) Delete From Middle OF The List.\n\n"); if(h1==NULL) printf("\n\n\t\t No Data Available !!!"); else deleteatmid(&h1); getch(); break; case 7: printf("\n\n\t\t (7) Delete From The End OF The List.\n\n"); if(h1==NULL) printf("\n\n\t\t No Data Available !!!"); else deleteatend(&h1); getch(); break; case 8: printf("\n\t\t ElementS OF The List Are .....\n\n\n\t\t "); display(&h1); getch(); break; case 9: exit(); default: printf("\n\t\t Wrong Process!!!"); } }while(1); } void create(int x,struct node **h1) { struct node *ptr,*new1; new1=malloc(sizeof(struct node)); new1->data=x; if(*h1==NULL) { *h1=new1; new1->next=*h1; } else { ptr=*h1; while(ptr->next!=*h1) ptr=ptr->next; ptr->next=new1; new1->next=*h1; } } void insertatbeg(struct node **h1,int item) { struct node *new1,*ptr; new1=malloc(sizeof(struct node)); new1->data=item; if(*h1==NULL) { *h1=new1; new1->next=*h1; } else { ptr=*h1; while(ptr->next!=*h1) ptr=ptr->next; new1->next=*h1; *h1=new1; ptr->next=*h1; } } void insertatmid(struct node **h1,int element) { struct node *ptr,*new1; int item; ptr=*h1; while(ptr->next!=*h1 && ptr->data!=element) ptr=ptr->next; if(ptr->data==element) { printf("\n\t\t ENTER The Element (0 TO Stop) :: "); scanf("%d",&item); while(item!=0) { new1=malloc(sizeof(struct node)); new1->data=item; new1->next=ptr->next; ptr->next=new1; ptr=new1; printf("\n\t\t ENTER The Element (0 TO Stop) :: "); scanf("%d",&item); } } else printf("\n\n\n\t\t Element NOT FInD !!!"); getch(); } void insertatend(struct node **h1,int item) { struct node *ptr,*new1; new1=malloc(sizeof(struct node)); new1->data=item; ptr=*h1; while(ptr->next!=*h1) ptr=ptr->next; ptr->next=new1; new1->next=*h1; } void deleteatbeg(struct node **h1) { struct node *ptr,*ptr1; ptr=*h1; ptr1=*h1; while(ptr->next!=*h1) ptr=ptr->next; *h1=(*h1)->next; ptr->next=*h1; free(ptr1); printf("\n FIRST NODE DeleteETED !!!"); } void deleteatmid(struct node **h1) { struct node *ptr1,*ptr; int item; printf("\n\n ENTER The Element TO BE DeleteETED :: "); scanf("%d",&item); ptr1=ptr=*h1; while(ptr->next!=*h1 && ptr->data!=item) { ptr1=ptr; ptr=ptr->next; } if(ptr==*h1) { while(ptr->next!=*h1) ptr=ptr->next; *h1=(*h1)->next; ptr->next=*h1; free(ptr1); } else if(ptr->data!=item) printf("\n\t\t Element NOT FOUND !!!"); else { ptr1->next=ptr->next; free(ptr); } } void deleteatend(struct node **h1) { struct node *ptr,*ptr1; ptr1=ptr=*h1; while(ptr->next!=*h1) { ptr1=ptr; ptr=ptr->next; } ptr1->next=*h1; free(ptr); printf("\n Last Node Deleted !!!"); } void display(struct node **h1) { struct node *ptr; ptr=*h1; do { printf("%d ",ptr->data); ptr=ptr->next; }while(ptr!=*h1); }
Home
» C
» circular-linked-list
» link-list
» Create A Circular Linked List And Insert And Delete An Element In To It
Post a Comment