Double Linked lists
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
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
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
Node creation
Algorithm: Struct Node
1. Start
2. Declare int data, Node *llink, Node *rlink
3. exit
Data null
null
Node creation
struct node
{
int data; //data item in node
struct node *llink,*rlink; // reference to leftt & right node
};
data rlink
llink
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
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
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”
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
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
Insertion- after an element key
20
head
10
null 30 null
25
Prev data next
temp
ptr
Insert 25 after 20
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
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
Deletion- from 1st location
• Delete node 10
1. head=head->rlink
2. head->llink= null
20
head
10
null null 30 null
Deletion- from last location
• Delete node 30
1. Set ptr=head
2. While(ptr->rlink!=null)
3. ptr=ptr->rlink
4. Set ptr1=ptr->llink
5. ptr1->rlink=null
6. free(ptr)
20 null
head
10
null 30 null
ptr
ptr1
Deletion- from intermediate location
• Delete node key=20
1. Set ptr=head
2. Repeat While (ptr-> data!=key)&& ptr->rlink!=null)
3. ptr=ptr->rlink
4. If(ptr->data=key)
5. ptr1=ptr->llink
6. ptr2=ptr->rlink
7. ptr1->rlink=ptr2
8. ptr2->llink=ptr1
9. free(ptr)
20
head
10
null 30 null
ptr
ptr1 ptr2
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
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

Double linked list.pptx

  • 1.
  • 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 Eid4 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
  • 5.
    Node creation Algorithm: StructNode 1. Start 2. Declare int data, Node *llink, Node *rlink 3. exit Data null null
  • 6.
    Node creation struct node { intdata; //data item in node struct node *llink,*rlink; // reference to leftt & right node }; data rlink llink
  • 7.
    Insertion • In adouble 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 1stlocation 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 thefront 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 lastlocation 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 lastlocation 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 anelement key 20 head 10 null 30 null 25 Prev data next temp ptr Insert 25 after 20
  • 13.
    Insertion- after anelement 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 adouble linked list, an element can be deleted: 1. From the 1st location 2. From the last location 3. From intermediate location
  • 15.
    Deletion- from 1stlocation • Delete node 10 1. head=head->rlink 2. head->llink= null 20 head 10 null null 30 null
  • 16.
    Deletion- from lastlocation • Delete node 30 1. Set ptr=head 2. While(ptr->rlink!=null) 3. ptr=ptr->rlink 4. Set ptr1=ptr->llink 5. ptr1->rlink=null 6. free(ptr) 20 null head 10 null 30 null ptr ptr1
  • 17.
    Deletion- from intermediatelocation • Delete node key=20 1. Set ptr=head 2. Repeat While (ptr-> data!=key)&& ptr->rlink!=null) 3. ptr=ptr->rlink 4. If(ptr->data=key) 5. ptr1=ptr->llink 6. ptr2=ptr->rlink 7. ptr1->rlink=ptr2 8. ptr2->llink=ptr1 9. free(ptr) 20 head 10 null 30 null ptr ptr1 ptr2
  • 18.
    Circular double linkedlist • 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 linkedlist • 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