DOUBLY LINKED LIST INTRODUCTION Doubly Linked List is
DOUBLY LINKED LIST
INTRODUCTION Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List.
CONT…. Doubly Linked List contains a link element called first and last. Each link carries a data field(s) and two link fields called next and prev. Each link is linked with its next link using its next link. Each link is linked with its previous link using its previous link. The last link carries a link as null to mark the end of the list.
OPERATION ON DLL Insertion at beginning Insertion at end Insertion after an element Deletion from beginning Deletion from end Deletion after an element Searching an element Traverse Forward Direction Traverse Backward Direction
STRUCTURE OF A NODE struct node { int data; struct node *next, *prev; } *start, *nn, *loc, *temp, *tail;
INSERTION AT BEG void insert_beg(int value) { nn=malloc(sizeof(struct node)); nn->data=value; nn->prev=NULL; if(start==NULL && tail==NULL) { start=nn; tail=nn; nn->next=NULL; } else { start->prev=nn; nn->next=start; start=nn; } }
INSERTION AT END void insert_end(int value) { nn=malloc(sizeof(struct node)); nn->data=value; if(start==NULL && tail==NULL) { start=nn; tail=nn; nn->next=NULL; nn->prev=NULL; } else { temp=start; while(temp->next!=NULL) { temp=temp->next; } nn->next=NULL; nn->prev=temp; temp->next=nn; tail=nn; }}
INSERT AFTER A NODE void insert_after(int item, int value) { nn=malloc(sizeof(struct node)); if(start==NULL && tail==NULL) { start=nn; tail=nn; nn->next=NULL; nn->prev=NULL; } else { loc=start; while(loc!=NULL) { if(loc->data==item && loc->next==NULL) { nn->prev=loc; nn->next=loc->next; loc->next=nn; nn->data=value; tail=nn; } else if(loc->data==item) { nn->prev=loc; nn->next=loc->next; loc->next->prev=nn; loc->next=nn; nn->data=value; } loc=loc->next; }}}
TRAVERSING FORWARD void traverse_for() { temp=start; while(temp!=NULL) { printf("%dt", temp->data); temp=temp->next; } }
TRAVERSING BACKWARD void traverse_back() { temp=tail; while(temp!=NULL) { printf("%dt", temp->data); temp=temp->prev; } }
DELETION FROM BEG void del_beg() { temp=start; if(start==NULL && tail==NULL) {printf("n List is empty"); } else if(start==tail) {temp=start; start=tail=NULL; printf("%d is deleted ", temp->data); free(temp); } else { temp=start; start->next->prev=NULL; start=start->next; printf("%d is deleted ", temp->data); free(temp); }}
DELETION AT END void del_end() { if(start==NULL && tail==NULL) { printf("n List is empty"); } else { temp=tail; tail=temp->prev; tail->next=NULL; printf("n%d is deleted", temp->data); free(temp); } }
DELETION AFTER A NODE else { void del_after(int item) temp=loc->next; { temp->next->prev=loc; loc=start; loc->next=temp->next; while(loc!=NULL) printf("n%d is deleted ", temp->data); { free(temp); if(loc->data==item) }} { if(loc->next==NULL) loc=loc->next; { } printf("There is no node to delete"); } break; }
SEARCHING A NODE void search(int item) { loc=start; while(loc!=NULL) { if(loc->data==item) { temp=loc; printf("%d is found at %d", item, temp->next); break; } loc=loc->next; } }
- Slides: 14