Download presentation

Presentation is loading. Please wait.

Published byKaelyn Chelton Modified over 2 years ago

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

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 http://creativecommons.org/licenses/by-nc-sa/3.0/ 4.5.2015Algorithms 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
4.5.2015 Queues

4
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

5
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

6
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

7
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

8
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

9
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

10
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

11
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

12
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

13
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

14
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

15
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

16
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

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

Similar presentations

OK

Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.

Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Maths ppt on exponents and powers Free download ppt on forest and wildlife Ppt on applied operations research analyst Ppt on e marketing Ppt on taking notes Ppt on internal structure of cpu diagram Download ppt on oxidation and reduction calculator Ppt on 48 laws of power Ppt on drainage system for class 9 Ppt on product data management