The Abstract Data Type Queue. Module 8. The Abstract Data Type Queue. The Abstract Data Type Queue. The Abstract Data Type Queue

Similar documents
Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

Queue with Array Implementation

CSCD 326 Data Structures I Queues

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

What Can You Use a Queue For?

CS302 - Data Structures using C++

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

Queue Linked List Implementation

Ashish Gupta, Data JUET, Guna

LIFO : Last In First Out

ITI Introduction to Computing II

ADTs Stack and Queue. Outline

CS302 - Data Structures using C++

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Stack and Queue. Stack:

PA3 Design Specification

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

ADTs Stack and Queue. Outline

CHAPTER 5. QUEUE v2 by

Objectives. Upon completion you will be able to:

An Introduction to Queues With Examples in C++

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3


Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

CSI33 Data Structures

Basic Data Structures

Announcements Queues. Queues

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Propedéutico de Programación

Stacks and Queues

.:: UNIT 4 ::. STACK AND QUEUE

Queues. 1 Introduction. 2 The Queue ADT. Prof. Stewart Weiss. CSci 235 Software Design and Analysis II Queues

CSEN 301 Data Structures and Algorithms

csci 210: Data Structures Stacks and Queues

CSC 1052 Algorithms & Data Structures II: Queues

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Data Abstraction and Specification of ADTs

1 P age DS & OOPS / UNIT II

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

Your Topic Goes Here Queue

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Postfix (and prefix) notation

Data Structures and Algorithms for Engineers

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSC148 Week 2. Larry Zhang

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

A queue is a linear collection whose elements are added on one end and removed from the other

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

CMSC 132: Object-Oriented Programming II. Stack and Queue

Randomized Queues and Deques

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

Introduction to Data Structures and Algorithms

CS 206 Introduction to Computer Science II

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

CSI33 Data Structures

Queues. Data Structures and Design with Java and JUnit Rick Mercer 18-1

What is an algorithm?

Basic Data Structures 1 / 24

infix expressions (review)

Stacks and Queues. David Greenstein Monta Vista

Name CPTR246 Spring '17 (100 total points) Exam 3

Lecture Data Structure Stack

Queue rear tail front head FIFO

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community

CSE 143. Lecture 4: Stacks and Queues

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Data Structures. Lecture 5 : The Queues

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Chapter 5. ADTs Stack and Queue

Dynamic Data Structures

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

Stacks and Queues. Gregory D. Weber. CSCI C243 Data Structures

Algorithms and Data Structures

CSC 1052 Algorithms & Data Structures II: Linked Queues

CS 206 Introduction to Computer Science II

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

Data Structure. Recitation VII

Chapter 2. Stack & Queues. M hiwa ahmad aziz

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

Data Structures (INE2011)

Priority Queue ADT. Revised based on textbook author s notes.

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

Sequence Abstract Data Type

CS24 Week 4 Lecture 2

Lecture 3: Stacks & Queues

Subject : Computer Science. Paper: Data Structures. Module: Priority Queue and Applications. Module No: CS/DS/14

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Transcription:

Module 8 Queues CS 147 Sam Houston State University Dr. McGuire A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO) property The first item inserted into a queue is the first item to leave 1 2 ADT queue operations Create an empty queue Determine whether a queue is empty Add a new item to the queue Remove from the queue the item that was added earliest Remove all the items from the queue Retrieve from the queue the item that was added earliest Queues Are appropriate for many real-world situations Example: A line to buy a movie ticket Have applications in computer science Example: A request to print a document A simulation A study to see how to reduce the wait involved in an application 3 4 Pseudocode for the ADT queue operations createqueue() // Creates an empty queue. isempty() // Determines whether a queue is empty Pseudocode for the ADT queue operations (Continued) dequeue() throws QueueException // Retrieves and removes the front of a queue. // Throws QueueException if the operation is // not successful. enqueue(newitem) throws QueueException // Adds newitem at the back of a queue. Throws // QueueException if the operation is not // successful 5 dequeueall() // Removes all items from a queue peek() throws QueueException // Retrieves the front of a queue. Throws // QueueException if the retrieval is not // successful 6 1

Some queue operations Simple Applications of the ADT Queue: Reading a String of Characters A queue can retain characters in the order in which they are typed queue.createqueue() while (not end of line) { Read a new character ch queue.enqueue(ch) } Once the characters are in a queue, the system can process them as necessary 7 8 Recognizing Palindromes Recognizing Palindromes A palindrome A string of characters that reads the same from left to right as its does from right to left To recognize a palindrome, a queue can be used in conjunction with a stack A stack can be used to reverse the order of occurrences A queue can be used to preserve the order of occurrences A nonrecursive recognition algorithm for palindromes As you traverse the character string from left to right, insert each character into both a queue and a stack Compare the characters at the front of the queue and the top of the stack The results of inserting a string into both a queue and a stack 9 10 s of the ADT Queue A queue can have either An array-based implementation A reference-based implementation Possible implementations of a queue A linear linked list with two external references A reference to the front A reference to the back 11 A reference-based implementation of a queue: a) a linear linked list with two external references 12 2

Possible implementations of a queue (Continued) A circular linked list with one external reference A reference to the back Inserting an item into a nonempty queue A reference-based implementation of a queue: b) a circular linear linked list with one external reference 13 14 Inserting an item into an empty queue: a) before insertion; b) after insertion Deleting an item from a queue of more than one item 15 16 A circular array eliminates the problem of rightward drift a) A naive array-based implementation of a queue; b) rightward drift can cause the queue to appear full A circular implementation of a queue 17 18 3

A problem with the circular array implementation front and back cannot be used to distinguish between queue-full and queue-empty conditions The effect of some operations of the queue in Figure 8-8 19 20 a) front passes back when the queue becomes empty b) back catches up to front when the queue becomes full 21 22 To detect queue-full and queue-empty conditions Keep a count of the queue items To initialize the queue, set front to 0 back to MAX_QUEUE 1 count to 0 Inserting into a queue back = (back+1) % MAX_QUEUE; items[back] = newitem; ++count; Deleting from a queue front = (front+1) % MAX_QUEUE; --count; 23 24 4

Variations of the array-based implementation Use a flag full to distinguish between the full and empty conditions Declare MAX_QUEUE + 1 locations for the array items, but use only MAX_QUEUE of them for queue items A more efficient circular implementation: a) a full queue; b) an empty queue 25 26 More to come Stayed for the next exciting episode of. Queues 27 5