Motivation for Queues

Similar documents
Postfix (and prefix) notation

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

Queues Fall 2018 Margaret Reid-Miller

CS 206 Introduction to Computer Science II

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

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

STACKS AND QUEUES. Problem Solving with Computers-II

CS 206 Introduction to Computer Science II

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Implementing a Queue with a Linked List

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

Foundations of Data Structures

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 165] Postfix Expressions

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

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

Using a Heap to Implement a Priority Queue

Lecture 4 Stack and Queue

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

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

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

Lecture Data Structure Stack

LIFO : Last In First Out

The Stack and Queue Types

Week 6. Data structures

March 13/2003 Jayakanth Srinivasan,

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

Data Structures and Algorithms

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

infix expressions (review)

1 P age DS & OOPS / UNIT II


DATA STRUCTURE UNIT I

Stacks and queues (chapters 6.6, 15.1, 15.5)

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Information Science 2

Stack and Queue. Stack:

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

Stacks Fall 2018 Margaret Reid-Miller

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

PA3 Design Specification

DEEPIKA KAMBOJ UNIT 2. What is Stack?

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Data Structure - Stack and Queue-

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

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

File Input and Output

Stacks, Queues and Hierarchical Collections

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

BBM 201 DATA STRUCTURES

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

12 Abstract Data Types

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

Stacks. Revised based on textbook author s notes.

Ashish Gupta, Data JUET, Guna

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

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Lecture 12 ADTs and Stacks

DATA STRUCTURES AND ALGORITHMS

CSE 230 Intermediate Programming in C and C++

Chapter 2. Stack & Queues. M hiwa ahmad aziz

VISUAL BASIC 2005 EXPRESS: NOW PLAYING

8. Fundamental Data Structures

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

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

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

10/26/2017 CHAPTER 3 & 4. Stacks & Queues. The Collection Framework

An Introduction to Trees

Stacks, Queues (cont d)

Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

Abstract Data Types. Abstract Data Types

Stacks and Queues. CSE Data Structures April 12, 2002

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

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

What Can You Use a Queue For?

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Arrays and Linked Lists

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Lauren Milne Summer 2015

Linked List. April 2, 2007 Programming and Data Structure 1

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

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

CS24 Week 4 Lecture 2

.:: UNIT 4 ::. STACK AND QUEUE

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

// The next 4 functions return true on success, false on failure

data structures and algorithms lecture 6

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

List, Stack, and Queues

CS 211 Programming Practicum Spring 2017

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Introduction to Data Structures

Transcription:

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 178] Motivation for Queues Some examples of first-in, first-out (FIFO) behavior: æ waiting in line to check out at a store æ cars on a street waiting to go through a light æ making a phone call calls are handled by the phone system in the order they are made A queue is a sequence of elements, to which elements can be added (enqueue) and removed (dequeue): elements are removed in the same order in which they were added.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 179] Specifying the Queue ADT Using the abstract state style of specification: æ The state of a queue is modeled as a sequence of elements. empty se- æ Initially the state of the queue is the quence. æ The effect of an enqueue(x) operation is to append xtotheend(rear or tail) of the sequence that represents the state of the queue. This operation returns nothing. æ The effect of a dequeue operation is to delete the first element (front or head) of the sequence that represents the state of the queue. This operation returns the element that was deleted. If the queue is empty, it should return some kind of error indication.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 180] Specifying the Queue ADT (cont d) Alternative specification using allowable sequences would give some rules (an algebra ). Some specific examples: æ enqueue(a) dequeue(a): allowable not allowable, since the queue is ini- æ dequeue(a): tially empty æ enqueue(a) enqueue(b) enqueue(c) dequeue(a) enqueue(d) dequeue(b): allowable æ enqueue(a) enqueue(b) dequeue(b): not allowable, a should be returned, not b Other popular queue operations: æ peek: return the front (head) element of the queue, but do not remove it from the queue æ size: return number of elements in queue æ empty: tells whether or not the queue is empty

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 181] Applications of Queues in Operating Systems The text discusses some applications of queues in operating systems: æ to buffer data coming from a running process going to a printer: the process can typically generate data to be printed much faster than the printer can print it, so the data is saved in order in a print queue æ a printer may be shared between several computers that are networked together. Jobs running concurrently may all want to access the printer. Their print jobs need to be queued up, to prevent them from colliding; only one at a time can be printed.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 182] Application of Queues in Discrete Event Simulators A simulation program is a program that mimics, or simulates, the behavior of some complicated realworld situation, such as æ the telephone system æ vehicular traffic æ weather These systems are typically too complicated to be modeled exactly mathematically, so instead, they are simulated: events take place in them according to some random number generator. For instance, æ at random times, new calls are placed or some existing calls finish æ at random times, some more cars enter the streets æ at random times, some turbulence occurs Some of these situations are particularly well described using queues; they are characterized by entities that are stored in queues while waiting for service, for instance, the telephone system and traffic.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 183] Using a Queue to Convert Infix to Postfix First attempt: Assume infix expression is fully parenthesized. For example: æ èèè22=7è + 4è æ è6, 2èè æ è7, èèè2 æ 3è+5èæè8, è4=2èèèè Pseudocode: create queue Q to hold postfix expression create stack S to hold operators not yet added to postfix expression while there are more tokens do get next token t if t is a number then enqueue t on Q else if t is an operator then push t on S else if t is ( then skip else if t is ) then pop S and enqueue result on Q endwhile return Q

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 184] Converting Infix to Postfix (cont d) Examples: æ èèè22=7è + 4è æ è6, 2èè Q: S: æ è7, èèè2 æ 3è+5èæè8, è4=2èèèè Q: S:

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 185] Converting Infix to Postfix with Precedence It is too restrictive to require parentheses around everything. Instead, precedence conventions tell which operations to do first, in the absence of parentheses. For instance, 4æ3+2 equals 12+2 = 14, not 4æ5 =20. We need to modify the above algorithm to handle operator precedence. æ * and / have higher precedence æ + and - have lower precedence æ ( has lowest precedence (a hack)

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 186] Converting Infix to Postfix with Precedence (cont d) create queue Q to hold postfix expression create stack S to hold operators not yet added to the postfix expression while there are more tokens do get next token t if t is a number then enqueue t on Q else if S is empty then push t on S else if t is ( then push t on S else if t is ) then while top of S is not ( do pop S and enqueue result on Q endwhile pop S // get rid of ( that ended while else // t is real operator and S not empty) while prec(t) <= prec(top of S) do pop S and enqueue result on Q endwhile push t on S endif endwhile while S is not empty do pop S and enqueue result on Q endwhile return Q

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 187] Converting Infix to Postfix with Precedence (cont d) For example: æ è22=7 +4èæè6, 2è Q: S: æ 7, è2 æ 3+5èæè8, 4=2è Q: S:

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 188] Implementing a Queue with an Array State is represented with: æ array A to hold the queue elements æ integer head that holds the index of A containing the oldest element (which will be returned by the next dequeue); initially 0 æ integer tail that holds the index of A containing the newest element (the most recent element to be enqueued); initially -1 Operation implementations: æ enqueue(x): tail++; A[tail]:= x æ dequeue(x): head++; return A[head-1] æ empty: return (tail < head) æ peek: return A[head] æ size: return tail - head + 1 Problem: you will march off the end of the array after very many operations, even if the size of the queue is always small compared to the size of A.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 189] Implementing a Queue with a Circular Array Wrap around to reuse the vacated space at the beginning of the array in a circular fashion, using mod operator %. æ enqueue(x): tail = (tail + 1) % A.length; A[tail] = x; æ dequeue(x): temp = A[head]; head = (head + 1) % A.length; return temp; æ empty: return (tail < head);??? The problem is that tail can wrap around and be in front of head, when the queue is not empty. 0 1 2 3 4 5 6 7 8 111 000 000 111 tail 111 000 head To get around this problem, add state component: æ integer count to keep track of number of elements

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 190] Expanding Size of Queue Dynamically To avoid overflow problem in circular array implementation of a queue, use same idea as for array implementation of stack: If array is discovered to be full during an enqueue, æ allocate a new array that is twice the size of the old one, æ copy the old array to the new one, æ enqueue new data item onto the new array, and æ free old array (if necessary) One complication with the queue, though, is that the contents of the queue might be in two sections: 1. from head to the end of the array, and 2. then from the beginning of the array to tail. Copying the new array must take this into account.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 191] Performance of Circular Array Performance of the circular array implementation of a queue: æ Time: All operations take O(1) time, except for enqueue in the case that the array is full æ space: the array allocated might be significantly larger than the size of the queue being represented. Potentially wasteful.