Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Chapter 3 Queues Andreas Savva. 2 Queues A data structure modeled after a line of people waiting to be served. A data structure modeled.

Similar presentations


Presentation on theme: "Data Structures Chapter 3 Queues Andreas Savva. 2 Queues A data structure modeled after a line of people waiting to be served. A data structure modeled."— Presentation transcript:

1 Data Structures Chapter 3 Queues Andreas Savva

2 2 Queues A data structure modeled after a line of people waiting to be served. 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) The first entry which is inserted is the first one that will be removed. (First In First Out : FIFO)

3 3 Application of Queues in our everyday life

4 4 Waiting for a printer Waiting for a printer Access to disk storage Access to disk storage In a time-sharing system use of the CPU In a time-sharing system use of the CPU Along with stacks, queues are one of the simplest data structures Application of Queues Most common than Stacks

5 5 Basic Operations for Queues Create a queue, leaving it empty. Create a queue, leaving it empty. Clear the queue leaving it empty. Clear the queue leaving it empty. Test whether the queue is Empty. Test whether the queue is Empty. Test whether the queue is Full. Test whether the queue is Full. Return the Size of the queue. Return the Size of the queue. Retrieve the first entry of the queue, provided the queue is not empty. 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. 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. 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. Append a new entry at the end of the queue, provided that the queue is not full. Print all the entries of the queue. Print all the entries of the queue.

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

7 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 8 Data Structure - Array 1 *... [0][1] [2] [max-1] One item: n items: n *****... [0][1] [2] [max-1] [n-1] [n] frontrear frontrear front is at position 0 rear is at position n-1

9 9 Create Queue Queue::Queue() // Pre:None. // Post:Initialize Queue to be empty. { } We use a constructor to initialize a new created queue as empty. 0... [0][1] [2] [max-1] [n-1] [n]countentry

10 10 Clear void Queue::clear() // Pre:None. // Post:All entries in the Queue have been //removed; it is now empty. { } 0... [0][1] [2] [max-1] [n-1] [n]countentry

11 11 Empty bool Queue::empty() const // Pre:None. // Post:Return true if the Queue is empty, //otherwise false is returned. { } 0... [0][1] [2] [max-1] [n-1] [n]countentry

12 12 Full bool Queue::full() const // Pre:None. // Post:Returns true if the Queue is full, //otherwise false is returned. { } max *******... [0][1] [2] [max-1] [n-1] [n]countentry

13 13 Size int Queue::size() const // Pre:None. // Post:Return the number of entries in the Queue. { } n *****... [0][1] [2] [max-1] [n-1] [n]countentry

14 14 entry 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. { } n *****... [0][1] [2] [max-1] [n-1] [n]count Retrieve

15 15 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. { } n ABCDE... [0][1] [2] [max-1] [n-1] [n]count n-1 BCDE count entry

16 16 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. { } n ABCDE... [0][1] [2] [max-1] [n-1] [n]count n-1 BCDE item = A entry

17 17 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. { } n *****... [0][1] [2] [max-1] [n-1] [n]count * n+1 entry

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

19 19 Data Structure – Circular Array

20 20 frontrear frontrear Queue containing one item Empty Queue frontrear Queue with one empty position frontrear Full Queue

21 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: int next(int n) const; int front, rear; Queue_entry entry[max]; };

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

23 23 Create Queue Queue::Queue() { } 0 1 2 3 4 5 6 78 9 10 11 12 13 14 15=max-1 front rear=-1

24 24 Clear void Queue::clear() { } 0 1 2 3 4 5 6 78 9 10 11 12 13 14 15=max-1 front rear=-1 rear * ** *

25 25 Empty bool Queue::empty() const { } 0 1 2 3 4 5 6 78 9 10 11 12 13 14 15=max-1 front rear=-1 rear * ** * Not Empty Empty

26 26 Next position int Queue::next(int n) const { } 0 1 2 3 4 5 6 78 9 10 11 12 13 14 15=max-1 front rear * ** * * * * * * * * front = next(front); rear = next(rear); entry[rear] = ‘*’

27 27 0 1 2 3 4 5 6 78 9 10 11 12 13 14 15=max-1 rear front ** * * * * * * * * * * Full bool Queue::full() const { } * * * * NOT FULL FULL

28 28 Size int Queue::size() const { } 0 1 2 3 4 5 6 78 9 10 11 12 13 14 15=max-1 front rear * ** * * rear – front + 1 max + * * * * * * * front rear

29 29 0 1 2 3 4 5 6 78 9 10 11 12 13 14 15=max-1 * * * * * * * front rearRetrieve Error_code Queue::retrieve(Queue_entry &item) const { } Retrieve

30 30 Serve or Dequeue Error_code Queue::serve() { } 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15=max-1 * * * * * * * front rear *

31 31 Retrieve and Serve Error_code Queue::retrieve_and_serve(Queue_entry &item) { } 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15=max-1 * * * * * * * front rear *Retrieve

32 32 Append or Enqueue Error_code Queue::append(const Queue_entry &item) { } rear 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15=max-1 * * * * * * * front * *

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


Download ppt "Data Structures Chapter 3 Queues Andreas Savva. 2 Queues A data structure modeled after a line of people waiting to be served. A data structure modeled."

Similar presentations


Ads by Google