queue - fordham university7 queue at application level • for what types of problems would be queue...

23
1 ADT Queue

Upload: others

Post on 17-Nov-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

1

ADT Queue

Page 2: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

2

Queues

Page 3: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

3

Queue of cars

Page 4: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

4

Queue at logical level

• A queue is an ADT in which elements are added to the rear and removed from the front

• A queue is a FIFO “last in, first out” structure.

Page 5: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

5

Queue at Logical Level

• What operations would be appropriate for a queue?

Page 6: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

6

Stack OperationsTransformers

• MakeEmpty • Enqueue • Dequeue

Observers • IsEmpty • IsFull

change state

observe state

Page 7: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

7

Queue at Application Level

• For what types of problems would be queue be useful for?

• various servers that serve requests in First Come First Serve order:

• printer server (a queue of print jobs), • disk driver (a queue of disk input/output

requests) • CPU scheduler (a queue of processes

waiting to be executed)

Page 8: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

8

Queue: Logical level

Page 9: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

9

Array-based Implementation

Page 10: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

10

Array-based Implementation

• An array with the front queue always in the first position

Dequeue() is inefficient: it takes time linear to queue length to move all elements forward

Enqueue A, B, C, D:

Dequeue:

need to shift all items

Page 11: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

11

Array-based Implementation

• An array with the front floats

What if we enqueue X, Y and Z?

Page 12: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

12

Array-based Implementation

• An array with the front floats, circular array

How to wrap around? (rear+1) % 5

Page 13: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

13

Array-based Implementation

Empty Queue

Full Queue

Need to differentiate! sol: * add a length member * reserve an empty slot

Page 14: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

14

Array-based Implementation

• An array with front indicate the slot before the front item, and this slot doest not store anything)

Empty queue: front==rear

Full queue: front==rear+1

Page 15: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

15

Array-based Implementation

private: int front; // index of front element -1 int rear; //index of queue rear element int maxQue; //size of array

ItemType * items; };

Page 16: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

16

Array-based implementation

QueType::QueType(int max=500) // Parameterized class constructor // Post: maxQue, front, and rear have been initialized. // The array to hold the queue elements has been dynamically // allocated. { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; }

Page 17: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

17

Array-based implementation

QueType::~QueType() // Parameterized class constructor // Post: maxQue, front, and rear have been initialized. // The array to hold the queue elements has been dynamically // allocated. { delete [] items; }

Page 18: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

18

Array-based implementation

void QueType::Enqueue(ItemType newItem) // Post: If (queue is not full) newItem is at the rear of the queue; // otherwise a FullQueue exception is thrown. { if (IsFull()) throw FullQueue(); else { rear = (rear +1) % maxQue; items[rear] = newItem; } }

Page 19: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

19

Array-based implementation

void QueType::Dequeue(ItemType& item) // Post: If (queue is not empty) the front of the queue has been // removed and a copy returned in item; // othersiwe a EmptyQueue exception has been thrown. { if (IsEmpty()) throw EmptyQueue(); else { front = (front + 1) % maxQue; item = items[front]; } }

Page 20: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

20

Array-based implementation

bool QueType::IsEmpty() const // Returns true if the queue is empty; false otherwise. { return (rear == front); }

bool QueType::IsFull() const // Returns true if the queue is full; false otherwise. { return ((rear + 1) % maxQue == front); }

Page 21: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

21

Linked-Structure implementation of Queue

How do you define the data member of Queue?

Page 22: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

22

Linked-Structure implementation of Queue

Page 23: queue - Fordham University7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers that serve requests in First Come First Serve

23

Linked-Structure implementation of Queue