CMPT 225 Lecture 8 – Queue.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
CHAPTER 7 Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks.
Queues.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Implementing and Using Stacks
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Data Structures - Queues
Objectives of these slides:
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
Unit 2 - Stack and Queue Slides 18 to end. Queue CMPT Anne Lavergne18.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues Chapter 8 (continued)
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
CS505 Data Structures and Algorithms
ADT description Implementations
COSC160: Data Structures: Lists and Queues
CC 215 Data Structures Queue ADT
Chapter 15 Lists Objectives
CMPT 120 Topic: Searching – Part 1
March 29 – Testing and Priority QUeues
Stacks and Queues.
Stack and Queue APURBO DATTA.
Data Structures and Database Applications Queues in C#
Topic 16 Queues Adapted from Mike Scott’s materials.
Building Java Programs
i206: Lecture 11: Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
i206: Lecture 10: Lists, Stacks, Queues
Queues, Deques and Priority Queues
Queues, Deques and Priority Queues
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queues Jyh-Shing Roger Jang (張智星)
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Lecture 2: Stacks and Queues
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Data Structures and Algorithms
Queues.
CMPT 225 Lecture 6 – Review of Complexity Analysis using the Big O notation + Comparing List ADT class implementations.
CMPT 225 Lecture 5 – linked list.
CMPT 225 Lecture 7 – Stack.
Data Structures & Programming
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

CMPT 225 Lecture 8 – Queue

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

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

Today’s menu Introducing another linear data collection -> Queue

Queue What can we do with a Queue? Source: https://compsci2014.wikispaces.com/file/view/queue_line_2.jpg/422130322/queue_line_2.jpg

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

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

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);

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);

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);

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

Step 3 - Implementing Queue as an ADT Array-based implementation

Step 3 - Implementing Queue as an ADT Link-based implementation

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?

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

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

√ 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

Next Lecture Sorting algorithms