Data Structures T1 Queues
Data Structures T1 Queues
A Level
Computer Science Unit 7
Paper 1 Data structures
1
Objectives
• Understand the concept of an abstract data type
• Be familiar with the concept and uses of a queue
• Describe the creation and maintenance of data within
a queue (linear, circular, priority)
• Describe and apply the following to a linear, circular
and priority queue
• add an item
• remove an item
• test for an empty queue
• test for a full queue
Queues
Unit 7 Data structures
Category Word
Elementary data type
Composite data type
Abstract data type
Queues
Unit 7 Data structures
Abstraction
• An abstract data type (ADT) is a logical description
of how we view the data and possible operations
• A queue of print jobs (add to the rear, remove from front)
• A stack of books (add to top, remove from top)
• A list of tasks to do (add to the end, remove most important)
Examples of queues
• What are some examples of queues in real life and
information processing systems?
• What operations can be carried out on a queue?
Queues
Unit 7 Data structures
Modelling a queue
• You might have thought of a queue at the cinema or
supermarket checkout, or a queue of jobs waiting to
be processed or printed
• There are four distinct operations:
• Add item to the rear of the queue
• Remove item from the front of the queue
• Check if the queue is empty
• Check if the queue is full
Queues
Unit 7 Data structures
front = ? rear = ?
Queues
Unit 7 Data structures
front = 2 rear = 2
Worksheet
• Complete Task 1
• Mochi (shown here) are traditional Japanese rice cakes!
Queues
Unit 7 Data structures
Circular queue
• A circular queue algorithm overcomes the problem of
reusing the spaces that have been freed by
dequeueing from the front of the array
• Pointers go: 0 1 2 3 4 0 1 …
• What function can you use to implement this?
• How will you test for a full queue?
Queues
Unit 7 Data structures
MOD function
• Complete to show the operation of the MOD function
MOD function
• Complete to show the operation of the MOD function
1 2 2 2
2 3 3 3
3 4 4 4
4 5 0 0
Queues
Unit 7 Data structures
Worksheet
• Complete Task 2 on the worksheet
Queues
Unit 7 Data structures
Pseudocode
• enqueue(item) – add an item to the rear
• dequeue – remove and return an item from the front
• isEmpty – indicates if the queue is empty
• isFull – indicates if the queue is full
Queues
Unit 7 Data structures
Priority queue
• In a priority queue, some items are allowed to ‘jump’
the queue
• This type of queue is used when the items arriving
have some type of priority associated with them
• What types of priority queues
exist in real-life?
Queues
Unit 7 Data structures
Priority queue
• Each item has a priority associated with it
• In the example of the buses, assume that:
• Buses with numbers ending with A are high priority
• Buses with numbers ending with B are medium priority
• Buses with numbers ending with C are low priority
Priority queue
• You have added the buses 92B, 64C, 87B, 25C and
142A to the queue
Dynamic vs static
• Static data structures are fixed in size
• Cannot grow, shrink, or be freed during execution
• An array is a static data structure
Worksheet
• Complete the ‘Accident and Emergency’ Task 3 on
the worksheet
Queues
Unit 7 Data structures
Plenary
• Fill in the following table showing a comparison
between a simple linear array queue, a circular
queue, and a circular priority queue
FIFO Queue (array) Circular Queue Priority Queue
Advantages
Disadvantages
Usage
Queues
Unit 7 Data structures
Answers
• Fill in the following table showing a comparison
between a simple linear array queue, a circular
queue, and a circular priority queue
FIFO Queue (array) Circular Queue Priority Queue
Simple to program Can reuse free
Gives preference to
Advantages Predictable spaces
more important items
memory usage
Fixed length
Additional
Single pass Slightly more
Disadvantages Can’t reuse complex to program
processing required
to keep order
spaces
Extension
• Changing the rules to serve the last person to arrive
first, reduces the average wait time
• Why does the other queue always move faster?
• http://sciencenordic.com/queues-move-faster-if-last-person-s
erved-first
• http://www.bbc.co.uk/news/magazine-34153628