Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 39

Doubly Linked List

Evolution of Doubly linked lists


With the linked list traversal is possible in only one direction. This limitation is overcome with the help of doubly linked lists.

Doubly Linked List


Each node will have : prev information / data pointer/address of next node pointer / address of previous node
next

elem

node

Each node references both its predecessor and its successor The Head pointer points to the first node in the List The Tail pointer Points to the last node in the List
3

Graphical Representation of Doubly Linked Lists with Head and Tail


Head node Data Next
Prev NULL

node Data Next Prev NULL

TAIL

Doubly Linked List Representation


struct node {
struct node *prev; int info; struct node *next;

}; typedef struct Node *NODE;


In a DLL each node has address previous and next node also.
5

Basic Operations on a DoublyLinked List


1. 2. 3. 4. Add a node. Delete a node. Search for a node. Traverse (walk) the list. Useful for counting operations or aggregate operations.

Note: the operations on a doubly-linked list are exactly the same that are required for a singly-linked list.
6

start

Traversing
NULL data next prev data next prev data NULL

while(ptr!==NULL)

{
ptr=ptr->next; }// traversing start to last node

How will you traverse last node to start? Ans:- using tail pointer

Adding Nodes to a DoublyLinked List Adding a Node


There are four steps to add a node to a doubly-linked list: Allocate memory for the new node. Determine the insertion point to be (pCur). Point the new node to its successor and predecessor. Point the predecessor and successor to the new node.

Current node pointer (pCur) can be in one of two states: it can contain the address of a node (i.e. you are adding somewhere after the first node in the middle or at the end) it can be NULL (i.e. you are adding either to an empty list or at the beginning of the list)
8

Adding Nodes to an Empty DoublyLinked List


Initial:
pNew pHead 39

Code:

pNew = (struct node *)

/*create node*/

malloc(sizeof(struct dllnode)); pNew -> data = 39; pCur pNew -> right = pHead; pNew -> left = pHead;
pHead = pNew;
pNew pHead 39

pCur

After:

start

Inserting at beginning
prev data next prev data NULL 1 next

NULL data next 2 prev tmp data

1. tmp->next=start;
2. start->prev=tmp; 3. start=temp;

4. tmp->prev=NULL;

prev data next 2 NULL data 3 start 1 next

prev data next

prev data NULL

10

Insertion
We visualize operation insertAfter(q, X), which returns position q

A q A q A

tmp X tmp

C
11

start

Insertion in between
q NULL data

next

prev

data

next

prev

data NULL

NULL data

next

tmp

1. q->next->prev = tmp; 2. tmp->next = q->next;

3. tmp->prev=q;
4. q->next = tmp;
12

Insertion in between
start

q NULL data

next

prev 1

data

next

prev

data NULL

NULL data

next 1. q->next->prev = tmp; 2. tmp->next = q->next; 3. tmp->prev=q; 4. q->next = tmp;


13

tmp

Insertion in between
start

q NULL data

next

prev 1

data

next 2 next

prev

data NULL

NULL data

tmp
1. q->next->prev = tmp; 2. tmp->next = q->next; 3. tmp->prev=q; 4. q->next = tmp;
14

Insertion in between
start

q NULL data

next

prev 1 prev

data

next 2

prev

data NULL

data

next 1. q->next->prev = tmp; 2. tmp->next = q->next; 3. tmp->prev=q; 4. q->next = tmp;


15

tmp

Insertion in between
start

q NULL data

next

prev 1

data

next 2 next

prev

data NULL

NULL data

tmp
1. q->next->prev = tmp; 4 2. tmp->next = q->next; 3. tmp->prev=q; 4. q->next = tmp;
16

Insertion at end
1. tmp->prev=q; 2. q->next=tmp; 3. tmp->next=NULL;

start q NULL data next prev data next prev 2 data NULL

null 1

data tmp

null
17

//insert a node into a linked list struct node *pNew; pNew = (struct node *) malloc(sizeof(struct node)); pNew -> data = item; if (q == NULL){ //add before first logical node or to an empty list pNew -> left = null; pNew -> right = start; start = pNew; } else { if (q -> right == NULL) { //add at the end pNew -> left = q; pNew -> right = q -> right; q -> right = pNew; }

code

18

Code..
else { //add in the middle q -> right->left = pNew; pNew -> right = q -> right; pNew -> left = q; q -> right = pNew; } } }

19

Deleting a Node from a DoublyLinked 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 (pCur). Change the predecessors and succesors link fields to point each other (see example). Recycle the node using the free() function.
20

Deletion from doubly list


Deletion at the beginning Deletion in between Deletion of last node

21

start

Deletion at the beginning


tmp NULL data next prev data next prev data NULL

tmp =start; start=start->next; start->prev=NULL; free(tmp);

22

start

tmp NULL data

next

prev

data

next

prev

data NULL

tmp =start; start=start->next; start->prev=NULL; free(tmp);

Deletion at the beginning

23

start

tmp NULL data

next

NULL data

next

prev

data NULL

tmp =start; start=start->next; start->prev=NULL; free(tmp);

Deletion at the beginning

24

Doubly Linked List


To delete the node that curr references
curr.getPrecede().setNext(curr.getNext()); curr.getNext().setPrecede(curr.getPrecede( ));

Figure 5-28
Reference changes for deletion
25

start

Deletion in between
NULL data next prev tmp data next prev data NULL

tmp=q->next; q->next=temp->next; tmp->next->prev=q; free(temp);


26

start

Deletion in between
NULL data next prev tmp data next 2 prev data NULL

tmp=q->next; q->next=temp->next; tmp->next->prev=q; free(temp);


27

start

Deletion in between
3 tmp data next 2 NULL data next prev prev data NULL

tmp=q->next; q->next=temp->next; tmp->next->prev=q; free(temp);


28

start

Deletion in between
3 NULL data next 2 prev data NULL

tmp=q->next; q->next=temp->next; tmp->next->prev=q; free(temp);


29

Deletion
We visualize remove(p), where p == last() p D

A A

B B

C C

D
A B C
30

Deletion of last node


start

NULL data

next

prev

data

next

tmp prev data NULL

If node to be deleted is last node of DLL then we will just free the last node and next part of second last node will be NULL:
tmp = q->next;// q is second last node free(tmp); q->next = NULL;
31

Deletion of last node


start

NULL data

next

prev

data

next

tmp = q->next;// q is second last node free(tmp); q->next = NULL;

32

Worst-cast running time


In a doubly linked list
+ insertion at head or tail is in O(1) + deletion at either end is on O(1) -- element access is still in O(n)

33

Searching a Doubly-Linked List


Notice that both the insert and delete operations on a linked list must search the list for either the proper insertion point or to locate the node corresponding to the logical data value that is to be deleted.
//search the nodes in a linked list pCur = pHead; //search until the target value is found or the end of the list is reached while (pCur != NULL && pCur -> data != target) { pCur = pCur -> right; } //determine if the target is found or ran off the end of the list if (pCur != NULL) found = 1; else found = 0;

34

special cases in DLL-circular DLL

Figure 5-27
a) A circular doubly linked list with a dummy head node; b) an empty list with a dummy head node
35

Circular DLL
Insertion and Deletion at middle operations are same as that of simple DLL What are the operation for Insertion/Deletion in front or last.
Insertion at beginning or last is same since list is circular Similarly deletion at beginning or last is same since list is circular
36

Advantages and Disadvantages


Advantages 1. The number of elements in a doubly linked list need not be known in advance. As and when required elements can be added to a list. 2. Insertions and deletions are much faster as there is no physical movement of elements of a list. 3. Traversal is possible in both the directions unlike a linear list where the traversal is possible in only one direction.
37

Advantages and Disadvantages contd


Limitations
1. Takes up additional memory for storing the previous pointer in each node. 2. Makes the insertion and deletion logic a bit complex.

38

Inserting at Specified Position


To insert a new node that newNode references before the node referenced by curr
newNode.setNext(curr); newNode.setPrecede(curr.getPrecede()); curr.setPrecede(newNode); newNode.getPrecede().setNext(newNode);
Figure 5-29
Reference changes for insertion

39

You might also like