In: Computer Science
C++ PROGRAM
Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and create a test to validate its functionality. The data consists of persons with the attributes of name, last name, age, height and weight.
- Remembrer that,
Their structure consists of:
And the following operations:
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
// define default capacity of the queue
#define SIZE 10
class person{
public:
string name;
string lastname;
int age;
double height;
double weight;
person(string Name,string Lastname,int Age,double Height,double
Weight){
name=Name;
lastname=Lastname;
age=Age;
height=Height;
weight=Weight;
}
void display(){
cout<<"\tname= "<<name<<"\n\tlastname=
"<<lastname<<"\n\tage= "<<age;
cout<<"\n\theight= "<<height<<"\n\tweight
"<<weight<<endl;
}
};
// Class for queue
template <class X>
class queue
{
X *arr; // array to
store queue elements
int capacity; // maximum capacity of the queue
int head; // head points to front
element in the queue (if any)
int tail; // end points to last element
in the queue
int count; // current size of the
queue
public:
queue(int size = SIZE);
// constructor
void pop();
void push(X x);
X top();
int size();
bool isEmpty();
bool isFull();
};
// Constructor to initialize queue
template <class X>
queue<X>::queue(int size)
{
arr = (X*)malloc(size*sizeof(X));
capacity = size;
head = 0;
tail = -1;
count = 0;
}
// Utility function to remove front element from the queue
template <class X>
void queue<X>::pop()
{
// check for queue underflow
if (isEmpty())
{
cout << "UnderFlow\nProgram
Terminated\n";
exit(EXIT_FAILURE);
}
head = (head + 1) % capacity;
count--;
}
// Utility function to add an item to the queue
template <class X>
void queue<X>::push(X item)
{
// check for queue overflow
if (isFull())
{
cout << "OverFlow\nProgram
Terminated\n";
exit(EXIT_FAILURE);
}
tail = (tail + 1) % capacity;
arr[tail] = item;
count++;
}
// Utility function to return front element in the queue
template <class X>
X queue<X>::top()
{
if (isEmpty())
{
cout << "UnderFlow\nProgram
Terminated\n";
exit(EXIT_FAILURE);
}
return arr[head];
}
// Utility function to return the size of the queue
template <class X>
int queue<X>::size()
{
return count;
}
// Utility function to check if the queue is empty or not
template <class X>
bool queue<X>::isEmpty()
{
return (size() == 0);
}
// Utility function to check if the queue is full or not
template <class X>
bool queue<X>::isFull()
{
return (size() == capacity);
}
int main()
{
// create a queue of capacity 4
queue<person> q(4);
//person(string name,string lastname,int age,double
height,double weight)
person a("nobita","nobi",12,3.2,21.3);
person b("shinchan","nohara",8,1.5,16.4);
person c("kainachi","mitsuba",11,3.1,20.5);
q.push(a);
q.push(b);
q.push(c);
cout << "Front element is: \n";
q.top().display();
q.pop();
q.push(c);
cout << "Queue size is " << q.size() << endl;
q.pop();
// q.pop();
q.pop();
if (q.isEmpty())
cout << "Queue Is
Empty\n";
else
cout << "Queue Is Not
Empty\n";
return 0;
}
output: