1
   public void push(E e)
  {
    list.add(e);
   }
public E pop()
{
    list.remove(list.size()-1);
}
public E peek()
{

}

public boolean empty()
{
   if ( list.size()== 0)
   {
       return false;
   }
   else
   {
       return true;
   }
}

This is part of a driver code my teacher gave me in order to under stand the stack. I understand what each part of the stack does, I am just not understanding how to implement the stack based on this code. I need help with the peek method mainly, but if you see other issues please let me know. I would appreciate the help.

1 Answers1

3
public E peek(){
  if(empty()) return null;

  int top = list.size()-1;
  return list.get(top);
}

AND empty method can be simplifized to:

public boolean empty(){
  return  list.size() == 0;
}

OR

public boolean empty(){
  return  list.isEmpty();
}

AND pop method should throws NoSuchElementException when stack is empty.

public E pop(){
  if(empty()) throw new NoSuchElementException();

  int top = list.size()-1;
  return list.remove(top);
}
holi-java
  • 29,655
  • 7
  • 72
  • 83
  • 1
    @GhostCat ^_^, I just want to helping someone with my ability that need helping . – holi-java Apr 16 '17 at 19:08
  • A person dumping homework here doesn't learn much when you are doing his homework. Or will you be around when he does his exam? It is okay to answer such questions, but that can be done without giving the full solution in code. – GhostCat Apr 16 '17 at 19:10
  • `return list.size() > 0;` to check empty ? – user3437460 Apr 16 '17 at 19:12
  • @GhostCat maybe you are right? He will spent more time to understand it if I'm not to helping him. – holi-java Apr 16 '17 at 19:12
  • @user3437460 sorry, I fixed it. – holi-java Apr 16 '17 at 19:13
  • Well, you never know. Sometimes they spend the time to digest your input, others come back and want everything broken down and explained in detail. Been there myself often enough :-) – GhostCat Apr 16 '17 at 19:14
  • @GhostCat I appreciate you trying to help me better understand I am just confused. I learn better by seeing it implemented and shown to me not by theory. – Jeff Johnson Apr 16 '17 at 19:16
  • @holi-java Thank you for writing it out for me I appreciate it truly – Jeff Johnson Apr 16 '17 at 19:19
  • @JeffJohnson not at all. but you must understand the code by yourself that GhostCat has been mentioned. nobody can helping you to understand it. – holi-java Apr 16 '17 at 19:22
  • No worries. People talking about answers they get usually spend that time to understand it. – GhostCat Apr 16 '17 at 19:27
  • @GhostCat yes, you are right, but this code is so simpler who will does not spent much time to understand it, and I have been made as clearly as I can. – holi-java Apr 16 '17 at 19:30
  • @GhostCat I have posted two new questions. Can you please have a look and answer? I badly need answers. – mallaudin Apr 16 '17 at 19:36
  • Public Holiday here, so probably no time on my side until tomorrow. And for the one question I saw about ssl certificates... No idea in the first place. :-( – GhostCat Apr 17 '17 at 04:46