+ All Categories
Home > Documents > What is a Queue?

What is a Queue?

Date post: 02-Jan-2016
Category:
Upload: fleur-mcconnell
View: 29 times
Download: 2 times
Share this document with a friend
Description:
What is a Queue?. Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear ), and elements are removed from the other end (the front ). A queue is a FIFO “first in, first out” structure. Queues. 2. - PowerPoint PPT Presentation
39
1 What is a Queue? Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front). • A queue is a FIFO “first in, first out” structure.
Transcript
Page 1: What is a Queue?

1

What is a Queue?

• Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front).

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

Page 2: What is a Queue?

Queues

2

Page 3: What is a Queue?

Queues: Application Level

3

• For what type of problems would queues be useful?• Print server

• maintains a queue of print jobs. • Disk driver

• maintains a queue of disk input/output requests. • Scheduler (e.g., in an operating system)

• maintains a queue of processes awaiting a slice of machine time.

Page 4: What is a Queue?

Application of Queue and Stack

4

• Palindromes: words or phrases that read the same in both directions, e.g. EYE, RACECAR, MADAM I'M ADAM, and :

• Testing if a given word or phrase is a palindrome:• Scan the string character by character, and store each

character into a stack(push) and queue(enqueue)

• After all characters have been processed, repeatedly pop a character from the stack, and deque a character from the queue, and compare them. Not match=>not a palindrome

• If finished comparing with no mismatches => a palindrome

Page 5: What is a Queue?

Queues: implementation level

5

class QueueType{public: QueueType(int max); QueueType(); ~QueueType(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType item); //add newItem to the rear of the queue. void Dequeue(ItemType& item); //remove front item from queue

Page 6: What is a Queue?

Array-Based Implementation

6

private:

ItemType* items; // Dynamically allocated array

int maxQue;

// Whatever else we need

};

Implementation level

Page 7: What is a Queue?

Array-Based Implementation

7

An array with the front of the queue fixed in the first position.

Enqueue A, B, C, D

Dequeue

Shift elements up by 1

What’s the problem with this design?

Page 8: What is a Queue?

Array-Based Implementation

8

Another data structure: An array where the front floats.

What happens if we add X, Y, and Z?

Page 9: What is a Queue?

Array-Based Implementation

9

Let the queue wrap around in the array; i.e. treat the array as a circular structure.

Page 10: What is a Queue?

Array-Based Implementation

10

Empty Queue

Full Queue

How can we tell the difference?

(a) Initial conditions

front = 2

rear = 2

(b) queue.Dequeue(item)

front = 3

rear = 2

A

[0] [1] [2] [3] [4]

[0] [1] [2] [3] [4]

Page 11: What is a Queue?

Array-Based Implementation

11

Reserve the slot (not used) before front item.

front is the index of the slot before front item.

Empty Queue

Full Queue

Page 12: What is a Queue?

12

DYNAMIC ARRAY IMPLEMENTATION

QueType

~QueType

Enqueue

Dequeue . . .

class QueType

Private Data:

front 1

rear 4

maxQue 5

items‘C’ ‘X’ ‘J’

items [0] [1] [2] [3] [4]

RE

SE

RV

ED

Page 13: What is a Queue?

Array-Based Implementation

13

private: int front; int rear; int maxQue;

ItemType* items;};

Completeimplementation level

Page 14: What is a Queue?

Array-Based Implementation

14

//returns true if the queue is emptybool QueueType::IsEmpty( ){ return ( rear == front );}

//returns true if the queue is fullbool QueueType::IsFull( ){ return ( (rear + 1) % maxQue == front )}

Page 15: What is a Queue?

Array-Based Implementation

15

//Pre: the queue is not full//Post: newItem is at the rear of the queuevoid QueueType::Enqueue(ItemType newItem){ rear = (rear + 1) % maxQue; items[rear] = newItem; }

Page 16: What is a Queue?

Array-Based Implementation

16

//pre: the queue is not empty//post: the front of the queue has been removed// and a copy returned in itemvoid QueueType::Dequeue(ItemType& item){ front = (front + 1) % maxQue; item = items[front];}

Page 17: What is a Queue?

Array-Based Implementation

17

QueueType::QueueType( int max) { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue];}

QueueType::QueueType( ) { maxQue = 500 + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue];}

Why is the array declaredmax + 1?

Page 18: What is a Queue?

Counted Queue*

18

Counted Queue

A queue that has an additional function GetLength that returns the number of items in the queue.

The CountedQueType class will inherit (extend) the QueType class.

What functions must be defined in class CountedQueType?

Page 19: What is a Queue?

Counted Queue*

19

class CountedQueueType : public QueueType{public: CountedQueueType(); void Enqueue(ItemType newItem); void Dequeue(ItemType& item); int GetLength( ) const;private: int length;};

Page 20: What is a Queue?

Counted Queue*

20

Page 21: What is a Queue?

Counted Queue*

21

void CountedQueueType::Enqueue(ItemType newItem)

{

try

{

QueueType::Enqueue(newItem);

length++;

}

catch(FullQueue)

{ throw FullQueue();}

}

Why must the call to Enqueue beembedded within a try/catch statement?

What is this?

Page 22: What is a Queue?

Counted Queue*

22

Does the derived class have access to the base class’s private data members in C++?

Page 23: What is a Queue?

Linked Implementation

23

Data structure for linked queue

What data members are needed?

Page 24: What is a Queue?

Linked Implementation

24

Enqueue(newItem)Set newNode to the address of newly allocated nodeSet Info(newNode) to newItemSet Next(newNode) to NULLSet Next(rear) to newNodeIf queue is empty

Set front to newNodeelse

Set Next(rear) to newNode

Page 25: What is a Queue?

Linked Implementation

25

Dequeue(item)Set tempPtr to frontSet item to Info(front)Set front to Next(front)if queue is now empty

Set rear to NULLDeallocate Node(tempPtr)

Page 26: What is a Queue?

Circular Linked Queue

• Make the linked queue circular:• Make the next member of rear node point to

the front node of the queue

• Maintain a single pointer: rear• Accessing front node is a constant time

operation:

rear->next

26

Page 27: What is a Queue?

Storage Requirements

27 What does this table tell you?

Page 28: What is a Queue?

Big-O Comparison (Queue)

Time Array-Based Implementation

Linked Implementation

Class constructor

Class destructor

O(1)

O(1)

O(1)

O(n)

IsFull()

IsEmpty()

O(1)

O(1)

O(1)

O(1)

Enqueue()

Dequeue()

O(1)

O(1)

O(1)

O(1)

28

Page 29: What is a Queue?

29

//--------------------------------------------------------// CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE #include "ItemType.h" // for ItemType template<class ItemType>class QueType {public:

QueType( ); QueType( int max ); // PARAMETERIZED CONSTRUCTOR~QueType( ); // DESTRUCTOR

. . .bool IsFull( ) const;void Enqueue( ItemType item );void Dequeue( ItemType& item );

private:int front;int rear;int maxQue; ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION

};29

Page 30: What is a Queue?

30

//--------------------------------------------------------// CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE cont’d//--------------------------------------------------------

template<class ItemType>QueType<ItemType>::QueType( int max ) // PARAMETERIZED{

maxQue = max + 1; front = maxQue - 1;

rear = maxQue - 1;items = new ItemType[maxQue]; // dynamically allocates

}

template<class ItemType>bool QueType<ItemType>::IsEmpty( )

{return ( rear == front )

} 30

Page 31: What is a Queue?

31

//--------------------------------------------------------// CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE cont’d//--------------------------------------------------------

template<class ItemType>QueType<ItemType>::~QueType( ){

delete [ ] items; // deallocates array}

. . .

template<class ItemType>bool QueType<ItemType>::IsFull( )

{ // WRAP AROUNDreturn ( (rear + 1) % maxQue == front )

} 31

Page 32: What is a Queue?

32

ALL PUBLIC MEMBERS OF QueType CAN BE INVOKED FOR OBJECTS OF TYPE CountedQueType

// DERIVED CLASS CountedQueType FROM BASE CLASS QueType

template<class ItemType>class CountedQueType : public QueType<ItemType> {public:

CountedQueType( ); void Enqueue( ItemType newItem );void Dequeue( ItemType& item );int LengthIs( ) const;// Returns number of items on the counted queue.

private:int length;

};

Page 33: What is a Queue?

3333

// Member function definitions for class CountedQue

template<class ItemType>

CountedQueType<ItemType>::CountedQueType( ) : QueType<ItemType>( )

{

length = 0 ;

}

template<class ItemType>

int CountedQueType<ItemType>::LengthIs( ) const

{

return length ;

}

Page 34: What is a Queue?

34

template<class ItemType>void CountedQueType<ItemType>::Enqueue( ItemType newItem )

// Adds newItem to the rear of the queue.// Increments length.

{length++;

QueType<ItemType>::Enqueue( newItem );}

template<class ItemType>void CountedQueType<ItemType>::Dequeue(ItemType& item )

// Removes item from the rear of the queue.// Decrements length.

{length--;

QueType<ItemType>::Dequeue( item );}

Page 35: What is a Queue?

35

class QueType<char>

QueType

~QueType

Enqueue

Dequeue . . .

Private Data:

qFront

qRear

‘C’ ‘Z’ ‘T’

Page 36: What is a Queue?

36

// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE

#include "ItemType.h" // for ItemType

template<class ItemType>class QueType {public:

QueType( ); // CONSTRUCTOR~QueType( ) ; // DESTRUCTORbool IsEmpty( ) const;bool IsFull( ) const;void Enqueue( ItemType item );void Dequeue( ItemType& item );void MakeEmpty( );

private:NodeType<ItemType>* qFront;NodeType<ItemType>* qRear;

};

36

Page 37: What is a Queue?

37

// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE continued

// member function definitions for class QueType

template<class ItemType>

QueType<ItemType>::QueType( ) // CONSTRUCTOR

{

qFront = NULL;

qRear = NULL;

}

template<class ItemType>

bool QueType<ItemType>::IsEmpty( ) const

{

return ( qFront == NULL )

}

37

Page 38: What is a Queue?

38

template<class ItemType>void QueType<ItemType>::Enqueue( ItemType newItem )

// Adds newItem to the rear of the queue.// Pre: Queue has been initialized.// Queue is not full.// Post: newItem is at rear of queue.

{NodeType<ItemType>* ptr;

ptr = new NodeType<ItemType>;ptr->info = newItem;ptr->next = NULL;if ( qRear == NULL )

qFront = ptr;else

qRear->next = ptr;qRear = ptr;

} 38

Page 39: What is a Queue?

39

template<class ItemType>void QueType<ItemType>::Dequeue( ItemType& item )

// Removes element from from front of queue// and returns it in item.// Pre: Queue has been initialized.// Queue is not empty.// Post: Front element has been removed from

queue.// item is a copy of removed element.

{NodeType<ItemType>* tempPtr;

tempPtr = qFront;item = qFront->info;qFront = qFront->next;if ( qFront == NULL )

qRear = NULL;delete tempPtr;

} 39


Recommended