Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Queues

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Queues

Data Structures and Algorithms


Queues

• Queue: a collection whose elements are added


at one end (the rear or tail of the queue) and
removed from the other end (the front or head
of the queue)
• A queue is a FIFO (first in, first out) data
structure
• Queues have two ends:
– Elements are added at one end.
– Elements are removed from the other end.
What is a queue?

• It is an ordered group of homogeneous items of


elements.
• The element added first is also removed first
(FIFO: First In, First Out).
• Any waiting line is a queue:
– The check-out line at a grocery store
– The cars at a stop light
– An assembly line
Uses of Queues in Computing
• For any kind of problem involving FIFO
data
• Printer queue (e.g. printer in Iqra Univ)
• Keyboard input buffer
• GUI event queue (click on buttons, menu
items)
• To encode messages (more on this later)
Uses of Queues in Computing

• In simulation studies, where the goal is to


reduce waiting times:
– Optimize the flow of traffic at a traffic
light
– Determine number of cashiers to have on
duty at a grocery store at different times
of day
– Other examples?
Queue Specification
• Definitions: (provided by the user)
– MAX_ITEMS: Max number of items that might be on
the queue
– ItemType: Data type of the items on the queue

• 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

• What if the queue is empty?


• What if there is only 1 element?
Queue After Adding Element
New element is added in a node at the end of the list,
rear points to the new node, and count is incremented

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

• The condition resulting from trying to


remove an element from an empty queue.

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

• Queuing System: consists of servers and


queues of objects to be served.

• Simulation: a program that determines how


long items must wait in line before being
served.
Case Study: Simulation (cont.)
• Inputs to the simulation:
(1) the length of the simulation
(2) the average transaction time
(3) the number of servers
(4) the average time between job arrivals
Case Study: Simulation (cont.)
• Parameters the simulation must vary:
(1) number of servers
(2) time between arrivals of items

• Output of simulation: average wait time.

You might also like