Queue using linked list small problem

Hello there!

I am implementing a Queue using linked list, my code was working perfectly until I made a copyQueue() function that copies queue from one queue to another.

After hours of looking and trying in my code, I finally give up and I need your help with it. My code won't compile anymore.

These are the errors I get:

1
2
3
4
5
/home/ubuntu/workspace/source.cpp: In member function ‘voidQueue<Type>::copyQueue(const Queue<Type>&)’:
/home/ubuntu/workspace/source.cpp:46:15: error: expected type-specifier before 
‘nodeType’   front = new nodeType<Type>;
               ^
/home/ubuntu/workspace/source.cpp:46:15: error: expected ‘;’ before ‘nodeType’


My code
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
  #include <iostream>
#include <string>
#include <ostream>

using namespace std;

template <typename Type>
struct Node
{
	Type info;
	Node<Type> *Next;
};

template <typename Type>
class Queue
{
private:
	Node<Type>*front;
	Node<Type>*rear;
	void copyQueue(const Queue<Type>& otherQueue);
public:
	Queue();
	~Queue();
	int length(void);
	Queue(const Queue<Type> &x);
	void Enqueue(Type x);
	void Dequeue();
	void Print();
	bool isEmpty();
	void makeEmpty();
};

template <class Type>
void Queue<Type>::copyQueue(const Queue<Type>& otherQueue)
{
	Node<Type> *newNode, *current, *last;

	if (front != NULL)
		makeEmpty();

	if (otherQueue.front == NULL)
		front = NULL;
	else
	{
		current = otherQueue.front;
		front = new nodeType<Type>;
		front->info = current->info;
		front->Next = NULL;
		last = front;
		current = current->Next;
		while (current != NULL)
		{
			newNode = new Node<Type>;

			newNode->info = current->info;
			newNode->link = NULL;
			last->Next = newNode;
			last = newNode;
			current = current->Next;
		}
	}
}

template <typename Type>
void Queue<Type>::makeEmpty()
{
	Node<Type> *temp;
	while (front != NULL)
	{
		temp = front;
		front = front->Next;
		delete temp;
	}
}

template <typename Type>
int Queue<Type>::length(void)
{
	Node<Type> *pointer;
	int length = 0;
	pointer = front;
	while (pointer != NULL)
	{
		pointer = pointer->Next;
		length++;
	}

	return (length);
}

template <typename Type>
Queue<Type>::Queue()
{
	rear = NULL;
	front = NULL;
}

template <typename Type>
bool Queue<Type>::isEmpty()
{
	if (front == NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

template <typename Type>
void Queue<Type>::Enqueue(Type x)
{
	Node<Type> *temp;
	temp = new Node<Type>;
	temp->info = x;
	temp->Next = NULL;
	if (front == NULL)
	{
		front = temp;
	}
	else
	{
		rear->Next = temp;
	}

	rear = temp;

}

template <typename Type>
void Queue<Type>::Dequeue()
{
	Node<Type> *temp;
	temp = new Node<Type>;

	if (front == NULL)
	{
		cout << " Queue is Empty. " << endl;
	}
	else
	{
		temp = front;
		front = front->Next;
		cout << endl << temp->info << " was dequeued." << endl;
		delete temp;
	}
}

template <typename Type>
void Queue<Type>::Print()
{
	Node<Type> *pointer1;
	pointer1 = new Node<Type>;
	pointer1 = front;
	
	if (front == NULL)
	{
		cout << "Queue is Empty." << endl;
	}
	else
	{
		while (pointer1 != NULL)
		{
			cout << pointer1->info << endl;
			pointer1 = pointer1->Next;
		}
	}
}

template <typename Type>
Queue<Type>::~Queue()
{
	makeEmpty();
}

int main()
{
	//NOTE: Queue is never full, only if we run out of memory.

	cout << "============== Int Queue ==============" << endl;
	Queue<int>IntQueue;
	IntQueue.isEmpty();
	IntQueue.makeEmpty();
	IntQueue.Dequeue();
	IntQueue.Enqueue(10);
	IntQueue.Enqueue(20);
	IntQueue.Enqueue(30);
	IntQueue.Enqueue(40);
	cout << "int length 3 = " << IntQueue.length() << endl;
	IntQueue.Dequeue();
	cout << "int length 4 = " << IntQueue.length() << endl;
	cout << "The int queue contains : " << endl;
	IntQueue.Print();

	cout << "============== String Queue ==============" << endl;
	Queue<string>FloatQueue;
	FloatQueue.isEmpty();
	FloatQueue.makeEmpty();
	FloatQueue.Dequeue();
	FloatQueue.Enqueue("bob");
	cout << "float length 3 = " << FloatQueue.length() << endl;
	FloatQueue.Enqueue("john");
	cout << "float length 4 = " << FloatQueue.length() << endl;
	FloatQueue.Enqueue("dan");
	FloatQueue.Dequeue();
	cout << "The float queue contains : " << endl;
	FloatQueue.Print();
	Queue<string> FloatQueue2 = FloatQueue;
	cout << "The float queue 2 contains:  "<< endl;
	FloatQueue2.Print();
	FloatQueue.makeEmpty();
	cout << "The float queue 3 contains:  " << endl;
	FloatQueue2.Print();

	return 0;
}
Last edited on
closed account (SECMoG1T)
Hi , am sure you dint want your line 46 to be
1
2
3
4
5
front = new nodeType<Type>;/// you have not defined type "nodeType" anywhere

/// am sure you intended 

front = new Node <Type>; ///the bug above could easily camoflage in your code and go unseen.  
makeEmpty() needs to set rear=NULL;.

Shouldn't Dequeue() return the dequeued element? Or there should be a way to look at the element at the front of the queue.

Dequeue() needs to check if it's deleting the last item. If so, it needs to set rear=NULL.

Remove lines 135 and 154. They create a new Node and then don't use it.
Topic archived. No new replies allowed.