Description
假设将循环队列定义为:以域变量rear和length分别指示循环队列中队尾元素的位置和内含元素的个数。编写相应的入队列和出队列的程序,并判断循环队列是否队满(在出队列的算法中要返回队头元素)。
Input
第一行输入队列大小N,第二行开始输入若干入队元素,队满时,停止入队,队满时输出yes,否则输出no。第三行输入出队元素。
Output
输出出队操作后的循环队列,并返回出队后的队头元素。
-
Sample Input
5 3 4 6 2 7 yes 4
-
Sample Output
6 2 7 6
#include<stdio.h>
#include<stdlib.h>
typedef struct{
int *Date;
int front;
int rear;
int length;
int max;
} Queue;
Queue* CreateQueue(int k)
{
Queue *Q;
Q = (Queue*)malloc(sizeof(Queue));
Q->max = k + 1;
Q->length=0;
Q->Date = (int*)malloc(sizeof(int)*Q->max);
Q->front = Q->rear = 0;
return Q;
}
void AddQ(Queue *Q, int item)
{
if((Q->rear+1)%Q->max == Q->front)
{
return;
}
else{
Q->rear = (Q->rear+1)%Q->max;
Q->Date[Q->rear] = item;
Q->length++;
}
}
void pop(Queue *Q)
{
if(Q->rear == Q->front)
{
printf("队列为空\n");
return;
}
else
{
Q->front = (Q->front+1)%Q->max;
Q->length--;
return;
}
}
int top(Queue *Q)
{
return Q->Date[Q->front];
}
void output(Queue *Q)
{
while(Q->front!=Q->rear)
{
printf("%d ",Q->Date[Q->front]);
Q->front++;
}
printf("%d\n",Q->Date[Q->rear]);
}
int main()
{
int num[1000]={0};
char s[10000];
char str[100];
int n,cnt=0;
scanf("%d",&n);
getchar();
Queue *q;
q=CreateQueue(n);
int i;
gets(s);
for(i=0;s[i];)
{
int t;
if (s[i] == ' ') {
i++;
continue;
}
else
{
for (int j = i; s[j] != ' ' && s[j]; j++)
{
num[cnt] = num[cnt] * 10 + (s[j] - '0');
t=j+1;
}
cnt++;
i=t;
}
}
for (int i = 0; i < cnt; i++)
{
AddQ(q, num[i]);
if (q->length == q->max - 1)
break;
}
scanf("%s",str);
int data;
scanf("%d",&data);
while (top(q) != data)
{
pop(q);
}
pop(q);
int t = top(q);
output(q);
printf("%d\n",t);
return 0;
}
题目描述有问题……凑合做了