循环队列

循环队列

用尾指针和队列元素个数建立循环队列

#include<iostream>
#include<cstdio>
#include<cstdlib>
#define maxsize 5
using namespace std;
int wz;
int dt;
typedef struct Sequenqueue
{
	int data[maxsize];
	int rear;
	int length;
}Sequenqueue;
Sequenqueue *Init_Sequenqueue()//初始化 
{
	Sequenqueue *q;
	q=(Sequenqueue *) malloc (sizeof (Sequenqueue));
	if(q!=NULL)
	{
		q->rear=0;
		q->length=0;
	}
	return q;
}
bool Sequenqueue_Empty(Sequenqueue *p)
{
	//if(p->head==p->end)
	if(p->length==0)
	return true;
	else
	return false;
} 
bool Sequenqueue_Full(Sequenqueue *p)
{
	//if((p->end+1)%maxsize==p->head)
	if(p->length==maxsize)
	return true;
	else
	return false;
}
int Enter_Sequenqueue(Sequenqueue *q,int x)
{
  if(Sequenqueue_Full(q))
  return 0;
  else
  {
  	q->data[q->rear]=x;
  	q->rear=(q->rear+1)%maxsize;
  	q->length ++;
  }
return 1;
}
int Free_Sequenqueue(Sequenqueue *q,int *x)
{
  if(Sequenqueue_Empty(q))
  return 0;
  else
  {
   *x=q->data[wz];
   	cout<<q->data[wz]<<endl;
   	q->length--;
    wz++;
  }
return 1;
}
int Get_Sequenqueue(Sequenqueue *q,int *x)
{
if(Sequenqueue_Empty(q))
  return 0;
else
*x=q->data[dt];
cout<<q->data[dt]<<endl;
return 1;
}
int main()
{
	int *z,d,*y;
	Sequenqueue *q;
	q=Init_Sequenqueue();
   	while(!Sequenqueue_Full(q))
	{
		
		cout<<"请输入你要入队的队列元素:";
		cin>>d;
		Enter_Sequenqueue(q,d); 
	}
	cout<<"请输出队头元素:";
			Get_Sequenqueue(q,y);
	while(!Sequenqueue_Empty(q))
		{
			cout<<"输出出队的队列元素:";
			Free_Sequenqueue(q,z);
		} 
		
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值