# Algorithms and data structures Protected by 4.5.2015.

## Presentation on theme: "Algorithms and data structures Protected by 4.5.2015."— Presentation transcript:

Algorithms and data structures Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/ 4.5.2015

4.5.2015 Queues

Algorithms and data structures, FER4.5.2015Queue n Queue is a linear list where insertion ( enqueueing ) is performed on one end and removal ( dequeueing ) on the other. Principle FirstInFirstOut ( FIFO ) Principle FirstInFirstOut ( FIFO ) Two indices are used ( front & rear ) Two indices are used ( front & rear ) – The end of the queue where insertion is performed is rear – The other end where removal is performed is at the beginning of the queue, front functions: enqueue, dequeue functions: enqueue, dequeue 4 / 17 7-41 rear 2 front

Algorithms and data structures, FER4.5.2015Circularity n An efficient way of queue implementation using static structure is a one dimensional array of a given data type, used circularly Circularity is achieved using the operator modulo ( % ) Circularity is achieved using the operator modulo ( % ) n One element of the array remains empty In this way we can differ between an empty and a full queue In this way we can differ between an empty and a full queue – Empty queue: rear == front – Full queue: (rear + 1) % maxelements == front If the whole array were filled with data, an additional element counter would be required If the whole array were filled with data, an additional element counter would be required  RedPoljem (QueueByArray) typedef struct { type array[MAXQUE]; int rear, front; } Queue; 5 / 17

Algorithms and data structures, FER4.5.2015 Circular list 6 / 17 queue->front queue->rear 0 1 2 3 4 5 6 78 9 10 11 12 13 14 15 13 12 8 -5 9 19 5 21 -42

Algorithms and data structures, FER4.5.2015 An empty queue - circular array implementation 7 / 17 queue->front 0 1 2 3 4 5 6 78 9 10 11 12 13 14 15 queue->rear 0

Algorithms and data structures, FER4.5.2015 A full queue - circular array implementation 0 1 2 3 4 5 6 78 9 10 11 12 13 14 15 11 3 12 8 -5 9 19 21 -42 1 0 21 1 queue->rear queue->front 8 / 17

Algoritmi i strukture podataka, FER9 / 174.5.2015 queue->rear queue->frontEnqueueing int enqueue (type element, Queue *queue) { if ((queue->rear+1) % MAXQUE == queue->front) return 0; queue->rear++; queue->rear %= MAXQUE; queue->array[queue->rear] = element; return 1; } 0 1 2 5 6 7 8 9 10 11 12 13 14 15 3 4 1 0 3 8 9 74 6 1 2 5 27 -4 Calling program: #define MAXQUE 16 enqueue(5, &queue); enqueue(2, &queue); enqueue(7, &queue); enqueue(-4, &queue); enqueue(1, &queue); 16

Algorithms and data structures, FER4.5.2015 Dequeueing from a circular array - I int dequeue (type *element, Queue *queue) { if (queue->rear == queue->front) return 0; queue->front++; queue->front %= MAXQUE; *element = queue->array[queue->front]; return 1; } queue->rear queue->front 0 1 2 5 6 7 8 9 10 11 12 13 14 15 3 4 1 0 3 8 9 74 6 5 27 -4 Calling program: #define MAXQUE 16 dequeue(&number, &queue); 2 1 10 / 17

Algorithms and data structures, FER4.5.2015 queue->rear queue->front 0 1 2 5 6 7 8 9 10 11 12 13 14 15 3 4 1 0 3 8 9 74 6 1 2 5 Dequeueing from a circular array - II int dequeue (type *element, Queue *queue) { if (queue->rear == queue->front) return 0; queue->front ++; queue->front %= n; *element = queue->array[queue->front]; return 1; } 27 -4 Calling program: #define MAXQUE 16 dequeue(&number, &queue); 11 / 17

Algorithms and data structures, FER4.5.2015 Queue – list implementation I n A disadvantage while using array is the fixed memory allocation Engagement of not used memory (most often) Engagement of not used memory (most often) Insufficient memory (occasionally) Insufficient memory (occasionally) n List implementation with dynamic memory allocation Only memory in use is engaged Only memory in use is engaged The size of a queue is limited only due to the total available memory The size of a queue is limited only due to the total available memory typedef struct at atom; struct at { int element; struct at *next; }; typedef struct { atom *rear, *front; } Queue; next element atom 12 / 17

Algorithms and data structures, FER4.5.2015 Queue – list implementation II Empty queue 5242 Non empty queue  RedListom (QueueByList) queue->front queue->rear queue->front queue->rear void init_queue(Queue *queue){ queue->rear = NULL; queue->front = NULL; } 13 / 17

Algorithms and data structures, FER4.5.2015 Calling program: int enqueue (int element, Queue *queue) { atom *new; if (new = (atom*) malloc (sizeof (atom))) { new->element = element; new->next = NULL; if (queue->front == NULL) queue->front = new;// if queue is empty else (queue->rear)->next = new;// else, put at the end queue->rear = new;// remember the last return 1; } return 0; } Equeueing – empty queue Queue queue; init_queue(&queue); enqueue(52, &queue); 52 queue->front queue->rear new 14 / 17

Algorithms and data structures, FER4.5.2015 Enqueueing – non empty queue Calling program: int enqueue (int element, Queue *queue) { atom *new; if (new = (atom*) malloc (sizeof (atom))) { new->element = element; new->next = NULL; if (queue->front == NULL) queue->front = new;// if queue is empty else (queue->rear)->next = new;// else, put at the end queue->rear = new;// remember the last return 1; } return 0; } Queue queue; init_queue(&queue); enqueue (52, &queue); enqueue (42, &queue); 52 queue->front queue->rear new 42 15 / 17

Algorithms and data structures, FER4.5.2015Dequeueing int dequeue (int *element, Queue *queue) { atom *old; if (queue->front) { // if queue is not empty *element = queue->front->element; // element to be removed old = queue->front; // remember the current front queue->front = queue->front->next; // new front free (old); // release the memory of the removed element if (queue->front == NULL) queue->rear = NULL; // empty queue return 1; } return 0; } 52 queue->front queue->rear old 42 Calling program: dequeue(&number,&queue); 16 / 17

Algorithms and data structures, FER4.5.2015 Dequeueing of the last element int dequeue (int *element, Queue *queue) { atom *old; if (queue->front) { // if queue is not empty *element = queue->front->element; // element to be removed old = queue->front; // remember the current front queue->front = queue->front->next;// new front free (old);// release the memory of the removed element if (queue->front == NULL) queue->rear = NULL; // empty queue return 1; } return 0; } queue->front queue->rear old 42 Calling program: dequeue(&number,&queue); 17 / 17