Download presentation

Presentation is loading. Please wait.

Published byKaelyn Chelton Modified about 1 year ago

1
Algorithms and data structures Protected by

2
Creative Commons n You are free to: share — copy and redistribute the material in any medium or format share — copy and redistribute the material in any medium or format adapt — remix, transform, and build upon the material adapt — remix, transform, and build upon the material n Under the following terms: Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. NonCommercial — You may not use the material for commercial purposes. NonCommercial — You may not use the material for commercial purposes. ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. Text copied from Algorithms and data structures, FER Notices: You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. 2 / 17

3
Queues

4
Algorithms and data structures, FER Queue 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 / rear 2 front

5
Algorithms and data structures, FER Circularity 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

6
Algorithms and data structures, FER Circular list 6 / 17 queue->front queue->rear

7
Algorithms and data structures, FER An empty queue - circular array implementation 7 / 17 queue->front queue->rear 0

8
Algorithms and data structures, FER A full queue - circular array implementation queue->rear queue->front 8 / 17

9
Algoritmi i strukture podataka, FER9 / 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; } Calling program: #define MAXQUE 16 enqueue(5, &queue); enqueue(2, &queue); enqueue(7, &queue); enqueue(-4, &queue); enqueue(1, &queue); 16

10
Algorithms and data structures, FER 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 Calling program: #define MAXQUE 16 dequeue(&number, &queue); / 17

11
Algorithms and data structures, FER queue->rear queue->front 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; } Calling program: #define MAXQUE 16 dequeue(&number, &queue); 11 / 17

12
Algorithms and data structures, FER 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

13
Algorithms and data structures, FER 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

14
Algorithms and data structures, FER 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

15
Algorithms and data structures, FER 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 / 17

16
Algorithms and data structures, FER Dequeueing 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

17
Algorithms and data structures, FER 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

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google