ADT Queue Problem

I am doing a ADT Queue Assigment for my CS class. It uses inheritance. The program compiles but is not working as intended. I won't be posting the base class (a pure virtual class) and the header part of the derived class to save space.

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
#include "queue.h"
#include <iostream>
#include <string>

using namespace std;

struct spaceship
{
	int arrivalTime;
	string shipName;
	int fuelPurchase;
	int waitTime;
};

int main()
{
	MyQueue<spaceship> A;
	MyQueue<spaceship> B;
	MyQueue<spaceship> C;

	int numShips;
	spaceship* customer;


	cin >> numShips;
	customer = new spaceship[numShips];

	cout << A.size() <<endl;
	cout << B.size() <<endl;
	cout << C.size() <<endl;

	for (int i = 0; i < numShips; i++)
	{
		cin >> customer[i].arrivalTime;
		cin >> customer[i].shipName;
		cin >> customer[i].fuelPurchase;
		customer[i].waitTime = customer[i].arrivalTime + (3 * customer[i].fuelPurchase);

		if ((A.size() <= B.size()) && (A.size() <= C.size()))
		{
			A.enqueue(customer[i]);
			cout << "At time " << customer[i].arrivalTime << " " << customer[i].shipName << "joins port A" << endl;
			for (int j = 0;)
		}
		if ((B.size() < A.size()) && (B.size() <= C.size()))
		{
			B.enqueue(customer[i]);
			cout << "At time " << customer[i].arrivalTime << " " << customer[i].shipName << "joins port B" << endl;
			cout << B.size() <<endl;
		}
		if ((C.size() < A.size()) && (C.size() < B.size()))
		{
			C.enqueue(customer[i]);
			cout << "At time " << customer[i].arrivalTime << " " << customer[i].shipName << "joins port C" << endl;
			cout << C.size() <<endl;
		}
	}

	
	delete[] customer;

	return(0);

}

#include <iostream>
using namespace std;

template<class T>
const MyQueue<T>& MyQueue<T>::operator = (const MyQueue<T>& rhs)
{
	if (this != &rhs)
	{
		if (head != NULL)
			clear();

		head = NULL;
		Node<T>* rhsPtr = rhs.head;
		Node<T>* copyPtr = head = new Node<T>;
		copyPtr->data = rhsPtr->data;
		while (rhsPtr->next != NULL)
		{
			rhsPtr = rhsPtr->next;
			copyPtr->next = new Node<T>;
			copyPtr = copyPtr->next;
			copyPtr->data = rhsPtr->data;
		}

		copyPtr->next = NULL;
	}
	return(*this);
}

template<class T>
MyQueue<T>::MyQueue(const MyQueue<T>& rhs)
{
	head = tail = NULL;
	*this = rhs;
}

template<class T>
bool MyQueue<T>::isEmpty() const
{
	return(head == NULL);
}

template<class T>
const T& MyQueue<T>::front() const throw(Oops)
{
	if (head != NULL)
		return(head->data);
	else
		throw Oops("Error");
}

template<class T>
const T& MyQueue<T>::back() const throw(Oops)
{
	if (tail != NULL)
		return(tail->data);
	else
		throw Oops("Error");

}

template<class T>
void MyQueue<T>::enqueue(const T& x)
{
	Node<T>* newNode = new Node<T>;
	newNode->data = x;
	newNode->next = NULL;
	if (isEmpty())
	{
		tail = newNode;
		head = tail;
	}
	else
	{
		tail->next = newNode;
		tail = tail->next;
	}
}

template<class T>
void MyQueue<T>::dequeue()
{
	Node<T>* temp = head;
	if (isEmpty())
		return;
	if (head == tail)
		head = tail = NULL;
	else
	{
		head = head->next;
		delete temp;
	}
}

template<class T>
void MyQueue<T>::clear()
{
	Node<T>* del;
	del = head;
	while (head != NULL)
	{
		head = head->next;
		delete del;
		del = head;
	}
}

template<class T>
int MyQueue<T>::size() const
{
	Node<T>* temp;
	temp = head;
	int count = 0;
	while (temp != NULL)
	{
		++count;
		temp = temp->next;

	}
	return(count);

}

After I enter the first ships info (arrival time, name, fuel units purchased), it is being queued into all 3 MyQueue. I only need it to be queued to the shortest line (tiebreaker goes by alphabet). I have no idea why is this. Any help would be appreciated.
Last edited on
Add "else" on lines 45 and 51. Also, get rid of line 153 and 156. You might also want to check that rhs is not empty in your assignment operator function as well.
Topic archived. No new replies allowed.