ADT Queue 1 Queues 2 Queue of cars 3 Queue at logical level A - - PDF document

adt queue
SMART_READER_LITE
LIVE PREVIEW

ADT Queue 1 Queues 2 Queue of cars 3 Queue at logical level A - - PDF document

ADT Queue 1 Queues 2 Queue of cars 3 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. 4 Queue at Logical Level


slide-1
SLIDE 1

1

ADT Queue

2

Queues

slide-2
SLIDE 2

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.

slide-3
SLIDE 3

5

Queue at Logical Level

  • What operations would be appropriate for a

queue?

6

Stack Operations

Transformers

  • MakeEmpty
  • Enqueue
  • Dequeue

Observers

  • IsEmpty
  • IsFull

change state

  • bserve state
slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

21

Linked-Structure implementation

  • f Queue

How do you define the data member of Queue?

22

Linked-Structure implementation

  • f Queue
slide-12
SLIDE 12

23

Linked-Structure implementation

  • f Queue