循环队列(严3.30)

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;  
}  

题目描述有问题……凑合做了

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值