algo>queues

35
1 Queues

Upload: ain-ul-moiz-khawaja

Post on 07-Dec-2014

431 views

Category:

Education


1 download

DESCRIPTION

Queues

TRANSCRIPT

Page 1: Algo>Queues

1

Queues

Page 2: Algo>Queues

2

Queues

“A Queue is a special kind of list, where items are inserted at one end (the rear) And deleted at the other end (the front)”

Other Name: First In First Out (FIFO)

Difference from Stack:

Insertion go at the end of the list, rather than the beginning of the list.

Page 3: Algo>Queues

3

Common Operations on Queues(Queue ADT)

1. MAKENULL(Q): Makes Queue Q be an empty list.

2. FRONT(Q): Returns the first element on Queue Q.

3. ENQUEUE(x,Q): Inserts element x at the end of Queue Q.

4. DEQUEUE(Q): Deletes the first element of Q.

5. EMPTY(Q): Returns true if and only if Q is an empty queue.

Example:Line of customers in a bank

Page 4: Algo>Queues

4

Applications of Queues

Operating systemmulti-user/multitasking environments, where

several users or task may be requesting the same resource simultaneously.

Communication Softwarequeues to hold information received over

networks and dial up connections. (Information can be transmitted faster than it can be processed, so is placed in a queue waiting to be processed)

Some other?

Page 5: Algo>Queues

5

Implementation

StaticQueue is implemented by an array, and size of

queue remains fix

DynamicA queue can be implemented as a linked list,

and expand or shrink with each enqueue or dequeue operation.

Page 6: Algo>Queues

6

Page 7: Algo>Queues

7

A pointer Implementation of Queues

Keep two pointers: FRONT: A pointer to the first element of the

queue. REAR: A pointer to the last element of the queue.

x y .zFront

Rear

Page 8: Algo>Queues

8

A pointer Implementation of Queues

Q.front

Q.Rear

MAKENULL(Q)

Q.front

Q.Rear

ENQUEUE(x,Q)

.x

NULL

Page 9: Algo>Queues

9

Q.front

Q.Rear

ENQUEUE(y,Q)

x .y

Q.front

Q.Rear

DEQUEUE(Q)

.y

A pointer Implementation of Queues

Page 10: Algo>Queues

10

A class for Dynamic Queue implementationclass DynIntQueue{private:

struct QueueNode{

int value;QueueNode *next;

};

QueueNode *front;QueueNode *rear;int numItems;

public:DynIntQueue(void);

~DynIntQueue(void);void enqueue(int);

int dequeue(void); bool isEmpty(void); void makeNull(void);};

Page 11: Algo>Queues

11

Implemenaton

//************************// Constructor *//************************

DynIntQueue::DynIntQueue(void){

front = NULL;rear = NULL;numItems = 0;

}

//************************// Destructor *//************************

DynIntQueue::~DynIntQueue(void){

makeNull();}

Page 12: Algo>Queues

12

//********************************************// Function enqueue inserts the value in num *// at the rear of the queue. *//********************************************

void DynIntQueue::enqueue(int num){

QueueNode *newNode;

newNode = new QueueNode;newNode->value = num;newNode->next = NULL;if (isEmpty()){

front = newNode;rear = newNode;

}else{

rear->next = newNode;rear = newNode;

}numItems++;

}

Page 13: Algo>Queues

13

//**********************************************// Function dequeue removes the value at the *// front of the queue, and copies it into num. *//**********************************************

int DynIntQueue::dequeue(void){

QueueNode *temp;int num;if (isEmpty())

cout << "The queue is empty.\n";else{

num = front->value;temp = front->next;delete front;front = temp;numItems--;

}return num;

}

Page 14: Algo>Queues

14

//*********************************************// Function isEmpty returns true if the queue *// is empty, and false otherwise. *//*********************************************

bool DynIntQueue::isEmpty(void){

if (numItems)return false;

elsereturn true;

}

Page 15: Algo>Queues

15

//********************************************// Function makeNull dequeues all the elements *// in the queue. *//********************************************

void DynIntQueue::makeNull(void){

while(!isEmpty())dequeue();

}

Page 16: Algo>Queues

16

Program

// This program demonstrates the DynIntQeue classvoid main(void){

DynIntQueue iQueue;

cout << "Enqueuing 5 items...\n";// Enqueue 5 items.for (int x = 0; x < 5; x++)

iQueue.enqueue(x);

// Deqeue and retrieve all items in the queuecout << "The values in the queue were:\n";while (!iQueue.isEmpty()){

int value; value =iQueue.dequeue();cout << value << endl;

}}

Page 17: Algo>Queues

17

Program Ouput

Enqueuing 5 items...The values in the queue were:01234

Page 18: Algo>Queues

18

Array Implementation

First Element

Last Element

maxlength

Front

Second Element.

.

Rear

When queue is empty both front and rear are set to -1

While enqueueing increment rear by 1, and while dequeueing increment front by 1

When there is only one value in the Queue, both rear and front have same index

Can we implement Queue by using only one index variable Front or Rear??

YES, by moving elements of array to neighboring locations like we did in STACK but this is in-efficient

Why it is inefficient?

Page 19: Algo>Queues

19

Array Implementation

5 4 6 7 8 7 6

0 1 2 3 4 5 6 7 8

Front=0Rear=6

8 7 6

0 1 2 3 4 5 6 7 8

Front=4Rear=6

7 6 12 67

0 1 2 3 4 5 6 7 8

Front=5Rear=8

How can we insert more elements? Rear index can not move beyond the last element….

Page 20: Algo>Queues

20

Solution: Using circular queue

Allow rear to wrap around the array.

if(rear == queueSize-1)

rear = 0;

else

rear++;Or use module arithmetic

rear = (rear + 1) % queueSize;

Page 21: Algo>Queues

21

7 6 12 67

0 1 2 3 4 5 6 7 8

Front=5Rear=8

Enqueue 39 Rear=(Rear+1) mod Queue Size = (8+1) mod 9 = 0

39 7 6 12 67

0 1 2 3 4 5 6 7 8

Front=5Rear=0

Page 22: Algo>Queues

22

How to determine empty and full Queues?

It is some trickyNumber of approaches

A counter indicating number of values in the queue can be used (We will use this approach)

We will see another approach as well at the end

Page 23: Algo>Queues

23

Implementationclass IntQueue{private:

int *queueArray;int queueSize;int front;int rear;int numItems;

public:IntQueue(int);

~IntQueue(void); void enqueue(int); int dequeue(void); bool isEmpty(void); bool isFull(void); void clear(void);};Note, the member function clear, which clears the queue by resetting the front and rear indices, and setting the numItems to 0.

Page 24: Algo>Queues

24

IntQueue::IntQueue(int s) //constructor{

queueArray = new int[s];queueSize = s;front = -1;rear = -1;numItems = 0;

}

IntQueue::~IntQueue(void) //destructor

{ delete [] queueArray;

}

Page 25: Algo>Queues

25

//********************************************// Function enqueue inserts the value in num *// at the rear of the queue. *//********************************************

void IntQueue::enqueue(int num){

if (isFull())cout << "The queue is full.\n";

else{

// Calculate the new rear positionrear = (rear + 1) % queueSize;// Insert new itemqueueArray[rear] = num;// Update item countnumItems++;

}}

Page 26: Algo>Queues

26

//*********************************************// Function dequeue removes the value at the *// front of the queue, and copies t into num. *//*********************************************

int IntQueue::dequeue(void){

if (isEmpty())cout << "The queue is empty.\n";

else{

// Move frontfront = (front + 1) % queueSize;// Retrieve the front itemint num = queueArray[front];// Update item countnumItems--;

}return num;

}

Page 27: Algo>Queues

27

//*********************************************// Function isEmpty returns true if the queue *// is empty, and false otherwise. *//*********************************************

bool IntQueue::isEmpty(void){

if (numItems)return false;

elsereturn true;

}

Page 28: Algo>Queues

28

//********************************************// Function isFull returns true if the queue *// is full, and false otherwise. *//********************************************

bool IntQueue::isFull(void){

if (numItems < queueSize)return false;

elsereturn true;

}

Page 29: Algo>Queues

29

//*******************************************// Function clear resets the front and rear *// indices, and sets numItems to 0. *//*******************************************

void IntQueue::clear(void){

front = - 1;rear = - 1;numItems = 0;

}

Page 30: Algo>Queues

30

//Program demonstrating the IntQueue class

void main(void){

IntQueue iQueue(5);

cout << "Enqueuing 5 items...\n";// Enqueue 5 items.for (int x = 0; x < 5; x++)

iQueue.enqueue(x);

// Attempt to enqueue a 6th item.cout << "Now attempting to enqueue again...\n";iQueue.enqueue(5);

// Deqeue and retrieve all items in the queuecout << "The values in the queue were:\n";while (!iQueue.isEmpty()){

int value;iQueue.dequeue(value);cout << value << endl;

}}

Page 31: Algo>Queues

31

Program Output

Enqueuing 5 items...Now attempting to enqueue again...The queue is full.The values in the queue were:0

1

2

3

4

Page 32: Algo>Queues

32

Another implementation of Queues using Arraysclass CQueue{

int Data*,QueueSize,Front,Rear;public:

CQueue(int size);~CQueue(int size);bool IsFull();bool IsEmpty();void Enqueue(int num);int Dequeue();void MakeNull;

};

Page 33: Algo>Queues

33

CQueue::CQueue(int size){

Front=Rear=-1;Data=new int[size];

}void CQueue ::Enqueue(int num);{

if (IsFull()) { cout<<“Overflow” return; }if (IsEmpty() Rear=Front=0;else Rear=(Rear+1) % QueueSize;Data[Rear]=num;

}

Page 34: Algo>Queues

34

int CQueue ::Dequeue(int num);

{

if (IsEmpty()) { cout<<“Underflow”; return; }

int ReturnValue=Data[Front];

if (Front==Rear) //only one element in the queue

Front=Rear=-1;

else

Front=(Front+1) % QueueSize;

return ReturnValue;

}

Page 35: Algo>Queues

35

bool CQueue::IsEmpty()

{

if (Front==-1) return true;

else return false;

}

bool CQueue::IsFull()

{

If (((Rear+1)%QueueSize)==Front)

return true;

else return false;

}