#include
using namespace std;
template class myQueue;
template
class node
{
friend class myQueue;
node(const T& t) : data(t), next(0)
{
}
T data;
node *next;
};
template
class myQueue
{
public:
myQueue() : head(0), tail(0), length(0)
{
}
~myQueue()
{
destroy();
}
bool empty() const
{
//return length == 0;
return head == 0;
}
void push(const T&);
T& front() const;
void pop();
int getLen() const;
private:
void destroy();
node *head, *tail;
int length;
};
template
void myQueue::push(const T& val)
{
node *p = new node(val);
if(empty())
head = tail = p;
else
{
tail->next = p;
tail = p;
}
length++;
}
template
T& myQueue::front() const
{
return head->data;
}
template
void myQueue::pop()
{
if(empty())
{
cout << "empty" << endl;
return;
}
else
{
node *p = head;
head = head->next;
delete p;
length--;
}
}
template
void myQueue::destroy()
{
while(!empty())
{
pop();
length--;
}
}
template
int myQueue::getLen() const
{
return length;
}
int main(void)
{
// myQueue q;
// q.push(1);
// q.push(2);
// q.push(3);
// q.push(4);
// q.push(5);
// myQueue q;
// q.push(1.1);
// q.push(2.2);
// q.push(3.3);
// q.push(4.4);
// q.push(5.5);
myQueue q;
q.push("la");
q.push("lalala");
q.push("hehe");
q.push("hoho");
q.push("get it");
while(!q.empty())
{
int len = q.getLen();
cout << "queue length: " << len << endl;
cout << "front: ";
cout << q.front() << endl;
q.pop();
}
return 0;
}