Advertisement
Guest User

4.1

a guest
Dec 14th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. class ArrayStack {
  2.   private Object[] elements;
  3.   private int size;
  4.   private int capacity;
  5.  
  6.   /**
  7.    * Creates an empty ArrayStack with capacity 1.
  8.    */
  9.   public ArrayStack() {
  10.     capacity = 1;
  11.     size = 0;
  12.     elements = new Object[1];
  13.   }
  14.  
  15.   /**
  16.    * @return The size of this ArrayStack.
  17.    */
  18.   public int size() {
  19.     return size;
  20.   }
  21.  
  22.   /**
  23.    * @return `true` iff this ArrayStack is empty, `false` otherwise.
  24.    */
  25.   public boolean isEmpty() {
  26.     if(size == 0){
  27.       return true;
  28.     }else{
  29.       return false;
  30.     }
  31.   }
  32.  
  33.   /**
  34.    * @return `true` iff the size is equal to the capacity, `false` otherwise.
  35.    */
  36.   public boolean isFull() {
  37.     if(size == capacity){
  38.       return true;
  39.     }else{
  40.       return false;
  41.     }
  42.   }
  43.  
  44.   /**
  45.    * @return the top element of the stack without removing it
  46.    */
  47.   public Object peek() throws EmptyStackException {
  48.     if(capacity == 0 || size == 0){
  49.       throw new EmptyStackException();
  50.     }else{
  51.       return elements[size - 1];
  52.     }
  53.   }
  54.  
  55.   /**
  56.    * Adds `o` to the stack.
  57.    * If capacity of stack was too small, capacity is doubled and `o` is added.
  58.    *
  59.    * @param o
  60.    *     the element to add to the stack.
  61.    */
  62.   public void push(Object o) {
  63.     if(isFull()){
  64.       int capacitynew = 2 * capacity;
  65.      
  66.       Object[] temp = new Object[capacity];
  67.       for(int i = 0; i < elements.length; i++){
  68.         temp [i] = elements[i];
  69.       }
  70.      
  71.       elements = new Object[capacitynew];
  72.       for(int j = 0; j < temp.length; j++){
  73.         elements[j] = temp[j];
  74.       }
  75.      
  76.       capacity = capacitynew;
  77.      
  78.     }
  79.     elements[size] = o;
  80.     size++;
  81.   }
  82.  
  83.   /**
  84.    * Removes the top element from the stack.
  85.    * If removing top would make the stack use less than 25% of its capacity,
  86.    * then the capacity is halved.
  87.    *
  88.    * @return the element which was at the top of the stack.
  89.    * @throws EmptyStackException
  90.    *     iff the queue is empty
  91.    */
  92.   public Object pop() throws EmptyStackException {
  93.     if(size == 0){
  94.       throw new EmptyStackException();
  95.     }else{
  96.       Object e = elements[size - 1];
  97.       elements[size - 1] = null;
  98.       size--;
  99.       if(size < (capacity / 4)){
  100.         int newcapacity = capacity/2;
  101.        
  102.         Object[] temp = new Object[capacity];
  103.         for(int i = 0; i < size; i++){
  104.           temp [i] = elements[i];
  105.         }
  106.        
  107.         elements = new Object[newcapacity];
  108.         for(int j = 0; j < size; j++){
  109.           elements[j] = temp[j];
  110.         }
  111.        
  112.         capacity = newcapacity;
  113.        
  114.        
  115.        
  116.       }
  117.      
  118.       return e;
  119.    }
  120.   }
  121.  
  122.   /**
  123.    * @return a String representation of the ArrayStack
  124.    * Example output for ArrayStack with 2 elements and capacity 5:
  125.    * <ArrayStack[1,2]>(Size=2, Cap=5)
  126.    */
  127.   public String toString() {
  128.     if(size == 0){
  129.       return "<ArrayStack[]>(Size=0, Cap=" + capacity + ")";
  130.     }
  131.      String begin = "<ArrayStack[";
  132.      String result = begin;
  133.      for(int i = 0; i < size; i++){
  134.        if(i == size -1){
  135.          result += elements[i] + "]>";
  136.        }else{
  137.          result += elements[i] + ",";
  138.        }
  139.        
  140.      }
  141.      
  142.      result += "(Size=" + size + ", Cap=" + capacity + ")";
  143.      return result;
  144.   }
  145.  
  146.   // For testing, do not remove or change.
  147.   public Object[] getElements() {
  148.     return elements;
  149.   }
  150. }
  151. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement