Download presentation
Presentation is loading. Please wait.
1
Queues Chapter 4
2
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 list.
3
Operations to manage the queue are:
Clear() isEmpty() enqueue(el) dequeue() firstEl()
4
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
5
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.
6
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.
7
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
8
Basic Queue Operations cont’
Dequeue: deletes element at the front of the queue. apple Data Dequeue kiwi grape apple kiwi grape front rear front rear queue queue operation
9
Basic Queue Operations cont’
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
10
Basic Queue Operations cont’
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
11
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
12
Queue Linked List Design cont’
queueHead front <node pointer> count <integer> rear <node pointer> end queueHead node data <datatype> next <node pointer> end node 4 front count rear Queue head structure data next node structure
13
Queue Algorithms Create Queue algorithm createQueue queue.front = null
queue.rear = null queue.count = 0 end createQueue count rear front Queue head structure
14
Enqueue 1 1 2 Queue Queue Before After Case 1: insert into null queue
front count rear front count rear Queue Queue 1 plum plum data next newPtr data next newPtr Before After Case 1: insert into null queue front count rear front count rear Queue Queue 1 2 newPtr plum kiwi plum data next data next data next kiwi Before After data next newPtr Case 2: insert into queue
15
Enqueue If (queue full) end if allocate (newptr)
algorithm enqueue Insert (push) data into a queue. Post dataIn has been inserted Return true if successful, false if overflow If (queue full) return false end if allocate (newptr) newptr->data = dataIn newptr->next = null pointer if (queue.count = zero) queue.front = newPtr else queue.rear->next = newPtr queue.rear = newptr queue.count = queue.count + 1 return true end enqueue Enqueue
16
(Recycled) (Recycled) Dequeue 1 2 1 Queue Queue After Before
front count rear Queue front count rear Queue 1 plum plum (Recycled) data next deleteLoc data next After Before Case 1: delete only item in queue front count rear front count rear Queue Queue 2 1 plum kiwi plum kiwi (Recycled) data next data next data next data next deleteLoc Before After Case 2: delete item at front of queue
17
Dequeue If (queue.count is 0) end if Item = queue.front->data
algorithm 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) return false end if Item = queue.front->data deleteLoc = queue.front if (queue.count is 1) queue.rear = null pointer queue.front = queue.front->next queue.count = queue.count – 1 recycle (deleteLoc) return true end dequeue Dequeue
18
Queue Front if (queue.count is 0) end if
algorithm QueueFront This algorithm receives the data at the front of the queue without changing the queue contents. Post data passed back to caller Return true if successful, false if underflow if (queue.count is 0) return false end if dataout = queue.front->data return true end QueueFront
19
Empty Queue return (if queue.count equal 0) end emptyQueue
algorithm 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
20
Full Queue allocate (tempPtr) If (allocation successful) else end if
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) recycle(tempPtr) return false else return true end if end fullQueue
21
Queue Count return queue.count end Queuecount algorithm Queuecount
Returns the number of elements currently in queue. return queue.count end Queuecount
22
Destroy Queue ptr = queue.front Loop (ptr not null) end loop
algorithm destroyQueue This algorithm deletes all data from a queue. Post all data have been deleted and recycled ptr = queue.front Loop (ptr not null) deletePtr = ptr ptr = ptr->next recycle (deletePtr) end loop queue.front = null queue.rear = null queue.count = 0 return end destroyQueue
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
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
26
An entry in a priority queue is simply a (key, value) pair
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. An entry in a priority queue is simply a (key, value) pair In a priority queue, an entry with high priority is served before an entry with low priority, So the order is determined by key. Exe: Highest priority entry will served first ,so it is in the first order: kmin The smallest key: If we have a finite number of entries , then the smallest key, is the key that satisfies kmin< k for any other key k If two entries have the same key value, they are served according to their arrival in the queue.
27
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
28
Priority Queue ADT insertItem(k,e): insert element e with key k
extractMin( )/ removeMin() : return element with minimum key and remove from queue minElement( ): Return (but do not remove) an element with the smallest key; an error condition occurs if the priority queue is empty. minKey( ): Return a smallest key; an error condition occurs if the priority queue is empty size( ): return number of elements isEmpty( ): size == 0?
29
Priority Queue implementation
1) Implementing PQ with Unsorted Sequence: Keys in the sequence are not sorted Each call to insertItem(k, e) inserts element in the last of Sequence O(1) time Each call to extractMin( ) )/ removeMin() traverses the entire sequence to find the minimum, then removes element O(n) time What about other operations?
30
Priority Queue implementation
Implementing PQ with Unsorted Sequence: Example: Assume we have the elements stored in an unsorted sequence show here. To perform the extractMin()/ removeMin() operation, we have to inspect all elements to find the element (0,0) that has the smallest key. (8,8) (7,7) (0,0) (4,4) (1,1)
31
Priority Queue implementation
2) Implementing PQ with Sorted Sequence: Keys in the sequence are sorted Each call to insertItem(k, e) traverses sorted sequence to find correct position, then does insert O(n) worst case Each call to extractMin( )/removeMin() does remove first element O(1) time What about other operations?
32
Priority Queue implementation
2) Implementing PQ with Sorted Sequence: Example: To insert the pair (6,6), we have to scan through the sequence until we find the right place (between (4,4) and (7,7)). (8,8) (7,7) (4,4) (1,1) (0,0) (6,6)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.