Title: Queues
1Queues
Chapter 5
2Linear Lists
- Operations are
- Insertion
- Deletion
- Retrieval
- Traversal (exception for restristed lists).
Figure 3-2
3Queue
A queue is a linear list. Data can be inserted at
one end (rear) and deleted from the other end
(front).
First In First Out - FIFO
Figure 5-1
4Queue Operations
- There are four basic queue operations.
- Data can be inserted at the rear and processed
from the front. - Enqueue inserts an element at the rear of the
queue. - Dequeue deletes an element at the front of the
queue. - Queue Front examines the element at the front of
the queue. - Queue Rear examines the element at the rear of
the queue.
5Queue Operations
Figure 5-2
6Queue Operations
Figure 5-3
7Queue Operations
If there are no data in the queue, then the queue
is in an underflow state.
Figure 5-4
8Queue Operations
If there are no data in the queue, then the queue
is in an underflow state.
Figure 5-5
9Figure 5-6
10Queue Linked List Design
Figure 5-7
11Queue Data Structure
Figure 5-8
12Figure 5-9, Part I
13Queue Algorithms - Create Queue
- algorithm createQueue
- Allocates memory for a queue head node from
dynamic memory and returns its address to the
caller. - Pre Nothing
- Post head has been allocated and initialized
- Return heads address if successful, null if
memory owerflow. - if (memory available)
- allocate (newPtr)
- newPtr?front null pointer
- newPtr?rear null pointer
- newPtr?count 0
- return newPtr
- else
- return null pointer
- end createQueue
14Queue Algorithms - Enqueue
Figure 5-10
15Queue Algorithms - Enqueue
- algorithm enqueue(val queue lthead pointergt, val
item ltdataTypegt) - This algorithm inserts data into queue.
- Pre queue has been create
- Post item data have been inserted
- Return boolean, true if successful, false if
overflow. - if (queue full)
- return false
- allocate (newPtr)
- newPtr?data item
- newPtr?next null pointer
- if (queue?count zero) //inserting into null queue
- queue?front newPtr
- else // insert data and adjust meta data
- queue?rear?next newPtr
- queue?rear newPtr
- queue?count queue?count 1
- return true
- end enqueue
16Figure 5-9, Part II
17Figure 5-11
18Queue Algorithms - Dequeue
- algorithm dequeue(val queue lthead pointergt, ref
item ltdataTypegt) - This algorithm deletes a node from a queue.
- Pre queue has been create
- Post data at front of the queue returned to user
through item and front element deleted and
recycled. - Return boolean, true if successful, false if
overflow. - if (queue?count is 0)
- return false
- item queue?front?data
- deleteLoc queue?front
- if (queue?count is 1) // deleting only item in
queue - queue?rear null pointer
- queue?front queue?front?next
- queue?count queue?count 1
- recycle (deleteLoc)
- return true
- end dequeue
19Queuing Theory
- A single server queue can provide service to
only one customer at a time. - Multiserver queues, can provide service to many
customers at a time. - A customer is any person or thing needing
service. - The service is any activity needed to accomplish
the required result. - The rate at which customers arrive in the queue
for service is known as the - arrival rate.
- Service time is the avarage time required to
complete the processing of a - customer request.
Figure 5-12
20Queuing Theory
- Queuing theory attempts to predict some patterns,
such as - Queuing time, which is defined the avarage
length of time customers wait in the queue, - Avarage and maximum size of queue,
- Response time is an important statistical tool
for online computer systems - There are two factors that affect the performance
of queues - Arrival rate
- Service time
21Queue Data Structures
Figure 5-13
22Queues Array Implementation
Figure 5-15
23Queues Array Implementation
Figure 5-16
24- Store the address of the queue array.
- Store the max. number of elements in array.
Figure 5-17
25Exercise
- Imagine the contents of queue Q1 and Q2 are as
shown. What would be the content of Q3 after the
following code is executed? The queue contents
are shown front (left) to rear (right). - Q1 42 30 41 31 19 20 25 14 10
11 12 15 - Q2 1 4 5 4 10 13
- 1 Q3 createQueue
- 2 count 0
- 3 loop (not empty Q1 and not empty Q2)
- 1 count count 1
- 2 dequeue (Q1, x)
- 3 dequeue (Q2, y)
- 4 if (y equal count)
- 1 enqueue (Q3, x)
26Exercise
- Imagine the contents of queue Q1 and Q2 are as
shown. What would be the content of Q3 after the
following code is executed? The queue contents
are shown front (left) to rear (right). - Q1 42 30 41 31 19 20 25 14 10
11 12 15 - Q2 1 4 5 4 10 13
- 1 Q3 createQueue
- 2 count 0
- 3 loop (not empty Q1 and not empty Q2)
- 1 count count 1
- 2 dequeue (Q1, x)
- 3 dequeue (Q2, y)
- 4 if (y equal count)
- 1 enqueue (Q3, x)
Step count Q1 Q2 Q3 x y
1 0,1 42,30,.. 1,4,5,4,.. 42 42 1
2 2 30,41,.. 4,5,4,.. 30 4
3 3 42,31,19,.. 5,4,10,13 42 5
4 4 31,19,20,.. 4,10,13 42,31 31 4
5 5 19,20,25,.. 10,13 19 10
6 6 20,25,14,.. 13 20 13
7 25,14,10,.. NULL
Q3 42, 31