Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
1 CS212: DATA STRUCTURES Computer Science Department Lecture 7: Queues Lecture Contents 2 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. 24-May-17 Computer Science Department Queues 3 What is Queue? 24-May-17 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. Computer Science Department Queues 4 What is Queue? 24-May-17 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. Computer Science Department Example Applications 5 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. 24-May-17 Computer Science Department The Queue Abstract Data Type 6 What is Queue? 24-May-17 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. Computer Science Department The Queue Abstract Data Type 7 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. 24-May-17 Computer Science Department Queue Operations 8 Queue Operation 24-May-17 There are four basic queue operations Enqueue Dequeue Queue Front Queue Rear Computer Science Department Queues 9 Queue Operation 24-May-17 Computer Science Department Basic Queue Operations 10 Enqueue: inserts an element at the rear of the queue. grape Data apple front kiwi Enqueue rear queue apple kiwi rear front operation grape queue Basic Queue Operations 11 Dequeue: deletes element at the front of the queue. apple Data apple kiwi grape Dequeue rear front queue kiwi front operation grape rear queue Basic Queue Operations 12 Queue Front: Examines the element at the front of the queue. apple Data apple kiwi grape rear front queue Queue Front operation apple front kiwi grape rear Basic Queue Operations 13 Queue Rear: Examines the element at the rear of the queue. grape Data apple kiwi grape rear front queue Queue Rear operation apple front kiwi grape rear Additional Operations 14 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. 24-May-17 Computer Science Department A Simple Array-Based Queue Implementation 15 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. 24-May-17 Computer Science Department A Simple Array-Based Queue Implementation 16 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. 24-May-17 Computer Science Department A Simple Array-Based Queue Implementation 17 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. 24-May-17 Computer Science Department Using an Array in a Circular Way 18 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. 24-May-17 Computer Science Department Using an Array in a Circular Way 19 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. 24-May-17 Computer Science Department Using an Array in a Circular Way 20 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. 24-May-17 Computer Science Department Using an Array in a Circular Way 21 24-May-17 Computer Science Department Using the Modulo Operator to Implement a Circular Array 22 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. 24-May-17 Computer Science Department 23 24-May-17 Computer Science Department Queue Linked List Design 24 For a linked list implementation of a queue, we use two types of structures: a head and a node. apple kiwi grape fig rear front Conceptual queue 4 front apple kiwi count rear grape Physical queue fig Implementing a Queue with a Generic Linked List 25 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 . 24-May-17 Computer Science Department Implementing a Queue with a Generic Linked List 26 enqueue method: 24-May-17 Computer Science Department Implementing a Queue with a Generic Linked List 27 dnqueue method: 24-May-17 Computer Science Department Queue Linked List Design 28 For a linked list implementation of a queue, we use two types of structures: a head and a node. apple kiwi grape fig rear front Conceptual queue 4 front apple kiwi count rear grape Physical queue fig Priority Queues 29 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 30 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) 24-May-17 Computer Science Department Priority Queues 31 Applications: Hospital Emergency Rooms Stock market Priority Queue 32 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? 33 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 34 End Of Chapter References: • Text book, chapter5: stack & Queues 24-May-17 Computer Science Department