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
Queues Chapter 4 1 4.2 Queues It is a data structure in which the elements are added at one end, called the rear, and deleted from the other end, called the front or first. 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. A queue is an FIFO structure. One possible queue implementation is an array(circular array). A more natural queue implementation is a doubly linked 2 list. Operations to manage the queue are: 1. Clear() 2. isEmpty() 3. enqueue(el) 4. dequeue() 5. firstEl() 3 Basic Operations on a Queue InitializeQueue: Initializes the queue to an empty state DestroyQueue: Removes all the elements from the queue, leaving the queue empty IsEmptyQueue: Checks whether the queue is empty. If the queue is empty, it returns the value true; otherwise, it returns the value false IsFullQueue: Checks whether the queue is full. If the queue is full, it returns the value true; otherwise, it returns the value false 4 Basic Operations on a Queue Front: Returns the front (first) element of the queue; the queue must exist Back: Returns the last (rear) element of the queue; the queue must exist AddQueue: Adds a new element to the rear of the queue; the queue must exist and must not be full. DeleteQueue: Removes the front element of the queue; the queue must exist and must not be empty. 5 A queue is a linear list in which data can be inserted at one end, called rear, and deleted from the other end, called the front. It is a first in-first out (FIFO) data structure. no search, no adding in arbitrary positions, no sorting, no access to anything beyond the front and rear elements. 6 Basic Queue Operations Enqueue: inserts an element at the rear of the queue. grape Data apple front kiwi rear queue 7 Enqueue apple kiwi rear front operation grape queue Basic Queue Operations ’ cont Dequeue: deletes element at the front of the queue. apple Data apple kiwi Dequeue rear front queue 8 grape operation kiwi grape front rear queue Basic Queue Operations ’ cont Queue Front: Examines the element at the front of the queue. apple Data apple kiwi rear front queue 9 grape Queue Front operation apple front kiwi grape rear Basic Queue Operations ’ cont Queue Rear: Examines the element at the rear of the queue. grape Data apple kiwi rear front queue 10 grape Queue Rear operation apple front kiwi grape rear 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 rear front Conceptual queue 4 front apple 11 kiwi count grape Physical queue rear fig Queue Linked List Design cont’ 4 count front rear Queue head structure data node structure 12 next queueHead front <node pointer> count <integer> rear <node pointer> end queueHead node data next end node <datatype> <node pointer> Queue Algorithms Create Queue algorithm createQueue queue.front = null 2 queue.rear = null 3 queue.count = 0 end createQueue 1 0 front count rear Queue head structure 13 Enqueue Queue rear count front 0 plum data newPtr next rear count front Queue 1 newPtr Before plum data next After Case 1: insert into null queue Queue front count rear Queue 1 rear count front 2 newPtr plum data next Before 14 plum data kiwi data next Case 2: insert into queue next After kiwi data next newPtr algorithm enqueue Enqueue Insert (push) data into a queue. Post dataIn has been inserted Return true if successful, false if overflow If (queue full) 1 1 end if allocate (newptr) newptr->data = dataIn newptr->next = null pointer if (queue.count = zero) 2 3 4 5 6 1 1 9 10 11 15 queue.front = newPtr else 7 8 return false queue.rear->next = newPtr end if queue.rear queue.count return true end enqueue = newptr = queue.count + 1 Dequeue Queue rear count front 1 plum data next rear count front Queue 0 plum data deleteLoc next After Before Case 1: delete only item in queue Queue 2 plum data next kiwi data Before 16 rear count front next Queue deleteLoc count front rear 1 plum data next After Case 2: delete item at front of queue kiwi data next algorithm dequeue Dequeue This algorithm deletes a node from a queue. Post data at front of queue returned to user through item and front element deleted and recycled Return true if successful, false if overflow If (queue.count is 0) 1 1 end if Item = queue.front->data deleteLoc = queue.front if (queue.count is 1) 2 3 4 5 1 6 7 8 9 10 17 return false queue.rear = null pointer end if queue.front = queue.front->next queue.count = queue.count – 1 recycle (deleteLoc) return true end dequeue Queue Front algorithm QueueFront This algorithm receives the data at the front of the queue without changing the queue contents. Post Return if (queue.count is 0) 1 1 2 3 4 data passed back to caller true if successful, false if underflow return false end if dataout return true end QueueFront 18 = queue.front->data Empty Queue algorithm emptyQueue This algorithm checks to see if a queue is empty. Return 1 true if empty, false if queue return (if queue.count equal 0) end emptyQueue 19 has data Full Queue algorithm fullQueue This algorithm checks to see if a queue is full. The queue is full if memory cannot be allocated for another node. Return true if full, false if there is room for another node allocate (tempPtr) If (allocation successful) 1 2 1 2 else 3 1 4 recycle(tempPtr) return false return true end if end fullQueue 20 • Queue Count algorithm Queuecount Returns the number of elements currently in queue. 1 return queue.count end Queuecount 21 • Destroy Queue algorithm destroyQueue This algorithm deletes all data from a queue. Post ptr = queue.front Loop (ptr not null) 1 2 1 2 3 3 4 5 6 7 all data have been deleted and recycled deletePtr = ptr ptr = ptr->next recycle (deletePtr) end loop queue.front queue.rear queue.count return end destroyQueue 22 = null = null = 0 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 queue returns elements in priority order, order determined by key 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) 23 Priority Queues A priority queue is an ADT with an inserting accessing protocol: only the highest-priority element can be accessed. It is arranged to support access to the highest priority. A priority queue stores collection of entries. Each entry is a (key, value) pair. Applications: Hospital Emergency Rooms Stock market Keys in a priority queue can be arbitrary objects on which an order is defined. Two distinct items in a priority queue can have the same key. 24 Priority Queues Methods Main methods of the Priority Queue ADT insert(k, x) inserts an entry with key k and value x removeMin() removes and returns the entry with smallest key Additional methods minKey(k, x) returns, but does not remove, an entry with smallest key minElement() returns, but does not remove, the element of an item with smallest key size() isEmpty() 25