Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array based Queues A circular implementation. Implementation Option 1 As with a array based stack, there are multiple ways that a queue can be implemented.

Similar presentations


Presentation on theme: "Array based Queues A circular implementation. Implementation Option 1 As with a array based stack, there are multiple ways that a queue can be implemented."— Presentation transcript:

1 Array based Queues A circular implementation

2 Implementation Option 1 As with a array based stack, there are multiple ways that a queue can be implemented using an array Some are more efficient that others

3 Similarly to an array based list and stack, there are many identical data members int[] elements; int currentsize; int maxsize;

4 Begin by considering a queue, where the front of the queue is considered to ALWAYS be array element zero. The rear of the queue will always the next empty slot, which is the value of currentSize CurrentSize = 0 Front Rear

5 As each value is enqueued, it is placed in the slot referenced by rear, enqueue(77) yields: 77 CurrentSize = 1 Front Rear

6 Enqueue(11) yields 7711 CurrentSize = 2 Front Rear

7 In this implementation, there is no need to have additional data members for front and rear

8 public void enqueue( int newValue) { if (this.isFull() ) System.out.println("operation aborted queue is full"); else { this.elements[currentSize]= newValue; currentSize++; } }// end enqueue

9 public int dequeue() { int returnValue =0; if (this.isEmpty() ) System.out.println("Dequeue aborted, queue is empty"); else { returnValue = this.elements[0]; for (int i=0; i < currentSize -1; i++) this.elements[i] = this.elements[i+1]; currentSize--; }// end else return returnValue; }// end dequeue A disadvantage with this implementation is that elements must be shifted with each dequeue

10 Implementation 2, circular In a circular implementation of a queue there is NO shifting of values. Instead, there are two additional data members, front and rear, each of which reference a particular element in the array and which move as elements are dequeued and enqueued.

11 public class queue2 { private int[] elements; private int maxSize; private int currentSize; private int front; private int rear; // front and rear are initialized to the first element in the array public queue2(int max) { maxSize = max; currentSize = 0; front= 0; rear = 0; elements = new int[maxSize]; }// end constructor

12 Enqueue Instead of a “flat” series of cells, the array is viewed as a circle 0 4 3 2 1 7 5 6 maxSize=8

13 Front and rear are both initialized to element zero 0 4 3 2 1 7 5 6 maxSize=8 Front Rear

14 Each time a value is enqueued, it is inserted into the slot referenced by rear, and rear is incremented. 0 4 3 2 1 7 5 6 maxSize=8 Front Rear 100

15 This continues as long as the queue is not full, eventually rear will reference the last element 0 4 3 2 1 7 5 6 maxSize=8 Front Rear 100 18 1 9 99 4 66

16 Eventually a value will be enqueued into the last element and rear will be incremented and will equal maxsize, this means rear is past the end of the array, and should be reset to 0 0 4 3 2 1 7 5 6 maxSize=8 Front Rear 100 18 1 9 99 4 66 22 No further enqueues will occur, because the queue is full

17 public void enqueue( int newValue) { if ( this.isFull()) { throw new QueueException("The queue is full the value can not be added"); } // rear will be past the end of the physical array when // rear == maxsize // if this is true and the queue is not full, set rear to 0 if ( rear == maxSize) rear = 0; elements[rear] = newValue; ++currentSize; ++rear; }

18 Each time a value is dequeued, it is the one referenced by front, the value will be removed, and front incremented 0 4 3 2 1 7 5 6 maxSize=8 Front Rear 18 1 9 99 4 66 22 Front will always reference the next value to dequeue

19 public int dequeue(){ int tempValue=0; if (this.isEmpty()) { throw new QueueException("The queue is empty,there is nothing to remove"); } tempValue = elements[front]; // save the front element ++front; --currentSize; // after incrementing, if front == maxsize, it is past the // end of the physical array, reset to zero if (front == maxSize) front=0; return tempValue; }


Download ppt "Array based Queues A circular implementation. Implementation Option 1 As with a array based stack, there are multiple ways that a queue can be implemented."

Similar presentations


Ads by Google