Home > Mobile >  Last node is not printed in Linked List
Last node is not printed in Linked List

Time:06-17

I was trying to learn the Linked list and perform insertion operations from beginning of the list. while printing the nodes, the first node is not printed. Here is the core functions which I have written. Can someone help me?

struct Node //basic structure for a node
{
    ll data; //data which we want to store
    Node* link; //address of the next node;
};

Node* head=NULL;

void Insert(ll x) //insertion at beginning
{
    Node* temp=new Node();
    temp->data=x;
    temp->link=head; //we are linking new node with previously connected node
    head=temp;
}

void Print()
{
    Node* temp=head;
    while(temp->link!=NULL) //traversing the list until last element(last element.link = NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->link;
    }
    cout<<endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    f(i,0,5)
    {
        ll x;cin>>x;
        Insert(x);
    }
    Print();
    return 0;
}

CodePudding user response:

Your Print function requires that the last node is linked or it won't be printed. Since the last node is never linked, it will never be printed.

void Print()
{
    Node* temp = head;

    while(temp)     // <- corrected condition
    {
        std::cout << temp->data << ' ';
        temp = temp->link;
    }
    std::cout << '\n';
}

CodePudding user response:

It's because of your check in the while. The node will have link set as NULL, and therefore it will exit the while without printing it. My recommendation is changing the while check to (temp != NULL), but you can also fix it by putting a cout << temp->data; after the loop

CodePudding user response:

In general the function Print can invoke undefined behavior when it is called for an empty list due to the expression temp->link that uses a null pointer to access memory.

Another side effect is that the last node will be skipped due to the condition in the while loop (if the list has only one node then its value will not be outputted)

while(temp->link!=NULL)

The function can be declared and defined the following way

std::ostream & Print( std::ostream &os = std::cout )
{
    for ( const Node *current = head; current != nullptr; current = current->next )
    {
        os << current->data << " -> ";
    }

    return os << "null";
}

And in main the function can be called like

Print() << '\n';

The function is flexible. You can use it to write data in a file providing a corresponding file stream.

The function Insert can be simplified the following way

void Insert( ll x ) //insertion at beginning
{
    head = new Node { x, head };
}

Pay attention to that it is a bad idea to declare the pointer head in the global namespace. In this case all functions depend on the global variable and you can not for example to use two lists in your program.

So you should declare the pointer in main.

int main()
{
    Node *head = nullptr;
    //...

In this case for example the function Insert can look the following way

void Insert( Node * &head, ll x ) //insertion at beginning
{
    head = new Node { x, head };
}

and called in main like

Insert( head, x );
  • Related