1 / 26

Chapter 5

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.

jakins
Télécharger la présentation

Chapter 5

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Queues Chapter 5

  2. Linear Lists Operations are; Insertion Deletion Retrieval Traversal (exception for restristed lists). Figure 3-2

  3. 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

  4. 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.

  5. Queue Operations Figure 5-2

  6. Queue Operations Figure 5-3

  7. Queue Operations If there are no data in the queue, then the queue is in an underflow state. Figure 5-4

  8. Queue Operations If there are no data in the queue, then the queue is in an underflow state. Figure 5-5

  9. Figure 5-6

  10. Queue Linked List Design Figure 5-7

  11. Queue Data Structure Figure 5-8

  12. Figure 5-9, Part I

  13. 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) • newPtrfront = null pointer • newPtrrear = null pointer • newPtrcount = 0 • return newPtr • else • return null pointer end createQueue

  14. Queue Algorithms - Enqueue Figure 5-10

  15. 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) • 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

  16. Figure 5-9, Part II

  17. Figure 5-11

  18. 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 (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

  19. 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

  20. 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

  21. Queue Data Structures Figure 5-13

  22. Queues Array Implementation Figure 5-15

  23. Queues Array Implementation Figure 5-16

  24. Store the address of the queue array. • Store the max. number of elements in array. Figure 5-17

  25. 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)

  26. 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

More Related