- #include<iostream>
- using namespace std;
- template <typename T>struct node{
- T data;
- node<T> *next;
- };
- template <typename T>class Quen{
- private :
- node<T> *front,*rear;
- public :
- Quen()
- {
- front=new node<T>;
- front->next=NULL;
- rear=front;
- }
- ~Quen()
- {
- clearQuen();
- delete front;
- }
- void clearQuen()
- {
- node<T> *q,*p=front->next;
- while(p!=NULL)
- {
- q=p->next;
- delete p;
- p=q;
- }
- front->next=NULL;
- rear=front;
- }
- bool IsEmpty()
- {
- return front==rear;
- }
- int length()
- {
- node<T> *p=front->next;
- int n=0;
- while(p!=NULL)
- {
- p=p->next;
- n++;
- }
- return n;
- }
- bool push(T e)
- {
- node<T> *s=new node<T>;
- s->data=e;
- s->next=NULL;
- rear->next=s;
- rear=s;
- return true;
- }
- bool pop(T &e)
- {
- if(front==rear)
- return false;
- front->next=front->next->next;
- return true;
- }
- void visit()
- {
- node<T> *p=front->next;
- while(p!=NULL)
- {
- cout<<p->data<<",";
- p=p->next;
- }
- cout<<endl;
- }
- };
- int main()
- {
- Quen<int> Q;
- cout<<boolalpha<<Q.IsEmpty()<<endl;
- cout<<Q.length()<<":";
- Q.visit();
- for(int i=1;i<5;i++)
- {
- Q.push(i);
- cout<<Q.length()<<":";
- Q.visit();
- }
- Q.clearQuen();
- cout<<Q.length()<<":";
- Q.visit();
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/300720134877.html
来源: http://www.codesnippet.cn/detail/300720134877.html