Pointer based lists ????????

Again its me being new to c++. I get segmentation errors and i just plane dont understand pointer lists yet.

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*******************************************
 * Project:    GrocceyList
 * File:       GrocieItem.h
 * Author:     Jeremiah Johnson
 * Assginment: CH3 PP2
 * Date:       September 27, 2011, 10:23 PM
 *******************************************/

#ifndef GROCIEITEM_H
#define	GROCIEITEM_H

#include <string>

using namespace std;

/**
 * Grocie Item header.
 */
class GI {
public:

    /**
     * Constroctor for Grocie Item.
     */
    GI();

    /**
     * Deconstroctor for Grocei Item.
     */
    ~GI();

    /**
     * Sets the name of a Groce Item.
     * @param thing, name of the thing to be added.
     */
    void setName(string thing);

    /**
     * Retruns the name of the Grocie Item.
     * @return string, name of the item
     */
    string getName();

private:

    string name;

};

#endif
/* GROCIEITEM_H */

/*******************************************
 * Project:    GrocceyList
 * File:       GrocieItem.cpp
 * Author:     Jeremiah Johnson
 * Assginment: CH3 PP2
 * Date:       September 27, 2011, 10:23 PM
 *******************************************/

#include "GrocieItem.h"
#include <string>
// Variables //
string name;

// Methods //
/**
 * Constroctor for Grocie Item.
 */
GI::GI() {
    name = "Unamed";
}

/**
 * Deconstroctor for Grocei Item.
 */
GI::~GI() {
    delete this;
}

/**
 * Sets the name of a Groce Item.
 * @param thing, name of the thing to be added.
 */
void GI::setName(string thing) {
    name = thing;
}

/**
 * Retruns the name of the Grocie Item.
 * @return string, name of the item
 */
string GI::getName() {
    return name;
}

/*******************************************
 * Project:    GrocceyList
 * File:       GroceiNode.h
 * Author:     Jeremiah Johnson
 * Assginment: CH3 PP2
 * Date:       September 27, 2011, 10:22 PM
 *******************************************/

#ifndef GROCEINODE_H
#define	GROCEINODE_H

#include <string>

using namespace std;

/**
 * Grocie Node header.
 */
class GN {
public:

    /**
     * Construcrot for a grocie node
     */
    GN();

    /**
     * Deconstroctor for a grocie node
     */
    ~GN();

    /**
     * Adds item thing as a grocei item.
     * @param thing
     */
    void GI(string thing);

    GN *next;

};

#endif	/* GROCEINODE_H */

/*******************************************
 * Project:    GrocceyList
 * File:       GrocieNode.cpp
 * Author:     Jeremiah Johnson
 * Assginment: CH3 PP2
 * Date:       September 27, 2011, 10:22 PM
 *******************************************/

#include "GroceiNode.h"
#include <string>

/**
 * Construcrot for a grocie node
 */
GN() {

}

/**
 * Deconstroctor for a grocie node
 */
~GN() {

}

/**
 * Adds item thing as a grocei item.
 * @param thing
 */
void GI(string thing) {

}

/*******************************************
 * Project:    GrocceyList
 * File:       GrocieList.h
 * Author:     Jeremiah Johnson
 * Assginment: CH3 PP2
 * Date:       September 27, 2011, 10:22 PM
 *******************************************/

#ifndef GROCIELIST_H
#define	GROCIELIST_H

#include <string>

using namespace std;

/**
 * Grocie List header
 */
class GL {
public:

    /**
     * Constructor for the GrocieList or GL class.
     */
    GL();

    /**
     * Deconstructor for the GrocieList or GL class.
     */
    ~GL();

    /**
     * Inserts a new item into the grocie list.
     * @param thing, being inserted.
     * @return bool, weather or not thing was inserted.
     */
    bool insert(int spot);

    /**
     * Removes thing from the grocie list.
     * @param thing, being removed.
     * @return bool, weather or not thing was removed.
     */
    bool remove(string thing);

    /**
     * Retrives thing at spot pos from grocie list.
     * @param pos, spot thing is being retrived from.
     * @return bool, weather or not thing was retrived.
     */
    bool retrive(int pos);

    /**
     * Returns the lenght of the grocie list.
     * @return int, representing the lenght of the grocie list.
     */
    int length();

    /**
     * Returns weather or not the grocie list is empty.
     * @return bool, weather or not the grocie list is empty.
     */
    bool isEmpty();

private:

    GN *head;

};

#endif
/* GROCIELIST_H */

/*******************************************
 * Project:    GrocceyList
 * File:       GrocieList.cpp
 * Author:     Jeremiah Johnson
 * Assginment: CH3 PP2
 * Date:       September 27, 2011, 10:22 PM
 *******************************************/

#include "GrocieList.h"

// Variables //
int &head;

// Methods //
/**
 * Constructor for the GrocieList or GL class.
 */
GL::GL() {
    
}

/**
 * Deconstructor for the GrocieList or GL class.
 */
GL::~GL() {

}

/**
 * Inserts a new item into the grocie list.
 * @param thing, being inserted.
 * @return bool, weather or not thing was inserted.
 */
GL::insert(string thing) {

}

/**
 * Returns weather or not the grocie list is empty.
 * @return bool, weather or not the grocie list is empty.
 */
GL::isEmpty() {

}

/**
 * Returns the lenght of the grocie list.
 * @return int, representing the lenght of the grocie list.
 */
GL::length() {

}

/**
 * Removes thing from the grocie list.
 * @param thing, being removed.
 * @return bool, weather or not thing was removed.
 */
GL::remove(string thing) {

}

/**
 * Retrives thing at spot pos from grocie list.
 * @param pos, spot thing is being retrived from.
 * @return bool, weather or not thing was retrived.
 */
GL::retrive(int pos) {

}

/*******************************************
 * Project:    GrocceyList
 * File:       main.cpp
 * Author:     Jeremiah Johnson
 * Assginment: CH3 PP2
 * Date:       September 27, 2011, 10:23 PM
 *******************************************/

#include <cstdlib>
#include <string>
#include <iostream>
#include "GroceiNode.h"
#include "GrocieItem.h"
#include "GrocieList.h"

using namespace std;

/**
 * Program allows the user to create a grocie list with a text based interface.  The list can be
 * any length.
 */
int main() {

    string name;
    GI item;

    cin >> name;
    cout << endl;

    item.setName(name);


    cout << item.getName();

    return 0;
}

output:

g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/GroceiNode.o.d -o build/Debug/GNU-Linux-x86/GroceiNode.o GroceiNode.cpp
GroceiNode.cpp:15: error: expected unqualified-id before ‘)’ token
GroceiNode.cpp:22: error: declaration of ‘~GN’ as non-member
make[2]: *** [build/Debug/GNU-Linux-x86/GroceiNode.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `/home/bullfrog/NetBeansProjects/CnCpp/GrocceyList'
make[1]: Leaving directory `/home/bullfrog/NetBeansProjects/CnCpp/GrocceyList'

BUILD FAILED (exit value 2, total time: 211ms)

closed account (zb0S216C)
LiverEatnJohnson wrote:
delete this; (sic)

This is dangerous; handle this with care. Deleting an object that was never dynamically allocated can cause segmentation errors. Only an object that called new can use delete.

Which part of linked-lists don't you understand?

Wazzak
Last edited on
Those are compiler errors
GN::GN(){ like you did with GI and GL (by the way, use descriptive names)

1
2
3
GI::~GI() {
    delete this;
}
It's dangerous to perform suicide, you could die. (stack overflow)

Really, there is a conceptual error there.
The destructor should be used to release resources. By instance when a fstream dies, it commits pending changes and it closes the file
Last edited on
Ok i think in the end the GI will be dynamicly declared cause i dont want the list to have a limit Thanks for the help that is probly why i am getting segmentation errors now. I will try dynamicly allocating it.
What i dont understand is how to program the node and list to contain items. like what to put in the methods
I will give what I would have in the two classes:

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
class LinkListNode
{
public:
      // my data I would want to keep..
      int someNumber;
      string someString;

     // I am assuming I have a singly linked list.
     LinkedListNode* nextNode;
     LinkedListNode()
     {
           someNumber = 0;
           nextNode = NULL;
     }
};

class LinkedListContainer
{
private:
         LinkedListNode *headofList;
         int numberOfNodes;
public:
         LinkedListContainer()
         {
               headofList = NULL;
               numberOfNode = 0;
         }
         ~LinkedListContainer()
         {
                // clean up the list on destruction.
                LinkListNode* currentNode = headofList ;
                LinkedListNode* deleteNode = headofList;
          
                while(currentNode != NULL)
                {
                          deleteNode = currentNode;
                          currentNode = currentNode->nextNode;
                          delete deleteNode;
                }
          }

         // for appending to the end of list
         LinkListNode* appendToList(LinkListNode* newNode);

         // for inserting a node to a list, a node and a location as an integer
         LinkListNode* InsertAt(LinkListNode* newNode, int Index);

         // searching the list
         LinkListNode* Search(string searchString);
         LinkListNode* Search(int searchNumber);

         // counting the list;
         int CountNodes();

         // deleting all
         void DeleteAll();
         // deleting at some count down the list
         void DeleteAt(int deleteat);
         // delete Node // this one needs to search the list for the node to fix links
         void DeleteNode(LinkedListNode* deleteNode);

         // returning one at some count down the list.
         LinkedListNode* GetAt(int numberCount); 
};


I hope this is some food for thought

Thanks to everyone who helped me it works now if anyone wants the source code let me know.
Topic archived. No new replies allowed.