Lec 7 Double Linked List

Embed Size (px)

Citation preview

  • 8/10/2019 Lec 7 Double Linked List

    1/30

    Lecture : 7

    Doubly Linked List

    Instructor: Anum Masood

    1

  • 8/10/2019 Lec 7 Double Linked List

    2/30

    Drawback of Single Linked List

    In single linked list we can traverse only in onedirection because each node has address of nextnode only.

    Suppose we are in middle of linked list and wewant to perform operation with just previousnode then we have no way to go back onprevious node, we will again traverse fromstarting node.

    Above is the main drawback of single linked list.

  • 8/10/2019 Lec 7 Double Linked List

    3/30

    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;

    }

  • 8/10/2019 Lec 7 Double Linked List

    4/30

    Example: For Understanding Doubly

    Linked List

    start

    start

    next

    NULLPrev

    NULL

  • 8/10/2019 Lec 7 Double Linked List

    5/30

  • 8/10/2019 Lec 7 Double Linked List

    6/30

    Doubly Linked List

    start

  • 8/10/2019 Lec 7 Double Linked List

    7/30

    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.

    The only drawback is that each node has to contain the

    address information of previous node too.

    start

  • 8/10/2019 Lec 7 Double Linked List

    8/30

  • 8/10/2019 Lec 7 Double Linked List

    9/30

    Traversing a Doubly Linked List

    For processing the next element assign the address ofnext node to ptr as:

    ptr=ptr->next;

    Now ptr has the address of next node.

    Traverse each element of list through this assignmentuntil ptr has NULL value which is the next part value oflast element. So the doubly lined list can be traversed

    as:

    while(ptr!=NULL)

    ptr=ptr->next;

  • 8/10/2019 Lec 7 Double Linked List

    10/30

  • 8/10/2019 Lec 7 Double Linked List

    11/30

    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

  • 8/10/2019 Lec 7 Double Linked List

    12/30

    Case 1: Insertion at beginning

    Start points to the first node of doubly linked list.

    For insertion at beginning, assign the value of start tothe 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 wasbeginning node of the doubly linked list and prev partof second node will point to the new inserted node.

  • 8/10/2019 Lec 7 Double Linked List

    13/30

    Case 1: Insertion at beginning

    Now inserted node is the first node of the doublylinked list, so start will be reassigned as:

    start=temp;

    Now start will point to the inserted node which isthe first node of the doubly linked list.

    Assign NULL to prev part of inserted node sincenow it will become the first node and prev part offirst node is NULL

    temp->prev=NULL;

  • 8/10/2019 Lec 7 Double Linked List

    14/30

    Case 2: Insertion in between

    First traverse the doubly linked list for obtaining thenode 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 nextnode.

    Then assign the next part of previous node to the next

    part of inserted nose.

    Address of previous node will be assigned to the prevpart of inserted node and address of inserted node willbe assigned to next part of previous node.

  • 8/10/2019 Lec 7 Double Linked List

    15/30

    Case 2: Insertion in between

    q

  • 8/10/2019 Lec 7 Double Linked List

    16/30

    Case 2: Insertion in between

    Step: 1

    q->next->prev=temp;

    q

  • 8/10/2019 Lec 7 Double Linked List

    17/30

    Case 2: Insertion in between

    Step : 2

    temp->next=q->next;q

  • 8/10/2019 Lec 7 Double Linked List

    18/30

    Case 2: Insertion in between

    Step 3:

    temp->prev=q;

    q

  • 8/10/2019 Lec 7 Double Linked List

    19/30

    Case 2: Insertion in between

    Step 4:

    q->next=temp;

    q

  • 8/10/2019 Lec 7 Double Linked List

    20/30

  • 8/10/2019 Lec 7 Double Linked List

    21/30

    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.

  • 8/10/2019 Lec 7 Double Linked List

    22/30

    DELETION

  • 8/10/2019 Lec 7 Double Linked List

    23/30

    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

  • 8/10/2019 Lec 7 Double Linked List

    24/30

    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 pointsto 2nd node.

    Then NULL will be assigned to the start->prev

    Afterwards delete temp.

  • 8/10/2019 Lec 7 Double Linked List

    25/30

    Case 2: Deletion in between

    If the element is other than the first nodethen first traverse the doubly linked list forobtaining the node (containing key).

    Assign next part of deleted node to the nextpart of the previous node.

    Address of previous node will be assigned tothe prev part of next node.

  • 8/10/2019 Lec 7 Double Linked List

    26/30

    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 deletednode.

    After statement 1, temp contains the address of thenode to be deleted.

    Statement 2, next part of the previous node will pointto the next node of the node to be deleted

    Statement 3, prev part of the next node will point tothe previous node.

  • 8/10/2019 Lec 7 Double Linked List

    27/30

    Case 2: Deletion in between

    q

    q

    q

    q

    temp

    temp

    temp

  • 8/10/2019 Lec 7 Double Linked List

    28/30

    Case 2: Deletion in between

    start

    q

  • 8/10/2019 Lec 7 Double Linked List

    29/30

    Case 3: Deletion from End

    If the node to be deleted is last node of doubly linkedlist then next part of the 2nd last node will containNULL.

    1. temp = q->next; // last node2. free(temp)

    3. q->next= NULL;

    q points to the 2nd last node and q-> next points tolast node.

    Then NULL will be assigned to the start->prev

    Afterwards delete temp.

  • 8/10/2019 Lec 7 Double Linked List

    30/30

    Questions

    30