Sorry if the title is misleading, as wasnt sure how to correctly name the question I have.
Im working on a 2 part assignment, where first you need to create a QUEUE template that imitates a queue (FIFO) with enqueue and dequeue. I've done this ( I believe)
And the 2nd part - creating a Human class with any data that is declared withing queue template (i.e. Queue <Human> HumanQueue; ) - This is the part Im having some trouble with. I understand the premise of it, but I cant wrap my head around how to go about implementing this.
Heres the code I have at the moment for the queue template.
#include <iostream>
#include <cstdlib>
usingnamespace std;
// Default queue size
#define SIZE 10
// Queue class
template <class X>
class queue
{
X *arr; // array where to hold the elements
int capacity; // queue max size
int front; // first element in array
int rear; // last element in array
int count; // queue size
public:
queue(int size = SIZE);
void dequeue();
void enqueue(X x);
X peek();
int size();
bool isEmpty();
bool isFull();
};
// Constructor
template <class X>
queue<X>::queue(int size)
{
arr = new X[size];
capacity = size;
front = 0;
rear = -1;
count = 0;
}
// Removing first element from the queue
template <class X>
void queue<X>::dequeue()
{
if (isEmpty())
{
cout << "Queue is empty";
}
cout << "Removing " << arr[front] << '\n';
front = (front + 1) % capacity;
count--;
}
// Adding to the queue
template <class X>
void queue<X>::enqueue(X item)
{
if (isFull())
{
cout << "Queue is full";
}
cout << "Adding " << item << '\n';
rear = (rear + 1) % capacity;
arr[rear] = item;
count++;
}
// returns queues first element
template <class X>
X queue<X>::peek()
{
return arr[front];
}
// returns queue size
template <class X>
int queue<X>::size()
{
return count;
}
// Checks if empty
template <class X>
bool queue<X>::isEmpty()
{
return (size() == 0);
}
// Checks if full
template <class X>
bool queue<X>::isFull()
{
return (size() == capacity);
}
int main()
{
// Creates a queue with size 4
queue<string> q(4);
q.enqueue("a");
q.enqueue("b");
q.enqueue("c");
cout << "First element in queue is: " << q.peek() << endl;
q.dequeue();
q.enqueue("d");
cout << "First element now is: " <<q.peek() <<endl;
cout << "Queue size is " << q.size() << endl;
q.dequeue();
q.dequeue();
q.dequeue();
if (q.isEmpty())
cout << "Queue is empty\n";
else
cout << "Queue is not empty\n";
return 0;
}
Essentially Im looking for some pointers on how to create a class Human (with data such as Name, surname and age) and then using the queue template to imitate a queue where each Human is placed in it.
You create a Human object. You set the name, surname and age in that object. Then you use the queue class function enqueue.
1 2 3 4 5 6
struct Human
{
string name;
string surname;
int age;
};
1 2 3 4 5 6 7 8
Human a_human; // Create a human
a_human. name = "Mike"; // set the values in the human
a_human. surname= "Takahashi";
a_human. age= "40";
queue<Human> humanQueue; // Make a queue
humanQueue.enqueue(a_human); // Put that human in the queue.