how do I reverse a stack?

this is my code so far but it didn't work, why is it?

publicvoid reverseStack(Stack s){

Stack temp =new Stack();

while (! s.empty()){

temp.push(s.pop());

}

s = temp;

}

[455 byte] By [aditya15417a] at [2007-11-26 20:17:43]
# 1
> s = temp;This just changes a local variable. Call-by-value and all that.
DrLaszloJamfa at 2007-7-10 0:40:58 > top of Java-index,Java Essentials,Java Programming...
# 2
Try s.addAll(temp);instead.
DrLaszloJamfa at 2007-7-10 0:40:58 > top of Java-index,Java Essentials,Java Programming...
# 3
A better signature for the method would bepublic Stack reverseStack(Stack s)(And a better name for the method would be "reverse".)
DrClapa at 2007-7-10 0:40:58 > top of Java-index,Java Essentials,Java Programming...
# 4
> public Stack reverseStack(Stack s)Although you'd probably want to leave s in the same state you found it in!Then again, I'm not so sure. Just document your specs, I guess.Message was edited by: DrLaszloJamf
DrLaszloJamfa at 2007-7-10 0:40:58 > top of Java-index,Java Essentials,Java Programming...
# 5
Another solution:public void reverseStack(Stack s){Collections.reverse(s);}
DrLaszloJamfa at 2007-7-10 0:40:58 > top of Java-index,Java Essentials,Java Programming...
# 6
how about if I should return void here and I cant use Collections.reverse(s)
aditya15417a at 2007-7-10 0:40:58 > top of Java-index,Java Essentials,Java Programming...
# 7
> how about if ... I cant use Collections.reverse(s)Open up src.zip in the SDK and look at the implementation of java.util.Collections.reverse().
tschodta at 2007-7-10 0:40:58 > top of Java-index,Java Essentials,Java Programming...
# 8

public Stack reverseStack(final Stack s){

Stack temp = new Stack();

while (! s.empty()){

temp.push(s.pop());

}

return temp;

}

call the method like this

Stack myOriginalStack = new Stack();

// add stuff to your stack

myOriginalStack = reverseStack( myOriginalStack);

Don't forget java forces you to use certain patterns (techniques) that are clearer and easier to debug and modify, that is why there is no true pass by reference in java. It is far better to return a value than to modify it with a pass by reference. The final keyword was added to make sure you don't alter the original copy of the stack (good programming technique).

Message was edited by:

MicroDat

MicroData at 2007-7-10 0:40:58 > top of Java-index,Java Essentials,Java Programming...
# 9
one thing is that my teacher makes me to create the method header:[code]public void reverseStack(Stack s)so how do I do this? why can't I use s = temp at the end
aditya15417a at 2007-7-10 0:40:58 > top of Java-index,Java Essentials,Java Programming...
# 10

It's a common form to use void signatures if the argument is mutated.

Stack extends Vector so is O(1) access, so you can swap the first and last elements, then the second and first from last, and so on until you have reversed the whole Vector:

[0,1,2,3,4]

[4,1,2,3,0]

[4,3,2,1,0]

Pete

pm_kirkhama at 2007-7-10 0:40:58 > top of Java-index,Java Essentials,Java Programming...
# 11
> one thing is that my teacher makes me to create the> method header:> > public void reverseStack(Stack s)Then do as suggested in reply #2.> so how do I do this? why can't I use s = temp at the> endSee reply #1.
prometheuzza at 2007-7-10 0:40:58 > top of Java-index,Java Essentials,Java Programming...
# 12

one more thing I cant use vector because I haven't learned it. The only thing I can do is by creating a new stack and using the pop and push method. But as I posted on my very first post it fails somehow to get s reference to temp at the end. This is the only problem. I also tried to use s.addAll(temp) but somehow it wont give me the reversed stack

aditya15417a at 2007-7-10 0:40:59 > top of Java-index,Java Essentials,Java Programming...
# 13

> one more thing I cant use vector because I haven't

> learned it. The only thing I can do is by creating a

> new stack and using the pop and push method. But as I

> posted on my very first post it fails somehow to get

> s reference to temp at the end. This is the only

> problem. I also tried to use s.addAll(temp) but

> somehow it wont give me the reversed stack

Look at the Stack class: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html, it extends the java.util.Vector class. So if you cannot use the Vector, you can also not make use of the Stack class.

prometheuzza at 2007-7-10 0:40:59 > top of Java-index,Java Essentials,Java Programming...
# 14
Unless you have written your own implementation of a Stack class instead of the java.util.Stack class. In that case, we cannot help you unless we know how that (custom) Stack class looks like.
prometheuzza at 2007-7-10 0:40:59 > top of Java-index,Java Essentials,Java Programming...
# 15
so there's no other way to do this except if I make my own stack class not using the java.util.Stack?
aditya15417a at 2007-7-21 17:54:26 > top of Java-index,Java Essentials,Java Programming...
# 16

> so there's no other way to do this except if I make

> my own stack class not using the java.util.Stack?

If you can't make use of the java.util.Vector class, then you cannot use the java.util.Stack class since a java.util.Stack IS a java.util.Vector. In that case, you'll need to write your own Stack class.

Do you understand what it means that the java.util.Stack class extends the java.util.Vector class?

prometheuzza at 2007-7-21 17:54:26 > top of Java-index,Java Essentials,Java Programming...
# 17

If you use two temporary stacks, you can reverse it in place using only push, pop, and empty:

Stack temp = new Stack();

while (! s.empty()){

temp.push(s.pop());

}

// Now temp is the reverse of s.

Stack temp2 = new Stack();

// TO DO: Make temp2 a reverse of temp.

// That is, temp2 will be a copy of s.

-Fill in code here.--

// Reverse temp2 into the original s.

while (! temp2.empty()){

s.push(temp2.pop());

}

// Now s is the same as temp. That is, s has been reversed.

doremifasollatidoa at 2007-7-21 17:54:26 > top of Java-index,Java Essentials,Java Programming...
# 18

Here's the inheretance tree of the java.util.Stack class:

java.lang.Object

|

+--> java.util.AbstractCollection

|

+--> java.util.AbstractList

|

+--> java.util.Vector

|

+--> java.util.Stackso a Stack is an Object, an AbstractCollection, an AbstractList and a Vector. All the methods defined in those classes can be used by the Stack class. So again, if your teacher says you cannot make use of the java.util.Vector class, then you cannot make use of the java.util.Stack class.

prometheuzza at 2007-7-21 17:54:26 > top of Java-index,Java Essentials,Java Programming...
# 19

This is what I understand from this thread:

1) You have to supply your own Stack;

2) You cannot return anything from your method (i.e. it has to be 'void').

I just assume your stack is just capable of doing this:public class Stack {

public boolean isEmpty() { ... }

public Object pop() { ... }

public void push(Object element) { ... }

}

The following code fragment reverses your stack:private void dump(Stack a, Stack b) {

while (!a.isEmpty()) b.push(a.pop());

}

public void reverse(Stack s) {

Stack t= new Stack();

Stack u= new Stack();

dump(s, t);

dump(t, u);

dump(u, s);

}

... all a bit silly but it works ;-)

kind regards,

Jos

JosAHa at 2007-7-21 17:54:26 > top of Java-index,Java Essentials,Java Programming...
# 20
I like on how you do this, how do I make temp2 as the same stack as s? I cant jus't do temp2 = s right?
aditya15417a at 2007-7-21 17:54:26 > top of Java-index,Java Essentials,Java Programming...
# 21
> I like on how you do this, how do I make temp2 as the> same stack as s? I cant jus't do temp2 = s right?Can Jos and I play outside now?I mean, from here on you only need doremifasollatido's help with this, right?; )
prometheuzza at 2007-7-21 17:54:26 > top of Java-index,Java Essentials,Java Programming...
# 22

so here's my code, but still won't work:

Stack<Object> temp = new Stack<Object>();

Stack<Object> temp2 = new Stack<Object>();

int count = 0;

while (!s.empty()){

temp.push(s.pop());

count++;

}

for (int i = count; i>=0 ; i--){

temp2.push(temp.pop());

}

while(!temp2.empty()){

s.push(temp2.pop());

aditya15417a at 2007-7-21 17:54:26 > top of Java-index,Java Essentials,Java Programming...
# 23

> I like on how you do this, how do I make temp2 as the

> same stack as s? I cant jus't do temp2 = s right?

No you can't; please reread what I wrote:

1) s > t (t contains s reversed)

2) t > u (u contains what s contained before)

3) u > s (s contains what it contained but reversed now)

kind regards,

Jos

JosAHa at 2007-7-21 17:54:26 > top of Java-index,Java Essentials,Java Programming...
# 24

> so here's my code, but still won't work:

>

> ...

Now we can all go and play outside:import java.util.Stack;

public class StackTrouble {

public <T> void reverseStack(Stack<T> s){

Stack<T> temp = new Stack<T>();

temp.addAll(s);

s.clear();

while(!temp.isEmpty()) {

s.push(temp.pop());

}

}

public static void main(String[] args) {

StackTrouble st = new StackTrouble();

Stack<String> stack = new Stack<String>();

stack.push("A");

stack.push("B");

stack.push("C");

System.out.println("Before : "+stack);

st.reverseStack(stack);

System.out.println("After : "+stack);

}

}

prometheuzza at 2007-7-21 17:54:26 > top of Java-index,Java Essentials,Java Programming...
# 25
> Can Jos and I play outside now?No I can't: my mom wants me to stay in, wash my hands and take offthose muddy shoes; it's almost dinner time ;-)kind regards,Jos
JosAHa at 2007-7-21 17:54:26 > top of Java-index,Java Essentials,Java Programming...