+ 2

how do i start the add() method?

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.

26th Dec 2020, 11:30 AM
Dinilalim
Dinilalim - avatar
20 Réponses
+ 40
[SOLVED] #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 newData ) { if( size != 0 || size != 100 ) { size++; //increment size queue[size-1] = newData; //make new element as the last element } } }; 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; }
27th Dec 2020, 11:57 PM
Dinilalim
Dinilalim - avatar
+ 4
#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 int add(int x){ queue[size]=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; }
29th Jul 2021, 7:20 AM
Panchasara Jayesh Prakashbhai
Panchasara Jayesh Prakashbhai - avatar
+ 1
#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 newData ) { if( size != 0 || size != 100 ) { size++; //increment size queue[size-1] = newData; //make new element as the last element } } }; 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; } Good Luck
13th Dec 2021, 2:27 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
+ 1
1000% working //your code goes here int add(int a){ size++; queue[size-1]=a; a++;
18th Jul 2022, 7:17 PM
Pramod Rotte
+ 1
void add(int item){ this->size++; for(int i = 0; i < size; i++){ queue[size -1] = item; } }
4th Dec 2022, 3:07 PM
Rajesh Sharma
Rajesh Sharma - avatar
0
lensear namocatcat before we are going to help you, please present your attempt here. if you haven't done a try by yourself so far, please do so, and post it here. thanks!
26th Dec 2020, 11:33 AM
Lothar
Lothar - avatar
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 int add(int x){ queue[size]=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; }
3rd Feb 2021, 9:30 AM
Mohammed Shaaz.K
Mohammed Shaaz.K - avatar
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 newData ) { if( size != 0 || size != 100 ) { size++; //increment size queue[size-1] = newData; //make new element as the last element } } }; 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; }
13th Mar 2021, 6:58 AM
Dhaval Kanjariya
Dhaval Kanjariya - avatar
0
void add( int x ) { if( size != 100 ) { queue[size] = x; size++; } }
27th Mar 2021, 4:10 AM
Beyla
Beyla - avatar
0
Plz help me to solve this, I have tried a lot ,but it won't crt
3rd Jun 2021, 5:02 AM
Rithika A.B
Rithika A.B - avatar
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 newData ) { if( size != 0 || size != 100 ) { size++; //increment size queue[size-1] = newData; //make new element as the last element } } }; 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; }
15th Aug 2021, 2:28 AM
Phùng Châu Quốc
Phùng Châu Quốc - avatar
0
Please help me for this problem only.
23rd Aug 2021, 3:38 PM
Dipesh Khandekar
Dipesh Khandekar - avatar
0
[SOLVED] #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 newData ) { if( size != 0 || size != 100 ) { size++; //increment size queue[size-1] = newData; //make new element as the last element } } }; 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; }
10th May 2022, 10:58 AM
Saeed Rashedi
Saeed Rashedi - avatar
0
it is vary difficult to pass the next
30th Jul 2022, 7:51 PM
CHALACHEW WONDIE GOBEZIE
0
Please can you explain how your code works in relation to the question please Dinilalim
16th Sep 2022, 2:01 AM
Ojomo Jesubanke Josephine
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 newData ) { if( size != 0 || size != 100 ) { size++; //increment size queue[size-1] = newData; //make new element as the last element } } }; 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; }
21st Oct 2022, 2:28 AM
Arif Setyawan
Arif Setyawan - avatar
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
16th Nov 2022, 12:46 PM
Princenoel W Wanei
Princenoel W Wanei - avatar
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 newData ) { if( size != 0 || size != 100 ) { size++; //increment size queue[size-1] = newData; //make new element as the last element } } }; 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; }
5th Apr 2023, 5:36 PM
Pankaj
Pankaj - avatar
- 1
sorry, I'm just a beginner and this is all i could add to the code hehehe #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 item) { for(int i=0; i < size; i++) { queue[i] = queue[i + 1]; queue[i] = item; } 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; }
27th Dec 2020, 3:25 AM
Dinilalim
Dinilalim - avatar