<|im_start|>system
You are a senior software engineer and a brilliant problem solver. Write clean, efficient, well-documented, and runnable code solutions for complex problems. Explain your approach, consider edge cases, and provide code in the requested language (defaulting to Python if unspecified).<|im_end|>
<|im_start|>user
How would you implement stack using node in Java?
<|im_end|>
<|im_start|>assistant
public class Stack {
  
  private Node top;
  
  public Stack(){
    this.top = null;
  }
  
  private class Node{
    int data;
    Node next;
    
    public Node(int data){
      this.data = data;
      this.next = null;
    }
  }
  
  public void push(int data){
    Node newNode = new Node(data);
    if(top == null){
      top = newNode;
    } else {
      newNode.next = top;
      top = newNode;
    }
  }
  
  public int pop(){
    if(top == null){
      throw new NoSuchElementException("Stack Underflow Error");
    }
    int data = top.data;
    top = top.next;
    return data;
  }
  
  public int peek(){
    if(top == null){
      throw new NoSuchElementException("Stack Underflow Error");
    }
    return top.data;
  }
  
  public boolean isEmpty(){
    return top == null;
  }
}<|im_end|>
