1 Queues (Continued) Applications for a queue Linked queue implementation Array queue implementation Circular array queue implementation Reading L&C 3.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advertisements

§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
queues1 Queues Data structures that wait their turn.
Topic 9 The Queue ADT.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
CHAPTER 7 Queues.
5/4/20151 Queues Implementations. 5/4/20152 Outline Queues Basic operations Examples of useImplementations Array-based and linked list-based.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Queues Ellen Walker CPSC 201 Data Structures Hiram College.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 14 Queues. Chapter Scope Queue processing Using queues to solve problems Various queue implementations Comparing queue implementations Java Foundations,
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
1 Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C , 9.3.
Queues.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.
COS 312 DAY 20 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 6 Posted – Due April 16 – Questions? Queues.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
Linked Structures, LinkedStack
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
CS 367 Introduction to Data Structures Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
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 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
The Queue ADT.
Using Queues: Coded Messages
Queues Implementations 6/17/2018.
COS 312 DAY 21 Tony Gauvin.
Queues Queue Concept Queue Design Considerations
Queue data structure.
Queues Queues Queues.
Chapter 5 Queues.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stacks, Queues, and Deques
Stacks and Linked Lists
Data Structures & Programming
Presentation transcript:

1 Queues (Continued) Applications for a queue Linked queue implementation Array queue implementation Circular array queue implementation Reading L&C 3 rd : , nd :

2 Applications for a Queue A queue can be used as an underlying mechanism for many common applications –Simulation of client-server operations –Radix Sort –Scheduling processes in an operating system such as printer queues

3 Cycling through Code Keys The Caesar cipher is simple letter shifting Each letter is treated as its number 0-25 in the alphabet and each letter is encoded as: cipher value = (letter value + constant) % 26 The message is decoded letter by letter: letter value = (cipher value – constant) % 26 if (letter value < 0) letter value += 26 Using the constant 7, the word “queue” would be coded as “xblbl” Note: the word’s “pattern” is recognizable

4 Cycling through Code Keys The Caesar cipher is easy to solve because there are only 26 possible “keys” to try It can be made harder by cycling through a key set of values such as 3, 1, 7, 4, 2, 5 We put that sequence of numbers in a queue As we encode each letter, we dequeue a number for the constant and re-enqueue it - cycling through the entire key set as many times as needed for the message length

5 Cycling through Code Keys Using that queue of numbers as the constant values, the word “queue” becomes “tvlyg” Note: the word’s “pattern” is not recognizable If we are encoding a message containing the entire Unicode character set, we can omit the modulo 26 operator as in the text book code See L&C, Listing 5.2

6 Ticket Counter Simulation See L&C Listing 5.3 and 5.4 The simulation in this example sets up the queue with customers arriving at regular 15 second intervals This is not the most meaningful analysis because it doesn’t take into account the typical variations in arrival rate One customer every 15 seconds could mean 8 customers arriving at one time and then 2 minutes with no arriving customers

7 Linked Queue Implementation We can use the same LinearNode class that we used for LinkedStack implementation We use attribute names “front” and “rear” to have a meaning consistent with a queue Object of type T front LinearNode next; T element; Object of type T LinearNode next; T element; null count integer Object of type T LinearNode next; T element; rear

8 Linked Queue Implementation enqueue – O(1) public void enqueue (T element) { LinearNode node = new LinearNode (element); if (isEmpty()) front = node; else rear.setNext(node); rear = node; count++; } Note the difference between the enqueue method and the Stack push method

9 Linked Queue Implementation dequeue – O(1) public T dequeue () throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); T result = front.getElement(); front = front.getNext(); count--; if (isEmpty()) rear = null; return result; } Note the difference between the dequeue method and the stack pop method

10 Array Queue Implementation We can use an array of elements as a queue The front is implicitly index 0 and rear is the index of next available element in the array Object of type T null T [ ] queue rear Object of type T

11 Array Queue Implementation enqueue – O(1) public void enqueue (T element) { if (size() == queue.length) expandCapacity(); stack [rear++] = element; } expandCapacity is similar to private helper method used in ArraySet and Stack classes

12 Array Queue Implementation dequeue() – O(n) public T dequeue() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); T result = queue[0]; rear--; for (int scan = 0; scan < rear; scan++) queue[scan] = queue[scan + 1]; queue[rear] = null; return result; }

13 Array Queue Implementation Notice that the dequeue is O(n) due to the shifting of the elements in the array queue after the 0 th element has been copied out This introduces a potential performance problem that we would like to avoid Using the 0 th element of the array as the rear of the queue doesn’t solve the problem – just moves it to the enqueue operation With a better design, we can avoid it

14 Circular Array Queue Implementation This design eliminates the shifting of the elements as part of the dequeue operation Commonly called circular queue We keep an integer for both the front and rear of the queue in the array and never shift the elements in the array When we increment either front or rear to the length of the array, we do not expand the capacity. We set them back to zero to reuse the lower elements in the array

15 Circular Array Queue Implementation N-2 N front3 rear7 count4 7

16 Circular Array Queue Implementation N-2 N front 2rear N-2 count4 7

17 Circular Array Queue Implementation Method enqueue can not use: rear++; Method dequeue can not use: front++; To increment rear, enqueue must use: rear = (rear + 1) % queue.length; To increment front, dequeue must use: front = (front + 1) % queue.length;

18 Circular Array Queue Implementation When the front catches up to the rear (a snake eating its own tail?), our code must expand the capacity of the array (replacing the original array with a larger one) When our code expands the capacity, it must cycle through the original array from front index to rear index value as it copies from the smaller array to the larger array Then, it sets new values for front and rear