SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Data Structures

  Doubly Link List
Problem ….
   The member function deleteFromTail() of
    singly linked list indicates a problem
    inherent to singly linked lists.
   The nodes in singly linked lists contain only
    pointers to the successors;
   Therefore there is no immediate access to
    the predecessors.
   For this reason, deleteFromTail() was
    implemented with a loop allowed us to find
    the predecessor of tail.
   But the problem is
Problem ….
 We have to scan the entire list to stop
  right in front of the tail to delete it.
 For long lists and for frequent
  executions of deleteFromTail(), this
  may be an impediment to swift list
  processing.
 Is there any way to avoid this
  effort?
Solution
 We redefine the linked list so that
 Each node in a list has two pointers,

  one to the successor and one to the
  predecessor.
 A list of this type is called a doubly

  linked list.
       a      b         c          d 

head                                 tail
Doubly Linked List
             Operations
    The common operations on doubly linked lists are:
1.   AddToDLLHead(x) Inserts element x in front of the
     doubly linked list .
2.   AddToDLLTail(x) Inserts element x at the end of
     the doubly linked list.
3.   DeleteFromDLLHead() Deletes the first element of
     the doubly linked list.
4.   DeleteFromDLLTail() Deletes the last element of
     the doubly linked list.
5.   DLLDelete(x) Deletes the node of value x from the
     doubly linked list.
6.   DLLFind(x) Find the entry x in the doubly linked
     list.
7.   DLLprint() prints the contents of the doubly linked
     list.
8.   And so on i.e.as many operations as you
     required
Doubly Linked List
Operations AddToDLLTail(x)
   Two cases must be considered to add the
    node at the end of doubly linked list.

    1. Either we are creating the first node of the
       doubly linked list or

    1. We are inserting the node at the end of an
       existing doubly linked list.
Doubly Linked List
 Operations AddToDLLTail(x)
    First Case: Creating first node of a doubly
     linked list.
    We follow the following steps to create the first
     node of the doubly linked list.
1.   Check the value of the head of the doubly linked
     list.
        If this value is null it means we are creating the first node
        of the doubly linked list.
2.   An empty node of doubly linked list is created.

        It is empty in the sense that the program performing
        insertion does not assign any values to the data members
        of the node.
Doubly Linked List
 Operations AddToDLLTail(x)
3.   The node's info member is initialized to a particular
     value.
                           7

4. Since it is the only node of the doubly linked list
   therefore its next and previous members will be
   initialized to null.
                          7   

5. Since it is the only node of the doubly linked list
   therefore both head and tail points to this node of the
   doubly linked list.             7 
                           head                tail
Doubly Linked List
 Operations AddToDLLTail(x)
    Second Case: Inserting node at the
     end of an existing doubly linked
     list.
    We follow the following steps to insert
     the node at the end of an existing
     doubly linked list.
1.   Check the value of the head of the doubly
     list.
       If this value is not null it means doubly linked
       list already exists and we insert the node at the
       end of an existing doubly linked list.
                                7 
                        head               tail
Doubly Linked List
 Operations AddToDLLTail(x)
2.    An empty node of doubly linked list is
      created.

         It is empty in the sense that the program
         performing insertion does not assign any
         values to the data members of the node.
              7 
     head                tail
Doubly Linked List
 Operations AddToDLLTail(x)
3.    The node's info member is initialized to a particular value.

                    7                       8       
     head                        tail

4.    Because the node is being included at the end of the doubly
      linked list, the next member is set to null.
5.    Set the previous member of the new node to the value of tail,
      so that this member points to the last node in the list.

6.    Set the next member of the tail to point to the new node

                        7                        8       
      head
                                   tail
Doubly Linked List
 Operations AddToDLLTail(x)
7.   Now, the new node should become the last node of the
     linked list, therefore tail is set to point to the new node.


                     7                          8       
     head                     tail

                         7                          8       
       head                                                      tail
Doubly Linked List Operations
       DeleteFromDLLTail(x)
    Two cases must be considered to
     delete the node from the end of a
     linked list.

    1. Either a doubly linked list has only one
       node or



    1. A doubly linked list has more than one
       nodes.
Doubly Linked List Operations
        DeleteFromDLLTail(x)
     First Case: Linked List has only
      one node:
     We follow the following steps to
      handle this case:
1.    Check the values of the pointer
      variables head and tail.
        If both points to the same node, means
        doubly linked list has only one node.
                                7   
                      head               tail
Doubly Linked List Operations
        DeleteFromDLLTail(x)
2.    Set both head and tail pointers to
      null and return back the memory
      occupied by the node to the system.
Doubly Linked List Operations
         DeleteFromDLLTail(x)
      Second Case: Doubly Linked List
       has more than one nodes:
      We follow the following steps to
       handle this case:
 1.    Check the values of the pointer
       variables head and tail.
           If both points to the different nodes,
           means linked list has more than one
           nodes.
           7          8          9          4   
head                                                 tail
Doubly Linked List Operations
         DeleteFromDLLTail(x)
 2.    Set the variable tail to its
       predecessor.
          7        8          9      4   
head                                          tail

          7        8          9      4   
head
                               tail
Doubly Linked List Operations
         DeleteFromDLLTail(x)
 3.     Remove the last node of the doubly
        linked list, i.e. node next to the node
        pointed by tail pointer.
               7    8         9        4   
head
                               tail

                7    8         9
 head
                                tail
Doubly Linked List Operations
        DeleteFromDLLTail(x)
4.     The next of the tail node is a
       dangling reference; therefore next is
       set to null.
           7       8        9
head
                             tail

           7       8         9      
head
                              tail
Implementation of
           Doubly Linked List
template<class T>
class DLLNode {
public:
   T info;
   DLLNode *next, *prev;
   DLLNode() {
        next = prev = 0;
   }
   DLLNode(T el, Node *n = 0, Node *p = 0) {
        info = el;
        next = n;
        prev = p;
   }
};
Implementation of
              Doubly Linked List
template<class T>
class DoublyLInkedList {
private:
        DLLNode <T> *head, *tail;
Public:
     DoublyLinkedList(){
       head = tail =0;
     }
     void addToDLLTail(const T&)
     void deleteFromDLLTail();
     ……………………………..

};
Implementation of
                       Doubly Linked List
       template<class T>
       void DoublyLinkedList<T>:: addToDLLTail(const T& el)
       { if(head == 0)                     //List is empty
                                                                   7 
            head = tail = new DLLNode<T>(el); head                           tail
         else               // Insert node at the end of the list
         { tail = new DLLNode<T>(e1, 0, tail);
             tail -> prev -> next = tail
               }
       }

                  7                   8                     9   
head                                              tail                    tail
Implementation of
                   Doubly Linked List
template<class T>
void DoublyLinkedList<T>:: deleteFromDLLTail()
{ if(head != 0)                      //List not empty
    if(head == tail) { //If only one node in the list
           delete head;           head = tail = 0; }                7 
        else // more then one nodes in the list       head                    tail
        {
             tail = tail -> prev;
             delete tail -> next;
            tail -> next = 0;
           }
 }            7                      8                         9   
head                                            tail                       tail
Lab Exercise
    Implement the template class for doubly
     linked list with following member functions.
1.   A function which inserts node in front of
     doubly linked list.
2.   A function which inserts node at the end of
     doubly linked list
3.   A function which removes a node from the
     front of doubly linked list.
4.   A function which removes a node from the
     end of doubly linked list.
Lab Exercise
5.   A function which removes a node of
     particular value from doubly linked
     list.
6.   A function which finds a node of
     particular value from doubly linked
     list.
7.   A function which prints the contents of
     doubly linked list.

Weitere ähnliche Inhalte

Was ist angesagt?

Linked list
Linked listLinked list
Linked list
VONI
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
student
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
Kumar
 

Was ist angesagt? (20)

Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
linked list
linked listlinked list
linked list
 
Singly link list
Singly link listSingly link list
Singly link list
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Linked list
Linked listLinked list
Linked list
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
linked list
linked list linked list
linked list
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Data Structure (Double Linked List)
Data Structure (Double Linked List)Data Structure (Double Linked List)
Data Structure (Double Linked List)
 
Linked lists 1
Linked lists 1Linked lists 1
Linked lists 1
 
Linked list
Linked listLinked list
Linked list
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
Data Structure (Circular Linked List)
Data Structure (Circular Linked List)Data Structure (Circular Linked List)
Data Structure (Circular Linked List)
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
Link list
Link listLink list
Link list
 
linked list
linked list linked list
linked list
 

Andere mochten auch (8)

LINKED LISTS
LINKED LISTSLINKED LISTS
LINKED LISTS
 
Car Parking System (Singly linked list )
Car Parking System (Singly linked list ) Car Parking System (Singly linked list )
Car Parking System (Singly linked list )
 
Singly linked lists
Singly linked listsSingly linked lists
Singly linked lists
 
Linked list without animation
Linked list without animationLinked list without animation
Linked list without animation
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
Data structures1
Data structures1Data structures1
Data structures1
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
 
Double linked list
Double linked listDouble linked list
Double linked list
 

Ähnlich wie Data Structure Lecture 6

Data Structure Lecture 7
Data Structure Lecture 7Data Structure Lecture 7
Data Structure Lecture 7
Teksify
 
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
sauravmanwanicp
 
Use the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdfUse the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdf
sales87
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
MatthPYNashd
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docxSIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
edgar6wallace88877
 
Mod 4 Homeork Enhanced DoublyLinkedList Starter Code- # Do not modify.docx
Mod 4 Homeork Enhanced DoublyLinkedList Starter Code- # Do not modify.docxMod 4 Homeork Enhanced DoublyLinkedList Starter Code- # Do not modify.docx
Mod 4 Homeork Enhanced DoublyLinkedList Starter Code- # Do not modify.docx
Jason0x0Scottw
 

Ähnlich wie Data Structure Lecture 6 (20)

Data Structure Lecture 7
Data Structure Lecture 7Data Structure Lecture 7
Data Structure Lecture 7
 
Linked list
Linked list Linked list
Linked list
 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
 
Use the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdfUse the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdf
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
 
Ds lists
Ds listsDs lists
Ds lists
 
3.ppt
3.ppt3.ppt
3.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Data Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxData Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptx
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docxSIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Linked list (introduction) 1
Linked list (introduction) 1Linked list (introduction) 1
Linked list (introduction) 1
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
 
Mod 4 Homeork Enhanced DoublyLinkedList Starter Code- # Do not modify.docx
Mod 4 Homeork Enhanced DoublyLinkedList Starter Code- # Do not modify.docxMod 4 Homeork Enhanced DoublyLinkedList Starter Code- # Do not modify.docx
Mod 4 Homeork Enhanced DoublyLinkedList Starter Code- # Do not modify.docx
 
linked list
linked listlinked list
linked list
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
 

Mehr von Teksify (10)

HCTE C&C08(TDM)
HCTE C&C08(TDM) HCTE C&C08(TDM)
HCTE C&C08(TDM)
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
 
Data Structure Lecture 3
Data Structure Lecture 3Data Structure Lecture 3
Data Structure Lecture 3
 
Data Structure Lecture 2
Data Structure Lecture 2Data Structure Lecture 2
Data Structure Lecture 2
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
Ch3
Ch3Ch3
Ch3
 
Ch1 2
Ch1 2Ch1 2
Ch1 2
 
Variable power supply
Variable power supplyVariable power supply
Variable power supply
 
Use of rib tool in pro e
Use of rib tool in pro eUse of rib tool in pro e
Use of rib tool in pro e
 
Make lens of mobile by pro e
Make lens of mobile by pro eMake lens of mobile by pro e
Make lens of mobile by pro e
 

Kürzlich hochgeladen

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Kürzlich hochgeladen (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

Data Structure Lecture 6

  • 1. Data Structures Doubly Link List
  • 2. Problem ….  The member function deleteFromTail() of singly linked list indicates a problem inherent to singly linked lists.  The nodes in singly linked lists contain only pointers to the successors;  Therefore there is no immediate access to the predecessors.  For this reason, deleteFromTail() was implemented with a loop allowed us to find the predecessor of tail.  But the problem is
  • 3. Problem ….  We have to scan the entire list to stop right in front of the tail to delete it.  For long lists and for frequent executions of deleteFromTail(), this may be an impediment to swift list processing.  Is there any way to avoid this effort?
  • 4. Solution  We redefine the linked list so that  Each node in a list has two pointers, one to the successor and one to the predecessor.  A list of this type is called a doubly linked list. a b c d head tail
  • 5. Doubly Linked List Operations  The common operations on doubly linked lists are: 1. AddToDLLHead(x) Inserts element x in front of the doubly linked list . 2. AddToDLLTail(x) Inserts element x at the end of the doubly linked list. 3. DeleteFromDLLHead() Deletes the first element of the doubly linked list. 4. DeleteFromDLLTail() Deletes the last element of the doubly linked list. 5. DLLDelete(x) Deletes the node of value x from the doubly linked list. 6. DLLFind(x) Find the entry x in the doubly linked list. 7. DLLprint() prints the contents of the doubly linked list. 8. And so on i.e.as many operations as you required
  • 6. Doubly Linked List Operations AddToDLLTail(x)  Two cases must be considered to add the node at the end of doubly linked list. 1. Either we are creating the first node of the doubly linked list or 1. We are inserting the node at the end of an existing doubly linked list.
  • 7. Doubly Linked List Operations AddToDLLTail(x)  First Case: Creating first node of a doubly linked list.  We follow the following steps to create the first node of the doubly linked list. 1. Check the value of the head of the doubly linked list. If this value is null it means we are creating the first node of the doubly linked list. 2. An empty node of doubly linked list is created. It is empty in the sense that the program performing insertion does not assign any values to the data members of the node.
  • 8. Doubly Linked List Operations AddToDLLTail(x) 3. The node's info member is initialized to a particular value. 7 4. Since it is the only node of the doubly linked list therefore its next and previous members will be initialized to null. 7 5. Since it is the only node of the doubly linked list therefore both head and tail points to this node of the doubly linked list. 7 head tail
  • 9. Doubly Linked List Operations AddToDLLTail(x)  Second Case: Inserting node at the end of an existing doubly linked list.  We follow the following steps to insert the node at the end of an existing doubly linked list. 1. Check the value of the head of the doubly list. If this value is not null it means doubly linked list already exists and we insert the node at the end of an existing doubly linked list. 7 head tail
  • 10. Doubly Linked List Operations AddToDLLTail(x) 2. An empty node of doubly linked list is created. It is empty in the sense that the program performing insertion does not assign any values to the data members of the node. 7 head tail
  • 11. Doubly Linked List Operations AddToDLLTail(x) 3. The node's info member is initialized to a particular value. 7 8 head tail 4. Because the node is being included at the end of the doubly linked list, the next member is set to null. 5. Set the previous member of the new node to the value of tail, so that this member points to the last node in the list. 6. Set the next member of the tail to point to the new node 7 8 head tail
  • 12. Doubly Linked List Operations AddToDLLTail(x) 7. Now, the new node should become the last node of the linked list, therefore tail is set to point to the new node. 7 8 head tail 7 8 head tail
  • 13. Doubly Linked List Operations DeleteFromDLLTail(x)  Two cases must be considered to delete the node from the end of a linked list. 1. Either a doubly linked list has only one node or 1. A doubly linked list has more than one nodes.
  • 14. Doubly Linked List Operations DeleteFromDLLTail(x)  First Case: Linked List has only one node:  We follow the following steps to handle this case: 1. Check the values of the pointer variables head and tail. If both points to the same node, means doubly linked list has only one node. 7 head tail
  • 15. Doubly Linked List Operations DeleteFromDLLTail(x) 2. Set both head and tail pointers to null and return back the memory occupied by the node to the system.
  • 16. Doubly Linked List Operations DeleteFromDLLTail(x)  Second Case: Doubly Linked List has more than one nodes:  We follow the following steps to handle this case: 1. Check the values of the pointer variables head and tail. If both points to the different nodes, means linked list has more than one nodes. 7 8 9 4 head tail
  • 17. Doubly Linked List Operations DeleteFromDLLTail(x) 2. Set the variable tail to its predecessor. 7 8 9 4 head tail 7 8 9 4 head tail
  • 18. Doubly Linked List Operations DeleteFromDLLTail(x) 3. Remove the last node of the doubly linked list, i.e. node next to the node pointed by tail pointer. 7 8 9 4 head tail 7 8 9 head tail
  • 19. Doubly Linked List Operations DeleteFromDLLTail(x) 4. The next of the tail node is a dangling reference; therefore next is set to null. 7 8 9 head tail 7 8 9 head tail
  • 20. Implementation of Doubly Linked List template<class T> class DLLNode { public: T info; DLLNode *next, *prev; DLLNode() { next = prev = 0; } DLLNode(T el, Node *n = 0, Node *p = 0) { info = el; next = n; prev = p; } };
  • 21. Implementation of Doubly Linked List template<class T> class DoublyLInkedList { private: DLLNode <T> *head, *tail; Public: DoublyLinkedList(){ head = tail =0; } void addToDLLTail(const T&) void deleteFromDLLTail(); …………………………….. };
  • 22. Implementation of Doubly Linked List template<class T> void DoublyLinkedList<T>:: addToDLLTail(const T& el) { if(head == 0) //List is empty 7 head = tail = new DLLNode<T>(el); head tail else // Insert node at the end of the list { tail = new DLLNode<T>(e1, 0, tail); tail -> prev -> next = tail } } 7 8 9 head tail tail
  • 23. Implementation of Doubly Linked List template<class T> void DoublyLinkedList<T>:: deleteFromDLLTail() { if(head != 0) //List not empty if(head == tail) { //If only one node in the list delete head; head = tail = 0; } 7 else // more then one nodes in the list head tail { tail = tail -> prev; delete tail -> next; tail -> next = 0; } } 7 8 9 head tail tail
  • 24. Lab Exercise  Implement the template class for doubly linked list with following member functions. 1. A function which inserts node in front of doubly linked list. 2. A function which inserts node at the end of doubly linked list 3. A function which removes a node from the front of doubly linked list. 4. A function which removes a node from the end of doubly linked list.
  • 25. Lab Exercise 5. A function which removes a node of particular value from doubly linked list. 6. A function which finds a node of particular value from doubly linked list. 7. A function which prints the contents of doubly linked list.