ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List...

44
ADT Queue 1. 1. What is a Queue? What is a Queue? 2. 2. STL Queue STL Queue 3. 3. Array Implementation of Queue Array Implementation of Queue 4. 4. Linked List Implementation of Linked List Implementation of Queue Queue 5. 5. Priority Queue Priority Queue

Transcript of ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List...

Page 1: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

ADT Queue

1.1. What is a Queue?What is a Queue?

2.2. STL QueueSTL Queue

3.3. Array Implementation of QueueArray Implementation of Queue

4.4. Linked List Implementation of QueueLinked List Implementation of Queue

5.5. Priority QueuePriority Queue

Page 2: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

2

Queue

Which of the following cases use similar Which of the following cases use similar structures?structures? Cars lined up at tollgateCars lined up at tollgate Cars on a 4-lane highwayCars on a 4-lane highway Customers at supermarket check-outCustomers at supermarket check-out Books on a book shelfBooks on a book shelf Clients airport check-in counterClients airport check-in counter Pile of bath towels on linen closet shelfPile of bath towels on linen closet shelf

Page 3: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

3

Queue

Is a linear data structure with removal of Is a linear data structure with removal of items at one end (items at one end (frontfront) and insertion of ) and insertion of items at the opposite end (items at the opposite end (rearrear))

FIFO – first-in-first-outFIFO – first-in-first-out

front rear

Page 4: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

4

Queue Operations Enqueue(item)Enqueue(item)

Insert item at rearInsert item at rear Dequeue()Dequeue()

Remove item at frontRemove item at front Front()Front()

Return item at frontReturn item at front IsEmpty()IsEmpty()

Is queue empty?Is queue empty? IsFull()IsFull()

Is queue full?Is queue full?

Clear()Clear() Make queue emptyMake queue empty

Page 5: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

The Queue Operations

A queue is like a line A queue is like a line of people waiting for a of people waiting for a bank teller. The queue bank teller. The queue has a has a frontfront and a and a rearrear..

$ $

FrontRear

Page 6: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

The Queue Operations

New people must enter the queue at the New people must enter the queue at the rear. The C++ queue class calls this a rear. The C++ queue class calls this a pushpush, although it is usually called an , although it is usually called an enqueueenqueue operation. operation.

$ $

FrontRear

Page 7: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

The Queue Operations

When an item is taken from the queue, When an item is taken from the queue, it always comes from the front. The it always comes from the front. The C++ queue calls this a C++ queue calls this a poppop, although it , although it is usually called a is usually called a dequeuedequeue operation. operation.

$ $

FrontRear

Page 8: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

The Queue Class

The C++ standard The C++ standard template library has template library has a queue template a queue template class.class.

The template The template parameter is the parameter is the type of the items type of the items that can be put in that can be put in the queue.the queue.

template <class Item>class queue<Item>{public: queue( ); void push(const Item& entry); void pop( ); bool empty( ) const; Item front( ) const; …

};

Page 9: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

9

Array Implementation

Page 10: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

10

Problem with Array Implementation

8 23

12

7 14

front rear

With frequent queueing and dequeing, end of array can be reached quickly.

Can we somehow use the empty spaces?

Page 11: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

11

“Circular” Array

Page 12: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

12

Queue Implementation (2)

Suppose, currently,Suppose, currently, front = 5front = 5 rear = 9rear = 9

For next values,For next values, front = (front + 1) mod MAX_SIZE = 6front = (front + 1) mod MAX_SIZE = 6 rear = (rear + 1) mod MAX_SIZE = 0rear = (rear + 1) mod MAX_SIZE = 0

front rear

8 23

12

7 14

0 1 2 3 4 5 6 7 8 910

MAX_SIZE

Page 13: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

13

queue.h (Declarations)

#define MAX_SIZE 5#define NIL -1typedef char elemType;

class Queue {public: Queue(); void enqueue(elemType item); void dequeue(); elemType front(); bool isEmpty(); bool isFull();. . .

Page 14: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

14

queue.h (Declarations)

. . .private: elemType data[MAX_SIZE]; int front; int rear; int count; // to simply full, empty}; // Note ; ‘;’

Page 15: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

15

Queue() (Constructor)

Queue::Queue(){ front = 0; rear = -1; count = 0;}

Page 16: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

16

Enqueue()

void Queue::enqueue(elemType item){ if (!full()){ rear = (rear + 1) % MAX_SIZE; data[rear] = item; count++; }}

Page 17: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Efficiency of enqueue() operation

If it takes t seconds to enqueue an element If it takes t seconds to enqueue an element into a queue of 1000 elements, how long into a queue of 1000 elements, how long does it take to enqueue one into a queue of does it take to enqueue one into a queue of 2000 elements?2000 elements?

The same time.The same time. Thus, O(n) = 1.Thus, O(n) = 1.

17

Page 18: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

18

Dequeue()

void Queue::dequeue(){ if (!empty()){ result = data[front]; front = (front + 1) % MAX_SIZE; count--; }}

Page 19: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Efficiency of dequeue() operation

If it takes t seconds to dequeue an element If it takes t seconds to dequeue an element from a queue of 1000 elements, how long from a queue of 1000 elements, how long does it take to dequeue one from a queue of does it take to dequeue one from a queue of 2000 elements?2000 elements?

The same time.The same time. Thus, O(n) = 1.Thus, O(n) = 1.

19

Page 20: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Your Turn

Suppose you want to add another queue Suppose you want to add another queue operation:operation:void clear();void clear();// Postcondition: queue is made empty.// Postcondition: queue is made empty.

Write the implemenation code for the Write the implemenation code for the clear() clear() operation.operation.

Page 21: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

21

Front()

elemTypeQueue::front(){ if (!empty()){ return data[front]; else return NIL; }}

Page 22: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

22

empty()

bool Queue::empty(){ return count == 0;}

Page 23: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Your Turn

Write the implementation code for the operation Write the implementation code for the operation full().full().

bool Queue::isFull();bool Queue::isFull();// Postcondition: True is returned when queue// Postcondition: True is returned when queue// is full; false, otherwise.// is full; false, otherwise.

Page 24: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Array Implementation

Easy to implementEasy to implement But it has a limited capacity with a fixed arrayBut it has a limited capacity with a fixed array Or you must use a dynamic array for an Or you must use a dynamic array for an

unbounded capacityunbounded capacity Special behavior is needed when the rear reaches Special behavior is needed when the rear reaches

the end of the array.the end of the array.

[ 0 ][ 0 ] [1][1] [ 2 ][ 2 ] [ 3 ][ 3 ] [ 4 ][ 4 ] [ 5 ][ 5 ] . . .. . .

4 8 6

sizesize3

firstfirst0

lastlast2

Page 25: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Linked List Implementation

10

15

7

null

13

A queue can also be A queue can also be implemented with a linked implemented with a linked list with both a head and a list with both a head and a tail pointer.tail pointer.

front_ptr

rear_ptr

Page 26: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Linked List Implementation

10

15

7

null

13

Which end do you think is Which end do you think is the front of the queue? the front of the queue? Why?Why?

front_ptr

rear_ptr

Page 27: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Linked List Implementation

10

15

7

nullfront_ptr

13

The head_ptr points to the The head_ptr points to the front of the list.front of the list.

Because it is harder to Because it is harder to remove items from the tail remove items from the tail of the list.of the list.

rear_ptr

Front

Rear

Page 28: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Your Turn

Write the class interface—public and Write the class interface—public and private sections—for the linked list version private sections—for the linked list version of class queue.of class queue.

Page 29: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

29

queue.h (Declarations)

typedef int elemType;#define NIL -1

class Queue {public: Queue(); void enqueue(elemType item); void dequeue(); elemType front(); bool isEmpty();. . .

Page 30: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Queue Implemented by Linked List

class Queue { . . .private: struct Node { elemType value; Node *next; Node (elemType item, Node *link){ value = item; next = link; }; } Node* front; Node* rear; int count;};

Page 31: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Constructor

Queue Set head and rear to NULL. Set count to 0End queue

Page 32: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Enqueue

void enqueue (elemType item) Let temp point to new node(item, NULL). If (empty queue) Then Let front and rear point to new node Else Set rear->next pointer point to temp End If

Let rear point to temp Increment countEnd enqueue

Page 33: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Enqueue

void enqueue (elemType item) Node *t = new Nod(item, NULL); if (isEmpty(){ front = t; rear = t; } else { rear->next = t; } count++;}

Page 34: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

DequeueelemType dequeue() Set result to NIL If (queue is not empty) Then Set result to data of front node Let temp point to front node Set front to temp’s next Delete temp Decrement count End If If (front = NULL) Then Set rear to NULL Return resultEnd dequeue

Page 35: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Dequeue

elemType dequeue() elemType result = NIL; if (!isEmpty()){ result = front->value; Node *t = front; front = temp->next; delete t; count--; } if (front = NULL) rear = NULL;

return result;}

Page 36: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Clearclear() Loop (while front != NULL) Set temp to front Set front to front’s next Delete temp End Loop

Set rear to NULL Set count to 0End clear

Page 37: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Printprint() Set temp to front Loop(temp != NULL) Output temp’s data Set temp to temp’s next End LoopEnd print

Page 38: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Like stacks, queues have many applications.Like stacks, queues have many applications. Items enter a queue at the rear and leave a Items enter a queue at the rear and leave a

queue at the front.queue at the front. Queues can be implemented using an array Queues can be implemented using an array

or using a linked list.or using a linked list.

Summary

Page 39: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Priority Queue Stand-by queue for a flightStand-by queue for a flight

One with the highest priority is removed in One with the highest priority is removed in FIFO order—e.g., one who is attending FIFO order—e.g., one who is attending funeral, wedding.funeral, wedding.

Print job queuePrint job queue Job with the highest priority is removed in Job with the highest priority is removed in

FIFO order—e.g. one with 3 pages or lessFIFO order—e.g. one with 3 pages or less Emergency Room queueEmergency Room queue

Most serious case treated firstMost serious case treated first

Page 40: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

ADT Priority Queue

Insert item into PQInsert item into PQ Remove item with the highest priority frm PQRemove item with the highest priority frm PQ Change priority of a particular item in PQChange priority of a particular item in PQ Remove a particular item from PQRemove a particular item from PQ Check if PQ is emptyCheck if PQ is empty Clear PQClear PQ

Page 41: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Implementation of Priority Queue

queue

queue

queue

queue

queue

1

2

3

4

5

Use an array of queues (where index doubles as priority level).

Page 42: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Data Structure for Priority Queuetypedef int elemType;typedef int priorityType;const priorityType MAX = 5; class PriorityQueue { PriorityQueue(); void insert(elemType item, prioritType pNum); elemType removeNext(); . . .public: . . .private: Queue pq[MAX + 1]; // index 0 is unused};

Page 43: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Constructor

PriorityQueue(){ for (int i = 0; i < MAX + 1; i++){ pqp[i].clear(); } }

Page 44: ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Insert(elemType item, priorityType pNum)insert (elemType item, priorityType pNum){}

class PriorityQueue { PriorityQueue(); void insert(elemType item, prioritType pNum); elemType removeNext(); . . .public: . . .private: Queue pq[MAX + 1]; // index 0 is unused};