write a program to create a doubly link list. 1->creation. 2->insertion 3->deletion. #include<stdio.h> #include<stdlib.h> void creation(void); void display(void); void create_node(void); void insert_end(void); void insert_beginning(void); void insert_middle(void); void deletion(void); struct link_list { int data; struct link_list *next; struct link_list *prev; }*start=NULL; typedef struct link_list node; node *current,*nnew; int main() { int a; char ch; creation(); do { printf("\n enter your choice\n1->display\n2->insert at end\n3->insert at beginning\n4->insert b/w two nodes\n5->deletion\n"); scanf("%d",&a); switch(a) { case 1: display(); break; case 2: insert_end(); break; case 3: insert_beginning(); break; case 4: insert_middle(); break; case 5: deletion(); break; default: printf("wrong choice"); } printf("\n press any key to continue and press n to quit"); scanf("%s",&ch); }while(ch!='n'); return (0); } void creation(void) { int i=0,k; printf("how many nodes you want to create"); scanf("%d",&i); while(i!=0) { nnew=malloc(sizeof(node)); nnew->next=NULL; nnew->prev=NULL; if(start==NULL) { printf("enter any integer data"); scanf("%d",&k); nnew->data=k; start=nnew; current=start; } else { printf("enter any integer data"); scanf("%d",&k); nnew->data=k; current->next=nnew; nnew->prev=current; current=nnew; } i--; } } void create_node(void) { int k; nnew=malloc(sizeof(node)); nnew->next=NULL; nnew->prev=NULL; printf("enter the data for this node"); scanf("%d",&k); nnew->data=k; } void display(void) { int l; printf("enter 1 to display list from beginning and 2 to display it from last"); scanf("%d",&l); current=start; if(l==1) { int i=1; while(current!=NULL) { printf("%d:: %d\n",i ,current->data); current=current->next; i++; } } if(l==2) { int i=1; current=start; while(current->next!=NULL) { current=current->next; i++; } do { printf("%d:: %d\n",i ,current->data); current=current->prev; i--; }while(current!=NULL); } } void insert_end(void) { create_node(); current=start; while(current->next!=NULL) { current=current->next; } if(current->next==NULL) { current->next=nnew; nnew->prev=current; current=nnew; printf("node inserted successfully"); } } void insert_beginning(void) { create_node(); nnew->next=start; start=nnew; printf("node inserted successfully\n"); } void insert_middle(void) { int a,i,pos; current=start; printf("enter the serial number of node accept first and the last node "); scanf("%d",&pos); printf("enter 1 to insert after the selected node and 2 for before "); scanf("%d",&a); if(a==1) { current=start; for(i=1;i<pos;i++) { current=current->next; } nnew->next=current->next; nnew->prev=current; current->next=nnew; } else if(a==2) { current=start; for(i=1;i<pos-1;i++) { current=current->next; } nnew->next=current->next; nnew->prev=current; current->next=nnew; } else { printf("wrong choice"); } } void deletion(void) { int y,i; current=start; printf("enter the serial number of node you want to delete "); scanf("%d",&y); if(y==1) { start=start->next; } else { for(i=1;i<y-1;i++) { current=current->next; } if(current->next->next==NULL) { current->next=NULL; } else { current->next=current->next->next; } } printf("node is deleted"); }
I started this Blog to Share my learning's with the community. i share what I learn day by day and how I improve my skills etc. Hope you will find something useful in it.
Friday, 14 March 2014
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment