SlideShare uma empresa Scribd logo
1 de 182
Data Structures and Algorithms
Objectives


                In this chapter, you will learn to:
                   Implement a doubly-linked list
                   Implement a circular linked list
                   Apply linked lists to solve programming problems




     Ver. 1.0                                                         Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List


                Now consider a case of 100000 numbers display a linked
                Consider a sorted list in which you need tostored inthese
                numbers in a descending order.
                list.
                How want to display these numbers in ascending order,
                If youwill you solve this problem?
                what will node do?
                      Each you is linked to the next node in sequence.
                   Traverse thethat starting from the first list in the forward
                   This means list you can traverse the node.
                   direction 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.




     Ver. 1.0                                                             Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                   1.    Declare three variables / pointers ptr1,
                                                         ptr2, and ptr 3.
                     Algorithm to reverse a
                     singly-linked list.           3.    If there is only one node in the list:

                                                          a.    Exit.
          START
                                                   6.    Mark the first node in the list as ptr1.

                10       15      19      21   32   8.    Mark the successor of ptr1 as ptr2

                                                   10.   Mark the successor of ptr2 as ptr3.

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

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

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

                                                   19.   Make START point to ptr2.


     Ver. 1.0                                                                        Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                     •     Declare three variables / pointers ptr1,
                                                           ptr2, and ptr 3.

                                                     •     If there is only one node in the list:

                                                            a.    Exit.
          START
                                                     6.    Mark the first node in the list as ptr1.

                10          15      19     21   32   8.    Mark the successor of ptr1 as ptr2

                                                     10.   Mark the successor of ptr2 as ptr3.

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

                                                     14.   Make the next field of ptr2 point to ptr1.
                     ptr1    ptr2   ptr3
                                                     16.   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.

                                                     19.   Make START point to ptr2.


     Ver. 1.0                                                                          Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                     •     Declare three variables / pointers ptr1,
                                                           ptr2, and ptr 3.

                                                     •     If there is only one node in the list:

                                                            a.    Exit.
          START
                                                     6.    Mark the first node in the list as ptr1.

                10          15      19     21   32   8.    Mark the successor of ptr1 as ptr2

                                                     10.   Mark the successor of ptr2 as ptr3.

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

                                                     14.   Make the next field of ptr2 point to ptr1.
                     ptr1    ptr2   ptr3
                                                     16.   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.

                                                     19.   Make START point to ptr2.


     Ver. 1.0                                                                          Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                       1.    Declare three variables / pointers ptr1,
                                                             ptr2, and ptr 3.

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

                                                              a.    Exit.
          START
                                                       •     Mark the first node in the list as ptr1.

                10            15      19     21   32   •     Mark the successor of ptr1 as ptr2

                                                       •     Mark the successor of ptr2 as ptr3.
                ptr1                                   •     Make the next field of ptr1 point to NULL.

                                                       •     Make the next field of ptr2 point to ptr1.
                       ptr1    ptr2   ptr3
                                                       •     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.

                                                       19.   Make START point to ptr2.


     Ver. 1.0                                                                            Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                 1.    Declare three variables / pointers ptr1,
                                                       ptr2, and ptr 3.

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

                                                        a.    Exit.
          START
                                                 •     Mark the first node in the list as ptr1.

                10     15       19     21   32   •     Mark the successor of ptr1 as ptr2

                                                 •     Mark the successor of ptr2 as ptr3.
                ptr1   ptr2                      •     Make the next field of ptr1 point to NULL.

                                                 •     Make the next field of ptr2 point to ptr1.
                         ptr2   ptr3
                                                 •     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.

                                                 19.   Make START point to ptr2.


     Ver. 1.0                                                                      Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                               1.    Declare three variables / pointers ptr1,
                                                     ptr2, and ptr 3.

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

                                                      a.    Exit.
          START
                                               •     Mark the first node in the list as ptr1.

                10     15      19    21   32   •     Mark the successor of ptr1 as ptr2

                                               •     Mark the successor of ptr2 as ptr3.
                ptr1   ptr2   ptr3             •     Make the next field of ptr1 point to NULL.

                                               •     Make the next field of ptr2 point to ptr1.
                              ptr3
                                               •     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.

                                               19.   Make START point to ptr2.


     Ver. 1.0                                                                    Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                               1.    Declare three variables / pointers ptr1,
                                                     ptr2, and ptr 3.

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

                                                      a.    Exit.
          START
                                               •     Mark the first node in the list as ptr1.

                10     15      19    21   32   •     Mark the successor of ptr1 as ptr2

                                               •     Mark the successor of ptr2 as ptr3.
                ptr1   ptr2   ptr3             •     Make the next field of ptr1 point to NULL.

                                               •     Make the next field of ptr2 point to ptr1.

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

                                               19.   Make START point to ptr2.


     Ver. 1.0                                                                    Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                               1.    Declare three variables / pointers ptr1,
                                                     ptr2, and ptr 3.

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

                                                      a.    Exit.
          START
                                               •     Mark the first node in the list as ptr1.

                10     15      19    21   32   •     Mark the successor of ptr1 as ptr2

                                               •     Mark the successor of ptr2 as ptr3.
                ptr1   ptr2   ptr3             •     Make the next field of ptr1 point to NULL.

                                               •     Make the next field of ptr2 point to ptr1.

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

                                               19.   Make START point to ptr2.


     Ver. 1.0                                                                    Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                               1.    Declare three variables / pointers ptr1,
                                                     ptr2, and ptr 3.

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

                                                      a.    Exit.
          START
                                               •     Mark the first node in the list as ptr1.

                10     15      19    21   32   •     Mark the successor of ptr1 as ptr2

                                               •     Mark the successor of ptr2 as ptr3.
                ptr1   ptr2   ptr3             •     Make the next field of ptr1 point to NULL.

                                               •     Make the next field of ptr2 point to ptr1.

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

                                               19.   Make START point to ptr2.


     Ver. 1.0                                                                    Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                 1.    Declare three variables / pointers ptr1,
                                                       ptr2, and ptr 3.

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

                                                        a.    Exit.
          START
                                                 6.    Mark the first node in the list as ptr1.

                10        15     19    21   32   8.    Mark the successor of ptr1 as ptr2

                                                 10.   Mark the successor of ptr2 as ptr3.
                ptr1     ptr2   ptr3             12.   Make the next field of ptr1 point to NULL.
                       ptr1
                                                 14.   Make the next field of ptr2 point to ptr1.

                                                 16.   Repeat until ptr3 becomes NULL

                                                        •     Set ptr1 = ptr2
                                                        •     Set ptr2 = ptr3
                                                        •     Make ptr3 point to the next node
                                                              in its sequence.
                                                        •     Make the next field of ptr2 point to
                                                              ptr1.

                                                 19.   Make START point to ptr2.


     Ver. 1.0                                                                      Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                    1.    Declare three variables / pointers ptr1,
                                                          ptr2, and ptr 3.

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

                                                           a.    Exit.
          START
                                                    6.    Mark the first node in the list as ptr1.

                10      15           19   21   32   8.    Mark the successor of ptr1 as ptr2

                                                    10.   Mark the successor of ptr2 as ptr3.
                       ptr2     ptr3                12.   Make the next field of ptr1 point to NULL.
                     ptr1     ptr2
                                                    14.   Make the next field of ptr2 point to ptr1.

                                                    16.   Repeat until ptr3 becomes NULL

                                                           •     Set ptr1 = ptr2
                                                           •     Set ptr2 = ptr3
                                                           •     Make ptr3 point to the next node
                                                                 in its sequence.
                                                           •     Make the next field of ptr2 point to
                                                                 ptr1.

                                                    19.   Make START point to ptr2.


     Ver. 1.0                                                                         Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                     1.    Declare three variables / pointers ptr1,
                                                           ptr2, and ptr 3.

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

                                                            a.    Exit.
          START
                                                     6.    Mark the first node in the list as ptr1.

                10      15          19   21     32   8.    Mark the successor of ptr1 as ptr2

                                                     10.   Mark the successor of ptr2 as ptr3.
                               ptr3      ptr3        12.   Make the next field of ptr1 point to NULL.
                     ptr1    ptr2
                                                     14.   Make the next field of ptr2 point to ptr1.

                                                     16.   Repeat until ptr3 becomes NULL

                                                            •     Set ptr1 = ptr2
                                                            •     Set ptr2 = ptr3
                                                            •     Make ptr3 point to the next node
                                                                  in its sequence.
                                                            •     Make the next field of ptr2 point to
                                                                  ptr1.

                                                     19.   Make START point to ptr2.


     Ver. 1.0                                                                          Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                     1.    Declare three variables / pointers ptr1,
                                                           ptr2, and ptr 3.

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

                                                            a.    Exit.
          START
                                                     6.    Mark the first node in the list as ptr1.

                10      15          19   21     32   8.    Mark the successor of ptr1 as ptr2

                                                     10.   Mark the successor of ptr2 as ptr3.
                                         ptr3        12.   Make the next field of ptr1 point to NULL.
                     ptr1    ptr2
                                                     14.   Make the next field of ptr2 point to ptr1.

                                                     16.   Repeat until ptr3 becomes NULL

                                                            •     Set ptr1 = ptr2
                                                            •     Set ptr2 = ptr3
                                                            •     Make ptr3 point to the next node
                                                                  in its sequence.
                                                            •     Make the next field of ptr2 point to
                                                                  ptr1.

                                                     •     Make START point to ptr2.


     Ver. 1.0                                                                          Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                       1.    Declare three variables / pointers ptr1,
                                                             ptr2, and ptr 3.

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

                                                              a.    Exit.
          START
                                                       6.    Mark the first node in the list as ptr1.

                10      15          19     21     32   8.    Mark the successor of ptr1 as ptr2

                                                       10.   Mark the successor of ptr2 as ptr3.
                                    ptr1   ptr3        12.   Make the next field of ptr1 point to NULL.
                     ptr1    ptr2
                                                       14.   Make the next field of ptr2 point to ptr1.

                                                       16.   Repeat until ptr3 becomes NULL

                                                              •     Set ptr1 = ptr2
                                                              •     Set ptr2 = ptr3
                                                              •     Make ptr3 point to the next node
                                                                    in its sequence.
                                                              •     Make the next field of ptr2 point to
                                                                    ptr1.

                                                       19.   Make START point to ptr2.


     Ver. 1.0                                                                            Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                       1.    Declare three variables / pointers ptr1,
                                                             ptr2, and ptr 3.

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

                                                              a.    Exit.
          START
                                                       6.    Mark the first node in the list as ptr1.

                10   15          19        21     32   8.    Mark the successor of ptr1 as ptr2

                                                       10.   Mark the successor of ptr2 as ptr3.
                                 ptr1      ptr3        12.   Make the next field of ptr1 point to NULL.
                          ptr2          ptr2
                                                       14.   Make the next field of ptr2 point to ptr1.

                                                       16.   Repeat until ptr3 becomes NULL

                                                              •     Set ptr1 = ptr2
                                                              •     Set ptr2 = ptr3
                                                              •     Make ptr3 point to the next node
                                                                    in its sequence.
                                                              •     Make the next field of ptr2 point to
                                                                    ptr1.

                                                       19.   Make START point to ptr2.


     Ver. 1.0                                                                            Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                  1.    Declare three variables / pointers ptr1,
                                                        ptr2, and ptr 3.

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

                                                         a.    Exit.
          START
                                                  6.    Mark the first node in the list as ptr1.

                10   15   19        21     32     8.    Mark the successor of ptr1 as ptr2

                                                  10.   Mark the successor of ptr2 as ptr3.
                          ptr1      ptr3   ptr3   12.   Make the next field of ptr1 point to NULL.
                                 ptr2
                                                  14.   Make the next field of ptr2 point to ptr1.

                                                  16.   Repeat until ptr3 becomes NULL

                                                         •     Set ptr1 = ptr2
                                                         •     Set ptr2 = ptr3
                                                         •     Make ptr3 point to the next node
                                                               in its sequence.
                                                         •     Make the next field of ptr2 point to
                                                               ptr1.

                                                  19.   Make START point to ptr2.


     Ver. 1.0                                                                       Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                1.    Declare three variables / pointers ptr1,
                                                      ptr2, and ptr 3.

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

                                                       a.    Exit.
          START
                                                6.    Mark the first node in the list as ptr1.

                10   15   19        21   32     8.    Mark the successor of ptr1 as ptr2

                                                10.   Mark the successor of ptr2 as ptr3.
                          ptr1           ptr3   12.   Make the next field of ptr1 point to NULL.
                                 ptr2
                                                14.   Make the next field of ptr2 point to ptr1.

                                                16.   Repeat until ptr3 becomes NULL

                                                       •     Set ptr1 = ptr2
                                                       •     Set ptr2 = ptr3
                                                       •     Make ptr3 point to the next node
                                                             in its sequence.
                                                       •     Make the next field of ptr2 point to
                                                             ptr1.

                                                •     Make START point to ptr2.


     Ver. 1.0                                                                     Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                  1.    Declare three variables / pointers ptr1,
                                                        ptr2, and ptr 3.

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

                                                         a.    Exit.
          START
                                                  6.    Mark the first node in the list as ptr1.

                10   15   19        21     32     8.    Mark the successor of ptr1 as ptr2

                                                  10.   Mark the successor of ptr2 as ptr3.
                          ptr1      ptr1   ptr3   12.   Make the next field of ptr1 point to NULL.
                                 ptr2
                                                  14.   Make the next field of ptr2 point to ptr1.

                                                  16.   Repeat until ptr3 becomes NULL

                                                         •     Set ptr1 = ptr2
                                                         •     Set ptr2 = ptr3
                                                         •     Make ptr3 point to the next node
                                                               in its sequence.
                                                         •     Make the next field of ptr2 point to
                                                               ptr1.

                                                  19.   Make START point to ptr2.


     Ver. 1.0                                                                       Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                   1.    Declare three variables / pointers ptr1,
                                                         ptr2, and ptr 3.

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

                                                          a.    Exit.
          START
                                                   6.    Mark the first node in the list as ptr1.

                10   15   19      21        32     8.    Mark the successor of ptr1 as ptr2

                                                   10.   Mark the successor of ptr2 as ptr3.
                                  ptr1      ptr3   12.   Make the next field of ptr1 point to NULL.
                               ptr2      ptr2
                                                   14.   Make the next field of ptr2 point to ptr1.

                                                   16.   Repeat until ptr3 becomes NULL

                                                          •     Set ptr1 = ptr2
                                                          •     Set ptr2 = ptr3
                                                          •     Make ptr3 point to the next node
                                                                in its sequence.
                                                          •     Make the next field of ptr2 point to
                                                                ptr1.

                                                   19.   Make START point to ptr2.


     Ver. 1.0                                                                        Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                         1.    Declare three variables / pointers ptr1,
                                                               ptr2, and ptr 3.

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

                                                                a.    Exit.
          START
                                                         6.    Mark the first node in the list as ptr1.

                10   15      19         21        32     8.    Mark the successor of ptr1 as ptr2

                                                         10.   Mark the successor of ptr2 as ptr3.
                                        ptr1      ptr3   12.   Make the next field of ptr1 point to NULL.
                                               ptr2
                                                         14.   Make the next field of ptr2 point to ptr1.

                                                         16.   Repeat until ptr3 becomes NULL
                          ptr3 = NULL                           •     Set ptr1 = ptr2
                                                                •     Set ptr2 = ptr3
                                                                •     Make ptr3 point to the next node
                                                                      in its sequence.
                                                                •     Make the next field of ptr2 point to
                                                                      ptr1.

                                                         19.   Make START point to ptr2.


     Ver. 1.0                                                                              Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                       1.    Declare three variables / pointers ptr1,
                                                             ptr2, and ptr 3.

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

                                                              a.    Exit.
          START
                                                       6.    Mark the first node in the list as ptr1.

                10   15      19         21        32   8.    Mark the successor of ptr1 as ptr2

                                                       10.   Mark the successor of ptr2 as ptr3.
                                        ptr1           12.   Make the next field of ptr1 point to NULL.
                                               ptr2
                                                       14.   Make the next field of ptr2 point to ptr1.

                                                       16.   Repeat until ptr3 becomes NULL
                          ptr3 = NULL                         •     Set ptr1 = ptr2
                                                              •     Set ptr2 = ptr3
                                                              •     Make ptr3 point to the next node
                                                                    in its sequence.
                                                              •     Make the next field of ptr2 point to
                                                                    ptr1.

                                                       •     Make START point to ptr2.


     Ver. 1.0                                                                            Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)

                                                        •     Declare three variables / pointers ptr1,
                                                              ptr2, and ptr 3.

                                                        •     If there is only one node in the list:
                                                START
                                                               a.    Exit.
          START
                                                        6.    Mark the first node in the list as ptr1.

                10   15      19         21        32    8.    Mark the successor of ptr1 as ptr2

                                                        10.   Mark the successor of ptr2 as ptr3.
                                        ptr1            12.   Make the next field of ptr1 point to NULL.
                                               ptr2
                                                        14.   Make the next field of ptr2 point to ptr1.

                                                        16.   Repeat until ptr3 becomes NULL
                          ptr3 = 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.

                                                        19.   Make START point to ptr2.


     Ver. 1.0                                                                             Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)


                  What is the problem with the previous algorithm?
                  How can you solve this problem?
                        This need to adjust the links if eachthe three the list holds the
                        You problem can be solved for all node in variables
                        reference of itsvisit the next node. addition to its next node in
                        whenever you preceding node in
                        sequence.
                        Disadvantage of this approach:
                        Consider the following list: and time consuming for large lists.
                            This approach is inefficient


                START


                  10        15        19        21       23        25        32




     Ver. 1.0                                                                       Session 8
Data Structures and Algorithms
Implementing a Doubly-Linked List (Contd.)


                  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.


                START


                  10       15       19       21      23     25    32




                        Structure of a doubly-linked list




     Ver. 1.0                                                           Session 8
Data Structures and Algorithms
Representing a Doubly-Linked List


                A Linked list is represented in a program by defining two
                classes:
                 – Node class: In a doubly-linked list, each node needs to store:
                     – The information
                     – The address of the next node in sequence
                     – The address of the previous node

                   // Code in C#
                   class Node
                   {
                            public int info;
                            public Node next;
                            public Node prev;
                   }




     Ver. 1.0                                                              Session 8
Data Structures and Algorithms
Representing a Doubly-Linked List (Contd.)



                // Code in C++
                class Node
                {
                   public:
                   int info;
                   Node * next;
                   Node * prev;
                };




     Ver. 1.0                                Session 8
Data Structures and Algorithms
Representing a Doubly-Linked List
(Contd.)


                – DoubleLinkedList class: 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(){}
                  }



     Ver. 1.0                                                               Session 8
Data Structures and Algorithms
Representing 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();
                };




     Ver. 1.0                                                     Session 8
Data Structures and Algorithms
Just a minute


                How does the representation of a node in a doubly-linked
                list differ from that of a singly-linked list?




                Answer:
                   Unlike singly-linked list, in which each node stores the address
                   of only the next node in the list, each node in a doubly-linked
                   list holds the address of its previous node also.




     Ver. 1.0                                                              Session 8
Data Structures and Algorithms
Traversing a Doubly-Linked List
                                                            1.   Mark the first node in the list as
                                                                 currentNode.
                Algorithm to traverse a doubly a doubly linked list in the
                Write an algorithm to traverse
                                                      3. Repeat steps 3 and 4 until
                forward direction.
                linked list in the forward direction.      currentNode becomes NULL.

                                                            5.   Display the information
                                                                 contained in the node marked
                                                                 as currentNode.

                                                            7.   Make currentNode point to the
                                                                 next node in sequence.




     Ver. 1.0                                                                          Session 8
Data Structures and Algorithms
Traversing a Doubly-Linked List (Contd.)

                                                            2.   Mark the last node in the list as
                Algorithm to traverse a doubly a doubly linked list in the
                Write an algorithm to traverse                   currentNode.

                backward direction.
                linked list in the backward           4. Repeat steps 3 and 4 until
                                                           currentNode becomes NULL.
                direction.
                                                            6.   Display the information
                                                                 contained in the node marked
                                                                 as currentNode.

                                                            8.   Make currentNode point to the
                                                                 node preceding it.




     Ver. 1.0                                                                          Session 8
Data Structures and Algorithms
Inserting Nodes in a Doubly-Linked List


                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




     Ver. 1.0                                                          Session 8
Data Structures and Algorithms
Inserting a Node at the Beginning of the List


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




     Ver. 1.0                                                          Session 8
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
                                                     1.    Allocate memory for the new
                                                           node.
                Algorithm to insert a node in the
                                                     3.    Assign value to the data field of
                beginning of a doubly-linked list.         the new node.

                                                     5.    Make the next field of the new
                                                           node point to the first node in
                                                           the list.

                                                     7.    Make the prev field of START
                                                           point to the new node.

                                                     9.    Make the prev field of the new
                                                           node point to NULL.

                      START                          11. Make START, point to the new
                                                         node.


                        10          15        17          20




     Ver. 1.0                                                                    Session 8
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
                                               •    Allocate memory for the new
                                                    node.

                                               •    Assign value to the data field of
                                                    the new node.

                                               •    Make the next field of the new
                                                    node point to the first node in
                                                    the list.

                                               •    Make the prev field of START
                                                    point to the new node.

                newnode                        •    Make the prev field of the new
                                                    node point to NULL.

                          START                •    Make START, point to the new
                                                    node.


                           10
                            10    15   17          20




     Ver. 1.0                                                             Session 8
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
                                               •    Allocate memory for the new
                                                    node.

                                               •    Assign value to the data field of
                                                    the new node.

                                               •    Make the next field of the new
                                                    node point to the first node in
                                                    the list.

                                               •    Make the prev field of START
                                                    point to the new node.

                newnode                        •    Make the prev field of the new
                                                    node point to NULL.

                          START                •    Make START, point to the new
                  7                                 node.


                           10
                            10    15   17          20




     Ver. 1.0                                                             Session 8
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
                                                    •    Allocate memory for the new
                                                         node.
                             newnode.next = START
                                                    •    Assign value to the data field of
                                                         the new node.

                                                    •    Make the next field of the new
                                                         node point to the first node in
                                                         the list.

                                                    •    Make the prev field of START
                                                         point to the new node.

                newnode                             •    Make the prev field of the new
                                                         node point to NULL.

                          START                     •    Make START, point to the new
                  7                                      node.


                           10
                            10      15      17          20




     Ver. 1.0                                                                  Session 8
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
                                                    •    Allocate memory for the new
                                                         node.
                             newnode.next = START
                                                    •    Assign value to the data field of
                             START.prev = newnode        the new node.

                                                    •    Make the next field of the new
                                                         node point to the first node in
                                                         the list.

                                                    •    Make the prev field of START
                                                         point to the new node.

                newnode                             •    Make the prev field of the new
                                                         node point to NULL.

                          START                     •    Make START, point to the new
                  7                                      node.


                           10
                            10      15      17          20




     Ver. 1.0                                                                  Session 8
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
                                                    •    Allocate memory for the new
                                                         node.
                             newnode.next = START
                                                    •    Assign value to the data field of
                             START.prev = newnode        the new node.

                             newnode.prev = NULL    •    Make the next field of the new
                                                         node point to the first node in
                                                         the list.

                                                    •    Make the prev field of START
                                                         point to the new node.

                newnode                             •    Make the prev field of the new
                                                         node point to NULL.

                          START                     •    Make START, point to the new
                  7                                      node.


                           10
                            10      15      17          20




     Ver. 1.0                                                                  Session 8
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
                                                    •    Allocate memory for the new
                                                         node.
                             newnode.next = START
                                                    •    Assign value to the data field of
                             START.prev = newnode        the new node.

                             newnode.prev = NULL    •    Make the next field of the new
                                                         node point to the first node in
                             START = newnode             the list.

                                                    •    Make the prev field of START
                                                         point to the new node.

                newnode                             •    Make the prev field of the new
                                                         node point to NULL.

                          START                     •    Make START, point to the new
                  7                                      node.


                           10
                            10      15      17          20


                                                         Insertion complete


     Ver. 1.0                                                                  Session 8
Data Structures and Algorithms
Inserting a Node Between Two Nodes in the List


                Write an algorithm to insert a node between two nodes in a
                doubly-linked list.




     Ver. 1.0                                                        Session 8
Data Structures and Algorithms
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
                Insert 16
                   Algorithm to insert a node between        locate previous and current, execute the
                                                             following steps:
                   two nodes in a doubly-linked list.          a.    Make current point to the first
                                                                     node.
                                                               b.    Make previous point to NULL.
                START                                          c.    Repeat steps d and e until
                                                                     current.info > newnode.info or
                                                                     current = NULL.
                                                               d.    Make previous point to current.
                 10         15       17         20             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.




     Ver. 1.0                                                                         Session 8
Data Structures and Algorithms
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
                Insert 16                       locate previous and current, execute the
                                                following steps:
                                                  a.    Make current point to the first
                                                        node.
                                                  b.    Make previous point to NULL.
                START                             c.    Repeat steps d and e until
                                                        current.info > newnode.info or
                                                        current = NULL.
                                                  d.    Make previous point to current.
                  10
                   10       15   17   20          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.




     Ver. 1.0                                                            Session 8
Data Structures and Algorithms
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
                Insert 16                         locate previous and current, execute the
                                                  following steps:
                                                    •     Make current point to the first
                                                          node.
                                                    •     Make previous point to NULL.
                START                               •     Repeat steps d and e until
                                                          current.info > newnode.info or
                                                          current = NULL.
                                                    •     Make previous point to current.
                  10
                   10         15   17   20          •     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
                    current                       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.




     Ver. 1.0                                                              Session 8
Data Structures and Algorithms
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
                Insert 16                         locate previous and current, execute the
                                                  following steps:
                                                    •     Make current point to the first
                                                          node.
                                                    •     Make previous point to NULL.
                START                               •     Repeat steps d and e until
                                                          current.info > newnode.info or
                                                          current = NULL.
                                                    •     Make previous point to current.
                  10
                   10         15   17   20          •     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
                    current                       node.
                                             4.   Make the next field of the new node
          previous = NULL                         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.




     Ver. 1.0                                                              Session 8
Data Structures and Algorithms
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
                Insert 16                         locate previous and current, execute the
                                                  following steps:
                                                    •     Make current point to the first
                                                          node.
                                                    •     Make previous point to NULL.
                START                               •     Repeat steps d and e until
                                                          current.info > newnode.info or
                                                          current = NULL.
                                                    •     Make previous point to current.
                  10
                   10         15   17   20          •     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
                    current                       node.
                                             4.   Make the next field of the new node
          previous = NULL                         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.




     Ver. 1.0                                                              Session 8
Data Structures and Algorithms
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
                Insert 16                       locate previous and current, execute the
                                                following steps:
                                                  •     Make current point to the first
                                                        node.
                                                  •     Make previous point to NULL.
                START                             •     Repeat steps d and e until
                                                        current.info > newnode.info or
                                                        current = NULL.
                                                  •     Make previous point to current.
                  10
                   10       15   17   20          •     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
       previous current                         node.
                                           4.   Make the next field of the new node
          previous = NULL                       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.




     Ver. 1.0                                                            Session 8
Data Structures and Algorithms
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
                Insert 16                             locate previous and current, execute the
                                                      following steps:
                                                        •     Make current point to the first
                                                              node.
                                                        •     Make previous point to NULL.
                START                                   •     Repeat steps d and e until
                                                              current.info > newnode.info or
                                                              current = NULL.
                                                        •     Make previous point to current.
                  10
                   10       15         17   20          •     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
       previous current      current                  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.




     Ver. 1.0                                                                  Session 8
Data Structures and Algorithms
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
                Insert 16                             locate previous and current, execute the
                                                      following steps:
                                                        •     Make current point to the first
                                                              node.
                                                        •     Make previous point to NULL.
                START                                   •     Repeat steps d and e until
                                                              current.info > newnode.info or
                                                              current = NULL.
                                                        •     Make previous point to current.
                  10
                   10       15         17   20          •     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
       previous              current                  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.




     Ver. 1.0                                                                  Session 8
Data Structures and Algorithms
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
                Insert 16                         locate previous and current, execute the
                                                  following steps:
                                                    •     Make current point to the first
                                                          node.
                                                    •     Make previous point to NULL.
                START                               •     Repeat steps d and e until
                                                          current.info > newnode.info or
                                                          current = NULL.
                                                    •     Make previous point to current.
                  10
                   10       15     17   20          •     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
       previous previous current                  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.




     Ver. 1.0                                                              Session 8
Data Structures and Algorithms
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
                Insert 16                               locate previous and current, execute the
                                                        following steps:
                                                          •     Make current point to the first
                                                                node.
                                                          •     Make previous point to NULL.
                START                                     •     Repeat steps d and e until
                                                                current.info > newnode.info or
                                                                current = NULL.
                                                          •     Make previous point to current.
                  10
                   10       15       17       20          •     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
                    previous currentcurrent             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.




     Ver. 1.0                                                                    Session 8
Data Structures and Algorithms
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
                Insert 16                            locate previous and current, execute the
                                                     following steps:
                                                       •     Make current point to the first
                                                             node.
                                                       •     Make previous point to NULL.
                START                                  •     Repeat steps d and e until
                                                             current.info > newnode.info or
                                                             current = NULL.
                                                       •     Make previous point to current.
                  10
                   10       15    17       20          •     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
                    previous     current             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.




     Ver. 1.0                                                                 Session 8
Data Structures and Algorithms
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
                Insert 16      newnode                  locate previous and current, execute the
                                                        following steps:
                                                          a.    Make current point to the first
                                                                node.
                                                          b.    Make previous point to NULL.
                START                                     c.    Repeat steps d and e until
                                                                current.info > newnode.info or
                                                                current = NULL.
                                                          d.    Make previous point to current.
                  10
                   10       15           17   20          e.    Make current point to the next
                                                                node in sequence.
                                                   •    Allocate memory for the new node.
                                                   •    Assign value to the data field of the new
                    previous        current             node.
                                                   •    Make the next field of the new node
                                                        point to current.
                                                   •    Make the prev field of the new node
                                                        point to previous.
                                                   •    Make the prev field of current point to
                                                        the new node.
                                                   •    Make the next field of previous point to
                                                        the new node.




     Ver. 1.0                                                                    Session 8
Data Structures and Algorithms
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
                Insert 16      newnode                    locate previous and current, execute the
                                                          following steps:
                                                            a.    Make current point to the first
                                 16                               node.
                                                            b.    Make previous point to NULL.
                START                                       c.    Repeat steps d and e until
                                                                  current.info > newnode.info or
                                                                  current = NULL.
                                                            d.    Make previous point to current.
                  10
                   10       15           17     20          e.    Make current point to the next
                                                                  node in sequence.
                                                     •    Allocate memory for the new node.
                                                     •    Assign value to the data field of the new
                    previous          current             node.
                                                     •    Make the next field of the new node
                                                          point to current.
                                                     •    Make the prev field of the new node
                                                          point to previous.
                                                     •    Make the prev field of current point to
                                                          the new node.
                                                     •    Make the next field of previous point to
                                                          the new node.




     Ver. 1.0                                                                      Session 8
Data Structures and Algorithms
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
                              newnode                      locate previous and current, execute the
                                                           following steps:
                                                             a.    Make current point to the first
                                 16                                node.
                                                             b.    Make previous point to NULL.
                START                                        c.    Repeat steps d and e until
                                                                   current.info > newnode.info or
                                                                   current = NULL.
                                                             d.    Make previous point to current.
                 10
                  10        15          17       20          e.    Make current point to the next
                                                                   node in sequence.
                                                      •    Allocate memory for the new node.
                                                      •    Assign value to the data field of the new
                   previous           current              node.
                                                      •    Make the next field of the new node
                                                           point to current.
                        newnode.next = current        •    Make the prev field of the new node
                                                           point to previous.
                                                      •    Make the prev field of current point to
                                                           the new node.
                                                      •    Make the next field of previous point to
                                                           the new node.




     Ver. 1.0                                                                       Session 8
Data Structures and Algorithms
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
                              newnode                       locate previous and current, execute the
                                                            following steps:
                                                              a.    Make current point to the first
                                 16                                 node.
                                                              b.    Make previous point to NULL.
                START                                         c.    Repeat steps d and e until
                                                                    current.info > newnode.info or
                                                                    current = NULL.
                                                              d.    Make previous point to current.
                 10
                  10        15          17        20          e.    Make current point to the next
                                                                    node in sequence.
                                                       •    Allocate memory for the new node.
                                                       •    Assign value to the data field of the new
                   previous           current               node.
                                                       •    Make the next field of the new node
                                                            point to current.
                        newnode.next = current         •    Make the prev field of the new node
                                                            point to previous.
                        newnode.prev = previous        •    Make the prev field of current point to
                                                            the new node.
                                                       •    Make the next field of previous point to
                                                            the new node.




     Ver. 1.0                                                                        Session 8
Data Structures and Algorithms
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
                              newnode                       locate previous and current, execute the
                                                            following steps:
                                                              a.    Make current point to the first
                                 16                                 node.
                                                              b.    Make previous point to NULL.
                START                                         c.    Repeat steps d and e until
                                                                    current.info > newnode.info or
                                                                    current = NULL.
                                                              d.    Make previous point to current.
                 10
                  10        15          17        20          e.    Make current point to the next
                                                                    node in sequence.
                                                       •    Allocate memory for the new node.
                                                       •    Assign value to the data field of the new
                   previous           current               node.
                                                       •    Make the next field of the new node
                                                            point to current.
                        newnode.next = current         •    Make the prev field of the new node
                                                            point to previous.
                        newnode.prev = previous        •    Make the prev field of current point to
                                                            the new node.
                        current.prev = newnode         •    Make the next field of previous point to
                                                            the new node.




     Ver. 1.0                                                                        Session 8
Data Structures and Algorithms
Inserting a Node Between Two Nodes in the List (Contd.)
                                                       1.   Identify the nodes between which the
          Insertion complete                                new node is to be inserted. Mark them
                                                            as previous and current respectively. To
                              newnode                       locate previous and current, execute the
                                                            following steps:
                                                              a.    Make current point to the first
                                 16                                 node.
                                                              b.    Make previous point to NULL.
                START                                         c.    Repeat steps d and e until
                                                                    current.info > newnode.info or
                                                                    current = NULL.
                                                              d.    Make previous point to current.
                 10
                  10        15          17        20          e.    Make current point to the next
                                                                    node in sequence.
                                                       •    Allocate memory for the new node.
                                                       •    Assign value to the data field of the new
                   previous           current               node.
                                                       •    Make the next field of the new node
                                                            point to current.
                        newnode.next = current         •    Make the prev field of the new node
                                                            point to previous.
                        newnode.prev = previous        •    Make the prev field of current point to
                                                            the new node.
                        current.prev = newnode         •    Make the next field of previous point to
                        previous.next = newnode             the new node.




     Ver. 1.0                                                                        Session 8
Data Structures and Algorithms
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
                What is the problem with this                locate previous and current, execute the
                                                             following steps:
                algorithm?                                     a.    Make current point to the first
                                                                     node.
                   If current is NULL, then the new            b.    Make previous point to NULL.
                   node should be inserted at the              c.    Repeat steps d and e until
                                                                     current.info > newnode.info or
                   end of the list.                                  current = NULL.
                   However, in this case, step 6 will          d.    Make previous point to current.
                                                               e.    Make current point to the next
                   give an error.                                    node in sequence.
                                                        2.   Allocate memory for the new node.
                   This is because, NULL cannot         3.   Assign value to the data field of the new
                   have a prev field.                        node.
                                                        4.   Make the next field of the new node
                   Therefore, you need to modify             point to current.
                                                        5.   Make the prev field of the new node
                   this algorithm so that you can            point to previous.
                   also insert a node at the end of     6.   Make the prev field of current point to
                                                             the new node.
                   the list.                            7.   Make the next field of previous point to
                                                             the new node.




     Ver. 1.0                                                                         Session 8
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08
06 ds and algorithm session_08

Mais conteúdo relacionado

Mais de Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Último

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 

Último (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

06 ds and algorithm session_08

  • 1. Data Structures and Algorithms Objectives In this chapter, you will learn to: Implement a doubly-linked list Implement a circular linked list Apply linked lists to solve programming problems Ver. 1.0 Session 8
  • 2. Data Structures and Algorithms Implementing a Doubly-Linked List Now consider a case of 100000 numbers display a linked Consider a sorted list in which you need tostored inthese numbers in a descending order. list. How want to display these numbers in ascending order, If youwill you solve this problem? what will node do? Each you is linked to the next node in sequence. Traverse thethat starting from the first list in the forward This means list you can traverse the node. direction 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. Ver. 1.0 Session 8
  • 3. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. Algorithm to reverse a singly-linked list. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. 12. Make the next field of ptr1 point to NULL. 14. Make the next field of ptr2 point to ptr1. 16. 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. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 4. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) • Declare three variables / pointers ptr1, ptr2, and ptr 3. • If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. 12. Make the next field of ptr1 point to NULL. 14. Make the next field of ptr2 point to ptr1. ptr1 ptr2 ptr3 16. 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. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 5. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) • Declare three variables / pointers ptr1, ptr2, and ptr 3. • If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. 12. Make the next field of ptr1 point to NULL. 14. Make the next field of ptr2 point to ptr1. ptr1 ptr2 ptr3 16. 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. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 6. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. ptr1 ptr2 ptr3 • 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. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 7. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 ptr2 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. ptr2 ptr3 • 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. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 8. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 ptr2 ptr3 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. ptr3 • 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. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 9. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 ptr2 ptr3 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. • 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. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 10. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 ptr2 ptr3 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. • 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. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 11. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START • Mark the first node in the list as ptr1. 10 15 19 21 32 • Mark the successor of ptr1 as ptr2 • Mark the successor of ptr2 as ptr3. ptr1 ptr2 ptr3 • Make the next field of ptr1 point to NULL. • Make the next field of ptr2 point to ptr1. • 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. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 12. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr2 ptr3 12. Make the next field of ptr1 point to NULL. ptr1 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 13. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr2 ptr3 12. Make the next field of ptr1 point to NULL. ptr1 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 14. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr3 ptr3 12. Make the next field of ptr1 point to NULL. ptr1 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 15. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr3 12. Make the next field of ptr1 point to NULL. ptr1 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. • Make START point to ptr2. Ver. 1.0 Session 8
  • 16. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr1 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 17. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 18. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 19. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. • Make START point to ptr2. Ver. 1.0 Session 8
  • 20. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 21. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 22. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 ptr3 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL ptr3 = NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 23. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 1. Declare three variables / pointers ptr1, ptr2, and ptr 3. 3. If there is only one node in the list: a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL ptr3 = NULL • Set ptr1 = ptr2 • Set ptr2 = ptr3 • Make ptr3 point to the next node in its sequence. • Make the next field of ptr2 point to ptr1. • Make START point to ptr2. Ver. 1.0 Session 8
  • 24. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) • Declare three variables / pointers ptr1, ptr2, and ptr 3. • If there is only one node in the list: START a. Exit. START 6. Mark the first node in the list as ptr1. 10 15 19 21 32 8. Mark the successor of ptr1 as ptr2 10. Mark the successor of ptr2 as ptr3. ptr1 12. Make the next field of ptr1 point to NULL. ptr2 14. Make the next field of ptr2 point to ptr1. 16. Repeat until ptr3 becomes NULL ptr3 = 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. 19. Make START point to ptr2. Ver. 1.0 Session 8
  • 25. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) What is the problem with the previous algorithm? How can you solve this problem? This need to adjust the links if eachthe three the list holds the You problem can be solved for all node in variables reference of itsvisit the next node. addition to its next node in whenever you preceding node in sequence. Disadvantage of this approach: Consider the following list: and time consuming for large lists. This approach is inefficient START 10 15 19 21 23 25 32 Ver. 1.0 Session 8
  • 26. Data Structures and Algorithms Implementing a Doubly-Linked List (Contd.) 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. START 10 15 19 21 23 25 32 Structure of a doubly-linked list Ver. 1.0 Session 8
  • 27. Data Structures and Algorithms Representing a Doubly-Linked List A Linked list is represented in a program by defining two classes: – Node class: In a doubly-linked list, each node needs to store: – The information – The address of the next node in sequence – The address of the previous node // Code in C# class Node { public int info; public Node next; public Node prev; } Ver. 1.0 Session 8
  • 28. Data Structures and Algorithms Representing a Doubly-Linked List (Contd.) // Code in C++ class Node { public: int info; Node * next; Node * prev; }; Ver. 1.0 Session 8
  • 29. Data Structures and Algorithms Representing a Doubly-Linked List (Contd.) – DoubleLinkedList class: 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(){} } Ver. 1.0 Session 8
  • 30. Data Structures and Algorithms Representing 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(); }; Ver. 1.0 Session 8
  • 31. Data Structures and Algorithms Just a minute How does the representation of a node in a doubly-linked list differ from that of a singly-linked list? Answer: Unlike singly-linked list, in which each node stores the address of only the next node in the list, each node in a doubly-linked list holds the address of its previous node also. Ver. 1.0 Session 8
  • 32. Data Structures and Algorithms Traversing a Doubly-Linked List 1. Mark the first node in the list as currentNode. Algorithm to traverse a doubly a doubly linked list in the Write an algorithm to traverse 3. Repeat steps 3 and 4 until forward direction. linked list in the forward direction. currentNode becomes NULL. 5. Display the information contained in the node marked as currentNode. 7. Make currentNode point to the next node in sequence. Ver. 1.0 Session 8
  • 33. Data Structures and Algorithms Traversing a Doubly-Linked List (Contd.) 2. Mark the last node in the list as Algorithm to traverse a doubly a doubly linked list in the Write an algorithm to traverse currentNode. backward direction. linked list in the backward 4. Repeat steps 3 and 4 until currentNode becomes NULL. direction. 6. Display the information contained in the node marked as currentNode. 8. Make currentNode point to the node preceding it. Ver. 1.0 Session 8
  • 34. Data Structures and Algorithms Inserting Nodes in a Doubly-Linked List 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 Ver. 1.0 Session 8
  • 35. Data Structures and Algorithms Inserting a Node at the Beginning of the List Write an algorithm to insert a node in the beginning of a doubly-linked list. Ver. 1.0 Session 8
  • 36. Data Structures and Algorithms Inserting a Node at the Beginning of the List (Contd.) 1. Allocate memory for the new node. Algorithm to insert a node in the 3. Assign value to the data field of beginning of a doubly-linked list. the new node. 5. Make the next field of the new node point to the first node in the list. 7. Make the prev field of START point to the new node. 9. Make the prev field of the new node point to NULL. START 11. Make START, point to the new node. 10 15 17 20 Ver. 1.0 Session 8
  • 37. Data Structures and Algorithms Inserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • Make the next field of the new node point to the first node in the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new node. 10 10 15 17 20 Ver. 1.0 Session 8
  • 38. Data Structures and Algorithms Inserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. • Assign value to the data field of the new node. • Make the next field of the new node point to the first node in the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new 7 node. 10 10 15 17 20 Ver. 1.0 Session 8
  • 39. Data Structures and Algorithms Inserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. newnode.next = START • Assign value to the data field of the new node. • Make the next field of the new node point to the first node in the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new 7 node. 10 10 15 17 20 Ver. 1.0 Session 8
  • 40. Data Structures and Algorithms Inserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. newnode.next = START • Assign value to the data field of START.prev = newnode the new node. • Make the next field of the new node point to the first node in the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new 7 node. 10 10 15 17 20 Ver. 1.0 Session 8
  • 41. Data Structures and Algorithms Inserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. newnode.next = START • Assign value to the data field of START.prev = newnode the new node. newnode.prev = NULL • Make the next field of the new node point to the first node in the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new 7 node. 10 10 15 17 20 Ver. 1.0 Session 8
  • 42. Data Structures and Algorithms Inserting a Node at the Beginning of the List (Contd.) • Allocate memory for the new node. newnode.next = START • Assign value to the data field of START.prev = newnode the new node. newnode.prev = NULL • Make the next field of the new node point to the first node in START = newnode the list. • Make the prev field of START point to the new node. newnode • Make the prev field of the new node point to NULL. START • Make START, point to the new 7 node. 10 10 15 17 20 Insertion complete Ver. 1.0 Session 8
  • 43. Data Structures and Algorithms Inserting a Node Between Two Nodes in the List Write an algorithm to insert a node between two nodes in a doubly-linked list. Ver. 1.0 Session 8
  • 44. Data Structures and Algorithms 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 Insert 16 Algorithm to insert a node between locate previous and current, execute the following steps: two nodes in a doubly-linked list. a. Make current point to the first node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 15 17 20 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. Ver. 1.0 Session 8
  • 45. Data Structures and Algorithms 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 Insert 16 locate previous and current, execute the following steps: a. Make current point to the first node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 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. Ver. 1.0 Session 8
  • 46. Data Structures and Algorithms 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 Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • 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 current 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. Ver. 1.0 Session 8
  • 47. Data Structures and Algorithms 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 Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • 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 current node. 4. Make the next field of the new node previous = NULL 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. Ver. 1.0 Session 8
  • 48. Data Structures and Algorithms 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 Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • 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 current node. 4. Make the next field of the new node previous = NULL 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. Ver. 1.0 Session 8
  • 49. Data Structures and Algorithms 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 Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • 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 previous current node. 4. Make the next field of the new node previous = NULL 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. Ver. 1.0 Session 8
  • 50. Data Structures and Algorithms 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 Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • 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 previous current current 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. Ver. 1.0 Session 8
  • 51. Data Structures and Algorithms 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 Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • 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 previous current 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. Ver. 1.0 Session 8
  • 52. Data Structures and Algorithms 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 Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • 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 previous previous current 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. Ver. 1.0 Session 8
  • 53. Data Structures and Algorithms 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 Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • 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 previous currentcurrent 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. Ver. 1.0 Session 8
  • 54. Data Structures and Algorithms 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 Insert 16 locate previous and current, execute the following steps: • Make current point to the first node. • Make previous point to NULL. START • Repeat steps d and e until current.info > newnode.info or current = NULL. • Make previous point to current. 10 10 15 17 20 • 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 previous current 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. Ver. 1.0 Session 8
  • 55. Data Structures and Algorithms 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 Insert 16 newnode locate previous and current, execute the following steps: a. Make current point to the first node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. • Make the prev field of the new node point to previous. • Make the prev field of current point to the new node. • Make the next field of previous point to the new node. Ver. 1.0 Session 8
  • 56. Data Structures and Algorithms 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 Insert 16 newnode locate previous and current, execute the following steps: a. Make current point to the first 16 node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. • Make the prev field of the new node point to previous. • Make the prev field of current point to the new node. • Make the next field of previous point to the new node. Ver. 1.0 Session 8
  • 57. Data Structures and Algorithms 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 newnode locate previous and current, execute the following steps: a. Make current point to the first 16 node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. newnode.next = current • Make the prev field of the new node point to previous. • Make the prev field of current point to the new node. • Make the next field of previous point to the new node. Ver. 1.0 Session 8
  • 58. Data Structures and Algorithms 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 newnode locate previous and current, execute the following steps: a. Make current point to the first 16 node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. newnode.next = current • Make the prev field of the new node point to previous. newnode.prev = previous • Make the prev field of current point to the new node. • Make the next field of previous point to the new node. Ver. 1.0 Session 8
  • 59. Data Structures and Algorithms 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 newnode locate previous and current, execute the following steps: a. Make current point to the first 16 node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. newnode.next = current • Make the prev field of the new node point to previous. newnode.prev = previous • Make the prev field of current point to the new node. current.prev = newnode • Make the next field of previous point to the new node. Ver. 1.0 Session 8
  • 60. Data Structures and Algorithms Inserting a Node Between Two Nodes in the List (Contd.) 1. Identify the nodes between which the Insertion complete new node is to be inserted. Mark them as previous and current respectively. To newnode locate previous and current, execute the following steps: a. Make current point to the first 16 node. b. Make previous point to NULL. START c. Repeat steps d and e until current.info > newnode.info or current = NULL. d. Make previous point to current. 10 10 15 17 20 e. Make current point to the next node in sequence. • Allocate memory for the new node. • Assign value to the data field of the new previous current node. • Make the next field of the new node point to current. newnode.next = current • Make the prev field of the new node point to previous. newnode.prev = previous • Make the prev field of current point to the new node. current.prev = newnode • Make the next field of previous point to previous.next = newnode the new node. Ver. 1.0 Session 8
  • 61. Data Structures and Algorithms 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 What is the problem with this locate previous and current, execute the following steps: algorithm? a. Make current point to the first node. If current is NULL, then the new b. Make previous point to NULL. node should be inserted at the c. Repeat steps d and e until current.info > newnode.info or end of the list. current = NULL. However, in this case, step 6 will d. Make previous point to current. e. Make current point to the next give an error. node in sequence. 2. Allocate memory for the new node. This is because, NULL cannot 3. Assign value to the data field of the new have a prev field. node. 4. Make the next field of the new node Therefore, you need to modify point to current. 5. Make the prev field of the new node this algorithm so that you can point to previous. also insert a node at the end of 6. Make the prev field of current point to the new node. the list. 7. Make the next field of previous point to the new node. Ver. 1.0 Session 8

Notas do Editor

  1. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  2. Ask the students to write the algorithm first and then show them the solution for the same.
  3. Ask the students to write the algorithm first and then show them the solution for the same.
  4. Tell the students that a circular linked list can be a singly circular or a doubly circular linked list.
  5. Ask the students to write the algorithm first and then come up with the solution.
  6. Tell the students that inserting a node in the middle of a circular linked list is same as that of a singly-linked list. This is because the only difference between the two lists is with the arrangement in the address field of the last node. Therefore, the process of insertion of a node at the end of the two lists is different.
  7. Ask the students to write an algorithm to insert a node between two nodes in a circular linked list.
  8. Tell the students that in a circular linked list, the process of deleting a node other than the first and the last node is same as that of a singly-linked list. However, the algorithm to locate the node to be deleted is different in a circular linked list. Ask the student to write the algorithm to locate the node to be deleted in a circular linked list.
  9. Ask the student to write an algorithm to delete a node other than the first and the last node in a circular linked list.
  10. Linked lists also offers the following applications: To store the data typed in an open Word document. To store the coordinates of the various pixels that constitute a snake in the Snake game. To implement a new data type that can store an integer of any length. To store the details of various objects drawn on a powerpoint slide.
  11. Linked lists also offers the following applications: To store the data typed in an open Word document. To store the coordinates of the various pixels that constitute a snake in the Snake game. To implement a new data type that can store an integer of any length. To store the details of various objects drawn on a powerpoint slide.
  12. Linked lists also offers the following applications: To store the data typed in an open Word document. To store the coordinates of the various pixels that constitute a snake in the Snake game. To implement a new data type that can store an integer of any length. To store the details of various objects drawn on a powerpoint slide.