Download presentation

Presentation is loading. Please wait.

Published byMarianna Dede Modified over 3 years ago

1
Queues CS-212 Dick Steflik

2
Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their chronological order (oldest first) examples: grocery store line, tube feed ammunition magazine, digital shift register,

3
Applications discrete event simulation - digital delay line task scheduling in an operation system staging area for data acquisition systems I/O buffers print queues sorting

4
front rear checkout

5
ADT Methods create – make the queue and initialize empty enqueue - add an item (at the back, move it up as far as possible dequeue - return the value of the item at the front of the queue then delete the item and move all items forward one position. destroy – dispose of the queue

6
Overflow / Underflow Same problem as stacks –trying to dequeue from an empty queue –trying to enqueue to a full queue Solution: –isEmpty - return false/true depending if the queue is empty; true if it is, false otherwise –isFull – return false/ depending if the queue is empty; true if it is, false otherwise

7
Data strategies use an array to hold items and use an int as an index for the array to indicate where the back of the queue is same as above, but use a dynamic array same as above but treat array as if it were circular and use two ints, one to indicate the front and the other to indicate the back make a type using a struct with two ints for the front and rear pointers and an array for the data part of the queue use a struct to define a node and add nodes dynamically as they are needed; use one static pointer to a node to point at the front node and another to point at the back node.

8
Simple array implementation #define front 0 //assume front is always at location 0 of data int back = 0; int size = 0; int data[MAXSIZE] = {0}; void enqueue(int a) { data[back] = a ; back++; size++ }; int dequeue(void) { int temp = data[front] ; for (i=0 ; i < back ; i++) data[i] = data[i+i] ; back--; return temp; }; int isEmpty() { return back == 0; }; int isFull() { back == MAXSIZE; };

9
Static struct Implementation typedef struct { int front,rear; int data[MAXQSIZE] queue; void enqueue(queue * p, int v) { if (isFull(p)) printf(“queue is full \n”); else { p->data[p->rear] = v; p->rear += 1; } int main() { queue q1 = {0}; enqueue(&q1,50); }

10
Circular Queue Please read: http://en.wikipedia.org/wiki/Circular_bufferhttp://en.wikipedia.org/wiki/Circular_buffer Think of the array as being circular; i.e. the item following the item in the last array position is the first array position, this can be done using the mod (%) operator. Mod returns the integer remainder after a division. Assume the data portion of a queue has 10 elements and first and last start at the same position (this is the empty situation), lets say 5. 0 1 2 3 4 5 6 7 8 9 front rear empty

11
adding enqueue(1) enqueue(2) enqueue(3) enqueue(4) enqueue(5) enqueue(6) 0 1 2 3 4 5 6 7 8 9 front rear 1 234 5 6 rear always points to next place to add so (rear++)%size when we added the 5, rear was 9 and (9+1)%10 = 0 so when we add the 6, rear was 0 and (0+1)%10 = 1 the queue is empty if front == rear, how do we sense a full condition?

12
when is it full? add a count element to the queue that always has the current number of elements –enqueue always increments the count –dequeue always decrements the count require that only maxsize-1 elements can ever be used, there is always one unused element (i.e. |rear-front| > 1), or if |rear-front| == 1 then queue is full

13
Priority Queues Operates like a queue except entry is at the back but items move up to the end of their priority list 11234 2 new item 11223 frontback 4

14
Priority Queues Because of the possible high amount of data movement (in moving an item up in the queue) there are better ways of implementing priority queues as either linked lists or trees. A linked list would produce a possible O(n) and a Tree could approach O(log 2 n) for insertion and O(1) for retrieval. We’ll examine these later in the course

Similar presentations

OK

Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.

Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Free ppt on mind power Ppt on eye os means Ppt on zener diode voltage Ppt on teachers day greetings Ppt on project schedule Ppt on ready to serve beverages in spanish Maths ppt on surface area and volume Ppt on satellite orbital locations Ppt on 60 years of indian parliament videos Ppt on attribution theory and child abuse