1 / 33

Data Structures

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.

donkor
Download Presentation

Data Structures

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. Andreas Savva Data Structures Chapter 3 Queues

  2. 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)

  3. Application of Queuesin our everyday life

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

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

  6. The Class Queue class Queue methods: Queue (constructor) clear empty full size retrieve serve retrieve_and_serve append print Data members

  7. 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]; };

  8. . . . . . . 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:

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

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

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

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

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

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

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

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

  17. 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 *

  18. Print void Queue::print() const // Pre: None. // Post: Display the entries of the Queue. { }

  19. Data Structure – Circular Array

  20. Queue containing one item front rear Empty Queue -1 front rear Queue with one empty position rear front Full Queue rear front

  21. 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]; };

  22. 0 15=max-1 14 1 13 2 3 12 4 11 5 10 6 9 7 8 Circular Queue

  23. 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() { }

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

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

  26. * 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

  27. * * * 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 *

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

  29. 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 { }

  30. 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() { }

  31. 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) { }

  32. 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) { }

  33. Print void Queue::print() const { }

More Related