Queue data structure assignment

This assignment wants me to create a Queue data structure using a LinkedList data structure we used in class.
Here is the assignment:
1. Implement the Queue data structure using the Linked List data structure.
a. Your queue should be a template class.
b. The queue should have push, pop, and peek functions.
i. Pop and peek functions operate in the same way as in the Stack class.
ii. The push function adds elements to the beginning of a Queue, making it a FILO (First In, Last Out) data structure rather than a FIFO (First In, First Out) data structure like a stack.
1. In order to implement the Queue data structure, you will have to add a prepend function to the Linked List class that inserts elements at the beginning of the list rather than at the end of the list.
c. Add a copy constructor that can produce a copy of the Queue class.
d. Overload the << operator to print out the entire contents of the Queue.
e. Overload the + operator that adds the right queue to beginning of the left queue. For example:

Queue<int> * q1 = new Queue<int>();
Queue<int> * q2 = new Queue<int>();
q1.push(5);
q1.push(6);
q2.push(9);
q2.push(1);
Queue<int> * q3 = (*q1) + (*q2);
cout << q3 << endl;

should produce the following output:

9
1
5
6


While trying to figure this out,I have some questions that would like to be answered.
1. What functions should be replaced by the push pop and peek functions?
2. Does a Queue data structure use the node class or something else?

Here is what I have so far...
Main.cpp
#include <iostream>
#include "Queue.h"

using namespace std;

void main(void)
{
Queue<int> * q1 = new Queue<int>(); //Constructor triggered
Queue<int> * q2 = new Queue<int>(); //Pointer, but no instance

q1.push(5);
q1.push(6);

q2.push(9);
q2.push(1);



Queue<int> * q3 = (*q1)+(*q2); //create a new linkedlist that has in it the contents of L1 followed by the conents of L2


cout << q3 << endl;




system("pause");
}

node.h
#include <iostream>


template<class C>
class Node
{
private:
C data;
Node<C> * next;
public:
Node();
Node<C> * getNext(void);
void setNext(Node<C> * newNext);
C getData();
void setData(C d);

};

template<class C>
Node<C>::Node(void)
{
next=NULL;
}
template<class C>
Node<C> * Node<C>::getNext(void)
{
return next;
}
template<class C>
void Node<C>::setNext(Node<C> * newNext)
{
next = newNext;
}
template<class C>
C Node<C>::getData(void)
{
return data;
}
template<class C>
void Node<C>::setData(C d)
{
data = d;
}

queue.h
#include "node.h"

template <class Q>
class Queue
{
private:
int length;
Node <Q> * list;

public:
Queue();//Regular Constructor
Queue(Queue<Q> * right);//Copy Constructor
void addItem(Q data);
void delItem(Q data);
void printList(void);
Q findAt(int pos);
int getLength(void);


};

template <class Q>
Queue<Q>::Queue(void)
{
list = NULL;
length = 0;
}

template<class Q>
Queue<Q>::Queue(Queue<Q> * right)
{
//this will refer to the object on the left of the =
//right will refer to the object on the right of the =
list = NULL;
length = 0;
for(int i=0;i<right->getLength();i++)
{
this->addItem(right->findAt(i));
}
}

template<class Q>
void Queue<Q>::addItem(Q data)
{
if(length==0)
{
//Create the first node oin the list
list = new Node<Q>();
list->setData(data);
length++;
}
else
{
//Append at the end of the list
Node<Q> * temp = list; //Make a copy of the memory address of the first node on the heap
while(temp->getNext() != NULL)
{
temp = temp->getNext();
}
Node<Q> * newNode = new Node<Q>(); //Construct node to add to list
//newNode->setData(data);
temp->setNext(newNode);
temp->getNext()->setData(data); // same as line above(newNode->setData(data);)
length++;
}
}

template<class Q>
void Queue<Q>::printList(void)
{
Node<Q> * temp = list;
while(temp != NULL)
{
cout << temp->getData() << endl;
temp = temp->getNext();
}
}

template<class Q>
int Queue<Q>::getLength(void)
{
return length;
}

template<class Q>
Q Queue<Q>::findAt(int pos)
{
if(pos < 0 || pos >=length)
{
return NULL;
}
else
{
Node<Q> * temp = list;
for(int i=0;i<pos;i++)
{
temp=temp->getNext();
}

return temp->getData();
}
}

template<class Q>
void Queue<Q>::delItem(Q data)
{
if(length==0)
{
return;
}
else if(length==1)
{
delete list;
length--;
}
else
{
Node<Q> * cur = list;
Node<Q> * prev;

if(cur->getData() == data)
{
Node<Q> * temp = cur->getNext();
delete cur;
list = temp;
length--;
}
else
{
prev = cur;
cur = cur->getNext();

while(cur != NULL)
{
if(cur->getData() == data)
{
Node<L> * temp = cur->getNext();
prev->setNext(temp); //Reroute the linked list around cur
delete cur;
length--;
return;
}
prev = cur;
cur = cur->getNext();
}


}
}
};

template<class Q>
std::ostream& operator<<(std::ostream& left, Queue<Q> * right)
{
if(right!=NULL)
{
for(int i=0;i<right->getLength();i++)
{
left << right->findAt(i)<<endl;
}
}
else
{
cout << "List not initialized" << endl;
}
return left;
}

template<class Q>
Queue<Q> *operator+(Queue<Q> left, Queue<Q> right)
{
Queue<Q> * newList = new Queue<Q>();

//Add the items in left to new list
for(int i=0;i<left.getLength();i++)
{
newList->addItem(left.findAt(i));
}
//Add the items in right to new list
for(int i=0;i<right.getLength();i++)
{
newList->addItem(right.findAt(i));
}
return newList;
}
Use the code tags, please.

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
Queue<int> * q1 = new Queue<int>();
Queue<int> * q2 = new Queue<int>();
q1.push(5);
q1.push(6);
q2.push(9);
q2.push(1);
Queue<int> * q3 = (*q1) + (*q2);
cout << q3 << endl;

should produce the following output :

9
1
5
6


While trying to figure this out, I have some questions that would like to be answered.
1. What functions should be replaced by the push pop and peek functions ?
2. Does a Queue data structure use the node class or something else ?

Here is what I have so far...
Main.cpp
#include <iostream>
#include "Queue.h"

using namespace std;

void main(void)
{
	Queue<int> * q1 = new Queue<int>();	//Constructor triggered
	Queue<int> * q2 = new Queue<int>();	//Pointer, but no instance

	q1.push(5);
	q1.push(6);

	q2.push(9);
	q2.push(1);



	Queue<int> * q3 = (*q1) + (*q2); //create a new linkedlist that has in it the contents of L1 followed by the conents of L2


	cout << q3 << endl;




	system("pause");
}

node.h
#include <iostream>


template<class C>
class Node
{
private:
	C data;
	Node<C> * next;
public:
	Node();
	Node<C> * getNext(void);
	void setNext(Node<C> * newNext);
	C getData();
	void setData(C d);

};

template<class C>
Node<C>::Node(void)
{
	next = NULL;
}
template<class C>
Node<C> * Node<C>::getNext(void)
{
	return next;
}
template<class C>
void Node<C>::setNext(Node<C> * newNext)
{
	next = newNext;
}
template<class C>
C Node<C>::getData(void)
{
	return data;
}
template<class C>
void Node<C>::setData(C d)
{
	data = d;
}

queue.h
#include "node.h"

template <class Q>
class Queue
{
private:
	int length;
	Node <Q> * list;

public:
	Queue();//Regular Constructor
	Queue(Queue<Q> * right);//Copy Constructor
	void addItem(Q data);
	void delItem(Q data);
	void printList(void);
	Q findAt(int pos);
	int getLength(void);


};

template <class Q>
Queue<Q>::Queue(void)
{
	list = NULL;
	length = 0;
}

template<class Q>
Queue<Q>::Queue(Queue<Q> * right)
{
	//this will refer to the object on the left of the =
	//right will refer to the object on the right of the =
	list = NULL;
	length = 0;
	for (int i = 0; i<right->getLength(); i++)
	{
		this->addItem(right->findAt(i));
	}
}

template<class Q>
void Queue<Q>::addItem(Q data)
{
	if (length == 0)
	{
		//Create the first node oin the list
		list = new Node<Q>();
		list->setData(data);
		length++;
	}
	else
	{
		//Append at the end of the list
		Node<Q> * temp = list;	//Make a copy of the memory address of the first node on the heap
		while (temp->getNext() != NULL)
		{
			temp = temp->getNext();
		}
		Node<Q> * newNode = new Node<Q>(); //Construct node to add to list
										   //newNode->setData(data);
		temp->setNext(newNode);
		temp->getNext()->setData(data); // same as line above(newNode->setData(data);)
		length++;
	}
}

template<class Q>
void Queue<Q>::printList(void)
{
	Node<Q> * temp = list;
	while (temp != NULL)
	{
		cout << temp->getData() << endl;
		temp = temp->getNext();
	}
}

template<class Q>
int Queue<Q>::getLength(void)
{
	return length;
}

template<class Q>
Q Queue<Q>::findAt(int pos)
{
	if (pos < 0 || pos >= length)
	{
		return NULL;
	}
	else
	{
		Node<Q> * temp = list;
		for (int i = 0; i<pos; i++)
		{
			temp = temp->getNext();
		}

		return temp->getData();
	}
}

template<class Q>
void Queue<Q>::delItem(Q data)
{
	if (length == 0)
	{
		return;
	}
	else if (length == 1)
	{
		delete list;
		length--;
	}
	else
	{
		Node<Q> * cur = list;
		Node<Q> * prev;

		if (cur->getData() == data)
		{
			Node<Q> * temp = cur->getNext();
			delete cur;
			list = temp;
			length--;
		}
		else
		{
			prev = cur;
			cur = cur->getNext();

			while (cur != NULL)
			{
				if (cur->getData() == data)
				{
					Node<L> * temp = cur->getNext();
					prev->setNext(temp); //Reroute the linked list around cur
					delete cur;
					length--;
					return;
				}
				prev = cur;
				cur = cur->getNext();
			}


		}
	}
};

template<class Q>
std::ostream& operator<<(std::ostream& left, Queue<Q> * right)
{
	if (right != NULL)
	{
		for (int i = 0; i<right->getLength(); i++)
		{
			left << right->findAt(i) << endl;
		}
	}
	else
	{
		cout << "List not initialized" << endl;
	}
	return left;
}

template<class Q>
Queue<Q> *operator+(Queue<Q> left, Queue<Q> right)
{
	Queue<Q> * newList = new Queue<Q>();

	//Add the items in left to new list
	for (int i = 0; i<left.getLength(); i++)
	{
		newList->addItem(left.findAt(i));
	}
	//Add the items in right to new list
	for (int i = 0; i<right.getLength(); i++)
	{
		newList->addItem(right.findAt(i));
	}
	return newList;
}
Topic archived. No new replies allowed.