Queues CS212 Dick Steflik Queues First In First

  • Slides: 14
Download presentation
Queues CS-212 Dick Steflik

Queues CS-212 Dick Steflik

Queues • First In, First Out operation – FIFO • As items are added

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,

Applications • • discrete event simulation digital delay line task scheduling in an operation

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

front rear checkout

front rear checkout

ADT Methods • create – make the queue and initialize empty • enqueue -

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

Overflow / Underflow • Same problem as stacks – trying to dequeue from an

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

Data strategies • use an array to hold items and use an int as

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.

Simple array implementation #define front 0 //assume front is always at location 0 of

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 is. Empty() { return back == 0; }; int is. Full() { back == MAXSIZE; };

Static struct Implementation typedef struct { int front, rear; int data[MAXQSIZE] void enqueue(queue *

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

Circular Queue Please read: http: //en. wikipedia. org/wiki/Circular_buffer Think of the array as being

Circular Queue Please read: http: //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 empty front rear

adding enqueue(1) enqueue(2) enqueue(3) enqueue(4) enqueue(5) enqueue(6) 0 1 2 6 3 4 5

adding enqueue(1) enqueue(2) enqueue(3) enqueue(4) enqueue(5) enqueue(6) 0 1 2 6 3 4 5 6 7 1 2 3 8 4 9 5 front 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?

when is it full? • add a count element to the queue that always

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

Priority Queues • Operates like a queue except entry is at the back but

Priority Queues • Operates like a queue except entry is at the back but items move up to the end of their priority list new item front back 1 1 2 3 1 1 2 2 4 3 4 2

Priority Queues • Because of the possible high amount of data movement (in moving

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