测试代码
#include<iostream>
using namespace std;
template<class datatype>
struct node{
datatype data;
node<datatype> *next;
};
template<class datatype>
class queue{
public:
queue()
{
node<datatype> *s;
s=new node<datatype>;
front=s;
rear=s;
}
~queue(){}
void push(datatype x)
{
node<datatype> *s;
s=new node<datatype>;
s->data=x;
rear->next=s;
rear=s;
}
datatype pop()
{
node<datatype> *s;
s=new node<datatype>;
s=front->next;
datatype x;
x=s->data;
front->next=s->next;
if(s->next==NULL)
rear=front;
delete s;
return x;
}
private:
node<datatype> *front,*rear;
};
int main()
{
queue<int > a;
a.push(1);
a.push(2);
a.push(3);
a.push(4);
cout<<a.pop()<<endl;
cout<<a.pop()<<endl;
cout<<a.pop()<<endl;
cout<<a.pop()<<endl;
return 0;
}
运行结果