Queues front rear dequeue enqueue Message queues in
- Slides: 33
Queues
front rear … … dequeue enqueue
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. Send a message to the queue Program #1 rear front Take a message from the queue Program #2
The Queue Abstract Data Type
This table shows a series of queue operations and their effects. The queue is empty initially. 5 3
A Queue Interface in Java
A Simple Array-Based Implementation To implement a queue with an array, we need: 1. An array of the size N 2. An index f for the front element 3. An index r for next empty slot or cell Q: 0 1 2 … f r N-1
As objects are enqueued and dequeued, the queue moves along in the array. For example: After enqueue: After dequeue: f r r f f r
When we increment f or r, we compute the result as (f + 1) mod N or (r + 1) mod N. In Java, the modulo operator is % (remainder operator). For example, if r = N - 1, then (r + 1) = N , therefore, (r + 1) mod N = 0 The value of r wraps around from N to 0. Q: 0 1 2 N - 1 … r f
Initially, we assign r = f = 0, indicating that the queue is empty. During the process, we may meet a situation where r = f = i (0 < i < N), which also indicates the queue is empty. Now we assume that we enqueue N objects into Q without dequeueing any of them: Q: 0 1 2 N - 1 … f r Then, we have r = f = 0. But in this case, we have a full queue. In general, when r = f = i (0 < i < N), it is possible that we have a full queue.
Problem: r = f may indicate that the queue is empty or that the queue is full. Solution: when |r – f| = N – 1, report that the queue is full.
Algorithms: size(): return the number (N - f + r) mod N 0 1 2 If r f, then r - f 0 N-1 N+r-1 N + (r - f ) N (N - f + r) mod N = - f + r = r - f If r < f, then f - r > 0 N - f + r = N – (f - r ) < N (N - f + r) mod N = N - f + r
Algorithms: is. Empty( ): return the result of the evaluation of the relationship f = r f r
Algorithms: front( ): if the queue is empty throw a Queue. Empty. Exception else return element Q[f] Q: 0 1 2 … f r N-1
enqueue(o): if queue size is N - 1 throw a Queue. Full. Exception else store the object to Q[r] assign (r + 1) mod N to r (r + 1) mod N Q: 0 1 2 … f r N-1
dequeue( ): if queue size is empty throw a Queue. Empty. Exception else save the element Q[f] to a variable temp make the element Q[f] a null object assign (f + 1) mod N to f (f + 1) mod N Q: 0 1 2 … f r N-1
Memory Allocation in Java
Data Structure Exercises 4. 1
- Queue front rear
- Definition of queue
- I automobile
- Message queues in rtos
- Message queues in unix
- What is a dequeue
- Queue front
- Queue front rear
- Enqueue
- Enqueue
- Enqueue
- Enqueue
- Java stack exercises
- Mgh
- Cqueue
- Adaptable priority queues
- What are stacks
- Priority queues: quiz
- Applications of priority queues
- Representation of queues
- Java stacks and queues
- Jenis jenis queue
- Office operations
- Dead front vs live front transformer
- Draw a warm front
- Front cover page for school magazine
- Linear vs circular queue
- The soldier poem meaning
- Rear axle construction
- Rear window dancer
- The flag should be in the rear of a procession and centered
- Federal seat belt law
- Orthographic projection
- Chapter 75