Summary

Lecture notes on the queue data structure, exploring queue implementations, efficiency, and related concepts. Topics such as restricted access, circular queues, and priority queues are discussed in the document.

Full Transcript

Data Structures and Algorithms Part 6: Queue 1 Data Structures and Algorithms Part 6 Outlines  Restricted Access - More Abstract  Queue FIFO data structure  Circular Queue.  Queue implementation using an array.  Implementation With an Item Count - Queue class m...

Data Structures and Algorithms Part 6: Queue 1 Data Structures and Algorithms Part 6 Outlines  Restricted Access - More Abstract  Queue FIFO data structure  Circular Queue.  Queue implementation using an array.  Implementation With an Item Count - Queue class methods - Implementation Without an Item Count  Efficiency of Queues  Deques  Priority Queues - Efficiency of Priority Queues  Queue implementation using a Linked list. 2 Data Structures and Algorithms Part 6 Restricted Access  In an array, any item can be accessed, either immediately—if its index number is known—or by searching through a sequence of cells until it’s found. In stack and queue, however, access is restricted: Only one item can be read or removed at a given time.  The interface of these structures is designed to enforce this restricted access. 3 Data Structures and Algorithms Part 6 More Abstract  Stacks, queues, and priority queues are more abstract entities than arrays and many other data storage structures.  They’re defined primarily by their interface.  the permissible operations that can be carried out on them.  The underlying mechanism used to implement them is typically not visible to their user. 4 Data Structures and Algorithms Part 6 Queue FIFO data structure  The word queue is British for line to “queue up ” means to get in line. In computer science a queue is a data structure that is somewhat like a stack, except that in a queue the first item inserted is the first to be removed (First-In-First-Out, FIFO) 5 Data Structures and Algorithms Part 6 6 Data Structures and Algorithms Part 6 7 Data Structures and Algorithms Part 6 A Circular Queue  Wrapping Around: To avoid the problem of not being able to insert more items into the queue even when it’s not full, the Front and Rear arrows wrap around to the beginning of the array. The result is a circular queue (sometimes called a ring buffer).  Remove some items from the front of the array. Now insert another item. You’ll see the Rear arrow wrap around from index 9 to index 0; the new item will be inserted there.  Insert a few more items. The Rear arrow moves upward as you’d expect. Notice that after Rear has wrapped around, it’s now below Front, the reverse of the original arrangement. You can call this a broken sequence: The items in the queue are in two different sequences in the array. 8 Data Structures and Algorithms Part 6 9 Data Structures and Algorithms Part 6 Queue implementation using array 10 Data Structures and Algorithms Part 6 11 Data Structures and Algorithms Part 6 12 Data Structures and Algorithms Part 6 Queue Class Methods  The insert() Method : The insert() method assumes that the queue is not full. We don’t show it in main(), but normally you should call insert() only after calling isFull() and getting a return value of false. Normally, insertion involves incrementing rear and inserting at the cell rear now points to. However, if rear is at the top of the array, at maxSize-1, then it must wrap around to the bottom of the array before the insertion takes place. This is done by setting rear to –1, so when the increment occurs, rear will become 0, the bottom of the array. Finally, n Items is incremented.  The isEmpty(), isFull(), and size() Methods : The isEmpty(), isFull(), and size() methods all rely on the Items field, respectively checking if it’s 0, if it’s maxSize, or returning its value. 13 Data Structures and Algorithms Part 6 Queue Class Methods  The remove() Method : The remove() method assumes that the queue is not empty. You should call isEmpty() to ensure this is true before calling remove(), or build this error-checking into remove(). Removal always starts by obtaining the value at front and then incrementing front. However, if this puts front beyond the end of the array, it must then be wrapped around to 0. The return value is stored temporarily while this possibility is checked. Finally, n Items is decremented.  The peek() Method : The peek() method is straight forward: It returns the value at front. Some implementations allow peeking at the rear of the array as well; such routines are called something like peekFront() and peekRear() or just front() and rear(). 14 Data Structures and Algorithms Part 6 Efficiency of Queues  As with a stack, items can be inserted and removed from a queue in O(1) time. 15 Data Structures and Algorithms Part 6 Queue Implemented using a Linked List  We need a doubly linked list to implement a queue and keep time complexity O(1)  We need to insert from one end of the linked list and remove from the other end. 16 Data Structures and Algorithms Part 6 Deques  A deque is a double-ended queue. You can insert items at either end and delete them from either end. The methods might be called insertLeft() and insertRight(), and removeLeft() and removeRight().  If you restrict yourself to insertLeft() and removeLeft() (or their equivalents on the right), the deque acts like a stack.  If you restrict yourself to insertLeft() and removeRight() (or the opposite pair), it acts like a queue.  A deque provides a more versatile data structure than either a stack or a queue  However, it’s not used as often as stacks and queues 17 Data Structures and Algorithms Part 6 Priority Queues  A priority queue is a more specialized data structure than a stack or a queue.  However, it’s a useful tool in a surprising number of situations.  Like an ordinary queue, a priority queue has a front and a rear, and items are removed from the front.  However, in a priority queue, items are ordered by key value so that the item with the lowest key is always at the front.  Items are inserted in the proper position to maintain the order. 18 Data Structures and Algorithms Part 6 19 Data Structures and Algorithms Part 6 20 Data Structures and Algorithms Part 6 21 Data Structures and Algorithms Part 6 22 Data Structures and Algorithms Part 6 Efficiency of Priority Queues  In the priority-queue implementation we show here, insertion runs in O(N) time, while deletion takes O(1) time. 23 Data Structures and Algorithms Part 6

Use Quizgecko on...
Browser
Browser