Cs212: Data Structures Computer Science Department Lecture 7: Queues.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Queues and Linked Lists
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
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.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
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.
Fundamentals of Python: From First Programs Through Data Structures
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
Queues.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Stacks, Queues, and Deques. 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.
Stacks, Queues, and Deques
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Stacks And Queues Chapter 18.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
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.
Stacks and Queues MR. Mohammad Alqahtani.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Lecture 6 of Computer Science II
Review Array Array Elements Accessing array elements
Week 4 - Monday CS221.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queues Chapter 4.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Program based on queue & their operations for an application
Chapter 15 Lists Objectives
CSE 373: Data Structures and Algorithms
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Queues Chapter 4.
Introduction to Data Structure
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.
Stacks, Queues, and Deques
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.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks, Queues, and Deques
CSC 143 Queues [Chapter 7].
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Linked Lists.
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.
Data Structures and Algorithms for Information Processing
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CE 221 Data Structures and Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
CS210- Lecture 6 Jun 13, 2005 Announcements
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Data Structures & Programming
Presentation transcript:

Cs212: Data Structures Computer Science Department Lecture 7: Queues

Lecture Contents What is Queue? Basic Queue Operation. The Queue Abstract Data Type. A Simple Array-Based Queue Implementation. Using an Array in a Circular Way. Using the Modulo Operator to Implement a Circular Array. Priority Queues. 14-Jan-19 Computer Science Department

Queues What is Queue? A queue is a linear list in which data can only be inserted at one end ,called rear , and deleted from the other end , called the front A queue is simply a waiting line that grows or shrinks by adding or taking elements form it. Unlike stack, it’s a structure in which both ends are used. 14-Jan-19 Computer Science Department

Queues Aqueue is a first in first out (FIFO) structure no search, What is Queue? Aqueue is a first in first out (FIFO) structure no search, no adding in arbitrary positions, no sorting, no access to anything beyond the front and rear elements. 14-Jan-19 Computer Science Department

Example Applications There are several possible applications for queues. Stores, theaters, reservation centers, and other similar services typically process customer requests according to the FIFO principle. A queue would therefore be a logical choice for a data structure to handle transaction processing for such applications. For example, it would be a natural choice for handling calls to the reservation center of an airline or to the box office of a theater. 14-Jan-19 Computer Science Department

The Queue Abstract Data Type What is Queue? the queue abstract data type defines a collection that keeps objects in a sequence where element access and deletion are restricted to the first element in the sequence, which is called the front of the queue, element insertion is restricted to the end of the sequence, which is called the rear of the queue. This restriction enforces the rule that items are inserted and deleted in a queue according to the first-in first-out (FIFO) principle. 14-Jan-19 Computer Science Department

The Queue Abstract Data Type The queue abstract data type (ADT) supports the following two fundamental methods: enqueue(e): Insert element e at the rear of the queue. dequeue( ): Remove and return from the queue the object at the front; an error occurs if the queue is empty. 14-Jan-19 Computer Science Department

Queue Operations There are four basic queue operations Enqueue Dequeue Queue Front Queue Rear 14-Jan-19 Computer Science Department

Queues Queue Operation 14-Jan-19 Computer Science Department

Basic Queue Operations Enqueue: inserts an element at the rear of the queue. grape Data Enqueue apple kiwi apple kiwi grape front rear front rear queue queue operation

Basic Queue Operations Dequeue: deletes element at the front of the queue. apple Data Dequeue kiwi grape apple kiwi grape front rear front rear queue queue operation

Basic Queue Operations Queue Front: Examines the element at the front of the queue. apple Data Queue Front apple kiwi grape apple kiwi grape front rear front rear queue operation

Basic Queue Operations Queue Rear: Examines the element at the rear of the queue. grape Data Queue Rear apple kiwi grape apple kiwi grape front rear front rear queue operation

Additional Operations Additionally, the queue ADT includes the following supporting methods: size( ) : Return the number of objects in the queue. isEmpty( ) : Return a Boolean value that indicates whether the queue is empty. front( ) : Return, but do not remove, the front object in the queue; an error occurs if the queue is empty. 14-Jan-19 Computer Science Department

A Simple Array-Based Queue Implementation We present a simple realization of a queue by means of an array, Q, of fixed capacity, storing its elements. The main rule with the queue ADT is that we insert and delete objects according to the FIFO principle, we must decide how we are going to keep track of the front and rear of the queue. 14-Jan-19 Computer Science Department

A Simple Array-Based Queue Implementation One possibility is to adapt the approach we used for the stack implementation, letting Q[0] be the front of the queue and then letting the queue grow from there. This is not an efficient solution, it requires that we move all the elements forward one array cell each time we perform a dequeue operation. 14-Jan-19 Computer Science Department

A Simple Array-Based Queue Implementation This will take O(n) time to perform the dequeue method,where n is the current number of objects in the queue. If we want to achieve constant time for each queue method, we need a different approach. 14-Jan-19 Computer Science Department

Using an Array in a Circular Way To avoid moving objects once they are placed in Q, we define two variables f and r, which have the following meanings: f is an index to the cell of Q storing the first element of the queue (which is the next candidate to be removed by a dequeue operation), unless the queue is empty (in which case f = r). r is an index to the next available array cell in Q. 14-Jan-19 Computer Science Department

Using an Array in a Circular Way Initially, we assign f = r = 0, which indicates that the queue is empty. Now, when we remove an element from the front of the queue, we increment f to index the next cell. Likewise, when we add an element, we store it in cell Q[r] and increment r to index the next available cell in Q. This scheme allows us to implement methods front, enqueue, and dequeue in constant time. 14-Jan-19 Computer Science Department

Using an Array in a Circular Way Consider, for example, what happens if we repeatedly enqueue and dequeue a single element N different times. We would have f = r = N. If we were then to try to insert the element just one more time, we would get an array-out-of-bounds error (since the N valid locations in Q are from Q[0] to Q[N − 1]), even though there is plenty of room in the queue in this case. To avoid this problem and be able to utilize all of the array Q, we let the f and r indices "wrap around" the end of Q. That is, we now view Q as a "circular array" that goes from Q[0] to Q[N − 1] and then immediately back to Q[0] again. 14-Jan-19 Computer Science Department

Using an Array in a Circular Way 14-Jan-19 Computer Science Department

Using the Modulo Operator to Implement a Circular Array Implementing this circular view of Q is actually pretty easy. Each time we increment f or r, we compute this increment as "(f + 1) mod N" or "(r + 1) mod N," respectively. Implementation of a queue using a circular array. The implementation uses the modulo operator to "wrap" indices around the end of the array and it also includes two instance variables, f and r, which index the front of the queue and first empty cell after the rear of the queue respectively. 14-Jan-19 Computer Science Department

14-Jan-19 Computer Science Department

Queue Linked List Design For a linked list implementation of a queue, we use two types of structures: a head and a node. apple kiwi grape fig front rear Conceptual queue 4 front count rear apple kiwi grape fig Physical queue

Implementing a Queue with a Generic Linked List We can efficiently implement the queue ADT using a generic singly linked list. we choose the front of the queue to be at the head of the list, and the rear of the queue to be at the tail of the list. In this way, we remove from the head and insert at the tail. we simply give a Java implementation for the fundamental queue methods . 14-Jan-19 Computer Science Department

Implementing a Queue with a Generic Linked List enqueue method: 14-Jan-19 Computer Science Department

Implementing a Queue with a Generic Linked List dnqueue method: 14-Jan-19 Computer Science Department

Queue Linked List Design For a linked list implementation of a queue, we use two types of structures: a head and a node. apple kiwi grape fig front rear Conceptual queue 4 front count rear apple kiwi grape fig Physical queue

Priority Queues a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue.

Priority Queues Priority queue returns elements in priority order, order determined by key. It stores collection of entries. Each entry is a (key, value) pair. Stacks and Queues: Removal order determined by order of inserting Priority Queue: Order determined by key ( Key may be part of element data or separate) 14-Jan-19 Computer Science Department

Priority Queues Applications: Hospital Emergency Rooms Stock market

Priority Queue Suppose that you have a few assignments from different courses. Which assignment will you want to work on first? You set your priority based on due days. Due days are called keys. Course Priority Due day Database Systems 2 October 3 UNIX 4 October 10 Data Structure & Algorithm 1 September 29 Structured Systems Analysis 3 October 7

What’s so different? Stacks and Queues: • Removal order determined by order of inserting (stack LIFO, Queue FIFO) Sequences: • User chooses exact placement when inserting and explicitly chooses removal order Priority Queue • Removal order determined by key • Key may be part of element data or separate Examples: Processes scheduled by CPU Hospital Emergency Rooms College admissions process for students

End Of Chapter References: Text book, chapter5: stack & Queues 14-Jan-19 Computer Science Department