1

The following code for creating a linked list runs an infinite loop after entering the no. of numbers.

I thought a lot as to what the mistake might be but could not find a solution. Its maybe a small error that is going unnoticed. Please help!

CODE:

#include<iostream.h>
#include<conio.h>

struct Node {
    int data;
    Node* link;
};

Node* head;

void Insert(int x);
void Print();

void main()
{ 
    clrscr();
    head=NULL;
    cout<<"How many numbers";
    int n,x;
    cin>>n;

    for(int i=0; i<n; i++)
    {
        cout<<"enter no";
        cin>>x;
        Insert(x);
        Print();  
    }

}

void Insert (int x)
{
    Node* temp;
    temp= new Node();
    temp->data=x;
    temp->link=head;
    head=temp;
}

void Print()
{
    Node* temp=head;
    while(temp!=NULL)
    {
        cout<<"List is:"<<temp->data;
        temp=temp->link;
    }
    cout<<"\n";
}
11
  • 6
    This is C++ from 1980. No one uses this version of the language today, and you should stop usung it and get yourself a modern compiler and locate some learning material that is not from the past millennium. Commented Dec 21, 2015 at 16:59
  • 2
    @bigOTHER: The major culprits are void main(), and iostream.h. Commented Dec 21, 2015 at 17:04
  • 1
    After modernising your code (using the correct headers, not using void main, removing clrscr), so that it compiles with yesterdays clang++, I can't make it go wrong. Exactly what do you do to make it go wrong? Commented Dec 21, 2015 at 17:05
  • 2
    In earlier versions of C++, you needed to write the functions before the main, or declare them at the beginning of your code. Commented Dec 21, 2015 at 17:08
  • 1
    I'm sorry if your school makes you use an obsolete compiler. It's not OK, but I guess you can't do much about it. In any case the answer by Scott is useful, read it. Commented Dec 21, 2015 at 17:19

1 Answer 1

4

It is not a right implementation of linked list. Every node should include a pointer to next one,not always the head. And the last node's pointer should be set to NULL. Insert function should contains two parameters, one for the data, and one for the linked list to pass in. When you traverse through the linked list, you should constantly check whether or not it is the end by checking whether the next pointer is NULL.

There are multiple way to implement a linked list, the more efficient way is to construct two struct. One for node, which include data and a pointer to the next, and one for linked list, which contains a pointer to the first and the last node. Every time you do a insertion, you insert to the end, and change the last pointer in linked list struct. Every time you start traverse, you start with first pointer.

By the way, you should really use the features provided by C++, like class and template to implement a linked list. C++ has even provides a good implementation of those sequential containers like linked list, dynamic array in standard template library. You should really try those new stuff, and use template and class to organize your code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.