list - Returning a copy of an object stack in java -


i have stack method that's supposed return reversed *copy* of this object. need this object link that object. thanks.

update

to clarify, that stack object that's created pushes items popped this object. want this object reference that object after this object gets empty. want return reversed copy of this object. clear?

public linkedstack<e> reversed() {     linkedstack<e> that= new linkedstack<e>();     if(this.isempty()){         return this;     }     else{         while(top!=null)             {                 that.push(pop());             }         }     return this;     } 

full class

import java.util.nosuchelementexception; //import java.util.stack; 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> = new linkedstack<e>();     if(this.isempty()){         return this;     }     else{         while(!this.isempty())             {                 that.push(pop());             }         }     return this;     } //returns string representation of stack public string tostring() {     string result="";     node<e> current=upnode;//set current node upnode     while(current !=null)     {//while link isn't null         result=result+(current.getinfo()).tostring()+"\n";//get info , call tostring         current=current.getlink();//get link of current node     }     return result;//return result     } }//end of linkedstack 

change while(top!=null) while(!this.isempty())


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -