You are on page 1of 4

NAME:

FARRIL FARDAN BIN DANIAL (AI090308)

LECTURER:
MISS SHAZANA MD ZIN

TITLE:

LAB SHEET 6 (QUEUE)


DIT 2014 – DATA STRUCTURES AND
ALGORITHMS

LAB SHEET 6

QUEUE

INSTRUCTOR: MISS SHAZANA MD ZIN

Question 1 :

A queue has the following operations: Insert, Remove, First, IsEmpty and Clear.
Insert allows the insertion of item at the back of the Queue. Remove delete the item
from the front of the Queue. First returns the value at the front of the Queue, but it
does not remove it from the Queue. IsEmpty simply checks to see if the Queue is
empty. Finally, Clear reintiliazies the Queue to its original empty status.

(20 marks)

#include <stdlib.h>
#include <iostream.h>

#define DefaultSize 5

class Queue
{
public:
Queue();
Queue(int QueueSize);
~Queue();
int First();
int Remove();
void Insert(int Value);
void Clear();
int IsEmpty();

private:
int *Array;
int Front;
int Rear;
int ArraySize;
int CurrentSize;

} ;

void Queue::Clear()
{
Front=0;
Rear=ArraySize-1;
CurrentSize=0;
} ;

Queue::Queue()
{
Clear();
Array = new int[DefaultSize];
ArraySize = DefaultSize;
} ;

Queue::Queue(int QueueSize)
{
Clear();
ArraySize = QueueSize;
Array = new int[QueueSize];
} ;

Queue::~Queue()
{
// delete[]Array;
} ;

void Queue::Insert(int Value)


{
if(CurrentSize>ArraySize)
{
cout<<"Queue is Full"<<endl;
return;
}

if (Rear==ArraySize-1)
Rear=0;
else
Rear++;
Array[Rear]=Value;
CurrentSize++;
} ;

int Queue::Remove()
{
int Temp;
if(IsEmpty())
{
cout<<"Queue Underflow"<<endl;
return 0;
}
Temp=Array[Front];
if(Front==ArraySize-1)
Front = 0;
else
Front++;
CurrentSize--;
return Temp;
} ;

int Queue::IsEmpty()
{
return(CurrentSize==0);
} ;

int Queue::First()
{
if(!IsEmpty())
return Array[Front];
else
cout<<"Queue Underflow"<<endl;
return 0;
} ;

/*
main()
{
Queue QueueBill(4);
QueueBill.First(4);
QueueBill.First(5);
QueueBill.First(5);
QueueBill.Remove();
QueueBill.Remove();
} ;
*/