Home > Enterprise >  In this Linked List why it is not allowing me to run again and create another node what is the error
In this Linked List why it is not allowing me to run again and create another node what is the error

Time:10-07

I am trying to make employee database using Linked list data structure but once I enter the value the option to run again is not available and also display function is not executes code stops before that I have checked the code sevearl times but I am not able to spot the error.

#include<iostream>

using namespace std;

class node
{
    public:
    int Emp_No;
    node *next;
    node()
    {
        next=NULL;
    }
};

class Link_List
{
    public:
    node *head;
    
    Link_List()
    {
        head==NULL;
    }
    void create();
    void display();
};

void Link_List::create()
{
    node *temp,*p;
    int again;
    do
    {
        temp=new node();
        cout<<"Enter Employee No.: ";
        cin>>temp->Emp_No;
        if (head==NULL)
        {
            head=temp;
        }
        else
        {
            p=head;
            while (p->next!=NULL)
            {
                p=p ->next;
            }
            p ->next=temp;
        }
        cout<<"Enter 1 to add more: ";
        cin>>again;
    } while (again==1);
}

void Link_List::display()
{
    node *p1;
    if (head==NULL)
    {
        cout<<"The linked list is empty"<<endl;
    }
    else
    {
        p1=head;
        while (p1!=NULL)
        {
            cout<<"Employee No:"<<p1 ->Emp_No<<endl;
            p1=p1->next;
        }
    }
}
int main()
{
    Link_List emp1;
    emp1.create();
    emp1.display();
    return 0;
}

Following is the output it just allows me to enter the value once then wothout asking for next it ends and also display function is also not executed here:

PS E:\Programming\C  > cd "e:\Programming\C  \" ; if ($?) { g   Linked_List.cpp -o Linked_List } ; if ($?) { .\Linked_List }
Enter Employee No.: 101
PS E:\Programming\C  >

CodePudding user response:

You got a typo in Link_List constructor. it should be:

head=NULL;

not

head==NULL;

It seems to work after replacement.

TIP: Although statically scanning code with your eyes makes you think better; A debugger is an essential tool you need to adopt.

CodePudding user response:

It should be head = NULL while defining Link_list constructor as per your code.

  • Related