0% found this document useful (0 votes)
79 views30 pages

Lec 7 Double Linked List

Doubly linked lists allow traversal in both directions by including a pointer to the previous node as well as the next node in each node. This overcomes the limitation of single linked lists where traversal is only possible in one direction. Doubly linked lists support efficient insertion and deletion at both the head and within the list by updating the next and previous pointers of the nodes before and after the insertion or deletion point.

Uploaded by

KhanWasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views30 pages

Lec 7 Double Linked List

Doubly linked lists allow traversal in both directions by including a pointer to the previous node as well as the next node in each node. This overcomes the limitation of single linked lists where traversal is only possible in one direction. Doubly linked lists support efficient insertion and deletion at both the head and within the list by updating the next and previous pointers of the nodes before and after the insertion or deletion point.

Uploaded by

KhanWasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Lecture : 7

Doubly Linked List


Instructor: Anum Masood

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

Doubly Linked List


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

Doubly Linked List

start

Doubly Linked List


Here struct node *prev is a pointer to structure, 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.
start

The only drawback is that each node has to contain the


address information of previous node too.

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.
Take ptr (same as q) as a pointer variable.
Start points to the first element of the list.
Initially assign the value of start to ptr, so ptr
also points to the first node of the list.

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.
Traverse each element of list through this assignment
until ptr has NULL value which is the next part value of
last element. So the doubly lined list can be traversed
as:
while(ptr!=NULL)
ptr=ptr->next;

Doubly Linked List Data-structure


start

Insertion into a Doubly Linked List


Insertion in a doubly linked list may be
possible in two ways:
1. Insertion at beginning
2. Insertion in between

Case 1: Insertion at beginning


Start points to the first node of doubly linked list.
For insertion at beginning, 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;
start->prev=temp;
Now inserted node points to the next node, 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


Now inserted node is the first node of the doubly
linked list, so start will be reassigned as:
start=temp;
Now start will point to the inserted node which is
the first node of the doubly linked list.
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;

Case 2: Insertion in between


First traverse the doubly linked list for obtaining the
node after which new element is to be inserted.
For inserting the element after the node assign the
address of inserted node to the prev part of the next
node.
Then assign the next part of previous node to the next
part of inserted nose.
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


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;
q

Case 2: Insertion in between


Step 4:
q->next=temp;
q

Case 2: Insertion in between


q->next->prev=temp;
temp->next=q->next;
temp->prev=q;
q->next=temp;
Here q point to the previous node (after that new node
will be inserted).
After statement , prev part of next node will point to
inserted node.
After statement 2, next part of the inserted node will point
to the next node.

Case 2: Insertion in between


After statement 3, prev part of inserted nose
will point to its previous node.
After statement 4, next part of previous node
will point to inserted node.

DELETION

Deletion from Doubly Linked List


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

Case 1: Deletion at beginning


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

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.

Case 2: Deletion in between


1.
temp = q->next;
2.
q->next = temp->next;
3.
temp->next->prev = q;
4.
free(temp);
Here q point to the previous node of the deleted
node.
After statement 1, temp contains the address of the
node to be deleted.
Statement 2, next part of the previous node will point
to the next node of the node to be deleted
Statement 3, prev part of the next node will point to
the previous node.

Case 2: Deletion in between


q
temp

q
temp

q
temp

Case 2: Deletion in between

start

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.
temp = q->next; // last node
2.
free(temp)
3.
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.

Questions

30

You might also like