Download - Queue in Data Structure
![Page 1: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/1.jpg)
QUEUE Data Structures
![Page 2: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/2.jpg)
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).
![Page 3: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/3.jpg)
Usage Scenarios
![Page 4: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/4.jpg)
Bus Stop Queue
Remove a person from the queue
Bus Stop
frontrear
rear rear rear rear
![Page 5: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/5.jpg)
Bus Stop Queue
Bus Stop
frontrear
rear rear
![Page 6: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/6.jpg)
Bus Stop Queue
Bus Stop
frontrear
rear
![Page 7: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/7.jpg)
Bus Stop Queue
Add a person to the queue A queue is a FIFO (First-In, First-Out) list.
Bus Stop
frontrear
rear
![Page 8: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/8.jpg)
A Print Queue Print queue: printing is much slower than
the process of selecting pages to print and so a queue is used
![Page 9: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/9.jpg)
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.
![Page 10: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/10.jpg)
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.
![Page 11: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/11.jpg)
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
![Page 12: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/12.jpg)
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
![Page 13: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/13.jpg)
Basic Queue Operations:UnderflowOverflow
![Page 14: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/14.jpg)
![Page 15: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/15.jpg)
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
![Page 16: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/16.jpg)
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
![Page 17: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/17.jpg)
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
![Page 18: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/18.jpg)
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
![Page 19: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/19.jpg)
Queue Implementation Issues Option 2
Shift all the remaining items at each dequeue operation
![Page 20: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/20.jpg)
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
![Page 21: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/21.jpg)
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
![Page 22: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/22.jpg)
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
![Page 23: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/23.jpg)
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(); };
![Page 24: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/24.jpg)
isfull template <class Qtype> bool
Queue<Qtype>::isfull() { if(rear+1==front || (rear+1==SIZE &&
front==0)) { return true; } else{ return false; } }
![Page 25: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/25.jpg)
isempty template <class Qtype> bool
Queue<Qtype>::isempty() { if(rear==-1) return true; } else{ return false; } }
![Page 26: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/26.jpg)
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; } }
![Page 27: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/27.jpg)
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; } }
![Page 28: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/28.jpg)
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.
![Page 29: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/29.jpg)
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
![Page 30: Queue in Data Structure](https://reader030.vdocuments.us/reader030/viewer/2022021507/58d13e1f1a28ab455d8b53e5/html5/thumbnails/30.jpg)
End