List Issue

Hello everyone.

this is my first forum post so i will try and follow the rules and make this as clear as i can.

I am working on a list for school, and i have run into trouble with a couple of the functions. two of the functions are called InsertBefore(int data) and InsertAfter(int data). The insert before function wants us to push all elements to the right and input a variable in the now empty spot. the insert after function wants us to input a variable after the current element and make the new element the current element.

i am also having trouble with the concatenation of 2 lists. For some reason my brain is not figuring out what is being asked and is attempting to explode on me.

Here is the code i have so far. Our instructor wrote all the function names and part of the main function that i will be posting, so all we have to do is fill in the space between the constructors and the methods

If someone would be able to look at the insertbefore and insertafter functions for me and point in me in the right direction as to how i am supposed to code this i would be very appreciated.

I am also open to criticisim, so if you see anything else wrong with the code i post, please feel free to let me know about it.

Alright here is the code. the 3 areas i am having trouble with are the 3 functions that are blank (lines 191, 205, and 243).
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
#include <iostream>
using namespace std;





const int MAX_SIZE = 30;



class List

{

	private:

	int end;

	int curr;

	int list[MAX_SIZE];



	public:

	// constructor

	// remember that an empty list has a "size" of -1 and its "position" is at -1

	List()

	{

        end = curr = -1;


	}

	// copy constructor

	// clones the list l and sets the last element as the current

	List(const List& l)

	{
        for(int i = 0; i <= l.end; i++)
                list[i] = l.list[i];

                


	}


	// copy constructor

	// clones the list l and sets the last element as the current

	void operator=(const List& l)

	{
        for(int i = 0; i <= l.end; i++)
                list[i] = l.list[i];

                


	}


	// navigates to the beginning of the list

	// this should not be possible for an empty list

	void First()

	{
        if (!IsEmpty())
           curr = 0;


	}


	// navigates to the end of the list

	// the end of the list does not necessarily correspond to its maximum size; it's just at the last existing element

	void Last()

	{
		if (!IsEmpty())
		   curr = end;

	}


	// navigates to the specified element (0-index)

	// this should not be possible for an empty list

	// this should not be possible for invalid positions

	void SetPos(int pos)

	{
		if (!IsEmpty())
		   pos = curr;

	}



	// navigates to the previous element

	// this should not be possible for an empty list

	// there should be no wrap-around

	void Prev()

	{
        if (!IsEmpty())
        {
           if (curr > 0)
           curr--;
        }


	}


	// navigates to the next element

	// this should not be possible for an empty list

	// there should be no wrap-around

	void Next()

	{
        if (!IsEmpty())
        {
           if (curr <= 29)
           curr++;
        }


	}


	// returns the location of the current element (or -1)

	int GetPos()

	{
		return curr;

	}


	// returns the value of the current element (or -1)

	int GetValue()

	{
		return list[curr];

	}


	// returns the size of the list

	// size does not imply capacity

	int GetSize()

	{
        return end;
	}


	// inserts an item before the current element

	// the new element becomes the current

	// this should not be possible for a full list

	void InsertBefore(int data)

	{


	}


	// inserts an item after the current element

	// the new element becomes the current

	// this should not be possible for a full list

	void InsertAfter(int data)

	{
	    


	}

	// returns if the list is empty

	bool IsEmpty()

	{
        return(end == -1);


	}


	// returns if the list is full

	bool IsFull()

	{
        return(end == 29);


	}


	// returns the concatenation of two lists

	// l should not be modified

	// l should be concatenated to the end of *this

	// the returned list should not exceed MAX_SIZE elements

	List operator+(const List& l) const

	{
		

	}


	// returns if two lists are equal (by value)

	bool operator==(const List& l) const

	{
        return (*this == l);


	}


	// returns if two lists are not equal (by value)

	bool operator!=(const List& l) const

	{
        return !(*this == l);


	}


	// returns a string representation of the entire list (e.g., 1 2 3 4 5)

	// the string "NULL" should be returned for an empty list

	friend ostream& operator<<(ostream& out, const List &l)

	{
                  for (int i = 0; i < l.curr; i++)
		  {
                  out << l.list[i] << " ";
                  return out;
		  }

		  if(l.curr == -1)
		  {
		  cout << "NULL";
		  }






	}
};

int main()

{

	List a, b;


	cout << a.GetValue() << endl;

	cout << "a(" << a.GetSize() << "/" << a.GetPos() << ") = " << a << endl;

	cout << "b(" << b.GetSize() << "/" << b.GetPos() << ") = " << b << endl;


	for (int i=1; i<=25; i++)

	{

		a.InsertAfter(i);

		b.InsertBefore(i);

	}


	cout << "a(" << a.GetSize() << "/" << a.GetPos() << ") = " << a << endl;

	cout << "b(" << b.GetSize() << "/" << b.GetPos() << ") = " << b << endl;

}
Last edited on
Hi
I am working on a linked list for school

Erm, no you're not. You're working on an array that's pretending to behave like a linked list. Except it'll be much slower (because it's an array and not a linked list).
See: http://en.wikipedia.org/wiki/Linked_list

Did your instructor ask you to use an array? If so then fine (s'pose ;), but if not I think you're on the wrong lines entirely.
The functions you're stuck on wouldn't normally be written that way - you'd have a pointer to a node in the linked list, not a data value.
thanks for the reply.

i don't know why i speccified linked list, my brain was still waking up at that point. But yes my instructor asked us to use an array to hold values for the list.

Also my instructor wrote those functions i am stuck on. i am just trying to fill in the blanks after them.

I do thank you for the reply.
Topic archived. No new replies allowed.