Reading from a linked list

closed account (jwC5fSEw)
Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Basic linked list
#include <iostream>
using namespace 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.
closed account (S6k9GNh0)
Don't mind me... Need to stop posting while I'm 1/4 awake and hungry....
Last edited on
closed account (jwC5fSEw)
And I know know if displaying a pointer will always go the way you want. Use a dynamic cast to cast it.


I'm not sure what this means.
closed account (jwC5fSEw)
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Basic linked list
#include <iostream>
using namespace std;

struct node {
    int num;
    node* ptr;
};

node* startNode;

void makeList(node* test, int n){
    int count = 1;
    test = new node;

    while (count < n){
        test->num = count;
        if (count == 1)
            startNode = test;
        test->ptr = new node;
        test = test->ptr;
        count++;
    }
    test->num = count;
    test->ptr = 0;
}

void printList(node* test){
    test = startNode;
    while (test->ptr != 0){
       cout << test->num << endl << (long)test->ptr << endl;
       test = test->ptr;
    }
    cout << test-> num << endl << (long)test->ptr << endl;
}

int main(){
    node* test;
    makeList(test, 10);
    printList(test);
    return 0;
}
Last edited on
Topic archived. No new replies allowed.