Hrbust-1940(队列)

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;

int main()
{
    int T;
    while(scanf("%d",&T)!=EOF)
    {
        getchar();
        queue<int>q;
        while(T--)
        {
            char s[20];
            scanf("%s",s);
            if(strcmp(s,"enqueue")==0)
            {
                int n;
                scanf("%d",&n);
                q.push(n);
            }
            if(strcmp(s,"dequeue")==0)
            {
                if(q.empty())
                    printf("this is empty!\n");
                else
                {
                    cout << q.front() << endl;
                    q.pop();
                }
            }
            if(strcmp(s,"query")==0)
            {
                if(q.empty())
                    printf("this is empty!\n");
                else
                {
                    while(!q.empty())
                    {
                        cout << q.front() << endl;
                        q.pop();
                    }
                }
            }
        }
        printf("\n");
    }
}
#include<stdio.h>
#include<string.h>
#include<malloc.h>

#define maxsize 100

typedef struct que
{
    int a[maxsize];
    int front;
    int rear;
} queue;

void Initqueue(queue *q)
{
    q->front=q->rear=0;
}

void Insertqueue(queue *q,int e)
{
    q->a[q->rear]=e;
    q->rear++;
}

void  Deletequeue(queue *q)
{
    if(q->front==q->rear)
    {
        printf("this is empty!\n");
    }
// e=q->a[q->front];
    else
    {
        int e=q->a[q->front];
        printf("%d\n",e);
        q->front++;
    }
}

void query(queue *q)
{
    while(q->front!=q->rear)
    {
        int e=q->a[q->front];
        printf("%d\n",e);
        q->front++;
    }
}

int main()
{
    int T;
    queue *q=(queue *)malloc(sizeof(queue));
    while(scanf("%d",&T)!=EOF)
    {
        Initqueue(q);
        while(T--)
        {
            char b[200];
            int e;
            getchar();
            scanf("%s",b);
            if(strcmp(b,"enqueue")==0)
            {
                scanf("%d",&e);
                Insertqueue(q,e);
            }
            if(strcmp(b, "dequeue")==0)
            {
                Deletequeue(q);
            }
            if(strcmp(b,"query")==0)
            {
                if(q->front==q->rear)
                    printf("this is empty!\n");
                else
                    query(q);
            }
        }
        printf("\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值