Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment6;
- public class QueueException extends RuntimeException
- {
- public QueueException()
- {
- }
- public QueueException(String msg)
- {
- super(msg);
- }
- }
- ------------------------------------------------------------------------
- package assignment6;
- public class QueueUnderflowException extends QueueException
- {
- public QueueUnderflowException()
- {
- super("Queue Underflow");
- }
- public QueueUnderflowException(String msg)
- {
- super(msg);
- }
- }
- ------------------------------------------------------------------------
- package assignment6;
- public class QueueOverflowException extends QueueException
- {
- public QueueOverflowException()
- {
- super("Queue Overflow");
- }
- public QueueOverflowException(String msg)
- {
- super(msg);
- }
- }
- ------------------------------------------------------------------------
- package assignment6;
- public abstract class DataElement
- {
- public abstract boolean equals(DataElement otherElement);
- public abstract int compareTo(DataElement otherElement);
- public abstract void makeCopy(DataElement otherElement);
- public abstract DataElement getCopy();
- }
- ------------------------------------------------------------------------
- package assignment6;
- public class QueueClass
- {
- private int maxQueueSize;
- private int count;
- private int queueFront;
- private int queueRear;
- private DataElement[] list;
- //initializes queue to an empty state
- public void initializeQueue()
- {
- for(int i = queueFront; i < queueRear; i = (i + 1) % maxQueueSize)
- list[i] = null;
- queueFront = 0;
- queueRear = maxQueueSize - 1;
- count = 0;
- }
- //determines whether the queue is empty
- public boolean isEmptyQueue()
- {
- return(count == 0);
- }
- //determines whether the queue is full
- public boolean isFullQueue()
- {
- return (count == maxQueueSize);
- }
- //returns the first element of the queue
- public DataElement front() throws QueueUnderflowException
- {
- if(isEmptyQueue())
- throw new QueueUnderflowException();
- DataElement temp = list[queueFront].getCopy();
- return temp;
- }
- //returns the last element of the queue
- public DataElement back() throws QueueUnderflowException
- {
- if(isEmptyQueue())
- throw new QueueUnderflowException();
- DataElement temp = list[queueRear].getCopy();
- return temp;
- }
- //adds queueElement to the queue
- public void addQueue(DataElement queueElement) throws QueueOverflowException
- {
- if(isFullQueue())
- throw new QueueOverflowException();
- queueRear = (queueRear + 1) % maxQueueSize; //use the mod operator to
- //advance queueRear because
- //the array is circular
- count++;
- list[queueRear] = queueElement.getCopy();
- }
- //removes the first element of the queue
- public void deleteQueue() throws QueueUnderflowException
- {
- if(isEmptyQueue())
- throw new QueueUnderflowException();
- count--;
- list[queueFront] = null;
- queueFront = (queueFront + 1) % maxQueueSize; //use the mod operator
- //to advance queueFront
- //because the array is
- //circular
- }
- //makes a copy of otherQueue
- public void copyQueue(QueueClass otherQueue)
- {
- if(this != otherQueue) //avoid self-copy
- copy(otherQueue);
- }
- //default constructor
- public QueueClass()
- {
- maxQueueSize = 100;
- queueFront = 0; //initialize queueFront
- queueRear = maxQueueSize - 1; //initialize queueRear
- count = 0;
- list = new DataElement[maxQueueSize]; //create the array to implement
- //the queue
- }
- //constructor with a parameter
- public QueueClass(int queueSize)
- {
- if(queueSize <= 0)
- {
- System.err.println("The size of the array to implement the queue "
- + "must be positive.");
- System.err.println("Creating an array of size 100.");
- maxQueueSize = 100;
- }
- else
- maxQueueSize = queueSize; //set maxQueueSize to queueSize
- queueFront = 0; //initialize queueFront
- queueRear = maxQueueSize - 1; //initialize queueRear
- count = 0;
- list = new DataElement[maxQueueSize]; //create the array to implement
- //the queue
- }
- //copy constructor
- public QueueClass(QueueClass otherQueue)
- {
- copy(otherQueue);
- }
- private void copy(QueueClass otherQueue)
- {
- list = null;
- System.gc();
- maxQueueSize = otherQueue.maxQueueSize;
- count = otherQueue.count;
- list = new DataElement[maxQueueSize];
- //copy otherStack into this stack
- for(int i = 0; i < count; i++)
- list[i] = otherQueue.list[i].getCopy();
- }
- public int size()
- {
- return count;
- }
- public void expandCapacity()
- {
- int newMaxSize = 200;
- int newArray[] = new int[newMaxSize];
- System.out.println("New Array size" + newArray.length);
- }
- @Override
- public String toString()
- {
- String s = " ";
- for(int i = 0; i < count; i++)
- s += list[i] + " ";
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement