06 ds and algorithm session 08

Upload: ashish-srivastava

Post on 14-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 06 DS and Algorithm Session 08

    1/182

    Ver. 1.0 Session 8

    Data Structures and AlgorithmsObjectives

    In this chapter, you will learn to:

    Implement a doubly-linked list

    Implement a circular linked list

    Apply linked lists to solve programming problems

  • 8/2/2019 06 DS and Algorithm Session 08

    2/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    Consider a sorted list of 100000 numbers stored in a linked

    list.

    If you want to display these numbers in ascending order,

    what will you do?

    Traverse the list starting from the first node.

    Implementing a Doubly-Linked List

    Now consider a case in which you need to display these

    numbers in a descending order.

    How will you solve this problem?

    Each node is linked to the next node in sequence.

    This means that you can traverse the list in the forwarddirection only.

    Such a linked list is called a singly-linked list.

    To display the numbers in the descending order, you need to

    reverse the linked list.

  • 8/2/2019 06 DS and Algorithm Session 08

    3/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    Algorithm to reverse a

    singly-linked list.

    1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    6. Make the next field of ptr1 point to NULL.

    7. Make the next field of ptr2 point to ptr1.

    8. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    c. Make ptr3 point to the next node in

    its sequence.

    d. Make the next field of ptr2 point to

    ptr1.

    9. Make START point to ptr2.

    START

    10 15 19 21 32

    Implementing a Doubly-Linked List (Contd.)

  • 8/2/2019 06 DS and Algorithm Session 08

    4/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    1. If there is only one node in the list:

    a. Exit.

    2. Mark the first node in the list as ptr1.

    3. Mark the successor of ptr1 as ptr2

    4. Mark the successor of ptr2 as ptr3.

    5. Make the next field of ptr1 point to NULL.

    6. Make the next field of ptr2 point to ptr1.

    7. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    c. Make ptr3 point to the next node in

    its sequence.

    d. Make the next field of ptr2 point to

    ptr1.

    8. Make START point to ptr2.

    ptr1 ptr2 ptr3

    Implementing a Doubly-Linked List (Contd.)

  • 8/2/2019 06 DS and Algorithm Session 08

    5/182

  • 8/2/2019 06 DS and Algorithm Session 08

    6/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr1 ptr2 ptr3

    ptr1

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    1. Mark the first node in the list as ptr1.

    1. Mark the successor of ptr1 as ptr2

    2. Mark the successor of ptr2 as ptr3.

    3. Make the next field of ptr1 point to NULL.

    4. Make the next field of ptr2 point to ptr1.

    5. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    c. Make ptr3 point to the next node in

    its sequence.

    d. Make the next field of ptr2 point to

    ptr1.

    6. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    7/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr2 ptr3

    ptr1 ptr2

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    1. Mark the successor of ptr1 as ptr2

    1. Mark the successor of ptr2 as ptr3.

    2. Make the next field of ptr1 point to NULL.

    3. Make the next field of ptr2 point to ptr1.

    4. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    c. Make ptr3 point to the next node in

    its sequence.

    d. Make the next field of ptr2 point to

    ptr1.

    5. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    8/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr3

    ptr1 ptr2 ptr3

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    1. Mark the successor of ptr2 as ptr3.

    1. Make the next field of ptr1 point to NULL.

    2. Make the next field of ptr2 point to ptr1.

    3. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    c. Make ptr3 point to the next node in

    its sequence.

    d. Make the next field of ptr2 point to

    ptr1.

    4. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    9/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr1 ptr2 ptr3

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    1. Make the next field of ptr1 point to NULL.

    1. Make the next field of ptr2 point to ptr1.

    2. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    c. Make ptr3 point to the next node in

    its sequence.

    d. Make the next field of ptr2 point to

    ptr1.

    3. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    10/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr1 ptr2 ptr3

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    6. Make the next field of ptr1 point to NULL.

    1. Make the next field of ptr2 point to ptr1.

    1. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    c. Make ptr3 point to the next node in

    its sequence.

    d. Make the next field of ptr2 point to

    ptr1.

    2. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    11/182

  • 8/2/2019 06 DS and Algorithm Session 08

    12/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr1 ptr2 ptr3

    ptr1

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    6. Make the next field of ptr1 point to NULL.

    7. Make the next field of ptr2 point to ptr1.

    8. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    a. Set ptr2 = ptr3

    b. Make ptr3 point to the next node in

    its sequence.

    c. Make the next field of ptr2 point to

    ptr1.

    9. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    13/182

  • 8/2/2019 06 DS and Algorithm Session 08

    14/182

  • 8/2/2019 06 DS and Algorithm Session 08

    15/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr2

    ptr3

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    6. Make the next field of ptr1 point to NULL.

    7. Make the next field of ptr2 point to ptr1.

    8. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    c. Make ptr3 point to the next node in

    its sequence.

    a. Make the next field of ptr2 point to

    ptr1.

    1. Make START point to ptr2.

    ptr1

  • 8/2/2019 06 DS and Algorithm Session 08

    16/182

  • 8/2/2019 06 DS and Algorithm Session 08

    17/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr2

    ptr3ptr1

    ptr2

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    6. Make the next field of ptr1 point to NULL.

    7. Make the next field of ptr2 point to ptr1.

    8. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    a. Set ptr2 = ptr3

    a. Make ptr3 point to the next node in

    its sequence.

    b. Make the next field of ptr2 point to

    ptr1.

    9. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    18/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr2

    ptr3ptr1 ptr3

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    6. Make the next field of ptr1 point to NULL.

    7. Make the next field of ptr2 point to ptr1.

    8. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    a. Make ptr3 point to the next node in

    its sequence.

    a. Make the next field of ptr2 point to

    ptr1.

    9. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    19/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr2

    ptr3ptr1

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    6. Make the next field of ptr1 point to NULL.

    7. Make the next field of ptr2 point to ptr1.

    8. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    c. Make ptr3 point to the next node in

    its sequence.

    a. Make the next field of ptr2 point to

    ptr1.

    1. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    20/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr3

    ptr2

    ptr1 ptr1

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    6. Make the next field of ptr1 point to NULL.

    7. Make the next field of ptr2 point to ptr1.

    8. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    a. Set ptr2 = ptr3

    b. Make ptr3 point to the next node in

    its sequence.

    c. Make the next field of ptr2 point to

    ptr1.

    9. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    21/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr3ptr1

    ptr2 ptr2

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    6. Make the next field of ptr1 point to NULL.

    7. Make the next field of ptr2 point to ptr1.

    8. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    a. Set ptr2 = ptr3

    a. Make ptr3 point to the next node in

    its sequence.

    b. Make the next field of ptr2 point to

    ptr1.

    9. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    22/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr3ptr1

    ptr2

    ptr3 = NULL

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    6. Make the next field of ptr1 point to NULL.

    7. Make the next field of ptr2 point to ptr1.

    8. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    a. Make ptr3 point to the next node in

    its sequence.

    a. Make the next field of ptr2 point to

    ptr1.

    9. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    23/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    10 15 19 21 32

    ptr1

    ptr2

    ptr3 = NULL

    Implementing a Doubly-Linked List (Contd.)1. Declare three variables / pointers ptr1,

    ptr2, and ptr 3.

    2. If there is only one node in the list:

    a. Exit.

    3. Mark the first node in the list as ptr1.

    4. Mark the successor of ptr1 as ptr2

    5. Mark the successor of ptr2 as ptr3.

    6. Make the next field of ptr1 point to NULL.

    7. Make the next field of ptr2 point to ptr1.

    8. Repeat until ptr3 becomes NULL

    a. Set ptr1 = ptr2

    b. Set ptr2 = ptr3

    c. Make ptr3 point to the next node in

    its sequence.

    a. Make the next field of ptr2 point to

    ptr1.

    1. Make START point to ptr2.

  • 8/2/2019 06 DS and Algorithm Session 08

    24/182

  • 8/2/2019 06 DS and Algorithm Session 08

    25/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    What is the problem with the previous algorithm?

    You need to adjust the links for all the three variables

    whenever you visit the next node.

    Disadvantage of this approach:

    This approach is inefficient and time consuming for large lists.

    How can you solve this problem?

    This problem can be solved if each node in the list holds the

    reference of its preceding node in addition to its next node in

    sequence.

    Consider the following list:

    START

    10 15 19 21 23 25 32

    Implementing a Doubly-Linked List (Contd.)

  • 8/2/2019 06 DS and Algorithm Session 08

    26/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    You can introduce an additional field in each node of a

    singly-linked list, which would hold the address of its

    previous node.

    Such a type of list is known as a doubly-linked list.

    Structure of a doubly-linked list

    START

    10 15 19 21 23 25 32

    Implementing a Doubly-Linked List (Contd.)

  • 8/2/2019 06 DS and Algorithm Session 08

    27/182

  • 8/2/2019 06 DS and Algorithm Session 08

    28/182

    Ver. 1.0 Session 8

    Data Structures and AlgorithmsRepresenting a Doubly-Linked List (Contd.)

    // Code in C++

    class Node

    {

    public:

    int info;

    Node * next;Node * prev;

    };

  • 8/2/2019 06 DS and Algorithm Session 08

    29/182

    Ver. 1.0 Session 8

    Data Structures and AlgorithmsRepresenting a Doubly-Linked List(Contd.)

    DoubleLinkedListclass: This class consists of a set of

    operations implemented on a linked list. In addition, it declares

    a variable/pointer, START, which will always point to the first

    node in the list.

    // Code in C#class DoubleLinkedList

    {

    Node START;

    DoubleLinkedList(){}

    public void addNode(int element){}

    public bool search(int element, ref Node previous,

    ref Node current){}

    public bool delNode(int element){}

    public void traverse() {}

    public void revtraverse(){}

    }

  • 8/2/2019 06 DS and Algorithm Session 08

    30/182

    Ver. 1.0 Session 8

    Data Structures and AlgorithmsRepresenting a Doubly-Linked List(Contd.)

    // Code in C++

    class DoubleLinkedList

    {

    Node *START;

    public:

    DoubleLinkedList();void addNode(int element);

    bool search(int element, Node *previous, Node

    *current);

    bool delNode(int element);

    void traverse();

    void revtraverse();

    };

  • 8/2/2019 06 DS and Algorithm Session 08

    31/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    How does the representation of a node in a doubly-linked

    list differ from that of a singly-linked list?

    Just a minute

    Answer:

    Unlike singly-linked list, in which each node stores the addressof only the next node in the list, each node in a doubly-linked

    list holds the address of its previous node also.

  • 8/2/2019 06 DS and Algorithm Session 08

    32/182

  • 8/2/2019 06 DS and Algorithm Session 08

    33/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    Write an algorithm to traverse a doubly linked list in the

    backward direction.

    Traversing a Doubly-Linked List (Contd.)1. Mark the last node in the list as

    currentNode.

    2. Repeat steps 3 and 4 until

    currentNode becomes NULL.

    3. Display the information contained

    in the node marked as

    currentNode.

    4. Make currentNode point to the

    node preceding it.

    Algorithm to traverse a doubly

    linked list in the backward

    direction.

  • 8/2/2019 06 DS and Algorithm Session 08

    34/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    A node can be inserted at any of the following positions in a

    doubly-linked list:

    Beginning of the list

    Between two nodes in the list

    End of the list

    Inserting Nodes in a Doubly-Linked List

  • 8/2/2019 06 DS and Algorithm Session 08

    35/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    Write an algorithm to insert a node in the beginning of a

    doubly-linked list.

    Inserting a Node at the Beginning of the List

  • 8/2/2019 06 DS and Algorithm Session 08

    36/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    15 17 20

    START

    Algorithm to insert a node in the

    beginning of a doubly-linked list.

    1. Allocate memory for the new

    node.

    2. Assign value to the data field of

    the new node.

    3. Make the next field of the new

    node point to the first node in the

    list.

    4. Make the prev field of STARTpoint to the new node.

    5. Make the prev field of the new

    node point to NULL.

    6. Make START, point to the new

    node.

    Inserting a Node at the Beginning of the List (Contd.)

    10

  • 8/2/2019 06 DS and Algorithm Session 08

    37/182

  • 8/2/2019 06 DS and Algorithm Session 08

    38/182

  • 8/2/2019 06 DS and Algorithm Session 08

    39/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START7

    newnode

    newnode.next = START

    15 17 20

    Inserting a Node at the Beginning of the List (Contd.)1. Allocate memory for the new

    node.

    2. Assign value to the data field of

    the new node.

    1. Make the next field of the new

    node point to the first node in the

    list.

    1. Make the prev field of STARTpoint to the new node.

    2. Make the prev field of the new

    node point to NULL.

    3. Make START, point to the new

    node.

  • 8/2/2019 06 DS and Algorithm Session 08

    40/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START.prev = newnode

    1010

    START7

    newnode

    15 17 20

    newnode.next = START

    Inserting a Node at the Beginning of the List (Contd.)1. Allocate memory for the new

    node.

    2. Assign value to the data field of

    the new node.

    3. Make the next field of the new

    node point to the first node in the

    list.

    1. Make the prev field of STARTpoint to the new node.

    1. Make the prev field of the new

    node point to NULL.

    2. Make START, point to the new

    node.

  • 8/2/2019 06 DS and Algorithm Session 08

    41/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START7

    newnode

    newnode.prev = NULL

    15 17 20

    START.prev = newnode

    newnode.next = START

    Inserting a Node at the Beginning of the List (Contd.)1. Allocate memory for the new

    node.

    2. Assign value to the data field of

    the new node.

    3. Make the next field of the new

    node point to the first node in the

    list.

    4. Make the prev field of STARTpoint to the new node.

    1. Make the prev field of the new

    node point to NULL.

    1. Make START, point to the new

    node.

  • 8/2/2019 06 DS and Algorithm Session 08

    42/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    START = newnode

    Insertion complete

    newnode.prev = NULL

    START.prev = newnode

    newnode.next = START

    1010 15 17 20

    7

    newnode

    Inserting a Node at the Beginning of the List (Contd.)1. Allocate memory for the new

    node.

    2. Assign value to the data field of

    the new node.

    3. Make the next field of the new

    node point to the first node in the

    list.

    4. Make the prev field of STARTpoint to the new node.

    5. Make the prev field of the new

    node point to NULL.

    1. Make START, point to the new

    node.

  • 8/2/2019 06 DS and Algorithm Session 08

    43/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    Write an algorithm to insert a node between two nodes in a

    doubly-linked list.

    Inserting a Node Between Two Nodes in the List

  • 8/2/2019 06 DS and Algorithm Session 08

    44/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    START

    Insert 16

    1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. Make the prev field of current point to

    the new node.

    7. Make the next field of previous point to

    the new node.

    15 17 20

    Algorithm to insert a node between

    two nodes in a doubly-linked list.

    Inserting a Node Between Two Nodes in the List (Contd.)

    10

  • 8/2/2019 06 DS and Algorithm Session 08

    45/182

  • 8/2/2019 06 DS and Algorithm Session 08

    46/182

  • 8/2/2019 06 DS and Algorithm Session 08

    47/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    15 17 20

    previous = NULL

    Insert 16

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    a. Make previous point to NULL.

    a. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    b. Make previous point to current.c. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. Make the prev field of current point to

    the new node.

    7. Make the next field of previous point to

    the new node.

    current

  • 8/2/2019 06 DS and Algorithm Session 08

    48/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    15 17 20

    previous = NULL

    Insert 16

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    a. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.b. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. Make the prev field of current point to

    the new node.

    7. Make the next field of previous point to

    the new node.

    current

  • 8/2/2019 06 DS and Algorithm Session 08

    49/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    15 17 20

    previous = NULL

    previous

    Insert 16

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.a. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. Make the prev field of current point to

    the new node.

    7. Make the next field of previous point to

    the new node.

    current

  • 8/2/2019 06 DS and Algorithm Session 08

    50/182

  • 8/2/2019 06 DS and Algorithm Session 08

    51/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    15 17 20

    current

    Insert 16

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    a. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.b. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. Make the prev field of current point to

    the new node.

    7. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    52/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    15 17 20

    currentprevious

    Insert 16

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.a. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. Make the prev field of current point to

    the new node.

    7. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    53/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    15 17 20

    currentcurrent

    Insert 16

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.a. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. Make the prev field of current point to

    the new node.

    7. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    54/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    current

    15 17 20

    Insert 16

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    a. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.b. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. Make the prev field of current point to

    the new node.

    7. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    55/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    current

    newnode

    15 17 20

    Insert 16

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    1. Allocate memory for the new node.

    1. Assign value to the data field of the new

    node.

    2. Make the next field of the new node

    point to current.

    3. Make the prev field of the new node

    point to previous.4. Make the prev field of current point to

    the new node.

    5. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    56/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    current

    16

    newnode

    15 17 20

    Insert 16

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    1. Assign value to the data field of the new

    node.

    1. Make the next field of the new node

    point to current.

    2. Make the prev field of the new node

    point to previous.3. Make the prev field of current point to

    the new node.

    4. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    57/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    current

    16

    newnode

    15 17 20

    newnode.next = current

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    1. Make the next field of the new node

    point to current.

    1. Make the prev field of the new node

    point to previous.2. Make the prev field of current point to

    the new node.

    3. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    58/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    current

    16

    newnode

    15 17 20

    newnode.next = current

    newnode.prev = previous

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    1. Make the prev field of the new node

    point to previous.1. Make the prev field of current point to

    the new node.

    2. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    59/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START

    current

    16

    newnode

    15 17 20

    newnode.next = current

    newnode.prev = previous

    current.prev = newnode

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.1. Make the prev field of current point to

    the new node.

    1. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    60/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    1010

    START16

    newnode

    current

    Insertion complete

    15 17 20

    newnode.next = current

    newnode.prev = previous

    current.prev = newnode

    previous.next = newnode

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. Make the prev field of current point to

    the new node.

    1. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    61/182

    Ver. 1.0 Session 8

    Data Structures and Algorithms

    What is the problem with thisalgorithm?

    If current is NULL, then the new

    node should be inserted at the

    end of the list.

    However, in this case, step 6 willgive an error.

    This is because, NULL cannot

    have a prev field.

    Therefore, you need to modify

    this algorithm so that you canalso insert a node at the end of

    the list.

    1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. Make the prev field of current point to

    the new node.

    7. Make the next field of previous point to

    the new node.

    Inserting a Node Between Two Nodes in the List (Contd.)

  • 8/2/2019 06 DS and Algorithm Session 08

    62/182

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    63/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    1. Allocate memory for the new node.

    2. Assign value to the data field of the new

    node.

    3. Make the next field of the new node

    point to current.

    4. Make the prev field of the new node

    point to previous.5. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    6. Make the next field of previous point to

    the new node.

    Insert 23

    Inserting a Node Between Two Nodes in the List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    64/182

    Ver. 1.0 Session 8

    1010

    START

    current

    15 17 20

    Insert 23

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    a. Make previous point to NULL.

    b. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    c. Make previous point to current.d. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    65/182

    Ver. 1.0 Session 8

    1010

    START

    current

    previous = NULL

    15 17 20

    Insert 23

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    a. Make previous point to NULL.

    a. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    b. Make previous point to current.c. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    66/182

    Ver. 1.0 Session 8

    1010

    START

    current

    15 17 20

    Insert 23

    previous = NULL

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    a. Repeat steps e and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.b. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    67/182

    Ver. 1.0 Session 8

    Insert 23

    1010

    START

    current

    15 17 20

    previous = NULL

    previous

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.a. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    68/182

    Ver. 1.0 Session 8

    1010

    START

    current

    15 17 20

    Insert 23

    current

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. Tolocate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.a. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    previous

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    69/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    Insert 23

    current

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    a. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.b. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    previous

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    70/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    Insert 23

    currentprevious

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.a. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    previous

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    71/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    Insert 23

    current current

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.a. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    72/182

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    73/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    Insert 23

    currentprevious

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.a. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    74/182

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    75/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    Insert 23

    current

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    a. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.b. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    previous

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    76/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    Insert 23

    previous current

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    a. Make previous point to current.a. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    previous

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    77/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    Insert 23

    current = NULL

    current

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.a. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    7. Make the next field of previous point to

    the new node.

    previous

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    78/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    current = NULL

    newnode

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    1. Allocate memory for the new node.

    1. Assign value to the data field of the new

    node.

    2. Make the next field of the new node

    point to current.

    3. Make the prev field of the new node

    point to previous.4. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    5. Make the next field of previous point to

    the new node.

    previous

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    79/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    current = NULL

    newnode

    23

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    1. Assign value to the data field of the new

    node.

    1. Make the next field of the new node

    point to current.

    2. Make the prev field of the new node

    point to previous.3. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    4. Make the next field of previous point to

    the new node.

    previous

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    80/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    newnode.next = current

    current = NULL

    newnode

    23

    newnode

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    1. Make the next field of the new node

    point to current.

    1. Make the prev field of the new node

    point to previous.2. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    3. Make the next field of previous point to

    the new node.

    previous

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    81/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    newnode.prev = previous

    23

    newnode

    current = NULL

    newnode.next = current

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    1. Make the prev field of the new node

    point to previous.1. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    2. Make the next field of previous point to

    the new node.

    previous

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    82/182

    Ver. 1.0 Session 8

    1010

    START 23

    15 17 20

    newnode

    current = NULL

    newnode.prev = previousnewnode.next = current

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.1. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    1. Make the next field of previous point to

    the new node.

    previous

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    83/182

    Ver. 1.0 Session 8

    1010

    START

    Insertion complete

    15 17 20

    newnode

    previous.next = newnode

    current = NULL

    23

    newnode

    newnode.prev = previousnewnode.next = current

    Inserting a Node Between Two Nodes in the List (Contd.)1. Identify the nodes between which the

    new node is to be inserted. Mark them

    as previous and current respectively. To

    locate previous and current, execute the

    following steps:

    a. Make current point to the first

    node.

    b. Make previous point to NULL.

    c. Repeat steps d and e until

    current.info > newnode.info or

    current = NULL.

    d. Make previous point to current.e. Make current point to the next

    node in sequence.

    2. Allocate memory for the new node.

    3. Assign value to the data field of the new

    node.

    4. Make the next field of the new node

    point to current.

    5. Make the prev field of the new node

    point to previous.6. If current is not NULL:

    a. Make the prev field of current

    point to the new node.

    1. Make the next field of previous point to

    the new node.

    previous

  • 8/2/2019 06 DS and Algorithm Session 08

    84/182

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    85/182

    Ver. 1.0 Session 8

    Deleting Nodes from a Doubly-Linked List

    You can delete a node from one of the following places in adoubly-linked list:

    Beginning of the list

    Between two nodes in the list

    End of the list

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    86/182

    Ver. 1.0 Session 8

    Write an algorithm to delete a node from the beginning of adoubly-linked list.

    Deleting a Node From the Beginning of the List

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    87/182

    Ver. 1.0 Session 8

    Algorithm to delete a node from thebeginning of a doubly-linked list.

    1. Mark the first node in the list as

    current.

    2. Make START point to the next

    node in sequence.

    3. If START is not NULL:/* If the

    node to be deleted is not the

    only node in the list */

    a. Assign NULL to the prevfield of the node marked

    as START.

    4. Release the memory of the node

    marked as current.

    START

    15 17 20

    Deleting a Node From the Beginning of the List (Contd.)

    10

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    88/182

    Ver. 1.0 Session 8

    1010

    START

    current

    15 17 20

    Deleting a Node From the Beginning of the List (Contd.)1. Mark the first node in the list as

    current.

    1. Make START point to the next

    node in sequence.

    2. If START is not NULL:/* If the

    node to be deleted is not the

    only node in the list */

    a. Assign NULL to the prevfield of the node marked

    as START.

    3. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    89/182

    Ver. 1.0 Session 8

    START = START.next

    1010

    START

    current

    15 17 20

    START

    Deleting a Node From the Beginning of the List (Contd.)1. Mark the first node in the list as

    current.

    1. Make START point to the next

    node in sequence.

    1. If START is not NULL:/* If the

    node to be deleted is not the

    only node in the list */

    a. Assign NULL to the prevfield of the node marked

    as START.

    2. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    90/182

    Ver. 1.0 Session 8

    START. prev = NULL

    1010

    current

    15 17 20

    START

    START = START.next

    Deleting a Node From the Beginning of the List (Contd.)1. Mark the first node in the list as

    current.

    2. Make START point to the next

    node in sequence.

    1. If START is not NULL:/* If the

    node to be deleted is not the

    only node in the list */

    a. Assign NULL to the prevfield of the node marked

    as START.

    1. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    91/182

    Ver. 1.0 Session 8

    Memory released

    Delete operation complete

    current

    17 20

    START

    START. prev = NULL

    START = START.next

    Deleting a Node From the Beginning of the List (Contd.)

    10

    1. Mark the first node in the list as

    current.

    2. Make START point to the next

    node in sequence.

    3. If START is not NULL:/* If the

    node to be deleted is not the

    only node in the list */

    a. Assign NULL to the prevfield of the node marked

    as START.

    1. Release the memory of the node

    marked as current.

    15

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    92/182

    Ver. 1.0 Session 8

    Write an algorithm to delete a node after any other node in adoubly-linked list.

    Deleting a Node Between Two Nodes in the List

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    93/182

    Ver. 1.0 Session 8

    15 17 20

    START

    Delete 17

    1. Mark the node to be deleted as current

    and its predecessor as previous. To

    locate previous and current, execute

    the following steps:

    a. Make previous point to

    NULL. // Set previous = NULL

    b. Make current point to the first

    node in the linked list.// Set

    current = START

    c. Repeat steps d and e until either

    the node is found or current

    becomes NULL.

    d. Make previous point to current.

    e. Make current point to the next

    node in sequence.

    2. Make the next field of previous point to

    the successor of current.

    3. Make the prev field of the successor of

    current point to previous.

    4. Release the memory of the node

    marked as current.

    Algorithm to delete a node betweentwo nodes in a doubly-linked list.

    Deleting a Node Between Two Nodes in the List (Contd.)

    10

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    94/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    Delete 17

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous. To

    locate previous and current, execute

    the following steps:

    a. Make previous point to

    NULL. // Set previous = NULL

    b. Make current point to the first

    node in the linked list.// Set

    current = START

    c. Repeat steps d and e until either

    the node is found or current

    becomes NULL.

    d. Make previous point to current.

    e. Make current point to the next

    node in sequence.

    1. Make the next field of previous point to

    the successor of current.

    2. Make the prev field of the successor of

    current point to previous.

    3. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    95/182

    Ver. 1.0 Session 8

    START

    15 17 20

    previous = NULL

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous. To

    locate previous and current, execute

    the following steps:

    a. Make previous point to

    NULL. // Set previous = NULL

    a. Make current point to the first

    node in the linked list.// Set

    current = START

    b. Repeat steps d and e until either

    the node is found or current

    becomes NULL.

    c. Make previous point to current.

    d. Make current point to the next

    node in sequence.

    2. Make the next field of previous point to

    the successor of current.

    3. Make the prev field of the successor of

    current point to previous.

    4. Release the memory of the node

    marked as current.

    10

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    96/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    previous = NULL

    current

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous. To

    locate previous and current, execute

    the following steps:

    a. Make previous point to

    NULL. // Set previous = NULL

    a. Make current point to the first

    node in the linked list.// Set

    current = START

    a. Repeat steps d and e until either

    the node is found or current

    becomes NULL.

    b. Make previous point to current.

    c. Make current point to the next

    node in sequence.

    2. Make the next field of previous point to

    the successor of current.

    3. Make the prev field of the successor of

    current point to previous.

    4. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    97/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    previous = NULL

    current

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous. To

    locate previous and current, execute

    the following steps:

    a. Make previous point to

    NULL. // Set previous = NULL

    b. Make current point to the first

    node in the linked list.// Set

    current = START

    a. Repeat steps d and e until either

    the node is found or current

    becomes NULL.

    a. Make previous point to current.

    b. Make current point to the next

    node in sequence.

    2. Make the next field of previous point to

    the successor of current.

    3. Make the prev field of the successor of

    current point to previous.

    4. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    98/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    previous = NULL

    current

    previous

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous. To

    locate previous and current, execute

    the following steps:

    a. Make previous point to

    NULL. // Set previous = NULL

    b. Make current point to the first

    node in the linked list.// Set

    current = START

    c. Repeat steps d and e until either

    the node is found or current

    becomes NULL.

    a. Make previous point to current.

    a. Make current point to the next

    node in sequence.

    2. Make the next field of previous point to

    the successor of current.

    3. Make the prev field of the successor of

    current point to previous.

    4. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    99/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    current

    previous

    current

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous. To

    locate previous and current, execute

    the following steps:

    a. Make previous point to

    NULL. // Set previous = NULL

    b. Make current point to the first

    node in the linked list.// Set

    current = START

    c. Repeat steps d and e until either

    the node is found or current

    becomes NULL.

    d. Make previous point to current.

    a. Make current point to the next

    node in sequence.

    1. Make the next field of previous point to

    the successor of current.

    2. Make the prev field of the successor of

    current point to previous.

    3. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    100/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    previous

    current

    previous

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous. To

    locate previous and current, execute

    the following steps:

    a. Make previous point to

    NULL. // Set previous = NULL

    b. Make current point to the first

    node in the linked list.// Set

    current = START

    c. Repeat steps d and e until either

    the node is found or current

    becomes NULL.

    a. Make previous point to current.

    a. Make current point to the next

    node in sequence.

    2. Make the next field of previous point to

    the successor of current.

    3. Make the prev field of the successor of

    current point to previous.

    4. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    101/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    current

    previous

    currentprevious

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous. To

    locate previous and current, execute

    the following steps:

    a. Make previous point to

    NULL. // Set previous = NULL

    b. Make current point to the first

    node in the linked list.// Set

    current = START

    c. Repeat steps d and e until either

    the node is found or current

    becomes NULL.

    d. Make previous point to current.

    a. Make current point to the next

    node in sequence.

    1. Make the next field of previous point to

    the successor of current.

    2. Make the prev field of the successor of

    current point to previous.

    3. Release the memory of the node

    marked as current.

  • 8/2/2019 06 DS and Algorithm Session 08

    102/182

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    103/182

    Ver. 1.0 Session 8

    current.next.prev = previous

    1010

    START

    15 17 20

    currentprevious

    previous. next = current. next

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous. To

    locate previous and current, execute

    the following steps:

    a. Make previous point to

    NULL. // Set previous = NULL

    b. Make current point to the first

    node in the linked list.// Set

    current = START

    c. Repeat steps d and e until either

    the node is found or current

    becomes NULL.

    d. Make previous point to current.

    e. Make current point to the next

    node in sequence.

    2. Make the next field of previous point to

    the successor of current.

    1. Make the prev field of the successor of

    current point to previous.

    1. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    104/182

    Ver. 1.0 Session 8

    1010

    START

    15 17 20

    currentprevious

    Deletion complete

    current.next.prev = previous

    previous. next = current. next

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous. To

    locate previous and current, execute

    the following steps:

    a. Make previous point to

    NULL. // Set previous = NULL

    b. Make current point to the first

    node in the linked list.// Set

    current = START

    c. Repeat steps d and e until either

    the node is found or current

    becomes NULL.

    d. Make previous point to current.

    e. Make current point to the next

    node in sequence.

    2. Make the next field of previous point to

    the successor of current.

    3. Make the prev field of the successor of

    current point to previous.

    1. Release the memory of the node

    marked as current.

  • 8/2/2019 06 DS and Algorithm Session 08

    105/182

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    106/182

    Ver. 1.0 Session 8

    15 17 20

    START

    Delete 20

    1. Mark the node to be deleted as current

    and its predecessor as previous.

    2. Make the next field of previous point to

    the successor of current.

    3. If the successor of current exists:

    a. Make the prev field of the

    successor of current point to

    previous.

    4. Release the memory of the node

    marked as current.

    Refer to the modified algorithm

    Deleting a Node Between Two Nodes in the List (Contd.)

    10

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    107/182

    Ver. 1.0 Session 8

    current

    1. Mark the node to be deleted as current

    and its predecessor as previous.

    1. Make the next field of previous point to

    the successor of current.

    2. If the successor of current exists:

    a. Make the prev field of the

    successor of current point to

    previous.

    3. Release the memory of the node

    marked as current.

    Delete 20

    1010 15 17 20

    START

    previous

    Deleting a Node Between Two Nodes in the List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    108/182

    Ver. 1.0 Session 8

    START

    1010 15 17 20

    currentprevious

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous.

    1. Make the next field of previous point to

    the successor of current.

    1. If the successor of current exists:

    a. Make the prev field of the

    successor of current point to

    previous.

    2. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    109/182

    Ver. 1.0 Session 8

    currentprevious

    START

    1010 15 17 20

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous.

    2. Make the next field of previous point to

    the successor of current.

    1. If the successor of current exists:

    a. Make the prev field of the

    successor of current point to

    previous.

    1. Release the memory of the node

    marked as current.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    110/182

    Ver. 1.0 Session 8

    current

    Delete operation complete

    Deleting a Node Between Two Nodes in the List (Contd.)1. Mark the node to be deleted as current

    and its predecessor as previous.

    2. Make the next field of previous point to

    the successor of current.

    3. If the successor of current exists:

    a. Make the prev field of the

    successor of current point to

    previous.

    1. Release the memory of the node

    marked as current.

    previous

    START

    1010 15 17 20

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    111/182

    Ver. 1.0 Session 8

    You can use a singly-linked list.However, the weapons need to be displayed in a sequence

    repeatedly in a round robin manner.

    Therefore, once all the weapons have been displayed, the

    pointer must start again with the first weapon in the list.

    This requires the pointer to be reinitialized each time the

    end of the list is reached.

    In this situation, it would be good if after traversing the node

    corresponding to the last weapon, the pointer could

    automatically move to the first weapon in the list.

    This can be implemented by using a circular linked list.

    Implementing a Circular Linked List

    Let us suppose you are developing an action game in whicha player is given a set of n weapons.

    Each weapon appears on the screen after a specific time

    period.

    The player is required to select the weapon within 10

    seconds or else the weapon becomes unusable.

    Once the nth weapon is displayed, the weapon that came

    first is displayed again and the process continues.

    Which data structure will you use to store the list of

    weapons in this case?

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    112/182

    Ver. 1.0 Session 8

    You can implement a circular linked list by linking the lastnode back to the first node in the list.

    START

    10 15 9 11 23 5 12

    Implementing a Circular Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    113/182

    Ver. 1.0 Session 8

    In a circular linked list, the last node holds the address ofthe first node.

    In a circular linked list, you need to maintain avariable/pointer, LAST, which always point to the last node.

    10 15 9 11 23 5 12

    LAST

    Implementing a Circular Linked List (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    114/182

    Ver. 1.0 Session 8

    Representing a Circular Linked ListTo represent a circular linked list, you need to declare two

    classes, Node and List:Node class: The declaration of the Node class of a circular

    linked list is the same as that of a singly-linked list.

    List class: This class consists of a set of operations

    implemented on a linked list. These operations are insertion,

    deletion, search, and traversal. It also contains the declarationof a variable/pointer, LAST, that always points to the last node

    in the list.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    115/182

    Ver. 1.0 Session 8

    Representing a Circular Linked List (Contd.)// Code in C#

    class List{

    private Node LAST;

    List()

    {

    LAST = NULL;

    }

    public void addNode(int element) {}

    public bool search(int element, ref Node

    previous, ref Node current){}

    public bool delNode(int element) {}

    public void traverse() {}

    }

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    116/182

    Ver. 1.0 Session 8

    Representing a Circular Linked List (Contd.)// Code in C++

    class List{

    Node * LAST;

    public:

    List()

    {

    LAST = NULL;

    }

    void addNode(int element);

    bool search(int element, Node *previous, Node

    *current);

    bool delNode(int element);

    void traverse();

    };

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    117/182

    Ver. 1.0 Session 8

    Traversing a Circular Linked List

    Write an algorithm to traverse a circular linked list.

    1. Make currentNode point to

    the successor of the node

    marked as LAST, such that

    currentNode points to the first

    node in the list.

    2. Repeat step 3 and 4 until

    currentNode = LAST.

    3. Display the information

    contained in the node marked

    as currentNode.

    4. Make currentNode point to

    the next node in its sequence.

    5. Display the information

    contained in the node marked

    as LAST.

    Algorithm to traverse a circular linkedlist.

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    118/182

    Ver. 1.0 Session 8

    Inserting a Node in a Circular Linked List

    In a circular linked list, you can insert a node at any of thefollowing positions:

    Beginning of the list

    End of the list

    Between two node in the list

    Data Structures and Algorithms

  • 8/2/2019 06 DS and Algorithm Session 08

    119/182

    Ver. 1.0 Session 8

    Write an algorithm to insert a node in the beginning of acircular linked list.

    Inserting a Node at the Beginning of the List

  • 8/2/2019 06 DS and Algorithm Session 08

    120/182

    Data Structures and Algorithms

    Inserting a Node at the Beginning of the List (Contd.)

  • 8/2/2019 06 DS and Algorithm Session 08

    121/182

    Ver. 1.0 Session 8

    1010 15 17 20

    newnode

    LAST

    1. Allocate memory for the new

    node.

    1. Assign value to the data field

    of the new node.

    2. Make the next field of the

    new node point to the

    successor of LAST.

    3. Make the next field of LAST

    point to the new node.

    Data Structures and Algorithms

    Inserting a Node at the Beginning of the List (Contd.)

  • 8/2/2019 06 DS and Algorithm Session 08

    122/182

    Ver. 1.0 Session 8

    1010 15 17 20

    7

    newnode

    LAST

    1. Allocate memory for the new

    node.

    1. Assign value to the data field

    of the new node.

    1. Make the next field of the

    new node point to the

    successor of LAST.

    2. Make the next field of LAST

    point to the new node.

    Data Structures and AlgorithmsInserting a Node at the Beginning of the List (Contd.)

  • 8