Queues 1 Queue a queue represents a sequence

  • Slides: 17
Download presentation
Queues 1

Queues 1

Queue a queue represents a sequence of elements where elements can be added at

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

Queue front 3 back

Queue front 3 back

Queue Operations classically, queues only support two operations enqueue 1. dequeue 2. 4 add

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

Queue Optional Operations optional operations 1. size number of elements in the queue 2.

Queue Optional Operations optional operations 1. size number of elements in the queue 2. is. Empty 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. is. Full 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

Enqueue q. enqueue("A") 1. A F B 6 The enqueue operation adds elements to

Enqueue q. enqueue("A") 1. A F B 6 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.

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

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

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

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

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

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

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

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

The dequeue operation removes an element from the front of the queue, and returns

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

Dequeue 1. String s = q. dequeue() 2. s = q. dequeue() 12 B

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

Dequeue 1. String s = q. dequeue() 2. s = q. dequeue() 3. s

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

Dequeue 1. String s = q. dequeue() 2. s = q. dequeue() 3. s

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

Dequeue 1. String s = q. dequeue() 2. s = q. dequeue() 3. s

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

FIFO queue is a First-In-First-Out (FIFO) data structure 16 the first element enqueued in

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

Queue applications queues are useful whenever you need to hold elements in their order

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