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.

Slides:



Advertisements
Similar presentations
QUEUE Education is the best friend. An educated person is respected everywhere. Education beats the beauty and the youth. Chanakya.
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
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 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.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
Introduction to C Programming CE Lecture 12 Circular Queue and Priority Queue Data Structures.
5/4/2015ITK 2751 Queue a FIFO (First In First Out) efbh front rear poll, remove offer peek, element capacity size.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
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.
CS Data Structures Chapter 3 Stacks and Queues.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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.
Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
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.
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.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using C++
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.
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.
Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-1 Chapter 3 Stacks and Queues Introduction to Data Structures CHAPTER 3 STACKS and QUEUES 3.1 The Stack.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
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.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
1 Chapter 5 Stacks. 2 Topics LIFO (last-in-first-out) structure Implementations –Use class LinearList or Chain –from scratch Applications –parentheses.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
Review Array Array Elements Accessing array elements
Using Queues: Coded Messages
Stacks and Queues.
Queues Queues Queues.
CSCE 3110 Data Structures & Algorithm Analysis
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
DATA STRUCTURE QUEUE.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser 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.
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.

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.  Switchbox routing.  Method invocation and return.  Try-catch-throw implementation. Application in which the stack may be replaced with a queue.  Rat in a maze. Results in finding shortest path to exit.

Wire Routing

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

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  IsFullQ … return true iff queue is full  IsEmptyQ … return true iff queue is empty  AddQ … add an element at the rear of the queue  DeleteQ … delete and return 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.

Queue in an Array  DeleteQ() => delete queue[0] –O(queue size) time  AddQ(x) => if there is capacity, add at right end –O(1) time abcde

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

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

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

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

Circular Array 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

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

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

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

Delete 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 lastOperationIsAddQ. Following each AddQ set this variable to true. Following each DeleteQ set to false. Queue is empty iff (front == rear) && !lastOperationIsAddQ Queue is full iff (front == rear) && lastOperationIsAddQ

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