Lecture : 7

Doubly Linked List
Instructor: Anum Masood

1

Drawback of Single Linked List
• In single linked list we can traverse only in one
direction because each node has address of next
node only.
• Suppose we are in middle of linked list and we
want to perform operation with just previous
node then we have no way to go back on
previous node, we will again traverse from
starting node.
• Above is the main drawback of single linked list.

Solution of the problem: Doubly
Linked List
• To solve the problem of single linked list a data structure
named ‘doubly linked list’ is introduced.
• In doubly linked list each node has address of both nodes
which are previous and next node.
• The data structure for doubly linked list will be as:
struct node{
struct node *previous;
int info;
struct node *next;
}

Example: For Understanding Doubly Linked List start next NULL Prev NULL start .

} info .Doubly Linked List struct node{ struct node *prev. int info. struct node *next.

Doubly Linked List start .

Doubly Linked List • Here struct node *prev is a pointer to structure. start • The only drawback is that each node has to contain the address information of previous node too. which will contain the address of previous node. • Struct node *next contains the address of next node in the list. . • So in doubly linked list traversing can be performed in both direction.

Traversing/ Displaying a Doubly Linked List • In doubly linked list info is the information part. Next is the address of the next node and prev is the address of previous node. • Start points to the first element of the list. . so ptr also points to the first node of the list. • Take ‘ptr’ (same as q) as a pointer variable. • Initially assign the value of start to ‘ptr’.

So the doubly lined list can be traversed as: while(ptr!=NULL) ptr=ptr->next. • Traverse each element of list through this assignment until ptr has NULL value which is the next part value of last element.Traversing a Doubly Linked List • For processing the next element assign the address of next node to ptr as: ptr=ptr->next. • Now ptr has the address of next node. .

Doubly Linked List Data-structure start .

Insertion in between . Insertion at beginning 2.Insertion into a Doubly Linked List • Insertion in a doubly linked list may be possible in two ways: 1.

assign the value of start to the next part of the inserted node and address of the inserted node to the prev part of start as: temp->next=start. • Now inserted node points to the next node. . • For insertion at beginning. which was beginning node of the doubly linked list and prev part of second node will point to the new inserted node.Case 1: Insertion at beginning • Start points to the first node of doubly linked list. start->prev=temp.

Case 1: Insertion at beginning • Now inserted node is the first node of the doubly linked list. . so start will be reassigned as: start=temp. • Assign NULL to prev part of inserted node since now it will become the first node and prev part of first node is NULL temp->prev=NULL. • Now start will point to the inserted node which is the first node of the doubly linked list.

. • For inserting the element after the node assign the address of inserted node to the prev part of the next node. • Address of previous node will be assigned to the prev part of inserted node and address of inserted node will be assigned to next part of previous node.Case 2: Insertion in between • First traverse the doubly linked list for obtaining the node after which new element is to be inserted. • Then assign the next part of previous node to the next part of inserted nose.

Case 2: Insertion in between q .

q .Case 2: Insertion in between • Step: 1 q->next->prev=temp.

q .Case 2: Insertion in between • Step : 2 temp->next=q->next.

q .Case 2: Insertion in between • Step 3: temp->prev=q.

Case 2: Insertion in between • Step 4: q->next=temp. q .

prev part of next node will point to inserted node. • Here ‘q’ point to the previous node (after that new node will be inserted).Case 2: Insertion in between q->next->prev=temp. next part of the inserted node will point to the next node. • After statement 2. temp->prev=q. • After statement . . temp->next=q->next. q->next=temp.

prev part of inserted nose will point to it’s previous node. • After statement 4. . next part of previous node will point to inserted node.Case 2: Insertion in between • After statement 3.

DELETION .

Deletion at beginning 2.Deletion from Doubly Linked List • Insertion in a doubly linked list may be possible in two ways: 1. Deletion in between 3. Deletion of last node .

num. then assign: temp = start. • Then NULL will be assigned to the start->prev • Afterwards delete temp. • If first node contains the key i.e. . start = start->next.Case 1: Deletion at beginning • Start points to the first node of doubly linked list. // 2nd node into start start->prev= NULL. free(temp) • Start points to the first node and start-> next points to 2nd node.

Case 2: Deletion in between • If the element is other than the first node then first traverse the doubly linked list for obtaining the node (containing key). . • Assign next part of deleted node to the next part of the previous node. • Address of previous node will be assigned to the prev part of next node.

free(temp). 2. • Here ‘q’ point to the previous node of the deleted node. 4. prev part of the next node will point to the previous node. 3. temp = q->next. temp contains the address of the node to be deleted. . • After statement 1.Case 2: Deletion in between 1. temp->next->prev = q. next part of the previous node will point to the next node of the node to be deleted • Statement 3. q->next = temp->next. • Statement 2.

Case 2: Deletion in between q temp q temp q temp q .

Case 2: Deletion in between start q .

q->next= NULL. • q points to the 2nd last node and q-> next points to last node. • Then NULL will be assigned to the start->prev • Afterwards delete temp. temp = q->next.Case 3: Deletion from End • If the node to be deleted is last node of doubly linked list then next part of the 2nd last node will contain NULL. 1. . // last node 2. free(temp) 3.

Questions 30 .