Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Project Create a templated Queue Class with a node based variant. The follow

ID: 3824268 • Letter: C

Question

C++ Project

Create a templated Queue Class with a node based variant.

The following is an example of the NodeQueue header file that will be holding elements of type class DataType. This class should be templated.

class NodeQueue

{

public:

NodeQueue(); //Instantiates a new queue object with no elements(Nodes)

NodeQueue(int size, const DataType& value); //Instantiate a new queue object that dynamically allocates at instantiation to hold int size number of elements(Nodes) all //initialized to be equal to parameter value

NodeQueue(const NodeQueue& other); //Instantiate a new Queue object which is a seperate copy of the other queue object getting copied

~NodeQueue(); //Destroys the instance of the queue object

NodeQueue& operator = (const NodeQueue& other_nodeQueue); //Assigns new value to the calling of the queue object which is an exact copy of the other_nodeQueue object //passed as a parameter. Returns a reference to the calling object to be used for cascading operator =

DataType& front(); //Returns reference to front element of the Queue (Ensure that the queue is not empty)

const DataType& front() const;

DataType& back(); // Returns a reference to back element of the Queue. (Ensure that queue is not empty)

const DataType& back() const;

void push(const DataType& value); // inserts at the back of the queue an element of the given value

void pop(); //Removes front element of queue

int size() const; // Returns size of current Queue

bool empty() const; // Returns true if queue is empty

bool full() const; //Return true if queue is full

void clear(); //After function is called the queue will be considered empty

friend std::ostream& operator<< (std::ostream& os, const NodeQueue& nodeQueue); //(Template optional) This will output queue object (sequentially pop until final element)

private:

Node *m_front; //Templated node pointer type pointing to the first element of the queue

Node *m_back; // Templated node pointer type pointing to the last element of the queue

}

//The following is the Node class holding elements of type class DataType. This class should also be templated

class Node{

public:

Node();

Node(const DataType& data, Node* next = NULL);

DataType& getData;

const DataType& getData() const;

friend class NodeQueue;

private:

Node * m_next;

DataType m_data;

};

Explanation / Answer

#include<iostream>

using namespace std;

template<class T>
class Node
{
private:
    Node * m_next;
    T m_data;
public:
    Node();
    Node(const T& data, Node* next);
    const T& getData() const;
    friend class NodeQueue;
};

template<class T>
Node<T>::Node()
{
m_next=NULL;
}

template<class T>
Node<T>::Node(const T& data, Node* next)
{
   m_next=next;
   m_data=data;
}

template<class T>
const T& Node<T>::getData() const
{
return m_data;
}


template<class T>
class NodeQueue
{

private:
   Node<T> *m_front; //Templated node pointer type pointing to the first element of the queue
   Node<T>   *m_back; // Templated node pointer type pointing to the last element of the queue

public:
   NodeQueue();
   NodeQueue(int size, const T& value);
   NodeQueue(const NodeQueue& other);
   ~NodeQueue();
   T& front();
   const T& front() const;
   T& back();
   const T& back() const;
   void push(const T& value);
   void pop();
   int size() const;
   bool empty() const;
   bool full() const;
   void clear();
  
           //Assigns new value to the calling of the queue object which is an exact copy of the other_nodeQueue object //passed as a parameter. Returns a reference to the calling object to be used for cascading operator =
   NodeQueue& operator = (const NodeQueue& other_nodeQueue)
   {
       Node<T> *temp=other_nodeQueue.m_front;
       Node<T> *t;

       m_front=new Node<T>(temp->m_data,NULL);
       m_back=m_front;
       temp=temp->m_next;

       while(temp!=NULL)
       {
       t=m_back;
       m_back=new Node<T>(temp->m_data,NULL);
       t->m_next=m_back;
       temp=temp->m_next;
       }

   }

        //(Template optional)   This will output queue object (sequentially pop until final element)
        std::ostream& operator<< (std::ostream& os, const NodeQueue& nodeQueue)
       {
          Node<T> *t=m_front;
      while(t!=m_back)
      {
         os<<t->m_data<<" ";
         t=t->m_next;
      }
          os<<t->m_data<<endl;
         return os;
       }  
};
m_data

//Instantiates a new queue object with no elements(Nodes)
template<class T>
NodeQueue<T>::NodeQueue()
{
m_front=NULL;
m_back=NULL;
}


//Instantiate a new queue object that dynamically allocates at instantiation to hold int size number of elements(Nodes) all //initialized to be equal to parameter value
template<class T>
NodeQueue<T>::NodeQueue(int size, const T& value)
{
   Node<T> *t;
   for(int i=0;i<size;i++)
{
   if(m_front==NULL){
      m_front=new Node<T>(value,NULL);
      m_back=m_front;}
   else
   {
     t=m_back;
     m_back=new Node<T>(value,NULL);
     t->m_next=m_back;
   }
}

}

//Instantiate a new Queue object which is a seperate copy of the other queue object getting copied
template<class T>
NodeQueue<T>::NodeQueue(const NodeQueue& other)
{
Node<T> *temp=other.m_front;
Node<T> *t;

m_front=new Node<T>(temp->m_data,NULL);
m_back=m_front;
temp=temp->m_next;

while(temp!=NULL)
{
    t=m_back;
    m_back=new Node<T>(temp->m_data,NULL);
    t->m_next=m_back;
    temp=temp->m_next;
}

}

//Destroys the instance of the queue object
template<class T>
NodeQueue<T>::~NodeQueue()
{
Node<T> *next;
   while (m_front)
        {
       next = m_front->m_next;
       delete m_front;
       m_front = next;
   }
        m_front=m_back=NULL;
}

//Returns reference to front element of the Queue (Ensure that the queue is not empty)
template<class T>
const T& NodeQueue<T>::front() const
{

   if(m_front==NULL)
   {
    cout<<"Queue is empty ";
    return *this;
   }
   else
   {
      return m_front->m_data;

   }
}   

// Returns a reference to back element of the Queue. (Ensure that queue is not empty)
template<class T>
const T& NodeQueue<T>::back() const
{
if(m_front==NULL)
   {
    cout<<"Queue is empty ";
    return *this;
   }
   else
   {
      return m_back->m_data;

   }
}

// inserts at the back of the queue an element of the given value
template<class T>
void NodeQueue<T>::push(const T& value)
{
   Node<T> *p = new Node<T>;
   p->data = value;
   p->m_next =NULL;
        if (m_front)
            m_back->m_next = p; // queue not empty
   else
           m_front = p;             // queue empty
   m_back= p
}

//Removes front element of queue
template<class T>
void NodeQueue<T>::pop()
{_back

   if (empty())
        {
          cout<<"Queue is empty";
          return;
        }
   Node<T> *p = m_front;
   m_front = m_front->m_next;
   delete p;
}

// Returns size of current Queue
template<class T>
int size() const
{
int count=0;
Node<T> *t=m_front;
while(t!=m_back)
{
     count++;
     t=t->m_next;
}
count++;
return count;
}

// Returns true if queue is empty
template<class T>
bool empty() const
{
if(m_front==NULL)
   return true;
else
   return false;
}

//Return true if queue is full
template<class T>
bool full() const
{
Node<T> *p = new Node<T>;
if(p==NULL)    //no more memeory to insert into the queue
    return 1;
else
    return 0;
}

//After function is called the queue will be considered empty
template<class T> //we already have destructor
void clear()
{

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote