Reverse method won't work
This method is supposed to return a copy of a this object, reversed. So it creates a new stack called that such that when all the items from this are popped onto that, this will reference that and the reversed() method will return this. When this is returned, it won't be empty because it's a copy of that.
I'm having trouble making a copy of this and linking it to that.

Currently when I run some tests, I get an empty stack for some reason.
Specifically when I create a
LinkedStack<String>stack1 =new LinkedStack<String>();
and push strings onto it. Then when I do

stack1.reversed(); 
System.out.println(stack1);

I get an empty stack. Any ideas on how to fix this?

public LinkedStack<E> reversed()
{
    LinkedStack<E> that= new LinkedStack<E>();
    if(!this.isEmpty()){
        return this;
    }
    else{
        while(this.isEmpty())
            {
            that.push(pop());
            }
        return this;
        }
    }

Here's my LinkedStack class

public class LinkedStack<E>{
    @SuppressWarnings("hiding")
    public class Node<E>{
        private E info;
        private Node<E> link;
        public Node(E info,Node<E>link){
            this.info=info;
            this.link=link;
        }//Node constructor
    public void Setinfo(E info){this.info =info;}

    public E getinfo(){return info;}

    public void setLink(Node<E> newLink){this.link=newLink;}

    public Node<E> getLink(){return this.link;}
}//end of node

protected Node<E> upnode;
public LinkedStack(){
    upnode=null;
}

//isEmpty method
public boolean isEmpty(){
    if(upnode==null){
        return true;
    }
    else
        return false;
}
//item push
public void push(E item)
{
    Node<E> sth=new Node<E>(item,upnode);
    sth.setLink(upnode);
    upnode=sth;
}
//LinkedStack push
public void push(LinkedStack<E> s)
{
    if(s.isEmpty()==true)
    {
        throw new NoSuchElementException();
    }
    else{
        while(!(s.isEmpty()))
        {
            this.push(s.pop());
        }

    }
}
//peek method
public E peek()
    {
    if(upnode==null){
        throw new NoSuchElementException();
        }
    else
        return upnode.getinfo();
    }
//pop method
public E pop()
{
    if(upnode==null){
        throw new NoSuchElementException();
    }
    else{
        E item=peek();
        upnode=upnode.link;
        return item;
    }
}

public int size(){
int ct=0;
if(this.isEmpty()==true){
    throw new NoSuchElementException();
}
else{
    while(this.isEmpty()==false){
        ct++;
        upnode=upnode.getLink();
        }
    }
return ct;
}

//Reverse method
public LinkedStack<E> reversed()
{
    LinkedStack<E> that = new LinkedStack<E>();
    if(this.isEmpty()){
        return this;
    }
    else{
        while(!this.isEmpty())
            {
                that.push(pop());
            }
        }
    return this;
    }
//Returns a string representation of this stack
public String toString()
{
    String result="";
    Node<E> current=upnode;//set the current node to upnode
    while(current !=null)
    {//while link isn't null
        result=result+(current.getinfo()).toString()+"\n";//get info and call toString
        current=current.getLink();//Get the link of the current node
    }
    return result;//return result
    }
}//end of LinkedStack

Let's look at reversed() real quick here...

if(!this.isEmpty()){
    return this;
}

"If I'm not empty, just return myself"--probably not what you meant.

Seems like it should be the other way around: "If I'm empty, return myself." But that's problematic too... it's a reference to the same object, which again I don't think you mean. I'd pass a new empty stack.

while(this.isEmpty())

"Only build the stack if I'm empty"--also probably not what you meant.

return this;

Wait, it looks like you're building that and then ignoring it completely. That can't be right. You must have meant to return that, right?

that.push(pop());

Combined with the above, this is why you're getting an empty stack. The call to pop() is emptying out the current stack, and then you're returning it.

So:

First, fix up how you're building that and return it, not the original object.

Second, figure out how to actually copy the contents of this without actually removing them. You should be able to follow the nodes directly, starting with upnode, and push them until there aren't any left.

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.