SHARE
TWEET

generics circle queue

a guest Oct 14th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Stack<T> {
  2.  
  3.     private int size = 0;
  4.     private int left = 0;
  5.     private int right = 0;
  6.  
  7.     private T[] elements;
  8.  
  9.     @SuppressWarnings("unchecked")
  10.     private Stack() {
  11.         elements = (T[]) new Object[16];
  12.     }
  13.  
  14.     @SuppressWarnings("unchecked")
  15.     private Stack(int changedSize) {
  16.         elements = (T[]) new Object[changedSize];
  17.     }
  18.  
  19.     private int add(int x) {
  20.         return (x + 1) % elements.length;
  21.     }
  22.  
  23.     private int dec(int x) {
  24.         if (x == 0) {
  25.             return elements.length - 1;
  26.         } else {
  27.             return x - 1;
  28.         }
  29.     }
  30.  
  31.     @SuppressWarnings("unchecked")
  32.     private void ensureCapacity(int changedSize) {
  33.         if (elements.length <= changedSize) {
  34.             System.out.println("Size = " + size + " , and ensureCapacity");
  35.  
  36.             T[] temp = (T[]) new Object[2 * changedSize];
  37.             int index = 0;
  38.             for (int i = left; i < right; i = add(i)) {
  39.                 temp[index++] = elements[i];
  40.             }
  41.             left = 0;
  42.             right = index;
  43.             elements = temp;
  44.         }
  45.     }
  46.  
  47.     public void enqueue(T elem) {
  48.         assert elem != null;
  49.         ensureCapacity(++size);
  50.         elements[right] = elem;
  51.         right = add(right);
  52.     }
  53.  
  54.     public T element() {
  55.         assert size > 0;
  56.         return elements[left];
  57.     }
  58.  
  59.     public T peek() {
  60.         assert size > 0;
  61.         return elements[dec(right)];
  62.     }
  63.  
  64.     public T dequeue() {
  65.         assert size > 0;
  66.         T ans = elements[left];
  67.         elements[left] = null;
  68.         left = add(left);
  69.         ensureCapacity(--size);
  70.         return ans;
  71.     }
  72.  
  73.     public int size() {
  74.         return size;
  75.     }
  76.  
  77.     public boolean isEmpty() {
  78.         return size == 0;
  79.     }
  80.  
  81.     public void clear() {
  82.         size = left = right = 0;
  83.     }
  84.  
  85.     public static void main(String[] args) {
  86.         Stack<Integer> stack = new Stack<>(7);
  87.         System.out.println(stack.isEmpty());
  88.         for (int i = 1; i <= 17; i++) {
  89.             stack.enqueue(i);
  90.         }
  91.         System.out.println(stack.size());
  92.         System.out.println(stack.isEmpty());
  93.         stack.clear();
  94.         System.out.println(stack.peek());
  95.     }
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top