#ifndef MYQUEUE_H
#define MYQUEUE_H
#include "node.h"
#include <cstdlib>
template <class objT>
class myqueue {
private:
node<objT> * it; node<objT> * head;
public:
objT read() {if (it != NULL) return it -> data;}
bool lineup(objT o)
{
if (it == NULL)
{
it = new node<objT>;
it -> data = o;
it -> next = new node<objT>;
it -> next = NULL;
head = it;
}
else
{
while (it -> next != NULL)
{ it = it -> next; }
it -> next = new node<objT>;
it = it -> next;
it -> data = o;
it -> next = new node<objT>;
it -> next = NULL;
it = head;
head = it;
}
returntrue;
}
objT dequeue()
{
if (it != NULL)
{
objT dat = read(); //it -> data;
discard();
return dat;
}
elsereturn NULL;
}
int thesize()
{
if (it != NULL)
{
int counter = 1;
node<objT> * T; T = it;
while (T -> next != NULL)
{ T = T -> next; ++counter; }
it = head;
return counter;
}
return 0;
}
void discard(){
if (it != NULL)
{
node<objT> * t;
t = it -> next;
it = t;
head = it;
}
return;
}
myqueue()
{
it = new node<objT>;
head = new node<objT>;
head = it;
it = NULL;
}
myqueue(const myqueue & rhs)
{
it = rhs;
head = it;
}
myqueue(const objT & o)
{
it = new node<objT>;
head = new node<objT>;
it -> data = o;
it -> next = NULL;
head = it;
}
~myqueue()
{
del(head);
del (it);
}
};
#endif
I havn't tested myqueue(const & rhs) yet. Am I right in assuming that I don't need to initialize it = new node<objT> because the node that they are being assigned to have already been allocated?
And I'm thinking about getting rid of the last constructor, I can't really think of any reason why it would be useful.
I guess I can get read of head too? I had previously use it for keeping track, and because I've seen other implementations with a similar function, I might decide to keep it for now.
The first cout statement prints the values in the reverse order...
Why?
Commment that line out and uncomment the other 2 cout statements and the values are couted in the correct order.
I think that you should stop reinventing the wheel and just use the std::queue template class.
Here are a few comments.
1) your default constructor is counter intuitive. Why is it default constructing the first node? It ought to be constructing an empty queue.
2) The purpose of the copy constructor is to make an EXACT copy, nothing more nothing less. Yes you will have to dynamically allocate memory and copy each and every node otherwise you will not have properly copy constructed the object.
3) the documentation is terrible. I can't figure out what you are trying to do in many of the functions. Comments are a good thing.
4) Why are the del functions not part of the class?
5) I don't think that you can return NULL in the dequeue function.
The code contains memory leaks at several points. Lines 23, 31, and 34 are just three.
There are also some parts that, AFAICT, shouldn't compile, such as line 87.
EDIT:
5) I don't think that you can return NULL in the dequeue function.
You can, as long as objT can be constructed from the literal 0, as is the case with int. I any other case, the code will not compile.
bool lineup(objT o)
{
if (it == NULL)
{
it = new node<objT>;
it -> data = o;
it -> next = new node<objT>;
it -> next = NULL;
head = it;
delete it;it = head;
}
else
{
while (it -> next != NULL)
{ it = it -> next; }
it -> next = new node<objT>;
it = it -> next;
it -> data = o;
it -> next = new node<objT>;
it -> next = NULL;
it = head;
head = it;
}
returntrue;
}