#Queue Question

55 messages · Page 1 of 1 (latest)

sturdy jewel
#

If the contents are:

1 - Queue q = new Queue();
2 - q.enqueue(3);
3 - q.enqueue(10);
4 - if(q.dequeue()<5)
5 - q.dequeue();
6 - q.enqueue(1);
7 - q.enqueue(6);

I'm a little stumped by the line 4/5. I know initially it is [3, 10] but does it remove both elements or just the [3]?

As a result, I'm unsure if the correct answer is [10, 1, 6] OR [1, 6]

glad geodeBOT
#

This post has been reserved for your question.

Hey @sturdy jewel! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

dusty magnet
#

every time dequeue is called, it removes an element from the queue. what's done with the result isn't the queue's problem anymore

#

try going from there

sturdy jewel
dusty magnet
#

yep

sturdy jewel
# dusty magnet yep

so just to clarify line 4 will remove [3] and line 5 will remove [10] right

dusty magnet
#

yes

#

what would happen if you changed the 3 to a 7 in the enqueue on line 2 though?

sturdy jewel
sturdy jewel
dusty magnet
#

yep

glad geodeBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

sturdy jewel
#

is this the same for a stack

dusty magnet
#

well the order no, but popping something from the stack just plainly removes it just like dequeue

sturdy jewel
#

so for example, if i were to have

stack s = new Stack();
s.push(16);
s.push(10);
if (s.top()>15)
s.pop();
s.push(2);
s.push(3);

dusty magnet
#

that's any method, really

sturdy jewel
#

it would be 16,2, 3 right

dusty magnet
#

top is like a peek, pop is like dequeue

sturdy jewel
#

right

dusty magnet
#

peek/top do not remove from the structure

sturdy jewel
#

im just a little confused by the

if (s.top()>15)
s.pop();

#

bc i know it will begin the stack with 16 at the bottom then 10 next

[16, 10]

#

since 10 is less than 15, it fails so it wont do the next one but gets deleted

#

so itll be [16]

dusty magnet
#

it won't be deleted though

sturdy jewel
#

then withj the next two push lines its just [16, 2, 3]

#

ohhh

#

wait how did the one in queue get deleted twice then

dusty magnet
#

again, top is just inspection, pop is how you remove

dusty magnet
sturdy jewel
#

OHHHHH

#

i see

#

so top is just checking to see what value it is

#

where as dequeue is an actual method right

dusty magnet
#

top is also an actual method, wdym

sturdy jewel
#

oh my bad yes ur right

#

it is a methid

#

its just checking what value it is and doesnt actually remove it like pop does

dusty magnet
#

yes

sturdy jewel
#

wehre as in the queue one, it uses deqeueue for conditional which is not a method to check what value it is but rather remove from the queue

dusty magnet
#

top also doesn't really check it

#

it just gives it

sturdy jewel
#

i see

#

ya

dusty magnet
#

what you do with the value isn't its problem, same as in dequeue

#

but within the method itself, top does not remove, while dequeue or pop do

sturdy jewel
#

right

#

now lets just say it was

#

stack s = new Stack();
s.push(16);
s.push(17);
if (s.top()>15)
s.pop();
s.push(2);
s.push(3);

#

then it woudlve been [16, 2, 3] right

dusty magnet
#

yep