+ 5

Help with “Queue management part 1”

Have not idea what code should be added before void add() {}

30th Nov 2020, 3:38 PM
Zhilin Hu
29 odpowiedzi
+ 155
this is the missing part void add(int x) { size += 1; queue[size-1]=x; } I need an upvote on my answer please❤️
11th Dec 2020, 9:59 PM
Aya Sherbak
Aya Sherbak - avatar
+ 41
#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; } This is the full code if somebody want to see all projects code follow me
3rd Jan 2021, 2:57 PM
Coder Ayush
Coder Ayush - avatar
+ 8
Raul Sanchez My solution would be easier to understand. Using size++, means you first assign to queue[size] before incrementing the size for next loop. For their solution, they incremented it first by using +=. So they have to -1 when it comes to assignment. A 3 liner code solution: https://code.sololearn.com/cZL0r3xLCF5Z/?ref=app
15th Jan 2021, 1:36 AM
Lam Wei Li
Lam Wei Li - avatar
+ 3
17th Dec 2020, 3:18 PM
Lam Wei Li
Lam Wei Li - avatar
+ 3
Lam Wei Li void add(int id) { queue[size++] = id } Thanks I'm understanding this a lot better. Your code helps. I now understand that your assigning each ID to the new given space. And that's true. I needed time to rethink. Since they increment some space first. They use the "-1" to get the last one. I almost forgot that going over memory would give an error.
15th Jan 2021, 2:09 AM
Raul Sanchez
Raul Sanchez - avatar
+ 3
#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 id) { queue[size++] = id; } }; int main() { Queue q; q.add(42); q.add(2); q.add(8);q.add(1); q.add(3);q.add(66); q.add(128); q.add(5); q.print(); return 0; } This is new full code.
5th Dec 2021, 3:12 PM
Srinivas vedullapalli
Srinivas vedullapalli - avatar
16th Dec 2020, 5:24 PM
Lam Wei Li
Lam Wei Li - avatar
+ 2
hi Lam , your solution saved 1 line
17th Dec 2020, 3:15 PM
Zhilin Hu
+ 2
I need someone to explain this to me
24th Oct 2021, 10:14 AM
Mohammed Alimam
Mohammed Alimam - avatar
+ 1
Nothing needs to be added before void add() {}. Fill in the missing code between the parentheses () so that it can pass an integer value. Add code between the braces {} to implement adding the integer to the queue array. Remember to update the size of the queue.
30th Nov 2020, 4:06 PM
Brian
Brian - avatar
+ 1
Aya Sherbak Coder Ayush Can you explain: queue[size-1] = x; I understand the 2nd line when you are adding more to the size. So the next item can be added. Can you explain the 3rd line. Especially what "x" is being assigned to?
14th Jan 2021, 8:52 PM
Raul Sanchez
Raul Sanchez - 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; } full code by me
21st Jul 2021, 6:24 PM
Hadi Ghorbani
Hadi Ghorbani - avatar
+ 1
Please post the correct ans
26th Jul 2021, 9:16 AM
Gyana BARIK
Gyana BARIK - avatar
+ 1
queue = ['John', 'Amy', 'Bob', 'Adam'] item = input() queue.append(item) print(queue)
31st Mar 2022, 9:47 PM
Abdelkhalek Nael Ahmad Allan
Abdelkhalek Nael Ahmad Allan - avatar
+ 1
queue = ['John', 'Amy', 'Bob', 'Adam'] item = input() queue.append(item) print(queue) ##this code is absolutely right
8th May 2022, 7:37 AM
PRAKHAR PRITHVIRAJ GOND
PRAKHAR PRITHVIRAJ GOND - avatar
+ 1
Can somebody explain this to me? if( size != 0 || size != 100 ) { size++; //increment size queue[size-1] = newData; //make new element as the last element } I get the other example but this one seems like the if would never be false. size++; //increment size queue[size-1] = newData; //make new element as the last element It seems like this would do the same thing as having the if statement
15th Jul 2022, 4:24 PM
Riggs
Riggs - avatar
+ 1
void add(int x){ queue[size]=x; size++; }
21st Aug 2022, 1:14 PM
Taha Faghani
Taha Faghani - avatar
+ 1
okey okey ı understand now. void Remove() { //function of remove for decrease the queue size if (size == 0) { cout << "Queue is empty" << endl; return; } else { for (int i = 0; i < size - 1; i++) { // we have decreased one number because one number will be deleted. queue[i] = queue[i + 1]; //here the first number is deleted, so since the first person has finished their transaction, their order is now invalid. //We substitute the second number for the first one, the third number for the second, etc. } size--; }
25th Jan 2023, 12:39 PM
selin
selin - avatar
0
Thank you, Brian. The queue is new mangement is new for me. Could you give me more detail ? here is the request: 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.
3rd Dec 2020, 3:01 PM
Zhilin Hu
0
Zhilin Hu I am pleased to see your attempt. It needs some corrections. Notice that queue is defined as an array, so use array syntax when working with queue. The purpose of the add() method is to add new values into the queue array. The queue starts out empty, so the size variable is initialized to zero. The program calls add() and passes a value that is meant to be appended onto the queue. The size variable keeps track of the number of values that have been appended to the queue, so add() should increment size by 1 each time a new value is added. (It is also decremented by 1 each time a value is removed). You might want to study this lesson on queues: https://www.sololearn.com/learn/643/?ref=app
3rd Dec 2020, 6:49 PM
Brian
Brian - avatar