linked lists

hi
i'm trying to build a linked list but i've run in to a problem...
for some reason i can't print it out?
it compiles just fine with no errors but when i run it nothing prints out..

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
41
42
#include <iostream>

using namespace std;

//node structure
struct node
{
    int data;
    node *next;
};

int main()
{
    //initialising node pointers (objects?)
    node *n = new node;
    node *tail = n;
    node *head = n;

    //giving node values
    for(int i = 0; i < 10; i++)
    {
        n->data = i;

        n = new node;

        tail->next = n;
    }
    tail->next = NULL;

    //printing node values
    n = head;
    while(n->next != NULL)
    {
        cout << n->data << " --> ";
        n = n->next;
    }



    cin.get();
    return 0;
}


i've also got two other (small) questions:

1)
i just recently learned about the stack and the heap and that every time you create something on the heap you should delete it when you're done...but how do i do that with a linked list?

2)
as you can see on line 14 it says "initialising nodes (objects?)"
that's because im not sure if you call it an object or something else when its from a structure?

thanks :)
Last edited on
After lines 16 and 17 tail and head are the same memory location as n
Which means head and tail are the same. So unless one of them is changes this should hold true. On line 26 tail->next is basically head->next, same on line 28. So when your loop starts the condition is already false because line 28 sets head->next to NULL.

1) the same type of loop that you are using to print. basically just fetch the "->next" pointer first, then delete the node, repeat on the pointer that was fetched before deleting until NULL is the value of the fetched pointer.

2) structs and classes are basically the same thing, so calling them objects is okay.
Last edited on
This seems to be a simple example of a linked list using a class description.

http://stackoverflow.com/questions/22141477/simple-linked-list-c

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;

class LinkedList{
    // Struct inside the class LinkedList
    // This is one node which is not needed by the caller. It is just
    // for internal work.
    struct Node {
        int x;
        Node *next;
    };

// public member
public:
    // constructor
    LinkedList(){
        head = NULL; // set head to NULL
    }

    // This prepends a new value at the beginning of the list
    void addValue(int val){
        Node *n = new Node();   // create new Node
        n->x = val;             // set value
        n->next = head;         // make the node point to the next node.
                                //  If the list is empty, this is NULL, so the end of the list --> OK
        head = n;               // last but not least, make the head point at the new node.
    }

    // returns the first element in the list and deletes the Node.
    // caution, no error-checking here!
    int popValue(){
        Node *n = head;
        int ret = n->x;

        head = head->next;
        delete n;
        return ret;
    }

// private member
private:
    Node *head; // this is the private member variable. It is just a pointer to the first Node
};

int main() {
    LinkedList list;

    list.addValue(5);
    list.addValue(10);
    list.addValue(20);

    cout << list.popValue() << endl;
    cout << list.popValue() << endl;
    cout << list.popValue() << endl;
    // because there is no error checking in popValue(), the following
    // is undefined behavior. Probably the program will crash, because
    // there are no more values in the list.
    // cout << list.popValue() << endl;
    return 0;
}

Topic archived. No new replies allowed.