// Basic linked list
#include <iostream>
usingnamespace std;
struct node {
int num;
node* ptr;
};
void makeList(node* test, int n){
int count = 1;
node* temp = new node;
while (count <= n){
temp->num = count;
temp->ptr = new node;
// cout << temp->num << endl << (long)temp->ptr << endl;
temp = temp->ptr;
if(count == n){
temp = new node;
temp->ptr = 0;
}
count++;
}
}
void printList(node* test){
node* temp = test;
do {
cout << temp->num << endl << (long)temp->ptr << endl;
temp = temp->ptr;
} while (temp->ptr != 0);
}
int main(){
node* test;
makeList(test, 5);
printList(test);
return 0;
}
The purpose of printList is to go through the list and print the value in num. The commented out line in makeList does what I want printList to do, but I need to have it in a separate function. Right now, the program compiles fine, but when I run it, it displays 0, newline, -1 before crashing and returning -1073741819. I can't understand what I'm doing wrong in printList.
Okay, looking the code, I realized that nowhere in makeList do I use test, the node* argument that the function accepts. I'm guessing this might have something do with the problem. I'm trying to put in these lines in various parts of the function:
1 2
test->num = temp->num;
test->ptr = temp->ptr;
Also, I put count++ under an else statement. Now, the console just doesn't display anything at all.
EDIT: FINALLY got it working properly! Here's the code: