Doubly Linked List Doubly Linked List Doubly Linked
Doubly Linked List
Doubly Linked List • 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. • A doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. • Each node contains a data field and two pointer fields, called links, that are references to the previous and to the next node in the sequence of nodes. • The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, NULL.
Doubly Linked List start NULL prev data next data prev next data next NULL
Doubly Linked List data structure Structure Declaration for node: struct node { struct node *prev; int data; struct node *next; } *start=NULL;
Functions declaration Following are the functions to be declared before main() struct node *create_list(struct node *start); void display(struct node *start); struct node *addtoempty(struct node *start, int data); struct node *addatbeg(struct node *start, int data); struct node *addatend(struct node *start, int data); struct node *addafter(struct node *start, int data, int item); struct node *addbefore(struct node *start, int data, int item); struct node *del(struct node *start, int data); struct node *reverse(struct node *start);
Program for doubly LL void main() { int choice, n, item; while(1) { printf("1. Create Listn"); printf("2. Displayn"); printf("3. Add to empty listn"); printf("4. Add at beginningn"); printf("5. Add at endn"); printf("6. Add aftern"); printf("7. Add beforen"); printf("8. Deleten"); printf("9. Reversen"); printf("10. Quitn"); printf("Enter your choice : "); scanf("%d", &choice);
switch(choice) { case 1: start=create_list(start); break; case 2: display(start); break; case 3: printf("Enter the element to be inserted : "); scanf("%d", &n); start=addtoempty(start, n); break; case 4: printf("Enter the element to be inserted : "); scanf("%d", &n); start=addatbeg(start, n); break;
case 5: printf("Enter the element to be inserted : "); scanf("%d", &n); start=addatend(start, n); break; case 6: printf("Enter the element to be inserted : "); scanf("%d", &n); printf("Enter the element after which to insert : "); scanf("%d", &item); start=addafter(start, n, item); break; case 7: printf("Enter the element to be inserted : "); scanf("%d", &n); printf("Enter the element before which to insert : "); scanf("%d", &item); start=addbefore(start, n, item); break;
case 8: printf("Enter the element to be deleted : "); scanf("%d", &n); start=del(start, n); break; case 9: start=reverse(start); break; case 10: exit(1); default: printf("Wrong choicen"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/
Create doubly LL struct node *create_list(struct node *start) { int i, n, value; printf("Enter the number of nodes : "); scanf("%d", &n); start=NULL; if(n==0) return start; printf("Enter the element to be inserted : "); scanf("%d", &value); start=addtoempty(start, value); for(i=2; i<=n; i++) { printf("Enter the element to be inserted : "); scanf("%d", &value); start=addatend(start, value); } return start; }/*End of create_list()*/
addtoempty() function struct node *addtoempty(struct node *start, int value) { struct node *tmp; tmp=(struct node *)malloc(sizeof(struct node)); tmp->prev=NULL; tmp->data=value; tmp->next=NULL; start=tmp; return start; }/*End of addtoempty( )*/
addatend() function struct node *addatend(struct node *start, int n) { struct node *tmp, *p; tmp=(struct node *)malloc(sizeof(struct node)); tmp->data=value; p=start; while(p->next!=NULL) { p=p->next; } p->next=tmp; tmp->next=NULL; tmp->prev=p; return start; }/*End of addatend( )*/
display() function void display(struct node *start) { struct node *p; if(start==NULL) { printf("List is emptyn"); return; } p=start; printf("List is : n"); while(p!=NULL) { printf("%d ", p->data); p=p->next; } printf("n"); }/*End of display() */
addatbeg() fuction struct node *addatbeg(struct node *start, int n) { struct node *tmp; tmp = (struct node *)malloc(sizeof(struct node)); tmp->prev=NULL; tmp->data=n; tmp->next=start; start->prev=tmp; start=tmp; return start; }/*End of addatbeg( )*/
addafter() function struct node *addafter(struct node *start, int n, int item) { struct node *tmp, *p; tmp=(struct node *)malloc(sizeof(struct node)); tmp->data=n; p=start; while(p!=NULL) { if(p->data==item) { tmp->prev=p; tmp->next=p->next; if(p->next!=NULL) p->next->prev=tmp; p->next=tmp; return start; printf("%d not present in the listnn", item); } return start; }/*End of addafter()*/ p=p->next; }
addbefore() function struct node *addbefore(struct node *start, int n, int item) q=start; { while(q!=NULL) struct node *tmp, *q; { if(start==NULL ) if(q->data==item) { { printf("List is emptyn"); tmp=(struct node *) return start; malloc(sizeof(struct node)); } tmp->data=n; if(start->data==item) tmp->prev=q->prev; { tmp->next = q; tmp = (struct node *) q->prev->next=tmp; malloc(sizeof(struct node)); q->prev=tmp; tmp->data=n; return start; tmp->prev=NULL; } tmp->next=start; q=q->next; start->prev=tmp; } start=tmp; printf("%d not present in the listn", item); return start; } }/*End of addbefore()*/
reverse() function struct node *reverse(struct node *start) { struct node *p 1, *p 2; p 1=start; p 2=p 1 ->next; p 1 ->next=NULL; p 1 ->prev=p 2; while(p 2!=NULL) { p 2 ->prev=p 2 ->next; p 2 ->next=p 1; p 1=p 2; p 2=p 2 ->prev; } start=p 1; printf("List reversedn"); return start; }/*End of reverse()*/
del() function struct node *del(struct node *start, int n) { struct node *tmp; if(start==NULL) { printf("List is emptyn"); return start; } if(start->next==NULL) /*only one node in the list*/ if(start->data==n) { tmp=start; start=NULL; free(tmp); return start; } else { printf("Element %d not foundn", n); return start; }
/*Deletion of first node*/ if(start->info== n) { tmp=start; start=start->next; start->prev=NULL; free(tmp); return start; }
/*Deletion in between*/ tmp=start->next; while(tmp->next!=NULL ) { if(tmp->info== n) { tmp->prev->next=tmp->next; tmp->next->prev=tmp->prev; free(tmp); return start; } tmp=tmp->next; }
/*Deletion of last node*/ if(tmp->info== n) { tmp->prev->next=NULL; free(tmp); return start; } printf("Element %d not foundn", n); return start; }/*End of del()*/
- Slides: 21