Program is supposed to enqueue doubles into a maximum array size of 6. However, it is not stopping at 5 intakes and also will not dequeue properly. It will simply spit out junk locations when dequeuing (after asking it show the queue) instead of dequeuing proper items in the queue. Or I'll put enqueue two items into the queue, and then ask it to dequeue, and it says "The queue is empty." How can I fix this? Thank you.
You have the makings of a very clean circular queue here.
To fix it, you need to break the task down much more carefully. That, and draw yourself a queue, complete with the words “first” and “rear” and arrows pointing to stuff. For each of the following operations, change your drawing to see how it works.
To enqueue, should you modify the items[rear] cell before or after updating rear?
To dequeue, do you need to modify any cells?
To showQueue, you need to write your class method. It is a loop. Hint:
for (int n = front; n != rear; n = (n + 1) % SIZE)
Since this is a member function, you do not need to dequeue anything, or check for emptiness, or stuff like that.
BTW, line 12 should read while (toupper(selection) != 'Q') {.
I would personally also initialize front and rear to zero, but that doesn’t really matter.
[edit] Also, line 11: items represents a single item (a single double). It should be named item, right?
#include <iostream>
class Queue
{
public:
Queue(int size_arg);
~Queue();
bool isFull();
bool isEmpty();
void makeEmpty();
void enqueue(double item);
double dequeue(double& item);
void showQueue();
private:
constint size;
int front;
int rear;
double* items;
};
Queue::Queue(int size_arg)
: size { size_arg }
, front { size - 1 }
, rear { size - 1 }
, items { newdouble[size] {} }
{
// Debug - to be deleted:
std::cout << "--> front is now " << front
<< "; rear is now " << rear << '\n';
}
Queue::~Queue()
{
delete[] items;
}
bool Queue::isFull()
{
return (rear + 1) % size == front;
}
bool Queue::isEmpty()
{
return front == rear;
}
void Queue::makeEmpty()
{
front = rear = size - 1;
}
void Queue::enqueue(double item)
{
rear = (rear + 1) % size;
items[rear] = item;
// Debug - to be deleted:
std::cout << "--> front is now " << front
<< "; rear is now " << rear << '\n';
}
double Queue::dequeue(double& item)
{
front = (front + 1) % size;
item = items[front];
// Debug - to be deleted:
std::cout << "--> front is now " << front
<< "; rear is now " << rear << '\n';
return item;
}
void Queue::showQueue()
{
// Debug - to be deleted:
std::cout << "--> front is now " << front
<< "; rear is now " << rear << '\n';
for (int n = front; n != rear; n = (n + 1) % size) {
std::cout << "element " << n << ": " << items[n] << '\n';
}
}
int main()
{
Queue queue(6);
double items {};
char selection {};
while (::toupper(selection) != 'Q') {
{
std::cout << "E - Enqueue\n""D - Dequeue\n""S - Show Queue\n""Q - Quit\n""Enter your choice: ";
std::cin >> selection;
}
switch (selection)
{
case'E':
case'e':
if ( !queue.isFull() ) {
std::cout << "Enter an item to enqueue: ";
}
std::cin >> items;
queue.enqueue(items);
break;
case'D':
case'd':
if (queue.isEmpty()) {
std::cout << "The queue is empty.\n";
// Why?
// std::exit(EXIT_FAILURE);
}
else {
queue.dequeue(items);
std::cout << "Item has been dequeued.\n";
}
break;
case'S':
case's':
std::cout << "Showing queue...\n";
queue.showQueue();
std::cout << "NULL\n\n";
break;
default:
break;
}
}
}
Output:
--> front is now 5; rear is now 5
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 9
--> front is now 5; rear is now 0
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 8
--> front is now 5; rear is now 1
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 7
--> front is now 5; rear is now 2
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 6
--> front is now 5; rear is now 3
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 5
--> front is now 5; rear is now 4
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
4
--> front is now 5; rear is now 5
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: e
Enter an item to enqueue: 3
--> front is now 5; rear is now 0
E - Enqueue
D - Dequeue
S - Show Queue
Q - Quit
Enter your choice: s
Showing queue...
--> front is now 5; rear is now 0
element 5: 4
NULL