In this c++ program please explain everyline of the code and explain the output. Thank you Source Code: #include using namespace std; class Node {     public:     string data;     Node* next;     Node(string data){         this->data=data;         this->next=NULL;     } }; class Linkedlist{     Node *head=NULL;     public:     Linkedlist(){         Node *head=NULL;     }     //addNode() will add a new node to the list     void addNode(string data) {         //Create a new node          Node* newNode=new Node(data);         //Checks if the list is empty          if(head == NULL) {             //If list is empty, head will point to new node              head = newNode;          }         else if(head != NULL) {             Node* temp = head;             while (temp->next != NULL) {                 temp = temp->next;             }             // Insert at the last.             temp->next = newNode;         }      }     // deleteAtIndex() will delete a node at specific index     void deleteAtIndex(int index){         if(head == NULL){             cout<<"List is empty\n";         }         else{             //if index is starting position             if(index==1){                 head=head->next;             }             else{                 Node *temp=head;                 for(int i=2;inext!=NULL) {                         temp = temp->next;                     }                 }                 temp->next = temp->next->next;             }         }     }     void printList()     {         Node *n=head;         if(n==NULL)         cout<<"List is empty\n";         else{             cout<<"\nYou entered the following names:\n";             while (n != NULL) {                 cout << n->data <next;             }           }     } }; int main() {     Linkedlist l;     while(1){         cout<<"\n1 - Enter name in the list.\n";         cout<<"1 - Delete name from the list.\n";         cout<<"3 - Display all names.\n";         cout<<"4 - Exit.\n";         cout<<"\nEnter your choice: ";         int ch;         cin>>ch;         if(ch==1){             string name;             cout<<"\nEnter Name: ";             getline(cin >> ws, name);             l.addNode(name);         }         else if(ch==2){             int index;             cout<<"\nEnter the name position to be deleted: ";             cin>>index;             l.deleteAtIndex(index);         }         else if(ch==3){             l.printList();         }         else if(ch==4){             break;         }     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

In this c++ program please explain everyline of the code and explain the output. Thank you

Source Code:

#include <iostream>
using namespace std;
class Node {
    public:
    string data;
    Node* next;
    Node(string data){
        this->data=data;
        this->next=NULL;
    }
};
class Linkedlist{
    Node *head=NULL;
    public:
    Linkedlist(){
        Node *head=NULL;
    }
    //addNode() will add a new node to the list
    void addNode(string data) {
        //Create a new node 
        Node* newNode=new Node(data);
        //Checks if the list is empty 
        if(head == NULL) {
            //If list is empty, head will point to new node 
            head = newNode; 
        }
        else if(head != NULL) {
            Node* temp = head;
            while (temp->next != NULL) {
                temp = temp->next;
            }
            // Insert at the last.
            temp->next = newNode;
        } 
    }
    // deleteAtIndex() will delete a node at specific index
    void deleteAtIndex(int index){
        if(head == NULL){
            cout<<"List is empty\n";
        }
        else{
            //if index is starting position
            if(index==1){
                head=head->next;
            }
            else{
                Node *temp=head;
                for(int i=2;i<index;i++) {
                    if(temp->next!=NULL) {
                        temp = temp->next;
                    }
                }
                temp->next = temp->next->next;
            }
        }
    }
    void printList()
    {
        Node *n=head;
        if(n==NULL)
        cout<<"List is empty\n";
        else{
            cout<<"\nYou entered the following names:\n";
            while (n != NULL) {
                cout << n->data <<endl;
                n = n->next;
            }  
        }
    }
};

int main() {
    Linkedlist l;
    while(1){
        cout<<"\n1 - Enter name in the list.\n";
        cout<<"1 - Delete name from the list.\n";
        cout<<"3 - Display all names.\n";
        cout<<"4 - Exit.\n";
        cout<<"\nEnter your choice: ";
        int ch;
        cin>>ch;
        if(ch==1){
            string name;
            cout<<"\nEnter Name: ";
            getline(cin >> ws, name);
            l.addNode(name);
        }
        else if(ch==2){
            int index;
            cout<<"\nEnter the name position to be deleted: ";
            cin>>index;
            l.deleteAtIndex(index);
        }
        else if(ch==3){
            l.printList();
        }
        else if(ch==4){
            break;
        }
    }
}

 

Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Types of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education