Queues
Queues
Queues
• Possible Operations
– MakeEmpty
– Bool_IsEmpty
– Bool_IsFull
– Enqueue (ItemType newItem)
– Dequeue (ItemType& item)
Enqueue (ItemType newItem)
• Function: Adds newItem to the rear of the
queue.
• Preconditions: Queue has been initialized
and is not full.
• Postconditions: newItem is at rear of queue.
Dequeue (ItemType& item)
• Function: Removes front item from queue
and returns it in item.
• Preconditions: Queue has been initialized
and is not empty.
• Postconditions: Front element has been
removed from queue and item is a copy of
removed element.
Queue Operations
• enqueue : add an element to the tail of a
queue
• dequeue : remove an element from the head
of a queue
• first : examine the element at the head of
the queue (“peek”)
• Other useful operations (e.g. is the queue
empty)
• It is not legal to access the elements in the
middle of the queue!
Implementation issues
• Implement the queue as a circular structure.
• How do we know if a queue is full or
empty?
• Initialization of front and rear.
• Testing for a full or empty queue.
Make front point to the element preceding the front
element in the queue (one memory location will be
wasted).
Initialize front and rear
Queue is empty now!!
rear == front
Queue Implementation Issues
• What do we need to implement a queue?
– A data structure (container) to hold the data
elements
– Something to indicate the front of the queue
– Something to indicate the end of the queue
Array Implementation of a Queue
• First Approach:
– Use an array in which index 0 represents one end
of the queue (the front)
– Integer value rear represents the next open slot in
the array (and also the number of elements
currently in the queue)
• Discussion: What is the challenge with this
approach?
An Array Implementation of a
Queue
A queue aq containing four elements
front
0 1 2 3 4
…
aq queue
4
rear
Queue After Adding an Element
Element is added at the array location given by the
(old) value of rear, and then rear is incremented.
0 1 2 3 4
…
aq queue
5
rear
Queue After Removing an Element
Element is removed from array location 0,
remaining elements are shifted forward one position
in the array, and then rear is decremented.
0 1 2 3 4
…
aq queue
4
rear
Queue Implementation
Using a Linked List
• Internally, the queue is represented as a linked list of
nodes, with each node containing a data element
• We need two pointers for the linked list
– A pointer to the beginning of the linked list (front of
queue)
– A pointer to the end of the linked list (rear of queue)
• We will also have a count of the number of items in the
queue
Linked Implementation of a
Queue
A queue q containing four elements
rear
q
front
4
count
Discussion
rear
q
front
5
count
Queue After a dequeue Operation
Node containing is removed from the front of the list
(see previous slide), front now points to the node that was
formerly second, and count has been decremented.
rear
q
front
4
count
Queue underflow
if(!IsEmpty(Queue))
Dequeue(item);
Example: recognizing palindromes
• A palindrome is a string that reads the same
forward and backward.
Able was I ere I saw Elba
• We will read the line of text into both a
stack and a queue.
• Compare the contents of the stack and the
queue character-by-character to see if they
would produce the same string of
characters.
Example: recognizing palindromes
Case Study: Simulation