0% found this document useful (0 votes)
75 views2 pages

Queue

This C++ program defines a Queue template class with methods to enqueue, dequeue, and check if the queue is empty or full. It implements a circular queue using an array to store values. A main function demonstrates using the Queue by enqueueing and dequeueing float values.

Uploaded by

Rabbi Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views2 pages

Queue

This C++ program defines a Queue template class with methods to enqueue, dequeue, and check if the queue is empty or full. It implements a circular queue using an array to store values. A main function demonstrates using the Queue by enqueueing and dequeueing float values.

Uploaded by

Rabbi Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<iostream>

#include<cstdlib>
#define default_value 16
using namespace std;

template< class T > class Queue


{
public:
Queue(int = default_value);//default constructor
~Queue()//destructor
{delete [] values;}
bool enQueue( T );
T deQueue();
bool isEmpty();
bool isFull();
private:
int size;
T *values;
int front;
int back;
};

template< class T > Queue<T>::Queue(int x):


size(x),//ctor
values(new T[size]),
front(0),
back(0)
{ /*empty*/ }

template< class T > bool Queue<T>::isFull()


{
if((back + 1) % size == front )
return 1;
else
return 0;
}

template< class T > bool Queue<T>::enQueue(T x)


{
bool b = 0;
if(!Queue<T>::isFull())
{
values[back] = x;
back = (back + 1) % size;
b = 1;
}
return b;
}

template< class T > bool Queue<T>::isEmpty()


{
if( back == front )//is empty
return 1;
else
return 0; //is not empty
}

template< class T > T Queue<T>::deQueue()


{
T val = -1;
if(!Queue<T>::isEmpty())
{
val = values[front];
front = ( front + 1 ) % size;
}
else
{
cerr << "Queue is Empty : ";
}
return val;

int main()
{
Queue <float> qu1(32);
float x = 1.1;
int i = 1; //declare software counter i
cout << "\n EnQueued values into qu1: ";

for( ; i < 10 ; i++) //start enter 10 elements into Qu1 object


{
if(qu1.enQueue(i*x))
cout << endl << i*x;
else
cout << "\n Queue (qu1) is full: ";
}
cout << "\n\n DeQueued values from qu1 : \n";
for(int j = 1 ; j < 10 ; j++)
cout << qu1.deQueue() << endl;

cout << endl << endl;


return 0;
}

You might also like