+ 5
Help with “Queue management part 1”
Have not idea what code should be added before void add() {}
29 ответов
+ 155
this is the missing part
void add(int x) {
size += 1;
queue[size-1]=x;
}
I need an upvote on my answer please❤️
+ 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
+ 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
+ 3
+ 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.
+ 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.
+ 2
A 3 liner code solution:
https://code.sololearn.com/cZL0r3xLCF5Z/?ref=app
+ 2
hi Lam , your solution saved 1 line
+ 2
I need someone to explain this to me
+ 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.
+ 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?
+ 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
+ 1
Please post the correct ans
+ 1
queue = ['John', 'Amy', 'Bob', 'Adam']
item = input()
queue.append(item)
print(queue)
+ 1
queue = ['John', 'Amy', 'Bob', 'Adam']
item = input()
queue.append(item)
print(queue)
##this code is absolutely right
+ 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
+ 1
void add(int x){
queue[size]=x;
size++;
}
+ 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--;
}
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.
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