Download presentation
Presentation is loading. Please wait.
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 Operations
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
FIFO list - only front element is visible
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 (remove item 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 uses mod operator to "circle back to start" 1st checks for empty queue
7
Static-array Implementation Example
typedef Complx QueueElement; const int CAPACITY = 8; int myFront, myBack; QueueElement myQueue[CAPACITY]; Complx X; enqueue(&myArray,X); -1 myFront myArray 7 6 5 4 3 2 1 myBack -1 initially empty (myFront, myBack are negative)
8
Caveats could be too small could be too large (wastes space)
must be careful managing index going past the "end" solution (TBD) is dynamic storage Lab2: build a queue. enqueue, front-output in seq'l order, dequeue
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.