Method returns empty stack when called
I have a stack method that's supposed to return a reversed *copy* of the this object. I need this object to link to that object. Thanks.
To clarify, the that stack object that's created pushes items that are popped from the this object. I want the this object to reference the that object after the this object gets empty. What I really want is to return a reversed copy of the this object. Clear?
Code :
public LinkedStack<E> reversed()
{
LinkedStack<E> that= new LinkedStack<E>();
if(this.isEmpty()){
return this;
}
else{
while(top!=null)
{
that.push(pop());
}
}
return this;
}
Currently when I create a LinkedStack object, say stack1 and push items onto it, stack1.reverse() doesn't return a reversed copy of stack1. It returns an empty stack instead.
What I'm looking for is for a way to reference that in the reverse method.
Re: Method returns empty stack when called
Hello.
Obviously it will return empty stack as per your logic.
Think if you need to return this or that.
Syed.
Re: Method returns empty stack when called
I want it to return a copy of the this object but with the contents in reverse order. I want to link this to that after all the items from this have been popped, but I can't find the right keyword.
Re: Method returns empty stack when called
Do you understand why your implementation empties the object? If you do not, then my advice would be to stop and spend some time understanding why. Do you know how to add them back? Have you tried reversing the underlying data structure which contains the E elements (which you haven't told us about)?
Re: Method returns empty stack when called
that.push(pop());
Doesn't that empty your original stack in the process?
I cannot think of any way using only Stack methods to create a reverse Stack that doesn't destroy the original one. (There might be a way, but I just can't think of one.)
Assuming, per your title of the class that you're using a LinkedList, not sure if it's a doubly linked list or a singly linked list or if you're using java's LinkedList class or one of your own, there is a way to get the end of the list, or the bottom.
You'd have to clone your first Stack. Then, using its LinkedList setup, you'd have to use getFirst() and getLast() and removeFirst() and removeLast() and addFirst() and addLast() through the list till you reach the middle, at which point it will have been flipped around.
Re: Method returns empty stack when called
Double posted here thread locked due to it being in a different post as well
As I had asked in that post how are you calling your method? Not only that you are now returning an empty stack and not a newly created stack which has reversed elements in it.
Think of calling this method as baseball.
1. You pitch the ball (Call the method)
2. The batter hits the ball (Method returns something)
3. Someone catches the ball (Value is assigned to something)
If you are missing one then you won't have your desired results. The reason I am guessing your other version wasn't working was because you were missing step number 3. You were probably just calling Object.reverse() which it was returning the ball but there was no catcher so the ball got lost and you were left with the original stack you called .pop() on.