Click here to Skip to main content
16,010,473 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
New to java and trying a stack problem below. Rather have the answer to my question than a solution if possible because I'll keep having the same problem, hope it makes sense;

Write a method reorder that takes a queue of integers as a parameter and that puts the integers into sorted (nondecreasing) order assuming that the queue is already sorted by absolute value. For example, suppose that a variable called q stores the following sequence of values:

front [1, 2, -2, 4, -5, 8, -8, 12, -15, 23] back

Notice that the values appear in sorted order if you ignore the sign of the numbers. The call of reorder(q); should reorder the values so that the queue stores this sequence of values:

front [-15, -8, -5, -2, 1, 2, 4, 8, 12, 23] back

Notice that the values now appear in sorted order taking into account the sign of the numbers. You may use one stack as auxiliary storage to solve this problem.

This is my code (not the best or even correct but....);

Java
import java.util.Queue;
import java.util.Stack;

public class Reorder {
	public static void main(args[] String) {
	}

	public static void reorder2(Queue<integer> q) {

		Stack<integer> s = new Stack<integer>();

		for (int i = 0; i < q.size(); i++) {
			int smaller = q.remove();
			if (smaller < q.remove()) {
				s.push(smaller);
			}
			q.add(q.remove());
			if (smaller > q.remove()) {
				s.push(q.remove());
			}
			q.add(smaller);
		}

		while (!s.isEmpty()) {
			q.add(s.pop());
		}

	}

}


Correct result is; [-15, -8, -5, -2, 1, 2, 4, 8, 12, 23]
My results is [1, -5, -15, -8, -2, -8]
What happened to 2, 4, 8, 12, 23? These numbers should be added back to the queue and compared with q.remove(). Then they should be added to stack eventually but they disappear somehow!?
Thanks
Posted
Updated 5-May-12 15:39pm
v3
Comments
amvest 6-May-12 4:15am    
Thanks much!

1 solution

I'll show your the first mistake; and you will fix it and do the rest, OK?

The mistake is: apparently, in the loop for for (int i = 0; i < q.size(); i++) { ... } you wanted to repeat the cycle the number of time equal to the size of the queue. It does not happen, because the condition i < q.size() is evaluated in each iteration, but you remove elements. Suppose you have 10 elements and remove an element in each iteration. After 6 iterations i becomes 5, 4 elements are still in the queue, and the loop ends because the condition is satisfied. You need to have:
Java
int queueSize = q.size(); 
for (int i = 0; i < queueSize; i++) {

...

}
Even though it looks equivalent, it is not — the behavior will be very different.

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 7-May-12 6:05am    
Good reply :-D
Sergey Alexandrovich Kryukov 7-May-12 12:18pm    
Thank you, Espen.
--SA
amvest 29-Jun-12 14:57pm    
Thanks
Sergey Alexandrovich Kryukov 29-Jun-12 17:47pm    
My pleasure.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900