Advertisement
Guest User

My Stack Implementation

a guest
Nov 17th, 2012
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1.     package uk.ac.shef.dcs.com262.coa10pjh;
  2.  
  3.     import java.util.Iterator;
  4.     import java.util.NoSuchElementException;
  5.  
  6.     public class myStack<Item> implements Iterable<Item> {
  7.         private int N;          // size of the stack
  8.         private Node first;     // top of stack
  9.  
  10.         private class Node {
  11.             private Item item;
  12.             private Node next;
  13.         }
  14.  
  15.         /**
  16.          * Create an empty stack.
  17.          */
  18.         public myStack() {
  19.             first = null;
  20.             N = 0;
  21.             assert check();
  22.         }
  23.  
  24.         public boolean isEmpty() {
  25.             return first == null;
  26.         }
  27.  
  28.         public int size() {
  29.             return N;
  30.         }
  31.  
  32.         public void push(Item item) {
  33.             Node oldfirst = first;
  34.             first = new Node();
  35.             first.item = item;
  36.             first.next = oldfirst;
  37.             N++;
  38.             assert check();
  39.         }
  40.  
  41.         public Item pop() {
  42.             if (isEmpty()) throw new NoSuchElementException("Stack underflow");
  43.             Item item = first.item;        // save item to return
  44.             first = first.next;            // delete first node
  45.             N--;
  46.             assert check();
  47.             return item;                   // return the saved item
  48.         }
  49.  
  50.         public Item peek() {
  51.             if (isEmpty()) throw new NoSuchElementException("Stack underflow");
  52.             return first.item;
  53.         }
  54.  
  55.         public String toString() {
  56.             StringBuilder s = new StringBuilder();
  57.             for (Item item : this)
  58.                 s.append(item + " ");
  59.             return s.toString();
  60.         }
  61.  
  62.         // check internal invariants
  63.         private boolean check() {
  64.             if (N == 0) {
  65.                 if (first != null) return false;
  66.             }
  67.             else if (N == 1) {
  68.                 if (first == null)      return false;
  69.                 if (first.next != null) return false;
  70.             }
  71.             else {
  72.                 if (first.next == null) return false;
  73.             }
  74.  
  75.             // check internal consistency of instance variable N
  76.             int numberOfNodes = 0;
  77.             for (Node x = first; x != null; x = x.next) {
  78.                 numberOfNodes++;
  79.             }
  80.             if (numberOfNodes != N) return false;
  81.  
  82.             return true;
  83.         }
  84.  
  85.         public Iterator<Item> iterator()  { return new ListIterator();  }
  86.  
  87.         // an iterator, doesn't implement remove() since it's optional
  88.         private class ListIterator implements Iterator<Item> {
  89.             private Node current = first;
  90.             public boolean hasNext()  { return current != null;                     }
  91.             public void remove()      { throw new UnsupportedOperationException();  }
  92.  
  93.             public Item next() {
  94.                 if (!hasNext()) throw new NoSuchElementException();
  95.                 Item item = current.item;
  96.                 current = current.next;
  97.                 return item;
  98.             }
  99.         }
  100.     }
  101.  
  102. Thanks.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement