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(); };
You must write the correct code for all member methods so that queue functionality can be...
You must write the correct code for all member methods so that queue functionality can be obtained. Changes to the class definition, instance variables, and signatures of the membership method are not allowed. This class has no main method. package javaclass; /** * A queue structure of Movie objects. Only the Movie values contained * in the queue are visible through the standard queue methods. The Movie * values are stored in a DoubleLink object provided in class as attribute....
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may not use any standard Java or C++ libraries. Assume your data structure only allows Strings. Implement the following operations for the data structure: Queue: enqueue, dequeue, create, isEmpty (10 points) Stack: push, pop, create, isEmpty (10 points) Here is a link to get started on transferring from Java to C++ http://www.horstmann.com/ccj2/ccjapp3.html (Links to an external site.) Upload a zip file with one implementation for...
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
C++ language We are given a Queue data structure that supports standard operations like Enqueue() and...
C++ language We are given a Queue data structure that supports standard operations like Enqueue() and Dequeue(): Enqueue(element): add a new element at the tail of the queue; Dequeue(): delete the element at the head of the queue. Show how to implement a stack using two queues. Analyze the running time of the stack operations: Push and Pop.
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.  
C++ Zybook LAB15.3.1: What order? (function templates Define two generic functions called GetVector() and CheckOrder(). GetVector()...
C++ Zybook LAB15.3.1: What order? (function templates Define two generic functions called GetVector() and CheckOrder(). GetVector() takes a string of items delimited by spaces and an item of a generic type (i.e., char, int, double, or string) and returns a vector of the given item type. CheckOrder() checks if the vector of items is in ascending, neither, or descending order. The function should return 'a' if the items are in ascending order, 'u' if the items are unordered, and 'd'...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT