Linked List/Stack/Queue Template problems

I've designed a linked list for an assignment and it works by it self fine, but when I inherited from it in a stack and queue template, it stopped working. Can someone help me?

[EDIT]: Oops, didn't see that how to ask questions. Okay, here we go.
I get something that calls itself a "class redefinition error," at least on the linked list. I really don't think this is the issue, as I'm not "redefining" the class anywhere, just using it in two differnet places.
Also, I'm getting errors that say my definitions are different than my declarations.
As for where I put the error catches, it doesn't seem to matter. As I said, this linked list code works on its own, throws and all.

Here's the code:
listNode.h
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
#ifndef LIST_NODE_H
#define LIST_NODE_H

template <typename T> 
class LinkedList;

template <typename T>
class listNode
{

friend class LinkedList<T>;

private:

	T value;
	listNode* next;

public:

	listNode(T nv);
	T getData();

};

template <typename T>
listNode<T>::listNode(T nv)
{
	value = nv;
	next = NULL;
}

template <typename T>
T listNode::getData()
{
	return value;
}

#endif 


LinkedList.h
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
//List Exception
#include <stdexcept>
#include <string>

class except : public std::logic_error
{
public:

	except(const std::string & message = " ")
		: std::logic_error(message.c_str()){};

};


//Out of Range Exception
//#include <stdexcept>
//#include <string>

class outOfRange : public std::out_of_range
{
public:
	outOfRange(const std::string & message = " ")
		: std::out_of_range(message.c_str())
	{};
};

//classes except and outOfRange were declared and defined here as they will be used by the LinkedList class
//and all classes that inherit from it

#include <sstream>

#include "listNode.h"

template <class T> 
class LinkedList
	//class LinkedList
{
private:
	
	listNode<T>* head;
	int size; //holds number of items in the list
	//Something that was suggested in the book that I thought was a great idea

	listNode<T>* find(int i);

public:

	//default constructor and destructor
	LinkedList() 
	{
		//set head to NULL and size to zero
		head = NULL;
		size = 0;
	};	
	~LinkedList();

	bool isEmpty();

	void linked_list_append(T nv);
	void linked_list_clear();
	int linked_list_count();
	void linked_list_insert_at(int i, T nv)
		throw (outOfRange, except);
	T linked_list_print(int i)
		throw (outOfRange);
	std::string linked_list_print_all();
	void linked_list_remove();
	void linked_list_remove_at(int i)
		throw (except);	
};

template <class T>
listNode<T>* LinkedList<T>::find(int i)
{
	//if i is not in range, return NULL
	if ((i < 1) || (i > linked_list_count()))
	{
		return NULL;
	}

	else
	{
		//make sure pntr is equal to head
		listNode<T> *pntr = head;
		
		//traverse the list
		for (int n = 1; n < i; n++)
		{
			pntr = pntr->next;
		}

		//return postion
		return pntr;
	}	
};

template <class T>
LinkedList<T>::~LinkedList()
{
	//deconstuct linked list with "clear" command
	linked_list_clear();
};

template <class T>
bool LinkedList<T>::isEmpty()
{
	if (size == 0)
	{
		//list is empty
		return true;
	}

	else
	{
		//list is not empty
		return false;
	}
};

template <class T>
void LinkedList<T>::linked_list_append(T nv)
{
	//used "insert at" at the end of the range
	linked_list_insert_at(linked_list_count()+1,nv);
};

template <class T>
void LinkedList<T>::linked_list_clear()
{
	listNode<T> *pntr = head;  // set the pointer derived from charlistNode<T> to the head
	
	while (pntr != NULL)
	{
		listNode<T> *trash = pntr; // node to be deleted must be tracked
		pntr = pntr->next; // This should move on to the next node, if there is any
		delete trash; // Delete the "trash" node	
	}
}

template <class T>
int LinkedList<T>::linked_list_count()
{
	//This is why I thought size was such a good idea!
	//rather than iterating though the whole list, I just return size,
	//which is carefully updated from insert and remove
	return size;	
}; 

template <class T>
void LinkedList<T>::linked_list_insert_at(int i, T nv)
	throw (outOfRange, except)
{
	//ensure i is in range
	if ( (i < 1) || (i > (linked_list_count()+1)))
	{
		throw outOfRange("outOfRange: Number does not exist in range");
	}

	else
	{
		try
		{
			//set the new pointer to list node, add new value, and increase size
			listNode<T> *newPntr = new listNode<T>;
			newPntr->value = nv;
			size++;
	
			if (i == 1)
			{
				//sets new pointer to head
				newPntr->next = head;
				head = newPntr;
			}

			else
			{
				//needs to find its place in the list
				listNode<T> *temp = find(i-1);

				//set old pntr value to new pointer
				newPntr->next = temp->next;
				temp->next = newPntr;
			}
		}	
	
		catch (bad_alloc e)
		{
			throw except("except: Cannot insert item");
		}
	}
}

template <class T>
T LinkedList<T>::linked_list_print(int i)
		throw (outOfRange)
{
	//check range
	if ( (i < 1) || (i > linked_list_count()))
	{
		throw outOfRange("outOfRange: Number does not exist in range");
	}

	else
	{
		//find the value, and return it
		listNode<T> *pntr = find(i);
		return pntr->value;
	}
};

template <class T>
std::string LinkedList<T>::linked_list_print_all()
{
	//string stream for the string
	std::stringstream out;
	for (int i = 1; i <= linked_list_count(); i++)
	{
		//put info in stringstream
		out << linked_list_print(i) << endl;
	}
	//return stringstream as a string
	return out.str();
};

template <class T>
void LinkedList<T>::linked_list_remove()
{
	//use remove at command at the end
	linked_list_remove_at(linked_list_count());	
};

template <class T>
void LinkedList<T>::linked_list_remove_at(int i)
	throw (except)
{
	//check for range
	if ( (i < 1) || (i > linked_list_count()))
	{
		throw outOfRange("outOfRange: Number does not exist in range");
	}

	else
	{
		listNode<T> *newPntr = new listNode<T>;
		size--;
		if (i == 1)
		{
			newPntr = head;
			head = head->next;
		}

		else
		{
			listNode<T> *temp = find(i-1);
			newPntr = temp->next;
			temp->next = newPntr->next;
		}

		newPntr->next = NULL;
		delete newPntr;
		newPntr = NULL;
	}	
};


StackLL.h
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
#pragma once
//Stack Exception

#include <stdexcept>
#include <string>

class exceptS : public std::logic_error
{
public:
	
	exceptS(const std::string & message = " ")
		: std::logic_error(message.c_str())
	{};

};

#include "LinkedList.h"

template<class T> class StackLL : protected LinkedList<T>
{
public:
	StackLL(void)
	{
		//set head to NULL and size to zero
		head = NULL;
		size = 0;	
	};
	~StackLL(void)
	{
		linked_list_clear();
	};

	bool isEmpty()
	{
		if (size == 0)
		{
			//list is empty
			return true;
		}

		else
		{
			//list is not empty
			return false;
		}
	};

	void push(T nv)
	{
		try
		{
			linked_list_insert_at(1, nv);
		}
		catch (except e)
		{
			throw exceptS("exceptS: Item was not accepted");
		}

		catch (outOfRange e)
		{
			throw exceptS("exceptS: Item was not accepted");
		}
	};

	void pop()
		throw (exceptS)
	{
		try
		{
			linked_list_remove();
		}
		catch (outOfRange e)
		{
			throw exceptS("exceptS: Stack is empty");
		}
	};

	T print_n_pop()
	{
		try
		{
			T out;
			out = linked_list_print(1);
			linked_list_remove();
			
		}
		catch (outOfRange e)
		{
			throw exceptS("exceptS: Stack is empty");
		}
		return out;
	};
	
	T getTop()
		throw (exceptS)
	{
		try
		{
			return linked_list_print(1);
		}

		catch
		{
			throw exceptS("exceptS: Stack is empty");
		}
	};
};


QueueLL.h - QueueLL.h is roughly the same as Stack, just designed as a queue instead of a stack
Last edited on
I don't really know much about templates yet so sorry can't offer much help there


To me the "throws" in the class definition are in a bad place or are screwing something up. They should be in the body of the function they are associated with like I see they are, which makes those in the class definition redundant.
Last edited on
I´m not sure about exception handling myself too, but why are you throwing something from the catch scope in the end of the code on line 104 - where are you throwing your exceptS? Isn´t try - throw - catch -keywords used together like this:

1. You try something.
2. Then you throw an exception...
3. ...and then catch it.

Now, it looks like you are doing it in this order - 1, 3 and 2.

Just leaves me wondering. Consider this and don´t think this is absolutely true, but this is how I´ve understood exception handling.
Topic archived. No new replies allowed.