代码:
#include <iostream> using namespace std; template <typename T> struct node{ T val; node *next; node(){ next=NULL; } node(T x){ val=x; next=NULL; } }; template <typename T> class Queue{ private : node<T> *l,*r; int len=0; public : Queue(){ node<T> *t=new node<T>(); l=r=t; len=0; } int size(){ return len; } bool empty(){ if(len==0) return true; return false; } T& front(){ if(len) return l->next->val; } T& back(){ if(len) return r->val; } void pop(){ if(len) { l=l->next; len--; } } void push(const T a){ node<T> *t=new node<T>(a); r->next=t; r=t; t=NULL; len++; } }; int main(){ Queue<string> Q; Q.push(string("abc1")); Q.push(string("abc2")); Q.push(string("abc3")); cout<<Q.front()<<endl; cout<<Q.back()<<endl; Q.pop(); cout<<Q.front()<<endl; cout<<Q.back()<<endl; return 0; }