Advertisement
MovehMakeh

Untitled

Mar 3rd, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. package myUtil;
  2.  
  3. public class LStack<T> extends java.lang.Object implements Stack<T>
  4. {
  5.     private Node<T> top = null;
  6.    
  7.     public LStack(){
  8.        
  9.     }
  10.    
  11.     public T push(T item){
  12.         top = new Node(item, top);
  13.         return item;
  14.     }
  15.     public T pop(){
  16.         T removed = top.getData();
  17.         top.setNext(top.getNext());
  18.         return removed;
  19.     }
  20.     public T peek(){
  21.         return this.top.getData();
  22.     }
  23.     public boolean empty(){
  24.         if (this.size() == 0){
  25.             return true;
  26.         }else{
  27.             return false;
  28.         }
  29.     }
  30.     public int size(){
  31.         if (top == null){
  32.             return 0;
  33.         }
  34.           int count = 1;
  35.           Node<T> next = top;
  36.           while (next.hasNext()){
  37.             count++;
  38.             next = next.getNext();
  39.           }
  40.           return count;
  41.         }
  42.     }
  43.     class Node<T> {
  44.         private T data;
  45.         private Node<T> next;
  46.  
  47.         public Node(T data, Node<T> next) {
  48.             this.data = data;
  49.             this.next = next;
  50.         }
  51.        
  52.         public boolean hasNext() {
  53.             return this.next != null;
  54.         }
  55.        
  56.         public Node<T> getNext() {
  57.             return this.next;
  58.         }
  59.        
  60.         public void setNext(Node<T> next) {
  61.             this.next = next;
  62.         }
  63.        
  64.         public T getData() {
  65.             return this.data;
  66.         }
  67.        
  68.         public void setData(T data) {
  69.             this.data = data;
  70.         }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement