2. Double Linked list
• Single linked list= one-way list
– List can be traversed in one direction only
• Double linked list= Two-way list
– List can be traversed in two directions
• A two- way list is a linear collection of data elements
called nodes where each node N is divided in to three
parts
– Data field contains the data of N
– LLINK field contains the pointer to the preceding Node in the
list
– RLINK field contains the pointer to the next node in the list
3. Double Linked list
– Data field contains the data of N
– Rlink field contains the pointer to the next Node in the list
– Llink field contains the pointer to the preceding node in the list
Data Rlink
Llink
4. Dr Zeinab Eid 4
Double Linked List
• Each node contain:
• Element/data
• link to the previous node
• link to the next node
llink rlink
data
10 20 30 40 null
head
node
6. Node creation
struct node
{
int data; //data item in node
struct node *llink,*rlink; // reference to leftt & right node
};
data rlink
llink
7. Insertion
• In a double linked list, a new node can be inserted:
1. At the first location
2. At the last location
3. At any position
8. Insertion at 1st location
1. head-> llink=temp
2. temp-> rlink=head
3. head=temp
temp
llink data rlink
20
head
10
null 30 null
5
null
9. Insertion in the front
1. Set ptr=head
2. Create a new node temp //struct node *temp=(struct node*)
malloc(sizeof(struct node));
3. If(temp!= NULL)
4. Read ‘item’ to be inserted
temp->data =item
5. head->llink=temp
6. temp-> rlink=head
7. head=temp
8. Else
9. Print “Unable to allocate memory”
10. Insertion at last location
1. ptr->rlink=temp
2. temp->llink=ptr
20
head
10
null 30 null
35 null
null
Prev data next
temp
ptr
11. Insertion at last location
1. Set ptr=head
2. Create a new node temp and read the element “item” to be inserted
3. temp->data= item
4. While(ptr-rlink!=null)
5. ptr=ptr->rlink
6 ptr->rlink=temp
7. temp->llink=ptr
20
head
10
null 30 null
35 null
null
Prev data next
temp
ptr
12. Insertion- after an element key
20
head
10
null 30 null
25
Prev data next
temp
ptr
Insert 25 after 20
13. Insertion- after an element key
1. Read value key after which new node is to be inserted
2. Create temp and read element to be inserted
3. Set ptr=head
4. Repeat while( ptr->data!=key)
5. ptr=ptr->rlink
6. If(ptr-> data==key)
6. temp->rlink=ptr->rlink //7. set ptr1=ptr->rlink
7. ptr->rlink->llink= temp ptr1->llink=temp
8. temp->llink=ptr
9. ptr->rlink=temp
14. Deletion
• In a double linked list, an element can be
deleted:
1. From the 1st location
2. From the last location
3. From intermediate location
18. Circular double linked list
• The advantages of both double linked list and circular
linked list are incorporated into another type of list
structure called circular double linked list
20 null
10 30
head
19. Circular double linked list
• Here rlink of last node and llink of the first node points
back to the head node.
• Llink of head node contain the address of leftmost node
• Rlink of head node contain the address of the rightmost
node
• Empty circular double linked list is represented as
• Here both llink and rlink of head points to itself