title: How does a stack work in a Linked Structure?
body: <p>I understand most of the following code but I am just unclear about how pop method works. I don't understand why when top.getNext() is called it returns the previous node. Same thing up top with the push method. Why is temp.setNext(top) being called and then top is being assigned to temp?</p>
  
  <pre><code>public class Stack {
  
  private Node top;
  
  public Stack()
  {
      top = null;
  }
  
  public void push(int v)
  {
      Node temp = new Node();
      temp.setValue(v);
      temp.setNext(top);
      top = temp;
  }
  
  public int peek()
  {
      return top.getValue();
  }
  
  public int pop()
  {
      int temp = top.getValue();
      top = top.getNext();
      return temp;
  }
  
  public boolean isEmpty()
  {
      return(top == null);
  }
  
  public void makeEmpty()
  {
      top = null;
  }
  
  }
  
  public class Node {
  
  private int value;
  private Node next;
  
  public void setValue(int v)
  {
      value = v;
  }
  
  public int getValue()
  {
      return value;
  }
  
  public void setNext(Node n)
  {
      next = n;
  }
  
  public Node getNext()
  {
      return next;
  }
  
  }
  </code></pre>
  
  <p>The set next code is what is really confusing me. </p>