java - Showing contents of a LinkedStack without removing them -


i beginner in java , working nodes. wondering if there way show content of list without having use .getnext method, because once use it, removes element on node literally removes node on top. trying in code using input store 2 string elements in new 2 nodes using method provetitle prove elements on list. once make sure elements still intact , use tostring method check list. note in class of book strange reason t isnt showing besides class , implemented class if put <> around it.

heres code:

mynode class:

public class mynode<t>  {  private t data; private mynode next;  public mynode(t _data) { data = _data; } public mynode(t _data, mynode _next) { data = _data; next = _next; }  public t getdata() { return data;     }  public void setdata(t _data) { data = _data;     }  public mynode getnext() {  return next;     }  public void setnext(mynode _next) {  next = _next;    }  } 

interface class:

public interface myinterface<t>  {    public void pushtitle(t data);    public t pop();    public t peek();    public string tostring();    public boolean isempty();    public int size();    public mynode getnode(); } 

book class, contains methods

public class book implements myinterface

{  private int count;  private t author;  private t title;  private int stock;   private mynode<t> top;   public book()  {      count = 0;      top = null;  }  @override  public mynode getnode()  {  return top;  }    @override  public void pushtitle(t title)  {  mynode<t> current = new mynode<>(title, top);  current.setnext(top);  top = current;  count++;  }   public void provetitle(t title)  {   t result;    mynode<t> current = top;       if(title.equals(current.getdata()))   {   result = current.getdata();   system.out.println("the title " + "'" + result + "'" + " exist.");    top = top.getnext();     }  }   @override  public t pop()  {  t result;  if(count == 0 || top == null )  {      system.out.println("list empty");  }  system.out.println("the element on top is:"  +  top.getdata());  result = top.getdata();  top = top.getnext();   count--;  return result;   }  @override   public t peek()  {      system.out.println("element on top is: " + top.getdata());      return top.getdata();  }  @override  public boolean isempty()  {      if(top == null)      {      system.out.println("the list empty");      }      else      {        system.out.println("the list not empty." + "it has" + count + "elements");          }    return top == null;     }  @override  public int size()  {      system.out.println("the size of list is" + count);  return count;      }  @override  public string tostring()  {   string result = "";   mynode current = top;   system.out.println("top");   while(current != null)   {   result += ("[" + current.getdata() + "]\n");   current = current.getnext();   }   return result + "bottom";  }  } 

main class:

package node; import java.util.scanner;  public class mydriver  {     public static void main(string[]args)     {     scanner input = new scanner(system.in);        book<string> title = new book<>();        mynode<string> current;     current = title.getnode();     string push;     string push2;      system.out.println("enter title of book 1");     push = input.nextline();     title.pushtitle(push);      system.out.println("enter title of book 2");     push2 = input.nextline();     title.pushtitle(push2);      title.provetitle(push);     title.provetitle(push2);       system.out.println(title.tostring());       } } 

output:

run: enter title of book 1 tiger enter title of book 2 crossed title 'crossed' exist. top [tiger] bottom build successful (total time: 7 seconds) 

you seem trying implement list using stack. these different data structures. trying easy in list. in stack requires have second stack. pop each object off current stack, push onto second stack.

if order doesn't matter, can once , switch using second stack. if order matter, have reverse process original stack.

also note better approach use list.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -