Presentation is loading. Please wait.

Presentation is loading. Please wait.

Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application.

Similar presentations


Presentation on theme: "Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application."— Presentation transcript:

1 Doubly Linked List

2 Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application  Advantage & disadvantage

3 Linked List  Linked list is data structure used to store similar data in memory (may or may not be in adjacent memory location )  Types of linked list 1. Singly linked list 2. Doubly linked list 3. Circular linked list

4 Structure of linked list Struct node { int info; struct node *next; }; infonext node

5 Doubly linked list  Doubly linked list is linked data structure that consist of sequentially linked records called nodes and each node consist of two fields called links.  Two fields(links): 1. Previous (backward) 2. Next(forward)  Head is the starting node or first node  Tail is the ending node or last node

6 Circular linked lists  Last node references the first node  Every node has a successor  No node in a circular linked list contains NULL

7 Representation of node  Node of doubly linked list consist of three parts: 1. previous 2. data 3. next prev data next node

8 Representation  If there is only one sentinel node then the list is circularly linked via the sentinel node. It can be concept as two singly linked list formed from the same data item, but in opposite sequencial order

9 Structure of node  doubly linked list is a list that contains links to links to next and previous nodes. Doubly linked list allows traversal in both ways typedef struct*node ; { void *data; struct node *next; struct node *prev; } node;

10 Dummy head nodes  Eliminates the special case for insertion into & deletion from beginning of linked list.  Dummy head node Always present, even when the linked list is empty. insertion & deletion algorithms initialize previous to reference the dummy head node rather than NULL

11 (a) A circular doubly linked list with a dummy head node (b) An empty list with a dummy head node

12 Double-ended list  Doubly –linked list can also be created as double- ended list  The reference to the last link permits to insert a new link directly at the end of the list as well as at the beginning.  This could not be done with ordinary linked list without traversing the whole list. NULL First last

13 Doubly linked list

14 Inserting a node in doubly linked list A node can be inserted: a)after given node b)before given node c) at the beginning of an empty list d) at the end of an empty list

15 Inserting a node in doubly linked list

16 Suppose a new node, B needs to be inserted after the node A Code: void insertafter() struct node *B; B=(struct node *)malloc(sizeof(struct node)); A->next = B->next; A->next = B; B->prev = A; (B->next)->prev = B;

17 Inserting a node in doubly linked list Suppose a new node, B needs to be inserted before the node C void insertbefore() struct node *B; B=(struct node *)malloc(sizeof(struct node)); C->prev=B->prev; C->prev=B; B->next=C; (B->next)->prev = B;

18 Inserting a node in doubly linked list Suppose a new node, B needs to be inserted before the node A Void insertbegin() Struct node *B; B=(struct node *)malloc(sizeof(struct node)); B->next=A; A->prev=B; B->prev=NULL;

19 Inserting a node in doubly linked list Suppose a new node, B needs to be inserted after the node C Void insertend() Struct node *B; B=(struct node *)malloc(sizeof(struct node)); C->next=B; B->prev=C; B->next=NULL;

20 Page 2020 Deleting a Node from a Doubly-Linked List Deleting a node requires that we logically remove the node from the list by changing various links and then physically deleting the node from the list (i.e., return it to the heap). Any node in the list can be deleted. Note that if the only node in the list is to be deleted, an empty list will result. In this case the head pointer will be set to NULL. To logically delete a node: ◦ First locate the node itself. ◦ Change the predecessor’s and succesor’s link fields to point each other. ◦ Recycle the node using the free() function.

21 Page 2121 Deleting the First Node from a Doubly-Linked List Deletion At First: If(first==NULL) printf(“memory of link list is empty”); else { *p=first First=first->rptr first->lptr=NULL Free(p) }

22 Page 2222 DELETION OF FIRST NODE Before: After: 7512446 75124 Recycled 7723 4677 p first NN NN

23 Page 2323 Deleting a Last Node From a Doubly-Linked List The Code if (first == NULL) { printf(“memory of link list is empty”); } Else { *p=first while(p->rptr != NULL) { P=p->rptr } p->lptr->rptr=NULL free(p)

24 Deletion At Last Node Before: Page 2424 After: 446814 Deleted Node 146844 NN NN P

25 Traversing the Doubly linklist 1.Traversal of a doubly-linked list can be in either direction. 2.In fact, the direction of traversal can change many times, if desired. Traversal is often called iteration, but that choice of terminology is unfortunate, for iteration has well-defined semantics (e.g., in mathematics) which are not analogous to traversal.iteration

26 A doubly linked list can be traversed either way and that too very conveniently. 1.Inorder/Forward Traversal 2.reversorder/Backward Traversal

27 Inorder Traversal To traverse the doubly linked list, we walk the list from the beginning, and process each element until we reach the last element. void traverseinorder(node *head) { while(head!=NULL) { printf("%dn",head->info); head=head->next; } To call the above function, use: traverseinorder(head);

28 Revers order traversal The following listing shows how to traverse a doubly linked list in the backward direction. Note that the tail pointer will need to be passed in a call to this function. void traversereverseorder(node *tail) { if(tail!=NULL) { printf("%dn",tail->info); //print data tail = tail->prev; // go to previous node } To call the above function, use: traversereverseorder(tail);

29 Applications of a doubly linked list - A great way to represent a deck of cards in a game. - Applications that have a Most Recently Used (MRU) list (a linked list of file names) - A stack, hash table, and binary tree can be implemented using a doubly linked list. - Undo functionality in Photoshop or Word (a linked list of state).

30 Advantage  We can traverse in both directions i.e. from starting to end and as well as from end to starting.  It is easy to reverse the linked list.  If we are at a node, then we can go to any node. But in linear linked list, it is not possible to reach the previous node.

31 Disadvantage  It requires more space per node because one extra field is required for pointer to previous node.  Insertion and deletion take more time than linear linked list because more pointer operations are required than linear linked list.

32 THANK YOU THANK YOU


Download ppt "Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application."

Similar presentations


Ads by Google