ch07 - Ch 7 - Queues Queues were invented as an ADT for...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Ch 7 - Queues • Queues were invented as an ADT for "last in last out" data storage. • Queues are used to provide "fair" service to people in banks, buying tickets, etc. • Queues are also used in computer systems to provide "fair" services. o printer queues o operating system scheduling queues o communication buffers o event simulation
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Queue Operations • The Queue ADT normally has the following operations. create - make a new queue destroy - delete all data on queue insert - put data at the end of the queue remove - get data from the front of the queue full - check for more room in the queue empty - checks queue for any data available • Insert and Remove are also called Enqueue and Dequeue
Background image of page 2
Queue Interface • Assume we want to store integers in a Queue. class Queue { public: Queue (); ~ Queue (); void insert ( int item); int remove (); bool isFull (); bool isEmpty (); private: TBA; } • We can change int's above and store any other data type in the queue.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Array Based Queues • We can implement a queue using a fixed size array and an integer "end" that keeps track of where last data was inserted. • By default, we assume that the "front" is in position 0 and move data in queue accordingly. • This approach requires a lot of data to be copied. • initial queue • insert will increment "end" and put data in that position • remove will give us data at index 0 and shift all data left and decrement "end" Eg:
Background image of page 4
Insert / Remove Implementation void insert( int item ) { //check size if ( end < MAX_SIZE ) data[++end] = item; //print error message else cout << "queue overflow\n" } int remove () { int item = data[0]; //shift data for ( int i=0; i<end; i++) data[i] = data[i+1]; //return result if( end >= 0 ) data[end--] = -1; return item; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
• We can improve on the simple array based queue by moving "front" and "end" to the right as data is inserted or removed. • We avoid array bounds error by wrapping the "front" and "end" indices around. • initial queue
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

ch07 - Ch 7 - Queues Queues were invented as an ADT for...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online