lec 8 queues

35
Queues

Upload: taqi-shah

Post on 06-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 1/35

Queues

Page 2: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 2/35

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)

Diff erence f rom Stack:

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

beginning of  the list.

Page 3: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 3/35

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: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 4/35

 Applications of Queues Operating system

multi-user/multitasking environments, whereseveral users or task may be requesting the

same resource simultaneously. Communication Software

queues to hold information received over networks and dial up connections.

(Information can be transmitted faster than itcan be processed, so is placed in a queuewaiting to be processed)

Some other?

Page 5: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 5/35

Implementation Static

Queue is implemented by an array, and size

of queue remains fix

Dynamic

 A queue can be implemented as a linked

list, and expand or shrink with each enqueueor d equeue operation.

Page 6: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 6/35

Page 7: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 7/35

 A pointer Implementation of 

QueuesKeep two pointers:

FRONT: A pointer to the first element of the

queue. RE AR: A pointer to the last element of the

queue.

x y

.zF ront 

Rear 

Page 8: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 8/35

 A pointer Implementation of 

Queues

Q.front 

Q.Rear 

MAKENULL(Q)

Q.front 

Q.Rear 

ENQUEUE(x,Q)

.x

 NULL

Page 9: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 9/35

Q.front 

Q.Rear 

ENQUEUE(y,Q)

x .y

Q.front 

Q.Rear 

DEQUEUE(Q)

.y

 A pointer Implementation of 

Queues

Page 10: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 10/35

A class for Dynamic Queue implementation

class 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: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 11/35

Implemenaton

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

//************************

DynIntQueue::DynIntQueue(void)

{

front = NULL;

rear = NULL;numItems = 0;

}

//************************

// Destructor *

//************************

DynIntQueue::~DynIntQueue(void)

{

 makeNull();

}

Page 12: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 12/35

//********************************************

// 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: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 13/35

//**********************************************

// 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: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 14/35

//*********************************************

// Function isEmpty returns true if the queue *

// is empty, and false otherwise. *

//*********************************************

 bool DynIntQueue::isEmpty(void)

{

if (numItems)

return false;

elsereturn true;

}

Page 15: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 15/35

//********************************************

// Function makeNull dequeues all the elements *

// in the queue. *

//********************************************

void DynIntQueue::makeNull(void)

{

 while(!isEmpty())

dequeue();

}

Page 16: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 16/35

Program

// This program demonstrates the DynIntQeue class

void 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 queue

cout << "The values in the queue were:\n";

 while (!iQueue.isEmpty())

{

int value;value =iQueue.dequeue();

cout << value << endl;

}

}

Page 17: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 17/35

Program Ouput

Enqueuing 5 items...

The values in the queue were:

0

1

2

3

4

Page 18: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 18/35

 ArrayImplementation

First Element

Last Element

ma xlength

Front

SecondElement.

.

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 neigh boring

locations like we did in STACK but this is in-

efficient

Why it is inefficient?

Page 19: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 19/35

 ArrayImplementation5 4 6 7 8 7 6

0 1 2 3 4 5 6 7 8

Front=0

Rear=6

8 7 6

0 1 2 3 4 5 6 7 8

Front=4

Rear=6

7 6 12 67

0 1 2 3 4 5 6 7 8

Front=5

Rear=8How can we insert more elements? R ear index can

not move beyond the last element«.

Page 20: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 20/35

Solution: Using circular queue Allow rear to wrap around the array.

if (r ear == queueSize-1)

r ear = 0;

else

r ear++; Or use module arithmetic

r ear = (r ear  + 1) % queueSize;

Page 21: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 21/35

7 6 12 67

0 1 2 3 4 5 6 7 8Front=5

Rear=8

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

39 7 6 12 67

0 1 2 3 4 5 6 7 8

Front=5

Rear=0

Page 22: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 22/35

How to determine empty and full

Queues?

It is some tricky

Number 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 theend

Page 23: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 23/35

Implementation

class 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: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 24/35

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: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 25/35

//********************************************

// 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 position

rear = (rear + 1) % queueSize;

// Insert new item 

queueArray[rear] = num;

// Update item count

numItems++;

}

}

Page 26: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 26/35

//*********************************************

// 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{

// Retrieve the front item 

int num = queueArray[front];

// Move front

front = (front + 1) % queueSize;

// Update item countnumItems--;

}

return num;

}

Page 27: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 27/35

//*********************************************

// Function isEmpty returns true if the queue *

// is empty, and false otherwise. *//*********************************************

 bool IntQueue::isEmpty(void)

{

if (numItems)

return false;

else

return true;

}

Page 28: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 28/35

//********************************************

// Function isFull returns true if the queue *

// is full, and false otherwise. *

//********************************************

 bool IntQueue::isFull(void)

{

if (numItems < queueSize)

return false;

else

return true;

}

Page 29: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 29/35

//*******************************************

// 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: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 30/35

//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 queue

cout << "The values in the queue were:\n";

 while (!iQueue.isEmpty())

{int value;

iQueue.dequeue(value);

cout << value << endl;

}

}

Page 31: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 31/35

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: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 32/35

 Another implementation of Queues

using Arraysclass CQueue

{

int Data*,QueueSize,Front,Rear;

public:CQueue(int size);

~CQueue(int size);

bool IsFull();

boolIsE

mpty();void Enqueue(int num);

int Dequeue();

void MakeNull;

};

Page 33: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 33/35

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: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 34/35

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: Lec 8 Queues

8/3/2019 Lec 8 Queues

http://slidepdf.com/reader/full/lec-8-queues 35/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;}