Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.

Similar presentations


Presentation on theme: "Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the."— Presentation transcript:

1 Queues 1

2 Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the sequence 2

3 Queue 3 back front

4 Queue Operations 4  classically, queues only support two operations 1. enqueue  add to the back of the queue 2. dequeue  remove from the front of the queue

5 Queue Optional Operations  optional operations 1. size  number of elements in the queue 2. isEmpty  is the queue empty? 3. peek  get the front element (without removing it) 4. search  find the position of the element in the queue 5. isFull  is the queue full? (for queues with finite capacity) 6. capacity  total number of elements the queue can hold (for queues with finite capacity) 5

6 Enqueue 1. q.enqueue("A") 6 A F B The enqueue operation adds elements to the back of the queue. If you enqueue an element into an empty queue then the front (F) and back (B) of the queue both refer to the same element.

7 Enqueue 1. q.enqueue("A") 2. q.enqueue("B") 7 A B B F

8 Enqueue 1. q.enqueue("A") 2. q.enqueue("B") 3. q.enqueue("C") 8 A B C F B

9 Enqueue 1. q.enqueue("A") 2. q.enqueue("B") 3. q.enqueue("C") 4. q.enqueue("D") 9 A B C D F B

10 Enqueue 1. q.enqueue("A") 2. q.enqueue("B") 3. q.enqueue("C") 4. q.enqueue("D") 5. q.enqueue("E") 10 A B C D E F B

11 Dequeue 1. String s = q.dequeue() removes and returns "A" 11 B C D E F B The dequeue operation removes an element from the front of the queue, and returns the element to the client. A s

12 Dequeue 1. String s = q.dequeue() 2. s = q.dequeue() removes and returns "B" 12 C D E F B B s

13 Dequeue 1. String s = q.dequeue() 2. s = q.dequeue() 3. s = q.dequeue() removes and returns "C" 13 D E B F C s

14 Dequeue 1. String s = q.dequeue() 2. s = q.dequeue() 3. s = q.dequeue() 4. s = q.dequeue() removes and returns "D" 14 E F B D s

15 Dequeue 1. String s = q.dequeue() 2. s = q.dequeue() 3. s = q.dequeue() 4. s = q.dequeue() 5. s = q.dequeue() removes and returns "E" 15 E s

16 FIFO  queue is a First-In-First-Out (FIFO) data structure  the first element enqueued in the queue is the first element that can be accessed from the queue 16

17 Queue applications  queues are useful whenever you need to hold elements in their order of arrival  serving requests of a single resource  cashier  printer queue  disk queue  CPU queue  web server 17


Download ppt "Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the."

Similar presentations


Ads by Google