Consider the below code of circular linked list. Your work is to update the same code, by providing the functionalities with tail pointer only.

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

Consider the below code of circular linked list. Your work is to update the same code, by providing the functionalities with tail pointer only.

 

 

#include <iostream>

using namespace std;

 

class Node{

    public:

    int data;

    Node *next;

};

 

class List{

    public:

    Node *head;

    List(){

        head = NULL;

    }

    void insert(int data){

        Node *nn = new Node;

        nn->data = data;

        if(head==NULL){

            head = nn;

            nn->next = head;

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

        }

        else{

            Node *temp = head;

            while(temp->next!=head)

                temp = temp->next;

            nn->next = head;

            temp->next=nn;

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

        }

    }

 

    void insertInBetween(Node *node, int data){

        Node *nn = new Node;

        nn->data = data;

        Node *temp = node->next;

        node->next = nn;

        nn->next = temp;

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

    }

   

    void display(){

        Node *temp = head;

        while(temp->next!=head)

        {

            cout<<temp->data<<endl;

            temp=temp->next;

        }

        cout<<temp->data<<endl;

    }

   

    void insertAtHead(int data){

        Node *nn = new Node;

        nn->data = data;

        nn->next = head;

        Node *tail = head;

        while(tail->next!=head)

            tail = tail->next;

        tail->next = nn;

        head = nn;

    }

   

    Node* search(int data){

        Node *temp = head;

        if(temp->data==data){

                cout<<"Data found"<<endl;

                return temp;

            }

        temp = temp->next;

        while(temp!=head)

        {

            if(temp->data==data){

                cout<<"Data found"<<endl;

                return temp;

            }

            temp = temp->next;

        }

        return NULL;

    }

   

    void deleteNode(int data){

        Node *temp = search(data);

        if(temp==NULL)

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

        else{

            if(head==temp){

                Node *tail = head;

                while(tail->next!=head)

                    tail = tail->next;

                tail->next = head->next;

                head = head->next;

            }

            else

            {

                Node *before = head;

                while(before->next != temp)

                    before = before->next;

                before->next = temp->next;

            }

            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(5);//node not found

    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