WordPress Hosting
Free Photos
WordPress Themes

Double Link List in C

#include<stdlib.h> #include<stdio.h> typedef struct node{ int data; struct node *next, *prev; }nptr; nptr *start=NULL; void createList(){ if(start!=NULL){ printf("\nList Exists\n"); return; }else{ nptr *tmp; tmp=(nptr *)malloc(sizeof(nptr)); printf("\nEnter Data\n"); scanf("%d",&tmp->data); tmp->prev=NULL; tmp->next=NULL; start=tmp; printf("\nLinked List Created\n"); } } void insertEnd(){ nptr *tmp,*t=start; tmp=(nptr *)malloc(sizeof(nptr)); printf("\nEnter Data\n"); scanf("%d",&tmp->data); tmp->next=NULL; while(t->next!=NULL){ t=t->next; } t->next=tmp; tmp->prev=t; printf("\nnode added at end\n"); } void insertFront(){ nptr *tmp; tmp=(nptr *)malloc(sizeof(nptr)); printf("\nEnter Data\n"); scanf("%d",&tmp->data); tmp->next=start; tmp->prev=NULL; start->prev=tmp; start=tmp; printf("\nnode added at front\n"); } void insertBetween(){ nptr *tmp,*t=start; int x; tmp=(nptr *)malloc(sizeof(nptr)); printf("\nEnter Data\n"); scanf("%d",&tmp->data); printf("\nEnter data of node after which new node will be inserted\n"); scanf("%d",&x); while(t->data!=x){ t=t->next; } tmp->prev=t; tmp->next=t->next; t->next->prev=tmp; t->next=tmp; printf("\nnode added in between\n"); } void deleteEnd(){ nptr *tmp=start; while(tmp->next->next!=NULL){ tmp=tmp->next; } free(tmp->next); tmp->next=NULL; printf("\nLast node deleted\n"); } void deleteFront(){ nptr *tmp=start; start->next->prev=NULL; start=start->next; free(tmp); printf("\nFirst node deleted\n"); } void deleteBetween(){ nptr *tmp=start,*t; int x; printf("\nEnter data of nptr to be deleted\n"); scanf("%d",&x); while(tmp->next->data!=x){ tmp=tmp->next; } t=tmp->next; tmp->next=t->next; t->next->prev=t->prev; free(t); printf("\nnode deleted from middle\n"); } void display(){ nptr *tmp=start; while(tmp!=NULL){ printf(" %d ",tmp->data); if(tmp->next!=NULL) printf(" -> "); tmp=tmp->next; } printf("\n\n\n"); } void displayRev(){ nptr *tmp=start; while(tmp->next!=NULL){ tmp=tmp->next; } while(tmp!=NULL){ printf(" %d ",tmp->data); if(tmp->prev!=NULL) printf(" <- "); tmp=tmp->prev; } printf("\n\n\n"); } void main(){ int ch; while(1){ printf("\n1 Create\n2 Insert End\n3 Insert Front\n4 Insert Middle\n5 Delete End\n6 Delete Front\n7 Delete middle\n8 Display\n9 Delete Rev\n10 Exit\n\n"); scanf("%d",&ch); switch(ch){ case(1):createList(); break; case(2):insertEnd(); break; case(3):insertFront(); break; case(4):insertBetween(); break; case(5):deleteEnd(); break; case(6):deleteFront(); break; case(7):deleteBetween(); break; case(8):display(); break; case(9):displayRev(); break; case(10):exit(0); } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.