• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C语言数据结构之队列使用范例

OC/C/C++ 水墨上仙 2754次浏览

C语言数据结构之队列使用范例

#include<iostream.h>
//#include<stdlib.h>
template<class T>
struct linknode
{
	T data;
	linknode<T> *link;
	linknode<T>(linknode<T> *ptr=NULL)
	{
		link=ptr;
	}
	linknode<T>(const T&item,linknode<T> *ptr=NULL)
	{
		link=ptr;
		data=item;
	}
};
template<class T>
class linkedqueue
{
	public:
		linkedqueue():rear(NULL),front(NULL)
		{
		}
		~linkedqueue()
		{
			makeempty();
		}
		bool enqueue(const T&x);
		bool dequeue(T&x);
		bool getfront(T&x)const;
		void makeempty();
		bool isempty()const
		{
			return (front==NULL)?true:false;
		}
		int getsize()const;
		void show()const;
		//friend ostream& operator<<(ostream& os,linkedqueue<T>& Q);
	protected:
		linknode<T> *front,*rear;
};
template<class T>
void linkedqueue<T>::makeempty()
{
	linknode<T> *p;
	while(front!=NULL)
	{
		p=front;
		front=front->link;
		delete p;
	}
};
template<class T>
bool linkedqueue<T>::enqueue(const T&x)
{
	int s;
	cout<<"请选择插入数据的端:1 队头 2 队尾 :";
	cin>>s;
	if(s==2)
	{
		if(front==NULL)
		{
			front=rear=new linknode<T>(x);
			if(front==NULL)
				return false;
		}
		else
		{
			rear->link=new linknode<T>(x);
			if(rear->link==NULL)
				return false;
			rear=rear->link;
		}
		return true;
	}
		
	else if(s==1)
	{
			if(front==NULL)
		{
			front=rear=new linknode<T>(x);
			if(front==NULL)
			return false;
		}
		else
		{
			linknode<T> *p;
			p=front;
			front=new linknode<T>(x);
			front->link=p;
		}
	return true;
	}
};
template<class T>
bool linkedqueue<T>::dequeue(T&x)
{
	if(isempty()==true)
		return false;
	linknode<T> *p=front;
	x=front->data;
	front=front->link;
	delete p;
	return true;
};
template<class T>
bool linkedqueue<T>::getfront(T&x)const
{
	if(isempty()==true)
		return false;
	x=front->data;
	return true;
};
template<class T>
int linkedqueue<T>::getsize()const
{
	linknode<T>*p;
	p=front;
	int k=0;
	while(p!=NULL)
	{
		p=p->link;
		k++;
	}
	return k;
};
template<class T>
void linkedqueue<T>::show()const
{
	linknode<T>* p=front;
	while(p!=NULL)
	{
		cout<<p->data<<"	";
		p=p->link;
	}
	cout<<endl;
};
void main()
{
	linkedqueue<int> myqueue;
	for(int i=1;i<=3;i++)
	{
		myqueue.enqueue(i);
	}
	myqueue.show();
	for(i=1;i<=3;i++)
	{
		myqueue.enqueue(i);
	}
	myqueue.show();
//	int x;
//	myqueue.dequeue(x);
//	cout<<x<<endl;
//	myqueue.show();
//	cout<<myqueue.getsize()<<endl;
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C语言数据结构之队列使用范例
喜欢 (0)
加载中……