循环队列
#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 10000
typedef struct
{
int *q;
int length,rear;
}SeqQueue;
void creat(SeqQueue* paqu)
{
(*paqu).q=(int *)malloc(sizeof(int)*(MAXNUM));
if((*paqu).q==NULL)
{
printf("Out Space\n");
}
else
{
(*paqu).length=0;
(*paqu).rear=0;
}
}
int full( SeqQueue paqu){
if(MAXNUM==paqu.length)return 1;
else return 0;
}
void enQueue_seq( SeqQueue* paqu, int x )
{
if ( full(*paqu) )
printf ( "Full queue.\n" );
else
{
(*paqu).q[(*paqu).rear] = x;
(*paqu).rear =((*paqu).rear+1)%MAXNUM;
(*paqu).length++;
}
}
int deQueue_seq( SeqQueue* paqu)
{
int p,head;
head=((*paqu).rear+MAXNUM-(*paqu).length)%MAXNUM;
if (!(*paqu).length)
printf ( "Empty Queue.\n" );
else
{ p=(*paqu).q[head];
(*paqu).length--;}
return p;
}
void shuchu( SeqQueue paqu){
int head,cnt;
head=(paqu.rear+MAXNUM-paqu.length)%MAXNUM;
for(cnt=1;cnt<=paqu.length;head=(head+1)%MAXNUM,cnt++){
printf("%d ",paqu.q[head]);
}
}
int get(SeqQueue paqu){
int head;
head=(paqu.rear+MAXNUM-paqu.length)%MAXNUM;
return paqu.q[head];
}
int main()
{
int length,i=0,p,cnt=0;
scanf("%d",&length);
SeqQueue Q;
creat(&Q);
char s[10000];
int num[10000]= {0},j;
getchar();
gets(s);
while(s[i]!='\0')
{
int t;
if (s[i] == ' ')
{
i++;
continue;
}
else
{
for (j = i; s[j] != ' ' && s[j]!='\0'; j++)
{
num[cnt] = num[cnt] * 10 + (s[j] - '0');
t=j+1;
}
cnt++;
i=t;
}
}
i=0;
while(i<length)
{
enQueue_seq( &Q, num[i]);
i++;
}
if(cnt>=length)printf("yes\n");
else printf("no\n");
int d;
scanf("%d",&d);
for(i=0;i<length;i++){
p=deQueue_seq(&Q);
if(p==d)break;
}
shuchu(Q);
printf("\n");
if(Q.length){ p=get(Q);
printf("%d",p);}
return 0;
}
不知道为什么提交显示结果错误