+ 1
Queueo Managment parte 1
You are working on a Queue management system and need to create the class to hold the queue data, which are customer IDs (integers). You make a Queue class, which has a size attribute, and an array, to hold the data of the queue. The class has a remove() method to remove the front item of the queue, a print() method to output the queue. You 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.
3 Respostas
0
#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;
}
//your code goes here
void add(int x){
queue[100-1]+=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;
}
+ 2
void add(int a){
queue[size] = a;
size++;
}
**I hope will help you guys
+ 1
Problem in add() function:
1. You are modifying the last element only. You should instead put new value <x> into the next free slot, noting on <size> value.
2. The use of += operator may not be working as you'd expect. Look at this ...
42 2 8 1 // initially
2 8 1 // after a call to remove()
8 129 // after 2 calls to remove() and call to add(128)
Why 129? because the slot where 128 was stored into had value 1. It was there before calls to remove().
P.S. Why you mark your own as best answer? the code still has things to fix though ...