Download presentation
Presentation is loading. Please wait.
1
CMPT 225 Lecture 8 – Queue
2
Last Lecture We saw how to … Describe Stack
Define public interface of Stack ADT Design and implement Stack ADT using various data structures Compare and contrast these various implementations using Big O notation Give examples of real-life applications (problems) where we could use Stack to solve the problem Solve problems using Stack ADT
3
Learning Outcomes At the end of this lecture, a student will be able to: Describe Queue Define public interface of Queue ADT Design and implement Queue ADT using various data structures Compare and contrast these various implementations using Big O notation Give examples of real-life applications (problems) where we could use Queue to solve the problem Solve problems using Queue ADT
4
Today’s menu Introducing another linear data collection -> Queue
5
Queue What can we do with a Queue?
Source:
6
What characterizes a Queue?
Only allows elements to be inserted at one end -> back and removed at the other -> front Access to other elements in a Queue is not allowed FIFO / LILO Fair: no starvation -> every element in the queue is processed Linear data collection Not a “general-purpose” ADT
7
Step 2 – Design - Queue operations
isEmpty: Is the queue empty? enqueue: Insert element at back of queue dequeue: Remove front element of queue peek: Retrieve front element of queue (but does not remove the element) dequeueAll: Remove all element from queue
8
Step 2 – Design – Queue public interface – Contract - 1
NOTE: Expressed in C++ and using template Class invariant: FIFO / LILO // Description: Returns true if this Queue is empty otherwise false. // Time Efficiency: O(1) bool isEmpty( ) const; // Description: Adds a new element to the back of this Queue. // Returns true if the addition is successful otherwise false. bool enqueue(const ElementType& newElement);
9
Step 2 – Design – Queue public interface – Contract - 2
// Description: Removes the front element of this Queue. // Returns true if the removal is successful otherwise false. // Precondition: The Queue is not empty. // Time Efficiency: O(1) bool dequeue( ); Alternative: // Description: Removes and returns the front element of this Queue. // Exceptions: Throws EmptyQueueException if this Queue is empty. ElementType dequeue( ) throw(EmptyQueueException);
10
Step 2 – Design – Queue public interface – Contract - 3
// Description: Removes all elements from this Queue. // Returns true if the removal is successful otherwise false. // Precondition: The Queue is not empty. bool dequeuAll( ); // Description: Returns the front element of this Queue. // Postcondition: This Queue is unchanged. // Exceptions: Throws EmptyQueueException if this Queue is empty. // Time Efficiency: O(1) ElementType peek( ) const throw(EmptyQueueException);
11
Keep in mind … When we design the underlying data structure of our Queue We need to decide where front and back are located in our underlying data structure
12
Step 3 - Implementing Queue as an ADT
Array-based implementation
13
Step 3 - Implementing Queue as an ADT
Link-based implementation
14
Step 3 - Implementing Queue as an ADT
List ADT-based implementation See the List ADT-based implementation of the Stack ADT class from the lecture notes on Stack From this example, can we do the same with the Queue ADT class, i.e., implement its public methods using the public methods of the List ADT class?
15
Queue ADT - Comparing both its implementations
Time efficiency of Queue ADT’s operations (worst case scenario) expressed using the Big O notation Operations array-based link-based List ADT-based isEmpty push pop peek popAll
16
When a Queue is appropriate
Examples of problem statements that would most appropriately be solved using a data collection Queue ADT class Pipeline architecture: When module A’s output is module B’s input in a asynchronous fashion or when module B reads its input at a lower rate than module A produces its output -> queue used as a buffer E.g.: Print queue, keyboard buffer Server requests: Instant messaging servers queue up incoming messages Database requests Operating systems often use queues to schedule CPU jobs
17
√ Learning Check We can now … Describe Queue
Define public interface of Queue ADT Design and implement Queue ADT using various data structures Compare and contrast these various implementations using Big O notation Give examples of real-life applications (problems) where we could use Queue to solve the problem Solve problems using Queue ADT
18
Next Lecture Sorting algorithms
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.