Results 1 to 2 of 2
  1. #1
    pyler is offline Member
    Join Date
    Jul 2012
    Posts
    18
    Rep Power
    0

    Default 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
    Java Code:
     LinkedStack<String>stack1 =new LinkedStack<String>();
    and push strings onto it. Then when I do
    Java Code:
    stack1.reversed(); 
    System.out.println(stack1);
    I get an empty stack. Any ideas on how to fix this?

    Java Code:
    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
    Java Code:
    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
    Last edited by pyler; 10-07-2013 at 06:57 PM. Reason: Added code

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Reverse method won't work

    Can you show your LinkedStack class?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. writing a reverse method
    By leoshiner in forum New To Java
    Replies: 1
    Last Post: 10-10-2011, 11:27 PM
  2. Without using reverse method
    By blindfolded916 in forum New To Java
    Replies: 3
    Last Post: 12-08-2010, 06:18 PM
  3. Reverse a string not using the substring method
    By kathyla18 in forum New To Java
    Replies: 17
    Last Post: 04-08-2009, 04:08 AM
  4. how do i reverse this method for sorting?Again!
    By PureAwesomeness in forum New To Java
    Replies: 2
    Last Post: 03-09-2009, 12:51 AM
  5. [SOLVED] how do i reverse this method for sorting?
    By PureAwesomeness in forum New To Java
    Replies: 3
    Last Post: 03-08-2009, 09:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •