330 likes | 463 Views
Andreas Savva. Data Structures. Chapter 3 Queues. Queues. A data structure modeled after a line of people waiting to be served. The first entry which is inserted is the first one that will be removed. ( First In First Out : FIFO ). Application of Queues in our everyday life.
E N D
Andreas Savva Data Structures Chapter 3 Queues
Queues • A data structure modeled after a line of people waiting to be served. • The first entry which is inserted is the first one that will be removed. (First In First Out : FIFO)
Application of QueuesMost common than Stacks • Waiting for a printer • Access to disk storage • In a time-sharing system use of the CPU • Along with stacks, queues are one of the simplest data structures
Basic Operations for Queues • Create a queue, leaving it empty. • Clear the queue leaving it empty. • Test whether the queue is Empty. • Test whether the queue is Full. • Return the Size of the queue. • Retrieve the first entry of the queue, provided the queue is not empty. • Serve: remove the first entry from the queue, provided the queue is not empty. • Retrieve and Serve : retrieves and remove the first entry from the queue, provided the queue is not empty. • Append a new entry at the end of the queue, provided that the queue is not full. • Print all the entries of the queue.
The Class Queue class Queue methods: Queue (constructor) clear empty full size retrieve serve retrieve_and_serve append print Data members
Stack implementation - Array typedef double Queue_entry; enum Error_code {success,overflow,underflow}; const int max = 100; class Queue { public: Queue(); void clear(); bool empty() const; bool full() const; int size() const; Error_code retrieve(Queue_entry &) const; Error_code serve(); Error_code retrieve_and_serve(Queue_entry &); Error_code append(const Queue_entry &); void print() const; private: int count; Queue_entry entry[max]; };
. . . . . . 1 n * * * * * * [2] [2] [n-1] [0] [0] [1] [1] [n] [max-1] [max-1] Data Structure - Array • frontis at position 0 • rearis at position n-1 front rear One item: front rear n items:
. . . 0 [2] [n-1] [0] [1] [n] [max-1] count Create Queue We use a constructor to initialize a new created queue as empty. Queue::Queue() // Pre: None. // Post: Initialize Queue to be empty. { } entry
. . . 0 [2] [n-1] [0] [1] [n] [max-1] count Clear void Queue::clear() // Pre: None. // Post: All entries in the Queue have been // removed; it is now empty. { } entry
. . . 0 [2] [n-1] [0] [1] [n] [max-1] count Empty bool Queue::empty() const // Pre: None. // Post: Return true if the Queue is empty, // otherwise false is returned. { } entry
. . . max * * * * * * * [2] [n-1] [0] [1] [n] [max-1] count Full bool Queue::full() const // Pre: None. // Post: Returns true if the Queue is full, // otherwise false is returned. { } entry
. . . n * * * * * [2] [n-1] [0] [1] [n] [max-1] count Size int Queue::size() const // Pre: None. // Post: Return the number of entries in the Queue. { } entry
. . . n * * * * * [2] [n-1] [0] [1] [n] [max-1] Retrieve count Retrieve Error_code Queue::retrieve(Queue_entry &item) const // Pre: None. // Post: If the Queue is not empty, the front of the queue is recorded // as item. Otherwise an Error_code of underflow is return. { } entry
. . . n A B C D E [2] [n-1] [0] [1] [n] [max-1] count n-1 B C D E Serve or Dequeue Error_code Queue::serve() // Pre: None. // Post: If the Queue is not empty, the front of the Queue is removed // Otherwise an Error_code of underflow is returned. { } count entry
. . . n A B C D E [2] [n-1] [0] [1] [n] [max-1] count n-1 B C D E Retrieve and Serve Error_code Queue::retrieve_and_serve(Queue_entry &item) // Pre: None. // Post: If the Queue is not empty, the front of the queue is removed and // recorded as item. Otherwise an Error_code of underflow is return. { } item = entry A
count . . . n * * * * * [2] [n-1] [0] [1] [n] [max-1] Append or Enqueue Error_code Queue::append(const Queue_entry &item) // Pre: None. // Post: If the Queue is not full, item is added at the end of the Queue. // If the Queue is full, an Error_code of overflow is returned // and the Queue is left unchanged. { } entry n+1 *
Print void Queue::print() const // Pre: None. // Post: Display the entries of the Queue. { }
Queue containing one item front rear Empty Queue -1 front rear Queue with one empty position rear front Full Queue rear front
The class Queue – Circular Array class Queue { public: Queue(); void clear(); bool empty() const; bool full() const; int size() const; Error_code retrieve(Queue_entry &) const; Error_code serve(); Error_code retrieve_and_serve(Queue_entry &); Error_code append(const Queue_entry &); void print() const; private: intnext(int n) const; intfront, rear; Queue_entry entry[max]; };
0 15=max-1 14 1 13 2 3 12 4 11 5 10 6 9 7 8 Circular Queue
front rear=-1 0 15=max-1 14 1 13 2 3 12 4 11 5 10 9 6 7 8 Create Queue Queue::Queue() { }
0 15=max-1 14 1 13 2 3 12 * * * * 4 11 5 10 front rear 9 6 7 8 Clear void Queue::clear() { } rear=-1
0 15=max-1 14 1 13 2 3 12 * * * * 4 11 5 10 front rear 9 6 7 8 Empty bool Queue::empty() const { } Not Empty Empty rear=-1
* 0 15=max-1 * 14 1 * 13 2 * 3 12 * * * * 4 11 5 10 9 6 7 8 Next position rear int Queue::next(int n) const { } * * * front = next(front); rear = next(rear); entry[rear] = ‘*’ front
* * * 0 15=max-1 * * 14 1 * * 13 2 * * 3 12 * * * * * 4 11 5 10 rear front 9 6 7 8 Full bool Queue::full() const { } * NOT FULL FULL *
front * * rear * * 0 15=max-1 * 14 * 1 * 13 2 3 12 * * * * * 4 11 rear front 5 10 9 6 7 8 Size int Queue::size() const { } max + rear – front + 1
front rear Retrieve * * * * 0 15=max-1 * 14 * 1 * 13 2 3 12 11 4 5 10 6 9 7 8 Retrieve Error_code Queue::retrieve(Queue_entry &item) const { }
front rear * * * * 0 15=max-1 * 14 * 1 * 13 2 3 12 11 4 5 10 6 9 7 8 Serveor Dequeue * Error_code Queue::serve() { }
front rear Retrieve * * * * 0 15=max-1 * 14 * 1 * 13 2 3 12 11 4 5 10 6 9 7 8 RetrieveandServe * Error_code Queue::retrieve_and_serve(Queue_entry &item) { }
front * * * * * 0 15=max-1 * 14 * 1 * 13 2 3 12 11 4 5 10 6 9 7 8 AppendorEnqueue rear * Error_code Queue::append(const Queue_entry &item) { }
Print void Queue::print() const { }