2. Queue Overview
• Queue ADT
• Basic operations of queue
– Push (or Enqueue), Pop (or Dequeue) etc.
• Implementations of queue using
– array
– linked list
3. Queue
• How to include a new person/item?
• How to remove a particular person/item?
4. Queue
• Collection of items into which items
may be added from rear end and
from which items may be removed
from the front end
5. The Queue ADT
• Fundamental operations:
– Enqueue: Equivalent to an insert
– Dequeue: Deletes the element from rear end
– isEmpty: Returns true if there are no elements in the queue
– ReadFront: Examines the element at the front of the queue
– ReadRear: Examines the elements at the rear of the queue
7. Queue ADT Specification
Structure: Elements are added to one end and removed from the other end.
Definitions: (Provided by user):
MAX_ITEMS: Maximum number of items that might be on the queue.
Item Type: Data type of the items on the queue.
Operations: (Provided by the ADT):
MakeEmpty
Functions: Sets queue to an empty state.
Preconditions: None
PostCondition: Queue is empty.
Boolean IsEmpty
Functions: Determine whether the queue is empty.
Preconditions: Queue has been initialized.
PostCondition: Function value = (queue is empty)
Boolean IsFull
Functions: Determine whether the queue is full.
Preconditions: Queue has been initialized.
PostCondition: Function value = (queue is full)
Enqueue (ItemType newItem)
Functions: Add newItem at the rear of the queue.
Preconditions: Queue has been initialized and is not full.
PostCondition: newItem at the rear of the queue.
Dequeue (ItemType& item)
Functions: Remove tem from front of queue and returns it in item.
Preconditions: Queue has been initialized and is not empty.
PostCondition: The element at front has been removed from the queue.
Item is copy of removed element.
8. Queue ADT
• Queue is known as FIFO (First In, First Out) lists.
– The last element inserted will be the last to be retrieved
9. Implementation of Queue
• Any list implementation could be used to implement a queue
– Arrays (static: the size of queue is given initially)
– Linked lists (dynamic: never become full, except when memory
overflows)
• We will explore implementations based on array and linked list
• Let’s see how to use an array to implement a queue first
10. Array Implementation
• Need to declare an array size ahead of time
• Associated with each queue are front and rear
– for an empty queue, set front and rear to -1
• enqueue (X)
– (1) Increment front by 1 if front = -1
– (2) Increment rear by 1
– (3) Set queue[rear] = X
• dequeue
– (1) Delete value to queue[front]
– (2) Increment front by 1
• These operations are performed in constant time?
11. Queue class
• Attributes of Queue
– capacity: the max size of queue
– rear: the index of the last element of queue
– front: the index of the first element of queue
– values: point to an array which stores elements of queue
• Operations of Queue
– IsEmpty: return true if queue is empty, return false
otherwise
– isFull: return true if queue is full, return false otherwise
– readFront: return the element at the front of queue
– readRear: return the element at the rear of queue
– enqueue: add an element to the rear of queue
– dequeue: delete the element from the front of queue
– displayQueue: print all the data in the queue
12. Implementing a Queue Class
• Define data members: consider storage structure(s)
• Attempt #1: Use an array with the rear and front equal to -1
e.g., Enqueue 75, Enqueue 89, Enqueue 64, …
front rear
75 89 64
[0] [1] [2] [3] [4]
• … Dequeue
front rear
89 64
[0] [1] [2] [3] [4]
front rear
89 64
[0] [1] [2] [3] [4]
Dequeue
(1) Delete queue[front]
(2) Increment front by 1
13. Problem?
front rear
89 64
[0] [1] [2] [3] [4]
OR
front rear
89 64
[0] [1] [2] [3] [4]
Dequeue ??????
(1) Delete value of queue[front]
(2) Increment front by 1?????
14. Queue class
class Queue {
public:
Queue(int size = 10); // constructor
~Queue() { delete [] values; } // destructor
bool isEmpty() { return rear == -1; }
bool isFull() { return (rear-front) == maxRear; }
double readFront();
double readRear();
void enqueue(double x);
void dequeue();
void displayQueue();
private:
int maxRear; // maxRear = capacity - 1
int rear; // index of currently inserted value
int front; // index of next element to be deleted
double [] values; // element array
};
15. Create Queue
• The constructor of Queue
– Allocate an array of size. By default,
size = 10.
– When the queue is full, rear will have its maximum
value, i.e. capacity – 1.
– Initially rear is set to -1. It means the queue is empty.
Queue::Queue(int capacity /*= 10*/) {
if(capacity<1) {
cout<<“cannot create queue of size below 1”;
return;
}
maxRear= capacity - 1;
values = new double[capacity];
front = rear = -1;
}
Although the constructor
dynamically allocates the queue
array, the queue is still static. The
size is fixed after the initialization.
16. Enqueue
• void Enqueue(double x);
– Add an element to the queue
– If the queue is full, print the error information.
– Note rear always represents the index of the
recently added element. Before adding an element,
increment rear.
void Queue::enqueue(const double x) {
if (IsFull())
cout << "Error: the queue is full." << endl;
else
values[++rear] = x; //is it correct?
}
17. Dequeue
• double dequeue()
– Removes and return the element at the front of the queue
– If the queue is empty, print the error information. (In this
case, the return value is useless.)
– Don’t forgot to shift all elements to left and modify rear
void Queue::dequeue() {
if (IsEmpty()) {
cout << "Error: the queue is empty." << endl;
return;
}
else {
shiftElementsLeft();
//rear???
}
}
18. Queue Rear
• double readRear()
– Return the element at rear of the queue
– Unlike dequeue, this function does not remove
the element
double Queue::readRear() {
if (IsEmpty()) {
cout << "Error: the queue is empty." << endl;
return -1;
}
else
return values[rear];
}
19. Queue Front
• double readFront()
– Return the element at front of the queue
– Unlike dequeue, this function does not remove
the element
double Queue::readFront() {
if (IsEmpty()) {
cout << "Error: the queue is empty." << endl;
return -1;
}
else
return values[front];
}