Guest User

Untitled

a guest
Apr 26th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class GenericStack {
  4.  
  5.     private Node <T> top;
  6.  
  7.     private class Node<T> {
  8.  
  9.         private T data;
  10.         private Node<T> next;
  11.  
  12.         public Node(T item) {
  13.             data = item;
  14.             next = null;
  15.         }
  16.  
  17.     }
  18.  
  19.     public GenericStack () {
  20.         top = null;
  21.     }
  22.  
  23.     public boolean isEmpty() {
  24.         return top = null;
  25.     }
  26.  
  27.     public T top() {
  28.         return top.data;
  29.     }
  30.  
  31.     public void push(t item) {
  32.         Node<T> n = new Node<T>(item);
  33.  
  34.         n.next = top;
  35.         top = n;
  36.  
  37.     }
  38.  
  39.     public T pop() throws EmptyStackException{
  40.  
  41.         if ( top == null )
  42.             throw new EmptyStackException();
  43.  
  44.          T n = top.data;
  45.         top = top.next;
  46.  
  47.         return n;
  48.     }
  49. }
Add Comment
Please, Sign In to add comment