Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only. FIFO.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Stacks, Queues, and Linked Lists
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
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.
Data Structures and Algorithms (60-254)
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.
Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only.
Queues CSE, POSTECH 2 2 Queues Like a stack, special kind of linear list One end is called front Other end is called rear Additions (insertions or enqueue)
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues.
Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
CHAPTER 7 Queues.
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.”
Data Structure Dr. Mohamed Khafagy.
Queue Chapter 9 Stacks Last in first out (LIFO)Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 3 1. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can.
CS Data Structures II Review COSC 2006 April 14, 2017
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Stacks  Standard operations: IsEmpty … return true iff stack is empty IsFull … return true iff stack has no remaining capacity Top … return top element.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
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.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Due: 2007/11/12. Problem 1 Rewrite function Push and Pop (Program 3.10 and 3.12) using an additional variable lastOp as discussed on Page 146. The queue.
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Data Structures & Algorithms
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem.
Computer Engineering Rabie A. Ramadan Lecture 6.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Midterm Review Linear list Stack queue. What is a data structure What is an abstract data type.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Click to edit Master text styles Stacks Data Structure.
 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.
Review Array Array Elements Accessing array elements
Using Queues: Coded Messages
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
Queues Queues Queues.
Stack and Queue.
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 Linear list. One end is called front. Other end is called rear.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
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.
پشته ها و صف ها.
Queues Linear list. One end is called front. Other end is called rear.
Presentation transcript:

Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only. FIFO (First In First Out)

Bus Stop Queue Bus Stop front rear

Bus Stop Queue Bus Stop front rear

Bus Stop Queue Bus Stop front rear

Bus Stop Queue Bus Stop front rear

Revisit Of Stack Applications Applications in which the stack cannot be replaced with a queue.  Parentheses matching.  Towers of Hanoi.  Method invocation and return. Application in which the stack may be replaced with a queue.  Rat in a maze. Results in finding shortest path to exit.

Wire Routing Represent as a grid in which components and already placed wires are denoted by blocked grid positions. (Can be used to solve the rat in the maze.)

Lee’s Wire Router start pin end pin Label all reachable squares 1 unit from start.

Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 2 units from start. 11 Store the position in the queue.

Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 3 units from start

Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 4 units from start

Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 5 units from start

Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 6 units from start

Lee’s Wire Router start pin end pin End pin reached. Traceback

Lee’s Wire Router start pin end pin 4 End pin reached. Traceback

Queue Operations  IsEmpty … return true iff queue is empty  Front … return front element of queue  Rear … return rear element of queue  Push … add an element at the rear of the queue  Pop … delete the front element of the queue

Queue in an Array  Use a 1D array to represent a queue.  Suppose queue elements are stored with the front element in queue[0], the next in queue[1], and so on.

Derive From arrayList  Pop() => delete queue[0], shift other elements one step left –O(queue size) time  Push(x) => if there is capacity, add at right end –O(1) time abcde

O(1) Pop and Push  to perform each opertion in O(1) time (excluding array doubling), we use a circular representation.

Custom Array Queue Use a 1D array queue. queue[] Circular view of array. [0] [1] [2][3] [4] [5]

Custom Array Queue Possible configuration with 3 elements. [0] [1] [2][3] [4] [5] AB C

Custom Array Queue Another possible configuration with 3 elements. [0] [1] [2][3] [4] [5] AB C

Custom Array Queue Use integer variables front and rear. –front is one position counterclockwise from first element –rear gives position of last element [0] [1] [2][3] [4] [5] AB C front rear [0] [1] [2][3] [4] [5] AB C front rear

Push An Element [0] [1] [2][3] [4] [5] AB C front rear Move rear one clockwise.

Push An Element Move rear one clockwise. [0] [1] [2][3] [4] [5] AB C front rear Then put into queue[rear]. D

Pop An Element [0] [1] [2][3] [4] [5] AB C front rear Move front one clockwise.

Pop An Element [0] [1] [2][3] [4] [5] AB C front rear Move front one clockwise. Then extract from queue[front].

Moving rear Clockwise [0] [1] [2][3] [4] [5] AB C front rear rear++; if ( rear = = capacity) rear = 0; rear = (rear + 1) % capacity;

Empty That Queue [0] [1] [2][3] [4] [5] AB C front rear

Empty That Queue [0] [1] [2][3] [4] [5] B C front rear

Empty That Queue [0] [1] [2][3] [4] [5] C front rear

Empty That Queue When a series of removes causes the queue to become empty, front = rear. When a queue is constructed, it is empty. So initialize front = rear = 0. [0] [1] [2][3] [4] [5] front rear

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear D

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear DE

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear DE F When a series of adds causes the queue to become full, front = rear. So we cannot distinguish between a full queue and an empty queue!

Ouch!!!!! Remedies.  Don’t let the queue get full. When the addition of an element will cause the queue to be full, increase array size. This is what the text does.  Define a boolean variable lastOperationIsPush. Following each push set this variable to true. Following each pop set to false. Queue is empty iff (front == rear) && !lastOperationIsPush Queue is full iff (front == rear) && lastOperationIsPush

Ouch!!!!! Remedies (continued).  Define an integer variable size. Following each push do size++. Following each pop do size--. Queue is empty iff (size == 0) Queue is full iff (size == arrayLength)  Performance is slightly better when first strategy is used.

Doubling Queue Capacity [0] [1] [2][3] [4] [5] AB C front rear DE BCDEA BCDEA BCDEA Before enlarge array After enlarge array Shift

Homework Sec. 3.5 Exercise 1 (a) P157  Write the program. (Find a path through the maze with Lee’s Wire Router algorithm introduced in this section)