linked list - Writing my own peek() method for my own LinkedList class in Java -


so i've been asked write peek() method linked list in java. thing object want "peek" private variable of type base exists in private class within linkedlist class. use peek() method return object , print out. know has accessing private variable can't quite make work have. here snippet of code:

class linkedstack <base> {     private class run     {         private base object;         private run next;         private run (base object, run next)         {             this.object = object;             this.next = next;          }     }     ...     public base peek()     {         if(isempty())         {             throw new illegalstateexception("list empty");         }         return object; //this throws error     }     ...     public void push(base object)     {         top = new run(object, top);     } } class driver {     public static void main(string []args)     {         linkedstack<string> s = new linkedstack<string>();         s.push("a");          system.out.println(s.peek());     } } 

thanks in advance help! appreciate it.

you should return top variable. don't see initialized, assume it's class variable since don't initialize in push method. do:

public base peek() {      if(isempty())      {          throw new illegalstateexception("list empty");      }     return top.object; //this throws error  } 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -