Advertisement
Knokoutz

Array Queue

Oct 10th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.08 KB | None | 0 0
  1. package assignment6;
  2.  
  3. public class QueueException extends RuntimeException
  4. {
  5.     public QueueException()
  6.     {
  7.     }
  8.    
  9.     public QueueException(String msg)
  10.     {
  11.         super(msg);
  12.     }
  13. }
  14. ------------------------------------------------------------------------
  15. package assignment6;
  16.  
  17. public class QueueUnderflowException extends QueueException
  18. {
  19.     public QueueUnderflowException()
  20.     {
  21.         super("Queue Underflow");
  22.     }
  23.    
  24.     public QueueUnderflowException(String msg)
  25.     {
  26.         super(msg);
  27.     }
  28. }
  29. ------------------------------------------------------------------------
  30. package assignment6;
  31.  
  32. public class QueueOverflowException extends QueueException
  33. {
  34.     public QueueOverflowException()
  35.     {
  36.         super("Queue Overflow");
  37.     }
  38.    
  39.     public QueueOverflowException(String msg)
  40.     {
  41.         super(msg);
  42.     }
  43. }
  44. ------------------------------------------------------------------------
  45. package assignment6;
  46.  
  47. public abstract class DataElement
  48. {    
  49.     public abstract boolean equals(DataElement otherElement);
  50.    
  51.     public abstract int compareTo(DataElement otherElement);
  52.    
  53.     public abstract void makeCopy(DataElement otherElement);
  54.    
  55.     public abstract DataElement getCopy();
  56. }
  57.  
  58. ------------------------------------------------------------------------
  59. package assignment6;
  60.  
  61. public class QueueClass
  62. {
  63.     private int maxQueueSize;
  64.     private int count;
  65.     private int queueFront;
  66.     private int queueRear;
  67.     private DataElement[] list;
  68.    
  69.     //initializes queue to an empty state
  70.     public void initializeQueue()
  71.     {
  72.         for(int i = queueFront; i < queueRear; i = (i + 1) % maxQueueSize)
  73.             list[i] = null;
  74.        
  75.         queueFront = 0;
  76.         queueRear = maxQueueSize - 1;
  77.         count = 0;
  78.     }
  79.    
  80.     //determines whether the queue is empty
  81.     public boolean isEmptyQueue()
  82.     {
  83.         return(count == 0);
  84.     }
  85.    
  86.     //determines whether the queue is full
  87.     public boolean isFullQueue()
  88.     {
  89.         return (count == maxQueueSize);
  90.     }
  91.    
  92.     //returns the first element of the queue
  93.     public DataElement front() throws QueueUnderflowException
  94.     {
  95.         if(isEmptyQueue())
  96.             throw new QueueUnderflowException();
  97.        
  98.         DataElement temp = list[queueFront].getCopy();
  99.         return temp;
  100.     }
  101.    
  102.     //returns the last element of the queue
  103.     public DataElement back() throws QueueUnderflowException
  104.     {
  105.         if(isEmptyQueue())
  106.             throw new QueueUnderflowException();
  107.        
  108.         DataElement temp = list[queueRear].getCopy();
  109.         return temp;
  110.     }
  111.    
  112.     //adds queueElement to the queue
  113.     public void addQueue(DataElement queueElement) throws QueueOverflowException
  114.     {
  115.         if(isFullQueue())
  116.             throw new QueueOverflowException();
  117.        
  118.         queueRear = (queueRear + 1) % maxQueueSize; //use the mod operator to
  119.                                                     //advance queueRear because
  120.                                                     //the array is circular
  121.         count++;
  122.         list[queueRear] = queueElement.getCopy();
  123.     }
  124.    
  125.     //removes the first element of the queue
  126.     public void deleteQueue() throws QueueUnderflowException
  127.     {
  128.         if(isEmptyQueue())
  129.                 throw new QueueUnderflowException();
  130.        
  131.         count--;
  132.         list[queueFront] = null;
  133.         queueFront = (queueFront + 1) % maxQueueSize;   //use the mod operator
  134.                                                         //to advance queueFront
  135.                                                         //because the array is
  136.                                                         //circular
  137.     }
  138.    
  139.     //makes a copy of otherQueue
  140.     public void copyQueue(QueueClass otherQueue)
  141.     {
  142.         if(this != otherQueue)  //avoid self-copy
  143.             copy(otherQueue);
  144.     }
  145.    
  146.     //default constructor
  147.     public QueueClass()
  148.     {
  149.         maxQueueSize = 100;
  150.        
  151.         queueFront = 0;                         //initialize queueFront
  152.         queueRear = maxQueueSize - 1;           //initialize queueRear
  153.         count = 0;
  154.         list = new DataElement[maxQueueSize];   //create the array to implement
  155.                                                 //the queue
  156.     }
  157.    
  158.     //constructor with a parameter
  159.     public QueueClass(int queueSize)
  160.     {
  161.         if(queueSize <= 0)
  162.         {
  163.             System.err.println("The size of the array to implement the queue "
  164.                     + "must be positive.");
  165.             System.err.println("Creating an array of size 100.");
  166.            
  167.             maxQueueSize = 100;
  168.         }
  169.         else
  170.             maxQueueSize = queueSize;           //set maxQueueSize to queueSize
  171.        
  172.         queueFront = 0;                         //initialize queueFront
  173.         queueRear = maxQueueSize - 1;           //initialize queueRear
  174.         count = 0;
  175.         list = new DataElement[maxQueueSize];   //create the array to implement
  176.                                                 //the queue
  177.     }
  178.    
  179.     //copy constructor
  180.     public QueueClass(QueueClass otherQueue)
  181.     {
  182.         copy(otherQueue);
  183.     }
  184.    
  185.         private void copy(QueueClass otherQueue)
  186.     {
  187.         list = null;
  188.         System.gc();
  189.        
  190.         maxQueueSize = otherQueue.maxQueueSize;
  191.         count = otherQueue.count;
  192.        
  193.         list = new DataElement[maxQueueSize];
  194.        
  195.         //copy otherStack into this stack
  196.         for(int i = 0; i < count; i++)
  197.             list[i] = otherQueue.list[i].getCopy();
  198.     }
  199.        
  200.         public int size()
  201.     {
  202.         return count;
  203.     }
  204.    
  205.     public void expandCapacity()
  206.     {
  207.         int newMaxSize = 200;
  208.         int newArray[] = new int[newMaxSize];
  209.         System.out.println("New Array size" + newArray.length);
  210.     }
  211.    
  212.     @Override
  213.     public String toString()
  214.     {
  215.         String s = " ";
  216.        
  217.         for(int i = 0; i < count; i++)
  218.             s += list[i] + " ";
  219.        
  220.         return s;
  221.     }  
  222. }
  223.  
  224.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement