Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 4 QUEUE CSEB324 DATA STRUCTURES & ALGORITHM.

Similar presentations


Presentation on theme: "CHAPTER 4 QUEUE CSEB324 DATA STRUCTURES & ALGORITHM."— Presentation transcript:

1 CHAPTER 4 QUEUE CSEB324 DATA STRUCTURES & ALGORITHM

2 What is a queue? Definition: A queue is a set of elements of the same type in which the elements are added at one end, called the back or rear, and deleted from the other end, called the front or first The general rule to process elements in a queue is that the customer at the front of the queue is served next and that when a new customer arrives, he or she stands at the end of the queue. That is, a queue is a First In First Out, or simply FIFO data structure. 2

3 Queues Implementation 3 Physical Model We must keep track both the front and the rear of the queue. One method is to keep the front of the array in the first location on the array. Then, we can simply increase the counter of the array to show the rear. Nevertheless, to delete an entry from this queue is very expensive, since after the first entry was served, all the existing entry need to be move back one position to fill in the vacancy. With a long queue this process can lead to poor performance.

4 Queues Implementation 4 Physical Model ‘a’‘c’‘d’‘g’‘v’‘e’ 012345 Before: ‘a’ at index 0 is deleted ‘c’‘d’‘g’‘v’‘e’ 012345 ‘c’‘d’‘g’‘v’‘e’ 012345 After: front rear

5 Queues Implementation 5 Linear Implementation Indicate the front and rear of the queue. We can keep track the entry of the queue without moving any entries.  Append an entry: increase the rear by one.  To get the entry: increase the front by one. Problem:  Queue will increase and never decrease  This lead to the end of the storage capacity

6 Queues Implementation 6 Linear Implementation ‘a’‘c’‘d’‘g’‘k’ 012345 Add ‘k’ to the queue: - rear + 1 (increase) front rear Delete ‘a’ in queue: - front + 1 (increase) ‘a’‘c’‘d’‘g’‘k’ 012345 front rear ‘c’‘d’‘g’‘k’‘z’ 012345 Add ‘z’ to the queue: - -rear + 1 (increase) Reach end of storage!! rear

7 Queues Implementation 7 Circular Arrays We can overcome the inefficient use of the space by using a circular array. In this way, as entry are added and removed from the queue, the head will continually chasing the tail around the array, thus you don’t have to worry about running out of space unless the queue is fully occupied

8 Queues Implementation 8 Circular Arrays

9 Queues Implementation 9   Circular Arrays : Boundary Condition to indicate whether a queue is empty or full. If there is exactly one entry in the queue, then the front index will equal to the rear index. When this one entry is removed, then the front will increase by 1, so that an empty queue is indicated when the rear is one position before the front.

10 Queues Implementation 10   Circular Arrays : Boundary Condition Now, suppose that the queue is nearly full whereby it only has one empty position left. Then the rear will be only one position behind the front, the same condition as empty queue.

11 Queues Implementation 11   Circular Arrays : Possible Solution Leaving one position to be empty in the array. full queue is when the rear is two positions behind the front. Introduce a new variable to indicate the queue is full or not. The variable could either:   A Boolean variable that will be used when the rear comes just before the front to indicate whether the queue is full or not. or   An integer variable that counts the number of entries in the queue. Use special value for rear and/or front indices. For example, the array entries are indexed from 0 to MAX-1; then an empty queue could be indicated by setting the rear index to –1.

12 Circular Queues in C -Array Concept- 12 PART 1

13 Sample Program 1 #define MAZQUEUE 10 typedef int QueueIndex; typedef char QueueEntry; typedef struct queue{ int count; QueueIndex front; QueueIndex rear; QueueEntry entry[MAXQUEUE]; } Queue; 13 determine the maximum item in the queue to avoid program crash to show current index/position in queue Determine front and rear of queue Size of array “entry”

14 Continue… Several operations that can be performed to a Queue : 1. Create a queue 2. Test for an empty queue 3. Test for a full queue 4. Append (add) an item into queue 5. Serve (delete) an item from the queue 6. return the number of entries in the queue 14 void CreateQueue( Queue *q) { q count = 0; q front = 0; q rear = -1; } void CreateQueue( Queue *q) { q count = 0; q front = 0; q rear = -1; } bool QueueEmpty(Queue *q){ return (q->count count <= 0);} bool QueueEmpty(Queue *q){ return (q->count count <= 0);} bool QueueFull(Queue *q){ return ( q->count >= MAXQUEUE); return ( q->count >= MAXQUEUE);} bool QueueFull(Queue *q){ return ( q->count >= MAXQUEUE); return ( q->count >= MAXQUEUE);} void Append(QueueEntry x, Queue *q){ if (QueueFull(q)) if (QueueFull(q)) printf ("full queue" ); else { else {q->count++; q->rear = (q->rear + 1 ) % MAXQUEUE; q->entry[q->rear] = x; }} void Append(QueueEntry x, Queue *q){ if (QueueFull(q)) if (QueueFull(q)) printf ("full queue" ); else { else {q->count++; q->rear = (q->rear + 1 ) % MAXQUEUE; q->entry[q->rear] = x; }} void Serve(QueueEntry *x, Queue *q){ if (QueueEmpty(q)) if (QueueEmpty(q)) printf ("empty queue"); printf ("empty queue"); else { else {q->count--; *x = q->entry[q->front]; q->front =(q->front + 1 ) % MAXQUEUE; }} void Serve(QueueEntry *x, Queue *q){ if (QueueEmpty(q)) if (QueueEmpty(q)) printf ("empty queue"); printf ("empty queue"); else { else {q->count--; *x = q->entry[q->front]; q->front =(q->front + 1 ) % MAXQUEUE; }} int QueueSize( Queue *q){ return q->count; } int QueueSize( Queue *q){ return q->count; }

15 Sample Program 1 void main() { int a = 3, b = 8, c = 12; Queue q; CreateQueue(&q); //create a queue Append(a, &q); //insert value of a in queue Append(c, &q); //insert value of c in queue Serve(&b,&q); //delete entry in queue ;store in b printf("B is now : %d\n", b); //display value b } output??? 15

16 Circular Queues in C -Linked List Concept- 16 PART 2

17 Queues Implementation 17 Linked List Overview

18 Data Structure typedef char QueueEntry; typedef struct queuenode { QueueEntry info; struct queuenode *next; } QueueNode; typedef struct queue { QueueNode *front; QueueNode *rear; } Queue; Queue *z; 18

19 Create Queue void CreateQueue(Queue *q){ q->front = q->rear = NULL; } 19 bool QueueEmpty( Queue *q) { return(q->front == NULL); } Test Empty

20 Create Node QueueNode *CreateNode(QueueEntry x) { QueueNode *p; p=QueueNode*)malloc(sizeof(QueueNode)); if(!p) printf("Unable to allocate memory "); else { p->info = x; p->next = NULL; } return p; } 20

21 Append void Append(QueueEntry x, Queue *q) { QueueNode *np; np=CreateNode(x); if (np == NULL) printf ("Can’t append – queue full" ); else if (QueueEmpty(q)) q->front = q->rear = np; else { q->rear->next = np; q->rear = np; } } 21

22 Serve void Serve(QueueEntry *x, Queue *q) { QueueNode *p; if(QueueEmpty(q)) printf (“Fail…Queue is empty"); else { p = q->front; q->front = q->front->next; if(QueueEmpty(q)) // if(q->front == NULL) q-> rear = NULL; *x = p->info; free(p); } } 22

23 Main () void main() { char alp; CreateQueue(z); Append('i',z); Append('f',z); Append('a',z); Serve(&alp,z); printf("Alp is %c \n",alp); } output??? 23

24 Queue Application Queue is used to synchronize between two different processes that run at different speed. For instance, CPU and Printer. A buffer or a spooler that uses queue techniques introduced, store all the printed item pass by CPU. It’s because of the CPU operation is faster. By using a spooler, CPU time can be better utilized. Queue can also be implemented as input/output buffer in operating system that wants to handle many devices with different speeds. 24

25 Circular Queues in C -Linked List Concept- 25 EXERCISE

26 26 Question

27 27 Question a) a)Give the data structure of the queue. a) a)Assume a queue Q of type above has been created and initialized. Write the function that receives Q as its parameter and compute the total amount of all orders.

28 That’s all for today TQ.. 28


Download ppt "CHAPTER 4 QUEUE CSEB324 DATA STRUCTURES & ALGORITHM."

Similar presentations


Ads by Google