My extremely basic program to help me understand linked lists is driving me up the wall. It accepts the input perfectly, but when I try to step through the list to output the list to the screen, the WHILE loop goes bonkers.
#include <iostream>
usingnamespace std;
struct newNode{
int info;
newNode* next;
};
int main()
{
newNode* head;
newNode* temp;
newNode* nextNode;
newNode* current;
int number;
int size=0;
while (true){
cout<<"Enter a number (negative number to end): ";
cin>>number;
if (number<0){
break;
}
else{
nextNode = new newNode;
nextNode->info = number;
if (size==0){
head = nextNode;
temp = nextNode;
}
temp->next = nextNode;
temp = nextNode;
//check that input goes where it's meant to
cout <<nextNode->info <<endl;
cout <<temp->info <<endl;
cout <<head->info <<endl;
}
size++;
}
//Just so that I can find where the output is suppose to start
cout <<"Input ended" <<endl <<endl;
current = head;
while (current != NULL) {
cout<< current->info <<endl;
current = current->next;
}
return 0;
}
and I've even had the output section as below before adding another pointer as per above: