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; } }
His uninitialized. It doesn't have aPmember you can use.Insertfunction -- and printing both values. Once you have that working perfectly, try writing theInsertfunction. Once you have that working perfectly... you get the idea.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?