2

This is a problem from a book testing you on LinkedLists, Stacks, and Queue. The goal is to print out the desired output from this short snippet of code. I have accompanied the code with my analysis.

LinkedList<Integer> a = new LinkedList<Integer>(); Stack<Integer> s = new Stack<Integer>(); Queue<Integer> q = new LinkedList<Integer>(); a.add( 2 ); a.add( 3 ); a.add( 5 ); a.add( 7 ); 

The LL: a = {2, 3, 5, 7}

for ( int i : a ) { System.out.print( i + " " ); s.push( i ); q.add( i ); } 

print output: 2 3 5 7

The stack: s = {2, 3, 5, 7}

The queue: q = {2, 3, 5, 7}

System.out.println(); for ( int i : a ) { s.push( i ); q.add( s.pop() + 5 ); System.out.print( s.pop() + " " ); } 

The stack: s = {2, 3, 5, 7, 2, 3, 5, 7}

The queue: q = {2, 3, 5, 7, 12, 10, 8, 7}. This is as a result of s.pop() + 5

Print output: 7 5 3 2

System.out.println(); for ( int i : a ) { System.out.print( q.remove() + " " ); System.out.print( q.remove() + " " ); } 

Print output: 2 3 5 7 12 10 8 7

So in summary, my print outputs are:

2 3 5 7

7 5 3 2

2 3 5 7 12 10 8 7

The answer to this problem, however, is this:

2 3 5 7

7 5 3 2

2 3 5 7 7 8 10 12

As you can see, the results don't match in the queue printing. I redid the problem twice, but could not determine whether I did something wrong in the addition (s.pop() + 5) or in my .pop() printing. Could someone give me some insight on what I did wrong?

1 Answer 1

2

I think your mistake is here, in the third code snippet:

for ( int i : a ) { s.push( i ); q.add( s.pop() + 5 ); System.out.print( s.pop() + " " ); } 

After i is pushed onto the stack, it is immediately popped off by q.add(s.pop() + 5);. Execution would go something like this:

Before:

s == [2, 3, 5, 7] q == [2, 3, 5, 7] 

First iteration:

2 is pushed onto s 2 is popped off of s 5 is added to 2 7 is added to q 7 is popped off s and printed 

Second iteration:

3 is pushed onto s 3 is popped off of s 5 is added to 3 8 is added to q 5 is popped off s and printed 

And so on and so forth.

So the proper result after that loop should be an empty stack and a queue that is:

[2, 3, 5, 7, 7, 8, 10, 12] 

Everything else is OK, I think.

Sign up to request clarification or add additional context in comments.

7 Comments

@theGreenCabbage No problem! Just a tip -- you can always type the code into an IDE and use a debugger to see exactly is going on for things like this.
Actually, could you explain a little in depth what is going on at q.add(s.pop()+5)? Which gets executed first, is it q.add or s.pop() or s.pop() + 5?
@theGreenCabbage Just updated my answer. The arguments for an operation/method are always evaluated before the operation/method is executed. So before q.add() can be called, s.pop() + 5 has to be evaluated. And before + can be evaluated, s.pop() and 5 have to be evaluated.
Thanks a lot! These order of operations were what got me, thank you again user :).
@theGreenCabbage Best way to do these on paper is to pretend you're a computer! Just go line by line doing exactly what the code tells you to do, breaking down things, following method calls, etc. as much as you need to. I can't really be more specific than that, unfortunately (that's all I usually have to think about when I do this kind of thing), but as long as you clear your head of any preconceptions about what the code is supposed to do you'll generally do fine.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.