Download Presentation

Data Structures( 数据结构 ) Course 5:Queue

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

Presentation Transcript

Vocabulary

5.1 Queue Operations

A queue is a linear list in which data can be inserted at one end, called the rear, and deleted from the other end, called the front. It is a first in-first out (FIFO) data structure.

Remove

(dequeue)

(Enqueue)

front

rear

A computer queue

Enqueue: Enqueue inserts an element at the rear of the queue.

grape

data

Enqueue

plum

kiwi

grape

plum

kiwi

front

rear

front

rear

Queue

Queue

Operation

Dequeue: Dequeue deletes an element at the front of the queue.

plum

data

Dequeue

grape

kiwi

plum

kiwi

grape

front

rear

front

rear

Operation

Queue

Queue

Queue Front:Queue front examines the element at the front of the queue.

plum

data

Queue

front

plum

kiwi

grape

plum

kiwi

grape

front

rear

front

rear

Operation

Queue

Queue

Queue Rear:Queue rear examines the element at the rear of the queue.

grape

data

Queue

rear

plum

kiwi

grape

plum

kiwi

grape

front

rear

front

rear

Operation

Queue

Queue

5.2 Queue Linked List Design

Data structure: For the linked list implementation of a queue, we use tow types of structures: a head and a node.

Queue head: The queue head contains the two pointers and a count of the queue.

Queue data node: The queue data node contains the user data and a link field pointing to the next node .

plum

kiwi

grape

fig

plum

kiwi

grape

fig

front

rear

Conceptual queue

rear

front

4

front

rear

Physical queue

data

next

count

front

rear

Head structure

Node structure

Queue Algorithms

Create queue: set the metadata pointers to null and the count to 0.

No queue

count

count

front

rear

front

rear

0

?

?

?

Before

After

Algorithm createQueue (ref queue <metadata>

queue.fornt = null

Queue.rear = null

Queue.count = 0

End createQueue

Enqueue: Three conditions need to be considered:

1.insert into an empty queue.

2. Insert into a queue with data.

3. Insertinto a queue when there is no memory left in the heap.

count

count

front

rear

front

rear

0

1

data

data

next

next

plum

plum

newPtr

newPtr

After

Before

Insert into empty queue

count

front

rear

newPtr

1

data

data

next

next

plum

kiwi

count

front

rear

newPtr

2

data

data

next

next

plum

kiwi

Algorithm enqueue (ref queue<metadata>

dataIn <dataType>

If (queue full)

1 return false

End if

Allocate (newPtr)

newPtr->data = dataIn

newPtr->next = null pointer

If (queue.count zero)

// inserting into null queue

1 queue.front = newPtr

Else // insert data

1 queue.rear->next = newPtr

End if

Queue.rear = newPtr

Queue.count = queue.count + 1

Return true

End enqueue

There are four ways to test if the queue is null 1.Front null

2.Rear null

3.Count 0

4.Emptyqueue

Before

After

Insert into queue with data

count

front

rear

1

data

next

plum

Dequeue:

1. Ensure that the queue contains data.

2. Pass the data back through the parameter list and then set the front pointer to the next item in the queue.

3. If the queue is now empty, set the rear pointer to null.

count

front

rear

0

(recycled)

deleteLoc

Before

After

Delete only item in queue

count

front

rear

2

data

data

next

next

plum

kiwi

Algorithm dequeue (ref queue <metadata>

ref item <dataType>)

If (queue.count is 0)

1 return false

End if

Item = queue.front->data

deleteLoc = queue.front

If (queue.count 1)

// Delete only item in queue

1 queue.rear = null pointer

End if

Queue.front = queue.front->next

Queue.count = queue.count – 1

Recycle (deleteLoc)

Return true

End dequeue

Before

count

front

rear

1

data

data

next

next

plum

kiwi

(recycled)

After

deleteLoc

Retrieving Queue Data: the logic of retrieving data is the same to that of dequeue except that the data are not deleted from the queue.

Algorithm queueFront ( val queue <metadata>,

ref dataOut <dataType>)

If (queue.count is 0)

1 return false

End if

dataOut = queue.front->data

Return true

End queueFront

Empty Queue: it returns true if the queue is empty and false if the queue contains data.

Algorithm emptyQueue ( val queue <metadata>)

Return (queue.count equal 0)

End emptyQueue

Full Queue: By allocating a node and then releasing the memory we can determine whether there is room for at least one more node.

Algorithm fullQueue ( val queue <metadata>)

Allocate (tempPtr)

If (allocate successful)

1 recycle (tempPtr)

2 return false

Else

1 return true

End if

End fullQueue

Queue Count: it returns the number of elements currently in the queue by returning the count found in the queue head node.

Algorithm Queuecount ( val queue <metadata>)

Return (queue.count)

End queueCount

Destroy Queue: it deletes all data in the queue and recycles their memory.

Algorithm destroyQueue ( ref queue <metadata>)

pWalker = queue.front

Loop (pWalker not null)

1 deletePtr = pWalker

2 pWalker = pWalker.next

3 recycle (deletePtr)

End loop

Queue.front = null

Queue.rear = null

Queue.count = 0

return

End destroyQueue

5.3 Queuing Theory

Queuing theory is a field of applied mathematics that is used to predict the performance of queues.

A Single-server queue can provide service to only one customer at a time.

Example: the hot-food vendor.

A Multi-server queue can provide service to many customers at a time.

Example: a bank in which there is one line with many bank tellers providing service.

Two elements to all queues

A customer is any person or thing needing service. Such as jobs in computer, packages being sent…

The service is any activity needed to accomplish the required result.

Two factors affect the queue

The arriving rate(比率) is the rate at which customers arrive in the queue for service. Depending on the service being provided, the arrival rate may be random or regular.

Service time is the average time required to complete the processing of a customer request.

The arriving rate and service time are the factors that most affect the performance of queues.

The faster customers arrive and the higher the service time, the longer the queue will be.

The ideal is arrival rate matches service time

The importance of queuing theory: it can predict the queue patterns including queue time(that is, the average length of time customers wait in the queue), the average size of the queue, and the maximum queue size. So, we can build a model of queue and used the model to study proposed changes to the system.

For example, In the banking queue, if we were able to add automation improvements that would reduce the average service by 15%,how many fewer tellers would we need?

Queue

Server

Queue

time

Service

time

Response

time

A queuing theory model

5.4 Queue Applications

Two queue implementations: Queue simulation and categorizing data

Queue simulation: a modeling activity used to generate statistics about the performance of queues.

An example: a saltwater taffy store on a beach boardwalk. The store has one window and a clerk can service only one customer at a time. The store also ships boxes of taffy anywhere in the country.The time to serve customers varies between 1 and 10 minutes.(8hs per day, 7 days a week)

Events:

completed process new customer module: determine the arrival of a new customer. The owner found that, on average , a customer arrives every 4 minutes. An arrival rate is simulated by using a random number generator that returns a values between 1 and 4.

If = 4, customer arrived; 1,2,3 customer not arrived.

server free module: determine whether the clerk is busy or idle. If the clerk is idle, then the next waiting customer in line can be served. If the clerk is busy, then the waiting customers remain in the queue.

Completed processing: determine whether it has completed processing for the current customer. Then processing time for the current customer is determined by a random number generator when the processing is started. When customers has been completely served, we gather statistics about sale and set server to an idle state

Data structures:

Four data structure are required for the queue simulation

Queue head: It contains two node pointers – front and rear – and a count of the number of elements currently in the queue.

Queue node: It contains the customer data and a next node pointer. The customer data consist of a sequential customer number and the arrival time.

Current Customer status: We use customer’s number, arrival time, the start time and the processing time to describe customer status.(random generator to calculate)

Simulation statistics: It stores the total number of customers processed in the simulation, the total and average service time, the total and average wait time, and the maximum number of customers in the queue at one time.

count

front

rear

2

custNum

arriveTime

next

startTime

svcTime

arriveTime

custNum

totWaitTime

maxQueueSize

totSvcTime

numCust

head

node

custStatus

simStats

Figure 5-13 queue data structures

Output: the statistics gathered during the simulation and the average queue wait time and average queue service time, the basic statistics for each customer: arrival time, start time, wait time, service time etc.