Download presentation
Presentation is loading. Please wait.
Published byAntonio Kaley Modified over 9 years ago
1
Ceng-112 Data Structures I 2007 1 Chapter 5 Queues
2
Ceng-112 Data Structures I 2007 2 Figure 3-2 Linear Lists Operations are; 1.Insertion 2.Deletion 3.Retrieval 4.Traversal (exception for restristed lists).
3
Ceng-112 Data Structures I 2007 3 Figure 5-1 First In First Out - FIFO A queue is a linear list. Data can be inserted at one end (rear) and deleted from the other end (front). Queue
4
Ceng-112 Data Structures I 2007 4 Queue Operations There are four basic queue operations. Data can be inserted at the rear and processed from the front. 1.Enqueue ; inserts an element at the rear of the queue. 2.Dequeue ; deletes an element at the front of the queue. 3.Queue Front; examines the element at the front of the queue. 4.Queue Rear; examines the element at the rear of the queue.
5
Ceng-112 Data Structures I 2007 5 Figure 5-2 Queue Operations
6
Ceng-112 Data Structures I 2007 6 Figure 5-3 Queue Operations
7
Ceng-112 Data Structures I 2007 7 Figure 5-4 Queue Operations If there are no data in the queue, then the queue is in an underflow state.
8
Ceng-112 Data Structures I 2007 8 Figure 5-5 If there are no data in the queue, then the queue is in an underflow state. Queue Operations
9
Ceng-112 Data Structures I 2007 9 Figure 5-6
10
Ceng-112 Data Structures I 2007 10 Figure 5-7 Queue Linked List Design
11
Ceng-112 Data Structures I 2007 11 Figure 5-8 Queue Data Structure
12
Ceng-112 Data Structures I 2007 12 Figure 5-9, Part I
13
Ceng-112 Data Structures I 2007 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. 1.if (memory available) 1.allocate (newPtr) 2.newPtr front = null pointer 3.newPtr rear = null pointer 4.newPtr count = 0 5.return newPtr 2.else 1.return null pointer end createQueue
14
Ceng-112 Data Structures I 2007 14 Figure 5-10 Queue Algorithms - Enqueue
15
Ceng-112 Data Structures I 2007 15 Queue Algorithms - Enqueue algorithm enqueue(val queue, val item ) 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. 1.if (queue full) 1.return false 2.allocate (newPtr) 3.newPtr data = item 4.newPtr next = null pointer 5.if (queue count zero) //inserting into null queue 1.queue front = newPtr 6.else // insert data and adjust meta data 1.queue rear next = newPtr 7.queue rear = newPtr 8.queue count = queue count +1 9.return true end enqueue
16
Ceng-112 Data Structures I 2007 16 Figure 5-9, Part II
17
Ceng-112 Data Structures I 2007 17 Figure 5-11
18
Ceng-112 Data Structures I 2007 18 Queue Algorithms - Dequeue algorithm dequeue(val queue, ref item ) 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. 1.if (queue count is 0) 1.return false 2.item = queue front data 3.deleteLoc = queue front 4.if (queue count is 1) // deleting only item in queue 1.queue rear = null pointer 5.queue front = queue front next 6.queue count = queue count – 1 7.recycle (deleteLoc) 8.return true end dequeue
19
Ceng-112 Data Structures I 2007 19 Figure 5-12 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.
20
Ceng-112 Data Structures I 2007 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
Ceng-112 Data Structures I 2007 21 Figure 5-13 Queue Data Structures
22
Ceng-112 Data Structures I 2007 22 Figure 5-15 Queues Array Implementation
23
Ceng-112 Data Structures I 2007 23 Figure 5-16 Queues Array Implementation
24
Ceng-112 Data Structures I 2007 24 Figure 5-17 Store the address of the queue array. Store the max. number of elements in array.
25
Ceng-112 Data Structures I 2007 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
Ceng-112 Data Structures I 2007 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 StepcountQ1Q2Q3xy 10,142,30,..1,4,5,4,..42 1 2230,41,..4,5,4,.. 304 3342,31,19,..5,4,10,13 425 4431,19,20,..4,10,1342,31314 5519,20,25,..10,13 1910 6620,25,14,..13 2013 7 25,14,10,..NULL
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.