循环队列
用尾指针和队列元素个数建立循环队列
#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);
}
}