simple linked list - class node

Here is the problem I am trying to figure out, and please let me know if I am understanding the question correctly:

Create a simple linked list to create a class list containing

1
2
3
4
5
6
7
8
9
10
11
12
13
class node {

void *info;
node *next;

public:

node (void *v) {info = v; next = 0; }
void put_next (node *n) {next = n;}
node *get_next ( ) {return next;}
void *get_info ( ) {return info;}

};


Initially fill the list and provide functions to insert/append nodes and remove nodes. Also display the contents of the list.

Write a driver program with 5 values passed (so that 5 nodes are created) as you insert/append, delete and display data to show the programs operation.

Here is my code, I believe I am understanding the questions (hopefully), if not and someone can reword it to make more sense that would be great.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
using namespace std;

// Provided node class
class node {
    void *info;
    node *next;
public:
    node (void *v) {info = v; next = 0; }
    void put_next (node *n) {next = n;}
    node *get_next ( ) {return next;}
    void *get_info ( ) {return info;}
};

// List class template
template <class T>
class list {
    node *start;
public:
    list (T v) {
        start = new node (&v);
    }

    // Insert method
    void insert (T *value, int place=-1) {
        node *temp = new node (value);

        // if going to the beginning, change the reference to start
        if (place == 0) {
            temp->put_next(start);
            start = temp;
        }

        // if not the beginning, determine where to put it
        else {
            node *before = start;

            // Loop to find the preceeding node
            for (int i = 1; before->get_next() != 0; i++) {
                if (i == place) { 
                    break;
                }
                before = before->get_next();
            }

            // Insert after preceeding node and point to the next node
            temp->put_next(before->get_next());
            before->put_next(temp);
        }
    }

    // Remove function
    void remove(int place) {

        // when removing from the beginning, change start pointer
        if (place == 0) {
            start = start->get_next();
        }

        // Find node to remove
        else {
            node *curr = start;
            for (int i = 1; curr != 0; i ++) {
                if (i == place) {
                    // remove the target node 
                    curr->put_next(curr->get_next()->get_next());
                    break;
                }
                curr = curr->get_next();
            }
        }
    }

    // Print nodes
    void display() {
        for (node *current = start; current != 0; current = current->get_next()) {
            cout << *(static_cast<T*>(current->get_info())) << endl;
        }
        cout << endl;
    }
};

int main() {
    int nine = 9;
    int eight = 8;
    int seven = 7;
    int six = 6;
    int five = 5;
    
    cout << "Create list holding '10'" << endl;
    list<int> *hold_list = new list<int>(10);
    
    cout << endl << "Placing " << nine << " at start of list." << endl;
    hold_list->insert(&nine,0);

    cout << endl << "Inserting " << eight << " at 2nd place." << endl;
    hold_list->insert(&eight,1);

    cout << endl << "Appending " << seven << " to the list." << endl;
    hold_list->insert(&seven);

    cout << endl << "Placing " << six << " at start of list." << endl;
    hold_list->insert(&six,0);

    cout << endl << "Inserting " << five << " at 3rd place." << endl;
    hold_list->insert(&five,2);

    cout << endl << "Show completed list:" << endl;
    hold_list->display();

    cout << "Removing the first element:" << endl;
    hold_list->remove(0);
    hold_list->display();

    cout << "Removing the last element:" << endl;   
    hold_list->remove(4); 
    hold_list->display();

    cout << "Removing the second element:" << endl;
    hold_list->remove(1);
    hold_list->display();
}



When I compile and run my code the output shows the following:
(I'm using Dev-C++ 5.11)


Create list holding '10'

Placing 9 at start of list.

Inserting 8 at 2nd place.

Appending 7 to the list.

Placing 6 at start of list.

Inserting 5 at 3rd place.

Show completed list:
6
9
5
8
7339556 ---- Where did this come from?
7

Removing the first node:
9
5
8
0 ---- Where did this come from?
7

Removing the last node:
9
5
8
4

Removing the second node:
9
8
1


This seemed to be due to my program I was using to compile the code as far as I can tell.
Last edited on
Topic archived. No new replies allowed.