0

Queue Management

Can someone explain to me what am I missing in my code? Here is what I've done: #include <iostream> using namespace std; class Queue { int size; int* queue; public: Queue() { size = 0; queue = new int[100]; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout <<endl; } void add(int x) { size++; } }; int main() { Queue q; q.add(42); q.add(2); q.add(8); q.add(1); q.print(); q.remove(); q.add(128); q.print(); q.remove(); q.remove(); q.print(); return 0; } The thing is that I need to create an add() method for the Queue class, that will take an item and add it to the end of the queue. The code needs to be fully working, so that the Queue declaration and manipulation code in main() works. I also have to increment the size after adding the item to the array. I'm almost there. It just that it keeps on resulting zero when I run it.

2nd Jan 2023, 8:20 PM
Gradi Maxime Kasita Mbatika
2 Réponses
+ 3
--snip-- void add(int x) { queue[size] = x; size++; } --snip-- You need to know that it's not good implementation.. But keep it up.
2nd Jan 2023, 9:29 PM
Smith Welder
Smith Welder - avatar
+ 1
Thank you Smith Welder. I appreciate it! 👍
2nd Jan 2023, 9:45 PM
Gradi Maxime Kasita Mbatika