C++数据结构--队列(链表实现)

测试代码

#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;
}

运行结果

在这里插入图片描述

发布了218 篇原创文章 · 获赞 523 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/weixin_44225182/article/details/105370592