0% found this document useful (0 votes)
125 views

Name: Farril Fardan Bin Danial (Ai090308)

This document contains a lab sheet question asking to implement a queue data structure with common queue operations like insert, remove, first, isempty and clear. It includes the full C++ implementation of a queue class with the required methods. The queue is implemented using a fixed-size array to store elements, with front and rear pointers to track the head and tail of the queue.

Uploaded by

farril
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

Name: Farril Fardan Bin Danial (Ai090308)

This document contains a lab sheet question asking to implement a queue data structure with common queue operations like insert, remove, first, isempty and clear. It includes the full C++ implementation of a queue class with the required methods. The queue is implemented using a fixed-size array to store elements, with front and rear pointers to track the head and tail of the queue.

Uploaded by

farril
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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();
} ;
*/

You might also like