1

I am new to C++. I have written a C++ code for linked list. I get a error message after running the code in eclipse saying "linkedlist.exe stopped working". Can anyone tell me where i am going wrong. In the code i create a linked list and insert few values in it. I then wrote a statement to print the elements.

#include<iostream> #include<cstdlib> using namespace std; struct Node { int data; Node* P; }; Node* H; void Insert(int data) { Node* temp=new Node(); temp->data=data; temp->P=NULL; Node* temp1=H; while(temp1->P!=NULL) { temp1=temp1->P; } temp1->P=temp; } int main() { cout<<"linked list"<<endl; Insert(1); Insert(2); Insert(3); Node* Print=H; while(Print!=NULL) { cout<<Print->data<<endl; } } 
6
  • 1
    H is uninitialized. It doesn't have a P member you can use. Commented Jun 7, 2014 at 3:53
  • Have you tried the debugger? Why not try compiling with all the warnings on Commented Jun 7, 2014 at 3:53
  • thanks. I added H=temp before Node* temp1=H; in Insert function, and H=NULL initially in main. The error disappeared. the values won't print though :( Commented Jun 7, 2014 at 4:06
  • 1
    Don't keep changing your code and saying "it's still not working". Simplify. Try constructing one node with a value, then printing the value of that node. Once you have that working perfectly, construct a second node and link it using the simplest code you can write for the task -- not a general Insert function -- and printing both values. Once you have that working perfectly, try writing the Insert function. Once you have that working perfectly... you get the idea. Commented Jun 7, 2014 at 4:25
  • 1
    I am new to C++. I have written a C++ code for linked list. If you're new to C++, why are you attempting to write a class that can only be written correctly by intermediate/advanced C++ programmers? Commented Jun 7, 2014 at 5:59

3 Answers 3

1
// Initialize H. Node* H = NULL; void Insert(int data) { Node* temp=new Node(); temp->data=data; temp->P=NULL; // If there is nothing in the list, make the new Node the head. if ( H == NULL ) { H = temp; } else { Node* temp1=H; while(temp1->P!=NULL) { temp1=temp1->P; } temp1->P=temp; } } 

Update

The while lop for printing the list needs to be:

while(Print!=NULL) { cout<<Print->data<<endl; Print = Print->P; // Missing in your code. } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. The error disappeared. I did this but i still cannot get the values to print on screen... :(
0

Firstly, give better names to variables. For example, Node* P doesn't make sense, you can name it as next, prev or whatever you want making you and other people understand. You can also take a look at naming conventions

Secondly, I don't think insert works. Are you trying to insert to first, or end or a position. I would write something like that:

void insert(int val, int pos){ Node newNode = null; newNode -> data = val; Node temp = H; if(pos<0){ H = newNode; H->P = temp; } else{ for(int i = 0; i < pos; i++){ if(temp->P != null) temp = temp->P else break; } //I found the position of the element you supposed to insert //You insert the element properly. //Make sure that you link the next element to newNode; } } 

You can learn insertion from this page

Finally, your print out always check the first element. If Print->data != null, you would always print the first element of the list.

Comments

0

Thanks guys for the help. I wrote the following and it worked just fine. This is a linked list code to insert data value at the end of list.

#include<iostream> #include<cstdlib> using namespace std; struct Node { int data; Node* next; }; Node* Head; void Insert(int data) { Node* temp=new Node(); temp->data=data; temp->next=NULL; if(Head==NULL) { Head=temp; return; } Node* temp1=Head; while(temp1->next!=NULL) { temp1=temp1->next; } temp1->next=temp; } int main() { Head=NULL; cout<<"linked list"<<endl; Insert(1); Insert(2); Insert(3); Node* Print=Head; while(Print!=NULL) { cout<<"data=" <<Print->data<<endl; Print=Print->next; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.