Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"

Similar presentations


Presentation on theme: "Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs""— Presentation transcript:

1 Queues 1

2 Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs" waiting for the CPU or an I/O device –tokens waiting to be processed –signals on a channel (wired or wireless) Simulations 2

3 3 Structural Concept A sequence of data items –Items may be ANY type integers chars arrays structs Operations –Items can be removed only at the front –Items can be added only at the end –Only the front item may be viewed

4 Queue enqueue dequeue FIFO list - only front element is visible front index 4

5 5 The Queue ADT Operations –Construct a queue –Test for queue is empty/full –Enqueue (add new item at end) –Front (retrieve value of item at front) does not remove item –Dequeue (remove item from front) next item moves to the front

6 6 Static-Array Implementation Operations & constants –Q_MAX specifies item total –enqueue increments index use mod operator to "circle back to start" 1 st checks for full queue –dequeue increments index uses mod operator to "circle back to start" 1 st checks for empty queue

7 Static-array Implementation Example myFront myArray 7654321076543210 typedef Complx QueueElement; const int CAPACITY = 8; int myFront, myBack; QueueElement myQueue[CAPACITY]; Complx X; enqueue(&myArray,X); initially empty (myFront, myBack are negative) myBack 7

8 Caveats could be too small could be too large (wastes space) must be careful managing index solution (TBD) is dynamic storage going past the "end" 8

9 Solutions careful mgmt of index values (front, back) bigger queue circular queue dynamic array (TBD) 9

10 10 Circular Queue Each array element (a node) is 2 items –index of next node –data Last node has index of first node Or keep separate int index –subscript of "last" node –1 st node = MOD(lastnode+1,CAPACITY)

11 Some More Uses of a Queue job scheduling graphic display sequencing disk I/O access message processing spaceship launch sequence data "buffering" 11

12 Buffering data produced faster than can be used need to store it storage is called a "buffer" any stack or queue can be called a buffer 12


Download ppt "Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs""

Similar presentations


Ads by Google