Question: Please help me write this node-based queue program in c++, thank you very much!: class NodeQueue{ friend std::ostream& operator < (std::istream& is, DataType& dataType); public:

Please help me write this node-based queue program in c++, thank you very much!:

class NodeQueue{

friend std::ostream& operator<<(std::ostream& os, //(i)

const NodeQueue& nodeQueue);

public:

NodeQueue(); //(1)

NodeQueue(size_t size, const DataType& value); //(2)

NodeQueue(const NodeQueue& other); //(3) ~NodeQueue(); //(4)

NodeQueue& operator= (const NodeQueue& rhs); //(5)

DataType& front(); //(6a)

const DataType& front() const; //(6b)

DataType& back(); //(7a)

const DataType& back() const; //(7b)

void push(const DataType& value); //(8)

void pop(); //(9)

size_t size() const; //(10)

bool empty() const; //(11)

bool full() const; //(12)

void clear(); //(13)

void serialize(std::ostream& os); //(14)

private:

Node *m_front;

Node *m_back;

};

The NodeQueue Class will contain the following private data members:

m_front, a Node Pointer type, pointing to the front element of the Queue.

m_back, a Node Pointer type, pointing to the back element of the Queue.

,will have the following public member functions:

(1) Default Constructor will instantiate a new Queue object with no elements (Nodes).

(2) Parametrized Constructor will instantiate a new Queue object, which will dynamically allocate at instantiation to hold size_t count number of elements (Nodes), all of them initialized to be equal to the parameter value.

(3) Copy Constructor will instantiate a new Queue object which will be a separate copy of the data of the other Queue object which is getting copied. Note: Consider why now you do need to implement this.

(4) Destructor will destroy the instance of the Queue object. Note: Consider why now you do need to implement this.

(5) operator= will assign a new value to the calling Queue object, which will be an exact copy of the rhs object passed as a parameter. Returns a reference to the calling object to be used for cascading operator= as per standard practice. Note: Consider why now you do need to implement this.

(6a,6b) front returns a Reference to the front element of the Queue. Note: Since it returns a Reference, before calling this method the user must ensure that the Queue is not empty.

(7a,7b) back returns a Reference to the back element of the Queue. Note: Since it returns a Reference, before calling this method the user must ensure that the Queue is not empty.

(8) push inserts at the back of the Queue an element of the given value. Note: No imposed maximum size limitations exist for the Node-based Queue variant.

(9) pop removes from the front element of the Queue. Note: Checking if the Queue is empty prior to popping an element makes sense.

(10) size will return the size of the current Queue.

(11) empty will return a bool, true if the Queue is empty.

(12) full will return a bool, true if the Queue is full. Note: Kept for compatibility, should always return false.

(13) clear performs the necessary actions, so that after its call the Queue will be empty. Note: Consider now how you will implement this in contrast to the other queue variant.

(14) serialize outputs to the parameter ostream os the complete content of the calling Queue object.

as well as a non-member overload for:

(i) operator<< will output (to terminal or file) the complete content of the nodeQueue object passed as a parameter.

Final Note about Node-based Queue: The required implementation will be a linear linked-list. This means that: a) It will have a front pointer to the first element of the Queue. b) It will have a back pointer to the last element of the Queue. c) You may modify the specifications to add an auxiliary variable to keep track of the count of elements in the Queue (e.g. an size_t m_size) if you so wish.

DataType.h and .cpp information:

DataType.h

#ifndef DATATYPE_H_

#define DATATYPE_H_

#include

class DataType{

friend std::ostream& operator<<(std::ostream& os, const DataType& dataType);

friend std::istream& operator>>(std::istream& is, DataType& dataType);

public:

DataType();

DataType(int intVal, double doubleVal);

bool operator==(const DataType& other_dataType) const;

DataType& operator= (const DataType& other_dataType);

int GetIntVal() const;

void SetIntVal(int i);

double GetDoubleVal() const;

void SetDoubleVal(double d);

private:

int m_intVal;

double m_doubleVal;

};

#endif //DATATYPE_H

/////////////////////////////////////////////////////////////////////////

DataType.cpp

#include "DataType.h"

#include

DataType::DataType(){

m_intVal = 0;

m_doubleVal = 0.0;

}

DataType::DataType(int intVal, double doubleVal){

m_intVal = intVal;

m_doubleVal = doubleVal;

}

bool DataType::operator==(const DataType& rhs) const{

return m_intVal==rhs.m_intVal && m_doubleVal==rhs.m_doubleVal; }

DataType& DataType::operator=(const DataType& rhs){

if (this != &rhs){

m_intVal = rhs.m_intVal;

m_doubleVal = rhs.m_doubleVal;

}

return *this;

}

int DataType::GetIntVal() const{

return m_intVal;}

void DataType::SetIntVal(int i){

m_intVal = i;}

double DataType::GetDoubleVal() const{

return m_doubleVal;}

void DataType::SetDoubleVal(double d){

m_doubleVal = d;}

std::ostream& operator<<(std::ostream& os, const DataType& dt){

os << "{" << dt.m_intVal << "," << dt.m_doubleVal << "}";

return os;

}

std::istream& operator>>(std::istream& is, DataType& dt){

char in_buf[255];

is >> in_buf;

dt.m_doubleVal = atof(in_buf);

dt.m_intVal = (int)dt.m_doubleVal;

dt.m_doubleVal -= dt.m_intVal;

return is;

}

Let me know if you have any questions or encounter any weird problems.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!