+ 1

C++ | Queue management part 2

can anyone share your solution. I can't get it right.

16th Jan 2021, 10:32 AM
MistaKoruto
MistaKoruto - avatar
8 Answers
+ 6
#include <iostream> using namespace std; class Queue { int size; int* queue; public: Queue() { size = 0; queue = new int[100]; } void add(int data) { queue[size] = data; size++; } 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; } Queue operator+(Queue obj){ Queue result; result.size = this->size; result.queue = this->queue; int length = obj.size; int n = 0; while(n<length){ result.add(obj.queue[n]); n++; } return result; } }; int main() { Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); Queue q2; q2.add(3); q2.add(66); q2.add(128); q2.add(5); Queue q3 = q1+q2; q3.print(); return 0; }
13th Apr 2021, 11:42 PM
Vaibhav Pande
+ 2
How about you copy your code into a Playground project and share that here, so that we can have a look together for what might be wrong with your approach?
16th Jan 2021, 10:47 AM
Shadow
Shadow - avatar
+ 1
Vaibhav Pande thanks for the help ^^)
24th Jun 2021, 1:23 PM
Carmen Morales
Carmen Morales - avatar
0
Hello guys. The code show above has a flaw. It is about the use of pointers to add the values in q1 to q3. You see, when you change the values on q1, the values in q3 will also change. i.e. If you remove the first item on q1 it will also be removed from q3. I am also learning and I could be wrong so I hope my opinion has been helpful. Let's keep it up!
22nd Apr 2021, 1:01 AM
Hatuey Mieses
Hatuey Mieses - avatar
0
#include <iostream> using namespace std; class Queue { int size; int* queue; public: Queue() { size = 0; queue = new int[100]; } void add(int data) { queue[size] = data; size++; } 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; } Queue operator+(Queue& obj) { Queue q3; q3.size = this->size; q3.queue = this->queue; for (int i = 0; i < obj.size; i++) { q3.add(obj.queue[i]); } return q3; } }; int main() { Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); Queue q2; q2.add(3); q2.add(66); q2.add(128); q2.add(5); Queue q3 = q1 + q2; q3.print(); return 0; }
27th Jul 2021, 11:39 AM
babycallmemaybe
0
I don't understand this code, really struggled with this section. I think most of the problem is with the &/this. So the crux of the solution is this: Queue operator+(Queue& obj) { Queue q3; q3.size = this->size; q3.queue = this->queue; for (int i = 0; i < obj.size; i++) { q3.add(obj.queue[i]); } return q3; Why do we even have to specify the q3.size or q3.queue if it's not used in the for loop? Also, why does the for loop have to use obj.size instead of q3.size?
17th Jan 2022, 2:17 AM
Christopher Blankenship
Christopher Blankenship - avatar
0
Christopher Blankenship I'm still learning but I believe q3 in the function is just the name of the object in that one function. It could be anything I believe(ie. addedQueue). In the main function we call the class and create the object in the main function to execute the code. We specify the size to let the code know how big the new array(q3) will be. The the obj size is the size of both queues (q3 = q1+2) so the array need to be big enough. q3.size tells how big and q3.queue assigned the obj queue to it. Then the for loop uses obj size as the maximum of the queues we are adding. Then add function actually assigns the q3 array with the obj.queue. Like I said I'm learning still and hope that maybe helped I had a lot of trouble with the project too.
19th Apr 2022, 7:22 PM
Tyler Briand
Tyler Briand - avatar
0
Hdvu
2nd Feb 2023, 9:59 PM
Norddine 3aloi
Norddine 3aloi - avatar