From b53f5c0645a711fa3045af5d968bf1fba0caaff6 Mon Sep 17 00:00:00 2001 From: rajput2107 <43978986+rajput2107@users.noreply.github.com> Date: Tue, 9 Oct 2018 15:36:57 +0530 Subject: [PATCH] InsertionandDeletion.c --- Insertion and deletion In Linked List | 116 ++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 Insertion and deletion In Linked List diff --git a/Insertion and deletion In Linked List b/Insertion and deletion In Linked List new file mode 100644 index 0000000..9045902 --- /dev/null +++ b/Insertion and deletion In Linked List @@ -0,0 +1,116 @@ +#include +#include +struct node{ +int data; +struct node *next,*prev; +}*h,*first,*t; +struct node* InsertAtEnd(struct node *s,int x) +{ + struct node *s1,*P; + P=(struct node*)malloc(sizeof(struct node)); + P->data=x; + P->next=NULL; + + if(s==NULL){ + printf("Insertion not possible"); + exit(0); + } + s1=s; + while(s->next!=NULL){ + s=s->next; + } + s->next=P; + P->prev=s; + return s1; + } + struct node* InsertAtStart(struct node *s,int x) + { struct node *p; + p=(struct node*)malloc(sizeof(struct node)); + p->data=x; + p->prev=NULL; + p->next=s; + s->prev=p; + s=p; + return s; + } + struct node* Del(struct node *s,int x){ + struct node *p; + p=s; + while(p->data!=x&&p->next!=NULL){ + p=p->next;} + p->prev->next=p->next; + p->next->prev=p->prev; + free(p); + p=NULL; + return s; + } + void display(struct node *F){ + struct node *t; + t=F; + printf("Item's are: "); + + while(t!=NULL) { + printf("%d=>",t->data); + t=t->next; + } + + } +main(){ +typedef struct node N; +int ch=1,c=0,x; +first=0; +while(ch){ + c++; +h=(N *)malloc(sizeof(N)); +h->next=NULL; +h->prev=NULL; +printf("enter data"); +scanf("%d",&(h->data)); +if(first!=0){ + t->next=h; + h->prev=t; + t=h; +} +else first=t=h; +printf("do you want to add more type 1 or 0"); +scanf("%d",&ch); +} +t->next=NULL; +t=first; +printf("items are"); +while(t!=NULL){ + printf("%d=>",t->data); + t=t->next; +} +printf("NULL\n"); +printf("elements=%d",c); +while(ch!=4){ +printf("menu\n"); + printf("1.Insert at end\n"); + printf("2.Insert at Start\n"); + printf("3.delete an element"); + printf("4.Exit"); + scanf("%d",&ch); + if(ch==1){ + printf("enter data"); + scanf("%d",&x); + InsertAtEnd(first,x); + display(first); + } + else if(ch==2){ + printf("enter data"); + scanf("%d",&x); + first=InsertAtStart(first,x); + display(first); + } + else if(ch==3){ + printf("enter data"); + scanf("%d",&x); + Del(first,x); + display(first); + } + else if(ch==4){ + break; + } +} +}