#include <iostream>
using namespace std;
template <typename T>
class MyQueue
{
public:
struct QNode
{
T data;
QNode *next;
QNode()
{
next=NULL;
}
};
MyQueue()
{
initQueue();
}
~MyQueue()
{
}
void initQueue()
{
length=0;
front=NULL;
rear=NULL;
}
bool clearQueue()
{
if (isEmpty())
{
return true;
}
else
{
while(front!=rear)
{
deQueue();
}
}
}
bool isEmpty()
{
if (rear==NULL&&front==NULL)
{
return true;
}
return false;
}
T getHead()
{
return front->next->data;
}
bool enQueue(T e)
{
QNode *Node;
Node=new QNode;
Node->data=e;
if (front==NULL&&rear==NULL)
{
front=new QNode;
rear=new QNode;
front->next=Node;
rear=Node;
}
else{
rear->next=Node;
rear=Node;
}
length++;
return true;
}
T deQueue()
{
if (isEmpty())
{
return 0;
}
T e;
QNode *p=new QNode;
if (front->next==rear)
{
p=rear;
front=NULL;
rear=NULL;
e=p->data;
free(p);
}
else
{
p=front->next;
front->next=front->next->next;
e=p->data;
free(p);
}
length--;
return e;
}
bool deQueue(QNode *e)
{
if (isEmpty())
{
return false;
}
QNode *p=new QNode;
if (front->next==rear)
{
p=rear;
front=NULL;
rear=NULL;
e=p;
free(p);
}
else
{
p=front->next;
front->next=front->next->next;
e=p;
free(p);
}
length--;
return true;
}
int getLength()
{
return length;
}
void displayQueue()
{
if (isEmpty())
{
cout<<"Empty Queue"<<endl;
return;
}
QNode *p=front->next;
while(p->next!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<rear->data;
cout<<endl;
}
private:
QNode *front;
QNode *rear;
int length;
};
int main()
{
MyQueue<int> queue;
for (int i=0;i<10;i++)
{
queue.enQueue(i+1);
}
queue.displayQueue();
queue.deQueue();
queue.displayQueue();
cout<<queue.getHead()<<endl;
queue.clearQueue();
queue.displayQueue();
return 0;
}