im working on a program dealing with template, class, and structs and i need a little assistance on moving forward with this program.. i have below my code and the directions.. feel free to revise what i have and push me forward
so far i have :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
#include <stdexcept>
using std::ostream;
using std::out_of_range;
template <class T>
struct QNode
{
QNode(const T & X)
T data;
QNode *next;
};
QNode::QNode(const T & x)
{
x = QNode;
next = NULL;
}
template <class T>
class Queue
{
private:
QNode *qfront;
QNode *qrear;
public:
Queue(); //default constructor
~Queue(); // destructor
Queue(const Queue& s); //copy constructor
Queue& operator=(const Queue& rightOp);
friend ostream& operator<< <>(ostream&, const Queue<T>&);
void clear();
int size();
bool empty();
bool front();
bool back();
void push();
void pop();
};
template <class T>
Queue<T>::Queue()
{
qfront = '\0';
qrear = '\0';
}
template <class T>
Queue<T>::~Queue()
{
Queue.clear();
}
template <class T>
Queue<T>::Queue(const Queue& s)
{
qront = '\0';
qrear = '\0';
}
template <class T>
Queue& Queue<T>::operator=(const Queue& rightOp)
{
if(this == &rightOp)
{
return *this;
}
}
template <class T>
ostream & operator<<(ostream & leftOp, const Queue<T> & rightOp)
{
}
template <class T>
void Queue<T>::clear()
{
data=0;
qfront='\0';
qrear='\0';
}
template <class T>
int Queue<T>::size()
{
return data;
}
template <class T>
bool Queue<T>::empty()
{
}
|
struct QNode
Data members
This template structure should have two data members: a member of the template parameter type to store an item to be inserted into the queue, and a pointer to a QNode. The pointer next will point to the next node in the linked list (or be NULL if this is the last node in the list).
Since the Queue class will need access to these data members, make them public (the default for a struct).
Methods
*
Constructor
The structure should have one constructor that takes an argument of the template parameter type. Make this argument a reference to const data. The constructor should copy the argument into the queue node and set the node's pointer to NULL.
class Queue
Data members
This class should have two data members, both pointers to QNodes. The pointer qFront will point to the front node in the queue (or be NULL if the queue is empty); the pointer qRear will point to the rear node in the queue (or be NULL if the queue is empty).
Methods and associated functions
*
Constructor
The class should have a default constructor that takes no arguments. The constructor should set both pointer data members to NULL.
*
Destructor
The class should have a destructor. The destructor can simply call the clear() method described below.
*
Copy constructor
The class should also have a proper copy constructor. If you choose to make a copy of the linked list by using the push method, make sure you set both the front and rear pointers for the new queue to NULL before you attempt to insert any nodes into it.
*
operator=
The assignment operator should be properly overloaded.
*
operator<<
The output operator should be overloaded so that an entire Queue can be sent to the standard output. As usual, this function will need to be a friend rather than a method. Declaring a template function to be a friend of a template class requires some special syntax - see the Implementation Hints below.
*
clear()
This method takes no arguments and returns nothing. It should properly set the queue back to the empty state. That means deleting all of the nodes in the queue and setting the front and rear pointers back to NULL.
*
size()
This method takes no arguments and returns an integer. It should return the current size of the queue; i.e., the number of data items currently stored in the queue. Since this is not stored as a data member of the queue class, you will have to traverse the linked list and count the nodes.
*
empty()
Returns true if there are no data items currently stored in the queue; otherwise returns false.
*
front()
This method takes no arguments and returns the template parameter type. If the queue is empty, this method should throw an out_of_range exception. Otherwise, it should return the data stored in the front node of the queue.
*
back()
This method takes no arguments and returns the template parameter type. If the queue is empty, this method should throw an out_of_range exception. Otherwise, it should return the data stored in the rear node of the queue.
*
push()
This method takes a reference to a constant item of the template parameter type as its argument (the item to insert into the queue). It returns nothing. The method should insert the item at the rear of the queue.
*
pop()
This method takes no arguments and returns nothing. If the queue is empty, this method should throw an out_of_range exception. Otherwise, it should remove the item at the front of the queue.
If you like, you may write private methods for the Queue class in addition to the methods described above. For example, you may want to write a copyList() method that can be called by both the copy constructor and overloaded assignment operator.