idastan97

CSCI152 L9 P1

Feb 11th, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.84 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////// Generic Stack ADT
  2. /**
  3.  *
  4.  * @author Dastan Iyembergen
  5.  * @param <T>
  6.  */
  7. public interface Stack<T> {
  8.    
  9.     /**
  10.      * Puts the given element on the top of the stack.
  11.      *
  12.      * @param value element to be added on the top of the stack
  13.      */
  14.     public void push(T value);
  15.    
  16.     /**
  17.      * Removes and returns the top most element of the stack
  18.      *
  19.      * @return the top most element of the stack
  20.      * @throws Exception if the stack is empty
  21.      */
  22.     public T pop() throws Exception;
  23.        
  24.     /**
  25.      * @return the size of the stack
  26.      */
  27.     public int getSize();
  28.    
  29.     /**
  30.      * Removes all elements from the stack
  31.      */
  32.     public void clear();
  33.        
  34.     /**
  35.      * @return a String representation of the stack
  36.      */
  37.     @Override
  38.     public String toString();
  39. }
  40.  
  41. //////////////////////////////////////////////////////////////////////////// Generic Stack Impl
  42. /**
  43.  *
  44.  * @author Dastan Iyembergen
  45.  * @param <T>
  46.  */
  47. public class ArrayStack<T> implements Stack<T>{
  48.     private T[] values;
  49.     private int size;
  50.    
  51.     public ArrayStack(){
  52.         values=(T[]) new Object[10];
  53.         size=0;
  54.     }
  55.    
  56.     @Override
  57.     public void push(T value) {
  58.         if (size==values.length){
  59.             T[] tmpArr=(T[]) new Object[2*values.length];
  60.             for (int i=0; i<values.length; i++)
  61.                 tmpArr[i]=values[i];
  62.             values=tmpArr;
  63.         }
  64.         values[size++]=value;
  65.     }
  66.  
  67.     @Override
  68.     public T pop() throws Exception {
  69.         if (size<=0){
  70.             throw new Exception("The stack is empty.");
  71.         }
  72.         T res=values[size-1];
  73.         values[size-1]=null;
  74.         size--;
  75.         return res;
  76.     }
  77.  
  78.     @Override
  79.     public int getSize() {
  80.         return size;
  81.     }
  82.  
  83.     @Override
  84.     public void clear() {
  85.         values=(T[]) new Object[10];
  86.         size=0;
  87.     }
  88.    
  89.     @Override
  90.     public String toString(){
  91.         String list="bottom[";
  92.         for (int i=0; i<size; i++){
  93.             list+=values[i];
  94.             if (i<size-1)
  95.             list+=", ";
  96.         }
  97.         return list+"]top";
  98.     }
  99. }
  100.  
  101. //////////////////////////////////////////////////////////////////////////// Generic Queue ADT
  102. /**
  103.  *
  104.  * @author Dastan Iyembergen
  105.  * @param <T>
  106.  */
  107. public interface Queue<T> {
  108.    
  109.     /**
  110.      * Adds an element to the end of the queue.
  111.      *
  112.      * @param value element to be added to the end of the queue
  113.      */
  114.     public void enqueue(T value);
  115.    
  116.     /**
  117.      * Removes and returns the front most element of the queue
  118.      *
  119.      * @return the front most element of the queue
  120.      * @throws Exception if the queue is empty
  121.      */
  122.     public T dequeue() throws Exception;
  123.        
  124.     /**
  125.      * @return the size of the queue
  126.      */
  127.     public int getSize();
  128.    
  129.     /**
  130.      * Removes all elements from the queue
  131.      */
  132.     public void clear();
  133.    
  134.     /**
  135.      * @return a String representation of the queue
  136.      */
  137.     @Override
  138.     public String toString();
  139. }
  140.  
  141. //////////////////////////////////////////////////////////////////////////// Generic Queue Impl
  142. /**
  143.  *
  144.  * @author Dastan Iyembergen
  145.  * @param <T>
  146.  */
  147. public class ArrayQueue<T> implements Queue<T> {
  148.    
  149.     private T[] values;
  150.     private int size;
  151.     private int front;
  152.     private int back;
  153.    
  154.     public ArrayQueue(){
  155.         values=(T[]) new Object[10];
  156.         size=0;
  157.         front=0;
  158.         back=9;
  159.     }
  160.    
  161.     @Override
  162.     public void enqueue(T value) {
  163.         if (size==values.length){
  164.             int i=front, j=0;
  165.             T[] tmp=(T[]) new Object[2*values.length];
  166.             do {
  167.                 tmp[j++]=values[i++];
  168.                 i%=values.length;
  169.             } while (i!=front);
  170.             values=tmp;
  171.             front=0;
  172.             back=j-1;
  173.         }
  174.         back=(++back)%values.length;
  175.         values[back]=value;
  176.         size++;
  177.     }
  178.  
  179.     @Override
  180.     public T dequeue() throws Exception {
  181.         if (size==0){
  182.             throw new Exception("The queue is empty.");
  183.         }
  184.         T result=values[front];
  185.         values[front++]=null;
  186.         front%=values.length;
  187.         size--;
  188.         return result;
  189.     }
  190.  
  191.     @Override
  192.     public int getSize() {
  193.         return size;
  194.     }
  195.  
  196.     @Override
  197.     public void clear() {
  198.         values=(T[]) new Object[10];
  199.         size=0;
  200.         front=0;
  201.         back=9;
  202.     }
  203.    
  204.     @Override
  205.     public String toString(){
  206.         String res="front[";
  207.         int i=front, j=0, c=0;
  208.         while (c<size) {
  209.             res+=values[i++];
  210.             i%=values.length;
  211.             if (c<size-1){
  212.                 res+=", ";
  213.             }
  214.             c++;
  215.         }
  216.         res+="]back";
  217.         return res;
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment