idastan97

CSCI152 L7 P1

Feb 6th, 2017
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////// IntQueue ADT (interface)
  2. public interface IntQueue {
  3.    
  4.     /**
  5.      * Adds an element to the end of the queue.
  6.      *
  7.      * @param value element to be added to the end of the queue
  8.      */
  9.     public void enqueue(int value);
  10.    
  11.     /**
  12.      * Removes and returns the front most element of the queue
  13.      *
  14.      * @return the front most element of the queue
  15.      * @throws Exception if the queue is empty
  16.      */
  17.     public int dequeue() throws Exception;
  18.        
  19.     /**
  20.      * @return the size of the queue
  21.      */
  22.     public int getSize();
  23.    
  24.     /**
  25.      * Removes all elements from the queue
  26.      */
  27.     public void clear();
  28.    
  29.     /**
  30.      * @return a String representation of the queue
  31.      */
  32.     @Override
  33.     public String toString();
  34. }
  35.  
  36. /////////////////////////////////////////////////////////////////////////// ArrayIntQueue Implementation
  37. public class ArrayIntQueue implements IntQueue{
  38.    
  39.     private int[] values;
  40.     private int size;
  41.     private int front;
  42.     private int back;
  43.    
  44.     public ArrayIntQueue(){
  45.         values=new int[10];
  46.         size=0;
  47.         front=0;
  48.         back=9;
  49.     }
  50.    
  51.     @Override
  52.     public void enqueue(int value) {
  53.         if (size==values.length){
  54.             int i=front, j=0;
  55.             int[] tmp=new int[2*values.length];
  56.             do {
  57.                 tmp[j++]=values[i++];
  58.                 i%=values.length;
  59.             } while (i!=front);
  60.             values=tmp;
  61.             front=0;
  62.             back=j-1;
  63.         }
  64.         back=(++back)%values.length;
  65.         values[back]=value;
  66.         size++;
  67.     }
  68.  
  69.     @Override
  70.     public int dequeue() throws Exception {
  71.         if (size==0){
  72.             throw new Exception("The queue is empty.");
  73.         }
  74.         int result=values[front++];
  75.         front%=values.length;
  76.         size--;
  77.         return result;
  78.     }
  79.  
  80.     @Override
  81.     public int getSize() {
  82.         return size;
  83.     }
  84.  
  85.     @Override
  86.     public void clear() {
  87.         values=new int[10];
  88.         size=0;
  89.         front=0;
  90.         back=9;
  91.     }
  92.    
  93.     @Override
  94.     public String toString(){
  95.         String res="front[";
  96.         int i=front, j=0, c=0;
  97.         while (c<size) {
  98.             res+=values[i++];
  99.             i%=values.length;
  100.             if (c<size-1){
  101.                 res+=", ";
  102.             }
  103.             c++;
  104.         }
  105.         res+="]back";
  106.         return res;
  107.     }
  108. }
  109.  
  110. ///////////////////////////////////////////////////////////////////////////// TestClass
  111. public class testClass {
  112.     public static void main(String[] args){
  113.        
  114.         IntQueue myQueue=new ArrayIntQueue();
  115.        
  116.         try {
  117.             System.out.println(myQueue.dequeue());
  118.         } catch (Exception ex){
  119.             System.out.println(ex.getMessage());
  120.         }
  121.        
  122.         for (int i=0; i<8; i++){
  123.             myQueue.enqueue(i+1);
  124.         }
  125.         System.out.println(myQueue);
  126.         System.out.println("Size: "+myQueue.getSize()+"\n");
  127.        
  128.         try {
  129.             myQueue.dequeue();
  130.             myQueue.dequeue();
  131.             myQueue.dequeue();
  132.         } catch (Exception ex){
  133.             System.out.println(ex.getMessage());
  134.         }
  135.         System.out.println(myQueue);
  136.         System.out.println("Size: "+myQueue.getSize()+"\n");
  137.        
  138.         for (int i=0; i<9; i++){
  139.             myQueue.enqueue(i+1);
  140.             myQueue.enqueue(i+1);
  141.             try {
  142.                 myQueue.dequeue();
  143.                 myQueue.dequeue();
  144.             } catch (Exception ex){
  145.                 System.out.println(ex.getMessage());
  146.             }
  147.             System.out.println(" iteration #"+(i+1));
  148.             System.out.println(myQueue);
  149.             System.out.println("Size: "+myQueue.getSize());
  150.         }
  151.         System.out.println();
  152.        
  153.         myQueue.clear();
  154.         System.out.println(myQueue);
  155.         System.out.println("Size: "+myQueue.getSize()+"\n");
  156.        
  157.         for (int i=0; i<22; i++){
  158.             myQueue.enqueue(i+1);
  159.         }
  160.         System.out.println(myQueue);
  161.         System.out.println("Size: "+myQueue.getSize()+"\n");
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment