+ All Categories
Home > Documents > queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level...

queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level...

Date post: 27-Jul-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
12
1 ADT Queue 2 Queues
Transcript
Page 1: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

1

ADT Queue

2

Queues

Page 2: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

3

Queue of cars

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 3: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

5

Queue at Logical Level

• What operations would be appropriate for a queue?

6

Stack OperationsTransformers

• MakeEmpty • Enqueue • Dequeue

Observers • IsEmpty • IsFull

change state

observe state

Page 4: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

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)

8

Queue: Logical level

Page 5: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

9

Array-based Implementation

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 6: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

11

Array-based Implementation

• An array with the front floats

What if we enqueue X, Y and Z?

12

Array-based Implementation

• An array with the front floats, circular array

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

Page 7: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

13

Array-based Implementation

Empty Queue

Full Queue

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

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 8: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

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

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 9: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

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

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 10: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

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

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 11: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

21

Linked-Structure implementation of Queue

How do you define the data member of Queue?

22

Linked-Structure implementation of Queue

Page 12: queue - storm.cis.fordham.eduzhang/cs2200/slides/queue_handout.… · 7 Queue at Application Level • For what types of problems would be queue be useful for? • various servers

23

Linked-Structure implementation of Queue


Recommended