Question

In: Computer Science

C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and...

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:

  • Head: Pointer to the first element of the queue
  • Tail: Pointer to the last element of the queue

And the following operations:

  • Pop: Removes the element at the head
  • Top: Returns the current element at the head
  • Push: Adds a new element at the tail
  • isEmpty: Determine if there are any elements in the queue

Solutions

Expert Solution

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:


Related Solutions

Solve in C++ program. Modify the use of queue data structure such that the array used...
Solve in C++ program. Modify the use of queue data structure such that the array used to implement the queue is dynamically allocated for a fast food autoservice
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked list. One way means that each node has only one pointer, Q, (not both a rear and a front pointer), so the program must use pointers-to-pointers. Include functions to insert(), remove(), for use in main() where the user is prompted "Enter "i" to insert a new element, "r" to remove an element, "q" to quit:"
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
Describe in pseudo-code, a linear-time algorithm for reversing a queue Q. To access the queue, you...
Describe in pseudo-code, a linear-time algorithm for reversing a queue Q. To access the queue, you are only allowed to use the basic functions of the queue ADT defined as follows (Hint: Using a stack, the basic stack functions defined in the textbook and in the class). class Queue { public: int size(); bool isEmpty(); Object front(); void enqueue(Object o); Object dequeue(); };
Study and implement data structure deque (double ended queue, pronounced as "deck"). IN C++ PLEASE
Study and implement data structure deque (double ended queue, pronounced as "deck"). IN C++ PLEASE
Write a c++program using queue to find the minimum value in a queue. Use the above...
Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.  
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
write a java code Write a generic program to compute the sum of 30 terms of...
write a java code Write a generic program to compute the sum of 30 terms of the following series. (181 - 5)/31 + (186 + 9)/34 - (191 - 13)/37 + (196 + 17)/40 + . . . . . 1 5 11 Note: the 3rd, 6th, 9th term etc, has a negative sign in front of parenthesis. User Input: None Expected output: Term Ratio Sum 1 5.677 5.677 2 5.735 11.413 3 -4.811 6.602
C language Write a program in C to implement Queue and its operation (like create, insert,...
C language Write a program in C to implement Queue and its operation (like create, insert, delete, search) using array data structure.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT