Upload
muhazzab-chouhadry
View
41
Download
0
Embed Size (px)
Citation preview
QUEUE Data Structures
Introduction A Data Structure in which data can only
be inserted at one end (the rear), and deleted from the other end (the front)
These restrictions ensure that data is processed through queue in the order in which they are received (FIFO).
Usage Scenarios
Bus Stop Queue
Remove a person from the queue
Bus Stop
frontrear
rear rear rear rear
Bus Stop Queue
Bus Stop
frontrear
rear rear
Bus Stop Queue
Bus Stop
frontrear
rear
Bus Stop Queue
Add a person to the queue A queue is a FIFO (First-In, First-Out) list.
Bus Stop
frontrear
rear
A Print Queue Print queue: printing is much slower than
the process of selecting pages to print and so a queue is used
The Unsuitability of a Print Stack Stacks are last-in, first-out (LIFO) The most recently selected document
would be the next to print Unless the printer stack is empty, your
print job may never get executed if others are issuing print jobs.
Program #1
Program #2front
rear
Send a message to the queue
Take a message fromthe queue
Message queues in an operating system
There are times that programs need to communicate with each other. Unix operating system provides message queue as one of the mechanisms to facilitate communication.
Example: Web spider
Search engines (Altavista, Google, etc) uses programs called spiders to discover new pages on the web.
Input = some particular seed page Repeatedly: select a previously-discovered
page, and traverse one of its hyperlinks.To ensure broad coverage of the entire web,spiders employ a queue data-structureto store & select discovered pages next page to
be explored
newly discoveredpages
Basic Queue Operations
Enqueue: Insertion of an element at rear of queue
Dequeue: deletion of an element at the front of queue
Queue front: examines the element at front of queue
Queue rear: examines element at rear of queue
Basic Queue Operations:UnderflowOverflow
Exercise What would be the contents of queue Q after the following
code is executed and the following data are entered?
Q = createQueueloop (not end of file)
read numberif (number not 0)
enqueue(Q, number)else
dequeue(Q)end if
end loop
Data: 5, 7, 12, 4, 0, 4, 8, 67, 23, 0, 44, 33, 6, 0
Queue Implementation Issues Consider a queue implemented in an
array of size 10 After 5 enqueue operations, the shaded
portion of the array has been filled:
0 1 2 9
front rear
Queue Implementation Issues After two dequeue operations, if array
contents are not shifted, we have:0 1 2 9
front rear
• The queue capacity is now reduced to 8 items
Queue Implementation Issues Option 1 using an underlying array:
enhance the enqueue operation to increase the size of the underlying array whenever there is an item in the final array location: dequeue is a fast operation; enqueue is fast
except when the array needs replacing Could use a large amount of space: once an
item is removed, its array location is never reused
Queue Implementation Issues Option 2
Shift all the remaining items at each dequeue operation
Circular Queues We can implement fixed-capacity queues
using arrays so that the queue capacity never decreases: use circular queues
Concept can be extended so that the underlying array can be replaced by a larger array if necessary
Circular Queues
Treat the underlying array as if it is circular: in an n-element array, the location “before” index 0 is index n-1; the location “after” index n-1 is index 0
10
1211
10
• Use modular arithmetic to compute next and previous indices
Circular Queues1
0
1211
10
10
1211
10
10
1211
10
After 7 enqueues
front
rear
After 5 dequeues
front
rear
After 8 more enqueues
front
rear
Array Based Queue Implementation template <class Qtype> class Queue { Qtype queue[SIZE]; int front, rear; public: Queue() { front = rear = -1; } void enqueue(Qtype num); Qtype dequeue(); bool isfull(); Qtype isempty(); };
isfull template <class Qtype> bool
Queue<Qtype>::isfull() { if(rear+1==front || (rear+1==SIZE &&
front==0)) { return true; } else{ return false; } }
isempty template <class Qtype> bool
Queue<Qtype>::isempty() { if(rear==-1) return true; } else{ return false; } }
enqueue template <class Qtype> void
Queue<Qtype>::enqueue(Qtype num) { if(isfull()) { cout << "Queue is full.\n"; } else{ if(front==-1){ front++; } rear = (rear + 1) % SIZE; queue[rear] = item; } }
dequeue template <class Qtype> Qtype Queue<Qtype>::dequeue() { if(isempty()) { cout << "Queue is empty.\n"; return -999; } else{ data=queue[front]; if(front==rear){ read=front=-1; } else{ front = (front + 1) % SIZE; } return data; } }
Priority Queues Works like an ordinary queue. However, the order of how things are
removed depend on a priority key. Ascending Priority Queue – The item with
the smallest key has the highest priority. Descending Priority Queue – The item
with the largest key has the highest priority.
They will be covered after later.
Queues vs. Stacks Stacks are a LIFO container => store data in the
reverse of order received
Queues are a FIFO container => store data in the order received
Stacks then suggest applications where some sort of reversal or unwinding is desired.
From an ordering perspective, then, Queue are the “opposite” of stacks
Easy solution to the palindrome problem
End