1 / 12

Queues

Queues. 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 .

jeroen
Download Presentation

Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Queues

  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

  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 front enqueue dequeue index FIFO list - only front element is visible

  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 (removeitem from front) • next item moves to the front

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

  7. myArray 7 6 5 4 3 2 1 0 myFront -1 Static-array Implementation Example typedefComplxQueueElement; constint CAPACITY = 8; intmyFront, myBack; QueueElementmyQueue[CAPACITY]; Complx X; enqueue(&myArray,X); myBack -1 initiallyempty (myFront, myBack are negative)

  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"

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

  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 • 1st 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"

  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

More Related