Stacks and Queues III

Similar documents
CSEN 301 Data Structures and Algorithms

ITI Introduction to Computing II

Queue with Array Implementation

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

1. Introduction. Lecture Content

Announcements Queues. Queues

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

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

CSC 1052 Algorithms & Data Structures II: Queues

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

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

ECE 242 Data Structures and Algorithms. Linked List I. Lecture 10. Prof.

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

Stack and Queue. Stack:

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

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

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

Queue rear tail front head FIFO

Ashish Gupta, Data JUET, Guna

Basic Data Structures

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

CSE 214 Computer Science II Heaps and Priority Queues

Basic Data Structures 1 / 24

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

What Can You Use a Queue For?

Queue Definition. Ordered list with property:

Queues. Queue ADT Queue Implementation Priority Queues

LECTURE OBJECTIVES 6-2

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

Stacks and Queues. CSE 373 Data Structures Lecture 6

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

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

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

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff

CSCD 326 Data Structures I Queues

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

Lecture Data Structure Stack

Chapter 18: Stacks And Queues

Queues. Stacks and Queues

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Stacks and Queues. David Greenstein Monta Vista

Chapter 2. Stack & Queues. M hiwa ahmad aziz

Ashish Jamuda CS 331 DATA STRUCTURES & ALGORITHMS COURSE FINAL EXAM

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lecture 4 Stack and Queue

More Data Structures (Part 1) Stacks

CS24 Week 4 Lecture 2

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

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

Chapter 18: Stacks And Queues

CS302 - Data Structures using C++

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

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

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Data Structures G5029

Queues. CITS2200 Data Structures and Algorithms. Topic 5

LIFO : Last In First Out

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

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

! A data type for which: ! An ADT may be implemented using various. ! Examples:

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

infix expressions (review)

No Aids Allowed. Do not turn this page until you have received the signal to start.

Lecture 3: Stacks & Queues

Queues Fall 2018 Margaret Reid-Miller

csci 210: Data Structures Stacks and Queues

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

Data Abstraction and Specification of ADTs

1. Stack Implementation Using 1D Array

An Introduction to Queues With Examples in C++

What is an algorithm?

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

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

Queue: Queue Representation: Basic Operations:

Your Topic Goes Here Queue

Standard ADTs. Lecture 19 CS2110 Summer 2009

Data Structure. Recitation VII

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

Queue ADT. B/W Confirming Pages

Abstract Data Types. Abstract Data Types

BlockingArrayQueue.java. Page 1

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

Linear Data Structures

ECE Fall st Midterm Examination (120 Minutes, closed book)

Data Structures and Object-Oriented Design III. Spring 2014 Carola Wenk

CSC 1052 Algorithms & Data Structures II: Linked Queues


Data Structures and Algorithms Winter Semester

Stacks and Queues

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

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Introduction to Data Structures and Algorithms

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

BBM 201 DATA STRUCTURES

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

CSC212:Data Structure

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

Transcription:

EE Data Structures and lgorithms http://www.ecs.umass.edu/~polizzi/teaching/ee/ Stacks and Queues III Lecture 8 Prof. Eric Polizzi

Queues overview First In First Out (FIFO) Input D Output Queues allow to get access to the first item inserted. If removed, you get access to the second item that was inserted, and so on. You cannot add/extract in the middle of queues Remark: Queue is the ritish word for Line Examples of queues in your daily life: Lines at the movies, TM, airport security, etc. Queues used to model real world situation: airplanes waiting to take off, data packet waiting to be transmitted over internet, etc. Simple and useful programming tool

Queues overview dding an item at the of the queue insert/ enqueue Removing an item from the of the queue remove/ dequeue ) enqueue (tail) (head) dequeue In contrast to Stack, a queue is open at both end pplications of queues (few examples quietly doing their job in your computer) Printing job management Storing keystroke data as you type at your keyboard (guarantee to stay in order using a queue) Packet forwarding in routers

Queues printing queue.pdf,. pdf,.pdf arrive to printer Operations using a Queue Now printing.pdf.pdf is finished. Now printing.pdf D.pdf comes D Now still printing.pdf D.pdf is finished. Now printing.pdf D.pdf is finished. Now printing D.pdf

Queue implementation One can use an array of objects to implement a queue maxsize- maxsize is the capacity of the queue /font indexes of the item at the / ( at the top makes insertion easier),, are primitive data (int, long, double, etc.) or objects fter D is added (enqueue/insert) fter leaves (dequeue/remove) maxsize- D maxsize- D 5

Queue implementation List of basic operations using a Queue? To do: Test Java applet Queue.html enqueue(item); dequeue(); peek() isempty() isfull() size() Problem: an array has a limited size, once reaches maxsize, what do we do? Example: fter leaves Shift all items to the after each dequeue but... this is too costly! Solution: Wrapped arround circular array maxsize- D 6

Queue implementation ircular array maxsize-... maxsize fter is added (enqueue) maxsize-... maxsize 7

Queues generic array implementation / Object stands for any data types or objects class Queue // variables // constructors // methods private int maxsize; // Queue capacity private int ; // Queue private int ; // Queue private int nitems; //#items in the queue private Object[] array; // array that holds the items public Queue(int maxsize) this.maxsize=maxsize; // set array size array = new Object[maxSize];//create array nitems = ; // no item yet = ; = -; Remark: It is possible to implement queue without the nitems field, but some methods (isempty, isfull, size) become more complex. 8

Queues generic array implementation / class Queue // variables // constructors // methods public boolean isempty() return(nitems==);//true if queue is empty public boolean isfull() return(nitems==maxsize);//true if queue is full public int size() return(nitems);// return curent number of items 9

Stacks generic array implementation / class Queue // variables // constructors // methods public Object peekfront() //peek item if (isempty()) throw new IllegalStateException("Queue is empty"); return array[]; //return item //////////////////////////////////////////////// public Object dequeue() //remove item if (isempty()) throw new IllegalStateException("Queue is empty"); Object temp=array[];//get item =(+)%maxsize; //increment wraparound nitems--; //decrement #items return temp; //////////////////////////////////////////////// public void enqueue(object item) //insert item if (isfull()) throw new IllegalStateException("Queue is full"); if(==maxsize-) =-;//increment wrap ++; array[]= item; //insert item nitems++; //increment #items

Queue Example We consider a Queue composed of int, can you guess the result? class Queuepp public static void main(string[] args) Queue myqueue = new Queue(5); for(int i=; i<=5; i++) myqueue.enqueue(i*); System.out.println(myqueue.dequeue()); myqueue.enqueue(6); System.out.println(myqueue.dequeue()); System.out.println(myqueue.dequeue()); System.out.println(myqueue.peekFront()); System.out.println(myqueue.dequeue()); System.out.println(myqueue.size()); Result is

Queue Example Printing Queue We consider a Queue composed of String, jobs are sent every minute, and it takes minutes to print; What is the status of the queue after minutes? class Queuepp public static void main(string[] args) Queue myqueue = new Queue(6); int time=; //Rq: time= will produce the same result while (time<=) myqueue.enqueue( job +time+.pdf ); if(time%==) myqueue.dequeue(); time++; System.out.println(myqueue.size()+ jobs in queues ); while(!myqueue.isempty()) System.out.println(myqueue.dequeue()); Results 5jobs in queues job6.pdf job7.pdf job8.pdf job9.pdf job.pdf