260 likes | 272 Views
Queues. Chapter 5. Linear Lists. Operations are; Insertion Deletion Retrieval Traversal (exception for restristed lists). Figure 3-2. Queue. 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.
E N D
Queues Chapter 5
Linear Lists Operations are; Insertion Deletion Retrieval Traversal (exception for restristed lists). Figure 3-2
Queue 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
Queue 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.
Queue Operations Figure 5-2
Queue Operations Figure 5-3
Queue Operations If there are no data in the queue, then the queue is in an underflow state. Figure 5-4
Queue Operations If there are no data in the queue, then the queue is in an underflow state. Figure 5-5
Queue Linked List Design Figure 5-7
Queue Data Structure Figure 5-8
Queue 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 head’s address if successful, null if memory owerflow. • if (memory available) • allocate (newPtr) • newPtrfront = null pointer • newPtrrear = null pointer • newPtrcount = 0 • return newPtr • else • return null pointer end createQueue
Queue Algorithms - Enqueue Figure 5-10
Queue Algorithms - Enqueue • algorithm enqueue(val queue <head pointer>, val item <dataType>) • 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) • newPtrdata = item • newPtrnext = null pointer • if (queuecount zero) //inserting into null queue • queuefront = newPtr • else // insert data and adjust meta data • queuerearnext = newPtr • queuerear = newPtr • queuecount = queuecount +1 • return true end enqueue
Queue Algorithms - Dequeue • algorithm dequeue(val queue <head pointer>, ref item <dataType>) • 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 (queuecount is 0) • return false • item = queuefrontdata • deleteLoc = queuefront • if (queuecount is 1) // deleting only item in queue • queuerear = null pointer • queuefront = queuefrontnext • queuecount = queuecount – 1 • recycle (deleteLoc) • return true end dequeue
Queuing 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
Queuing 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
Queue Data Structures Figure 5-13
Queues Array Implementation Figure 5-15
Queues Array Implementation Figure 5-16
Store the address of the queue array. • Store the max. number of elements in array. Figure 5-17
Exercise • 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)
Exercise • 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) Q3: 42, 31