_TruELecter_

Untitled

Dec 18th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.NoSuchElementException;
  3.  
  4. public class Stack<E> implements Iterable<E>{
  5.     private E[] arr;
  6.     private int top = -1; // highest element id
  7.  
  8.     public Stack(int cap) {
  9.         // Create array
  10.         this.arr = (E[]) new Object[cap];
  11.     }
  12.  
  13.     public E pop() {
  14.         // If no elements yet - throw exception
  15.         if (top < 0) {
  16.             throw new NoSuchElementException();
  17.         }
  18.  
  19.         E result = arr[top];
  20.         arr[top] = null; // Allow garbage collector clearing
  21.         top--; // decrease size
  22.  
  23.         // return element
  24.         return result;
  25.     }
  26.  
  27.     public void push(E e) {
  28.         // If stack is full - throw exception
  29.         if (top == arr.length) {
  30.             throw new ArrayIndexOutOfBoundsException();
  31.         }
  32.         top += 1; // increase size
  33.         arr[top] = e; // set element
  34.     }
  35.  
  36.     public String toString() {
  37.         if (top < 0) {
  38.             return "[]";
  39.         }
  40.  
  41.         StringBuilder sb = new StringBuilder();
  42.         sb.append("[");
  43.  
  44.         for (int i = 0; i < top; i++) {
  45.             sb.append(this.arr[i]).append(", ");
  46.         }
  47.  
  48.         return sb.append(this.arr[top]).append("]").toString();
  49.     }
  50.  
  51.     public static void main(String[] args) {
  52.  
  53.         Stack<String> stack = new Stack<>(11);
  54.         stack.push("hello");
  55.         stack.push("world");
  56.  
  57.         System.out.println(stack);
  58.  
  59.         stack.pop();
  60.         System.out.println(stack);
  61.  
  62.         stack.pop();
  63.         System.out.println(stack);
  64.  
  65.         Stack<Integer> intStack = new Stack<>(11);
  66.         intStack.push(1);
  67.         intStack.push(2);
  68.         System.out.println(intStack);
  69.  
  70.         for (Integer i: intStack) {
  71.             System.out.println(i);
  72.         }
  73.  
  74.         intStack.pop();
  75.         System.out.println(intStack);
  76.  
  77.         intStack.pop();
  78.         System.out.println(intStack);
  79.  
  80.         for (Integer i: intStack) {
  81.             System.out.println(i);
  82.         }
  83.     }
  84.  
  85.     @Override
  86.     public Iterator<E> iterator() {
  87.         return new StackIterator(this);
  88.     }
  89.  
  90.     private class StackIterator implements Iterator<E> {
  91.         private Stack<E> thisStack;
  92.         private int position;
  93.  
  94.         private StackIterator(Stack<E> thisStack) {
  95.             this.thisStack = thisStack;
  96.             position = 0;
  97.         }
  98.  
  99.         @Override
  100.         public boolean hasNext() {
  101.             return position <= thisStack.top;
  102.         }
  103.  
  104.         @Override
  105.         public E next() {
  106.             position++;
  107.             return thisStack.arr[position - 1];
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment