Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Labs.Lab6;
- //--------------------------------------------------------------------
- //
- // Laboratory 6 AQueue.jshl
- //
- // Class definition for the array implementation of the Queue ADT
- //
- // The student is to complete all missing or incomplete method
- // implementations for this class
- //
- //--------------------------------------------------------------------
- class AQueue implements Queue // Array-based queue class
- {
- // Data members
- private int maxSize; // Maximum number of elements in the queue
- private int front; // Index of the front element
- private int rear; // Index of the rear element
- private Object [] element; // Array containing the queue elements
- // Constructors
- public AQueue ( ) // Constructor: default size
- {
- setup(defMaxQueueSize);
- }
- public AQueue ( int size ) // Constructor: sets size
- {
- setup(size);
- }
- // Class methods
- private void setup(int size) // Called by Constructors only
- {
- if (size > 0){
- element = new Object[size];
- rear = size - 1;
- front = -1;
- maxSize = size;
- }
- }
- // Queue manipulation operations
- public void showStructure ( )
- // Array implementation. Outputs the elements in a queue. If the
- // queue is empty, outputs "Empty queue". This operation is intended
- // for testing and debugging purposes only.
- {
- int j; // Loop counter
- if ( front == -1 )
- System.out.println("Empty queue");
- else
- {
- System.out.println("front = " + front + " rear = " + rear);
- for ( j = 0 ; j < maxSize ; j++ )
- System.out.print(j + "\t");
- System.out.println( );
- if ( rear >= front )
- for ( j = 0 ; j < maxSize ; j++ )
- if ( ( j >= front ) && ( j <= rear ) )
- System.out.print(element[j] + "\t");
- else
- System.out.print(" \t");
- else
- for ( j = 0 ; j < maxSize ; j++ )
- if ( ( j >= front ) || ( j <= rear ) )
- System.out.print(element[j] + "\t");
- else
- System.out.print(" \t");
- System.out.println( );
- }
- } // showStructure for AQueue
- public void clear() {
- front = -1; //resets the queue by setting beginning and end to -1
- rear = -1;
- }
- public Object dequeue() {
- if (isEmpty()){
- System.out.println("Empty queue, cannot dequeue from.");
- return null;
- } else if (front == rear){ //dequeueing last element in queue only
- Object toReturn = element[front];
- clear();
- return toReturn;
- }
- else {
- Object toReturn = element[front];
- element[front] = null;
- front = (front + 1) % element.length;
- return toReturn;
- }
- }
- public void enqueue(Object newElement) {
- //check for if queue is not full
- if (isFull()){
- System.out.println("Full queue, cannot add more.");
- }
- else
- {
- rear = (rear + 1) % element.length;
- element[rear] = newElement;
- if (front == -1){ //Increment first time enqueueing an element
- front = 0;
- }
- }
- }
- public boolean isEmpty() {
- if (front == -1){ //checks that both the front and rear are the same
- //which means therefore there is nothing in the array
- return true;
- }
- return false;
- }
- public boolean isFull() {
- if (((rear+1)%element.length) == front)
- {
- return true;
- }
- return false;
- }
- // In-lab operations
- // These methods are NOT included in the interface Queue.
- // Since compiler errors will occur before some of these methods
- // are implemented, they have been temporarily commented out.
- // Remove the comment delimeters '/*' and '*/' once these methods
- // are implemented.
- public void putFront ( Object newElement ) // Insert at front
- {
- if (!isFull() && newElement != null){
- for (int i = 0; i < maxSize-1; i++) {
- element[i] = element[i + 1];
- }
- element[front] = newElement;
- }
- }
- public Object getRear ( ) // Get from rear
- {
- if(!isEmpty()){
- Object toReturn = element[rear];
- element[rear] = null;
- rear = (rear - 1) % element.length;
- return toReturn;
- } else {
- String fail = new String("Array empty.");
- return fail;
- }
- }
- }
- /* public int length ( ) // Number of elements
- { }
- */ // class AQueue
Add Comment
Please, Sign In to add comment