I have a generic LinkedStack class with a LinkedStack reversed method It creates a new LinkedStack say newLinkedstack and pushes items from the current stack by popping them. So at the end of the operation the current stack is left empty and newLinkedStack contains the same items/elements that were in "this" stack only in reverse order.

My code compiles but when I create and fill up a stack and then print out the contents, I only get one result.

public LinkedStack<T> rev()
{
LinkedStack<T> revStack=new LinkedStack<T>();
final int SIZE=this.size();

for(int i=0;i<SIZE;i++)
{
  revStack.push(this.pop());
 } 
return revStack;}

For example

LinkedStack<String> newLS = new LinkedStack<String>();
newLS.push("noid");
newLS.push("enilec");
newLS.push("River");
newLS.push("Healing");
newLS.push("Pain");

and newLS.rev() returns Pain instead of a reversed stack

you do understand that there are quite some possible reasons what might be going wrong, but that with only that snippet, chance is small we will find what it is?
for instance: what does your pop method do? what is the value of the SIZE variable?

newLS.rev() returns Pain instead of a reversed stack

On the basis of what you have posted... no, it doesn't!
The definition of the rev method above shows a return type of LinkedStack<T>, so there is zero possibility that it returns a value of "Pain". There is obviously something else going on here that you have misunderstood. We need more details to help any further

Here are my other methods

public T pop()
{
    T element=peek();
    top=top.next;
    return element;
}

I have two push methods this one moves every element from input stack to current stack leaving input stack from argument empty

  public void push(LinkedStack<T> stack){ 
    final int SZE = s.size();
    for(int i=0;i<SZE;i++){
        this.push(stack.pop());}}

Here's my other push method

public void push(T element)
{
   top=new Node<T>(item,top);
}

Also here's my node class
(it's nested in my LinkedStack class)

public class Node<T>{
private T data;
private Node<T> link;
public Node(T data,Node<T>link){
this.data=data;
this.link=link;}}

It's still iompossible for the rev() method to return a String. What's the result that makes you think it does?

final int SIZE=this.size();

can you explain this part a little ?

Looks like a bug to me... shouldn't it be stack.size() - the number of items to pop off stack and push onto this?

Although for clarity and safety I would prefer to see something like

while (stack.size() > 0) {
   push(stack.pop());
} 
Yes that's true. I meant to write stack.size() instead of s.size()

I'm no pro with stacks and nodes and such but this.size(); seems to lead to nothing. Same goes for s.size() in the push method. I'd start there.

I agree with @stuugie , that part doesn't make much sense to me.

I want reverse method to return a copy of the this object by referencing the reverseStack() object in the rev() method of LinkedStack

yes, I think they understand that, it's just the code you've shown us we don't get.
also: is this about the same problem as your other thread?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.