Queues

Click here to load reader

download Queues

of 25

description

Queues. Chapter 4. 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. - PowerPoint PPT Presentation

Transcript of Queues

Stacks and Queues

Chapter 4Queues14.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, its 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 list.

2Operations to manage the queue are:Clear()isEmpty()enqueue(el)dequeue()firstEl()

3Basic Operations on a Queue

InitializeQueue: Initializes the queue to an empty stateDestroyQueue: Removes all the elements from the queue, leaving the queue emptyIsEmptyQueue: Checks whether the queue is empty. If the queue is empty, it returns the value true; otherwise, it returns the value falseIsFullQueue: Checks whether the queue is full. If the queue is full, it returns the value true; otherwise, it returns the value false

4Front: Returns the front (first) element of the queue; the queue must existBack: Returns the last (rear) element of the queue; the queue must existAddQueue: 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.

Basic Operations on a Queue

5A 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.

6Basic Queue OperationsEnqueue: inserts an element at the rear of the queue.EnqueueData applekiwioperation grapequeueapplekiwiqueuefrontrearfrontreargrape7 Basic Queue Operations contDequeue: deletes element at the front of the queue.Data applekiwigrapequeueDequeueoperation kiwiqueueapplefrontrearfrontreargrape8Queue Front: Examines the element at the front of the queue.Data applekiwigrapequeueQueue Frontoperation applefrontrearapplekiwigrapefrontrear Basic Queue Operations cont9Queue Rear: Examines the element at the rear of the queue.Data applekiwigrapequeueQueue Rearoperation grapefrontrearapplekiwigrapefrontrear Basic Queue Operations cont10Queue Linked List DesignFor a linked list implementation of a queue, we use two types of structures: a head and a node.applekiwigrapefrontrearfigConceptual queuecount4rear front applekiwigrapefigPhysical queue11 Queue Linked List Design contQueue head structuredatanextnode structurequeueHead front countrearend queueHead

node datanextend nodecount4rear front 12Queue AlgorithmsCreate Queuealgorithm createQueue queue.front = nullqueue.rear = nullqueue.count = 0end createQueue Queue head structurecount0rear front 13EnqueueQueuecount0rear front datanextplumnewPtrQueuecount1rear front Case 1: insert into null queueBeforeAfterQueuecount2rear front Case 2: insert into queueBeforeAfterdatanextplumnewPtrQueuecount1rear front datanextplumdatanextkiwinewPtrdatanextplumkiwidatanextnewPtr14Enqueuealgorithm enqueue

Insert (push) data into a queue.PostdataIn has been insertedReturn true if successful, false if overflow

If (queue full)return falseend ifallocate (newptr)newptr->data = dataInnewptr->next = null pointerif (queue.count = zero)queue.front = newPtrelsequeue.rear->next = newPtrend ifqueue.rear = newptrqueue.count = queue.count + 1return trueend enqueue 15DequeueCase 1: delete only item in queueQueueCase 2: delete item at front of queueBeforeQueuecount1rear front datanextplumcount0rear front datanextplumdeleteLoc(Recycled)AfterQueuecount2rear front AfterdatanextplumkiwidatanextBeforeQueuecount1rear front datanextplumkiwidatanextdeleteLoc(Recycled)16Dequeuealgorithm dequeue

This algorithm deletes a node from a queue.Postdata at front of queue returned to user through item and front element deleted and recycledReturn true if successful, false if overflow

If (queue.count is 0)return falseend ifItem= queue.front->datadeleteLoc = queue.frontif (queue.count is 1)queue.rear = null pointerend ifqueue.front = queue.front->nextqueue.count = queue.count 1recycle (deleteLoc)return trueend dequeue 17Queue Front algorithm QueueFront

This algorithm receives the data at the front of the queue without changing the queue contents. Postdata passed back to callerReturn true if successful, false if underflow

if (queue.count is 0)return falseend ifdataout = queue.front->datareturn trueend QueueFront18Empty Queuealgorithm emptyQueue

This algorithm checks to see if a queue is empty.

Return true if empty, false if queue has data

return (if queue.count equal 0)end emptyQueue 19Full Queuealgorithm 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)recycle(tempPtr)return falseelsereturn true end ifend fullQueue20 Queue Countalgorithm Queuecount

Returns the number of elements currently in queue.

return queue.count

end Queuecount 21 Destroy Queuealgorithm destroyQueueThis algorithm deletes all data from a queue.Post all data have been deleted and recycled

ptr = queue.frontLoop (ptr not null)deletePtr= ptr ptr = ptr->nextrecycle (deletePtr)end loopqueue.front = null queue.rear = nullqueue.count = 0returnend destroyQueue22Priority Queuesa 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 keyStacks and Queues: Removal order determined by order of insertingPriority Queue: Order determined by key ( Key may be part of element data or separate)23Priority QueuesA 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 RoomsStock marketKeys 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.

24Priority Queues MethodsMain methods of the Priority Queue ADTinsert(k, x) inserts an entry with key k and value xremoveMin() removes and returns the entry with smallest key

Additional methodsminKey(k, x) returns, but does not remove, an entry with smallest keyminElement() returns, but does not remove, the element of an item with smallest keysize()isEmpty()

25