Revised based on textbook author’s notes.

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

Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
COMPSCI 105 SS 2015 Principles of Computer Science Queues.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
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.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Data Structures - Queues
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stacks And Queues Chapter 18.
ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue.
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.
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Queues and Priority Queue Implementations Chapter 14 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Chapter 15 Lists Objectives
Csc 2720 Data structure Instructor: Zhuojun Duan
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Queues Queues Queues.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Chapter 19: Stacks and 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.
Circular queue.
Queues.
DATA STRUCTURE QUEUE.
Queue.
Stacks and Queues.
Binary Tree Application Operations in Heaps
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues: Implemented using Arrays
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Abstract Data Type Abstract Data Type as a design tool
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
ADT Queue (Array Implementation)
CE 221 Data Structures and Algorithms
Circular Queues: Implemented using Arrays
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Queues and Priority Queue Implementations
Getting queues right … finally (?)
Revised based on textbook author’s notes.
Stacks and Queues.
Data Structures & Programming
Presentation transcript:

Revised based on textbook author’s notes. Circular Queue Revised based on textbook author’s notes.

Queue A restricted access container that stores a linear collection. Very common for solving problems in computer science that require data to be processed in the order in which it was received. Provides a first-in first-out (FIFO) protocol. New items are added at the back while existing items are removed from the front of the queue.

The Queue ADT A queue stores a linear collection of items with access limited to a first-in first-out order. New items are added to the back. Existing items are removed from the front. Queue() is_empty() len() enqueue( item ) dequeue()

Last time we discussed linked list implementation of the Queue ADT, today we will design and implement it in circular arrays.

Queue: Circular Array circular array – an array viewed as a circle instead of a line. Items can be added/removed without having to shift the remaining items in the process. Introduces the concept of a maximum-capacity queue that can become full.

Queue: Circular Array How should the data be organized within the array? count field – number of items in the queue. front and back markers – indicate the array elements containing the queue items.

Queue: Circular Array To enqueue an item: Suppose we enqueue 32: new item is inserted at the position following back back is advanced by one position count is incremented by one. Suppose we enqueue 32:

Queue: Circular Array To dequeue an item: Suppose we dequeue an item: the value in the front position is saved front is advanced by one position. count is decremented by one. Suppose we dequeue an item:

Queue: Circular Array Suppose we enqueue items 8 and 23:

Queue: Circular Array What happens if we enqueue 39? Since we are using a circular array, the same steps are followed. But since back is at the end of the array, it wraps around to the front.

Queue: Circular Array arrayqueue.py class Queue : def __init__( self, max_size ) : self._count = 0 self._front = 0 self._back = max_size - 1 self._qarray = Array( max_size ) def is_empty( self ) : return self._count == 0 # A new operation specifically for the circular array. def is_full( self ) : return self._count == len(self._qarray) def __len__( self ) : return self._count # ...

Queue: Circular Array arrayqueue.py class Queue : # ... def enqueue( self, item ): assert not self.is_full(), "Cannot enqueue to a full queue." max_size = len(self._qarray) self._back = (self._back + 1) % max_size self._qarray[self._back] = item self._count += 1 def dequeue( self ): assert not self.is_empty(), "Cannot dequeue from an empty queue." item = self._qarray[ self._front ] self._front = (self._front + 1) % max_size self._count -= 1 return item

Queue Analysis: Circular Array Queue Operation Worst Case q = Queue() O(1) len(q) q.is_empty() q.is_full() q.enqueue(x) x = q.dequeue()

Your Exercise The circular queue we just implemented uses a count to control how the queue operates. Your exercise is to implement the same circular queue without the count variable. The basic idea is to use the relation between front and back to manage the queue. Note that without a count, one can’t tell the difference between a full queue or empty queue if front == back, so the two have to be different when queue is empty or full.