You need to edit the same code so as to make it a circular doubly linked list. Implement the necessary functionality in all the given functions.  Note: A circular doubly linked list is a doubly linked list whose last next pointer points to the head node     #include using namespace std;   class Node{     public:     int data;     Node *next;     Node *prev; };   class List{     public:     Node *head;     List(){         head = NULL;     }         void display(){         Node *temp = head;         while(temp!=NULL)         {             cout<data<next;         }     }       void insert(int data){         Node *nn = new Node;         nn->next = NULL;         nn->prev = NULL;         nn->data = data;         if(head==NULL){             head = nn;             cout<<"Node inserted successfully at head"<next!=NULL)                 temp = temp->next;             temp->next=nn;             nn->prev = temp;             cout<<"Node inserted successfully at end"<data==data){                 cout<<"Data found"<next;         }         return temp;     }         void insertInBetween(Node *node, int data){         Node *nn = new Node;         nn->data = data;         Node *temp = node->next;         node->next = nn;         nn->next = temp;         nn->prev = node;         temp->prev = nn;         cout<data<<" inserted successfully after: "<data<data = data;         nn->next = head;         nn->prev = NULL;         head->prev = nn;         head = nn;     }         void deleteNode(int data){         Node *temp = search(data);         if(temp==NULL)             cout<<"Node not found"<next;                 head->prev = NULL;             }             else             {                 Node *before = temp->prev;             //    while(before->next != temp)             //        before = before->next;                 before->next = temp->next;                 temp->next->prev = before;             }             delete temp;         }     } };   int main() {     List list;     list.insert(10);     list.insert(20);     list.insert(30);     list.display();     Node *found = list.search(20);     if(found==NULL)         cout<<"Node not found"<data<

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

 You need to edit the same code so as to make it a circular doubly linked list. Implement the necessary functionality in all the given functions. 

Note: A circular doubly linked list is a doubly linked list whose last next pointer points to the head node

 

 

#include <iostream>

using namespace std;

 

class Node{

    public:

    int data;

    Node *next;

    Node *prev;

};

 

class List{

    public:

    Node *head;

    List(){

        head = NULL;

    }

   

    void display(){

        Node *temp = head;

        while(temp!=NULL)

        {

            cout<<temp->data<<endl;

            temp=temp->next;

        }

    }

 

    void insert(int data){

        Node *nn = new Node;

        nn->next = NULL;

        nn->prev = NULL;

        nn->data = data;

        if(head==NULL){

            head = nn;

            cout<<"Node inserted successfully at head"<<endl;

        }

        else{

            Node *temp = head;

            while(temp->next!=NULL)

                temp = temp->next;

            temp->next=nn;

            nn->prev = temp;

            cout<<"Node inserted successfully at end"<<endl;

        }

    }

   

    Node* search(int data){

        Node *temp = head;

        while(temp!=NULL)

        {

            if(temp->data==data){

                cout<<"Data found"<<endl;

                break;

            }

            temp = temp->next;

        }

        return temp;

    }

 

 

    void insertInBetween(Node *node, int data){

        Node *nn = new Node;

        nn->data = data;

        Node *temp = node->next;

        node->next = nn;

        nn->next = temp;

        nn->prev = node;

        temp->prev = nn;

        cout<<nn->data<<" inserted successfully after: "<<node->data<<endl;

    }

   

    void insertAtHead(int data){

        Node *nn = new Node;

        nn->data = data;

        nn->next = head;

        nn->prev = NULL;

        head->prev = nn;

        head = nn;

    }

   

    void deleteNode(int data){

        Node *temp = search(data);

        if(temp==NULL)

            cout<<"Node not found"<<endl;

        else{

            if(head==temp){

                head = head->next;

                head->prev = NULL;

            }

            else

            {

                Node *before = temp->prev;

            //    while(before->next != temp)

            //        before = before->next;

                before->next = temp->next;

                temp->next->prev = before;

            }

            delete temp;

        }

    }

};

 

int main() {

    List list;

    list.insert(10);

    list.insert(20);

    list.insert(30);

    list.display();

    Node *found = list.search(20);

    if(found==NULL)

        cout<<"Node not found"<<endl;

    else

        cout<<"Found node: "<<found->data<<endl;

    list.insertInBetween(found, 25);

    list.display();

    list.insertAtHead(5);

    list.display();

    list.deleteNode(10);

    list.display();

    return 0;

}

Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY