Name: Farril Fardan Bin Danial (Ai090308)
Name: Farril Fardan Bin Danial (Ai090308)
LECTURER:
TITLE:
LAB SHEET 6
QUEUE
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;
} ;
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();
} ;
*/