双向队列

双向队列

Time Limit: 1000MS Memory limit: 65536K

题目描述

      想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首;两头都可以做出队,入队的操作。
现在给你一系列的操作,请输出最后队列的状态;
命令格式:
LIN X  X表示一个整数,命令代表左边进队操作;
RIN X  表示右边进队操作;
ROUT
LOUT   表示出队操作;

输入

第一行包含一个整数M(M<=10000),表示有M个操作;
以下M行每行包含一条命令;
命令可能不合法,对于不合法的命令,请在输出中处理;

输出

输出的第一行包含队列进行了M次操作后的状态,从左往右输出,每两个之间用空格隔开;
以下若干行处理不合法的命令(如果存在);
对于不合法的命令,请输出一行X ERROR
其中X表示是第几条命令;

示例输入

8
LIN 5
RIN 6
LIN 3
LOUT
ROUT
ROUT
ROUT
LIN 3

示例输出

3
7 ERROR

提示

 

来源

wanglin

示例程序

 一开始的时候,拿到题后就开始用双向链表做,但是敲了大概快完成了,感觉实在是太乱了,于是看了下老师的给的思路,重新敲了一遍,感觉这样清楚了许多。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node
{
    int data;
    struct node *next;
}node;

typedef struct
{
    node *front;
    node *rear;
}queue;

int initqueue(queue &q)
{
    q.front = q.rear = (node *)malloc(sizeof(node));
    if(!q.front) exit(0);
    q.front->next = NULL;
    q.rear->next = NULL;
    return 1;
}
void enqueuel(queue &q,int e)
{
    node *p,*tail;
    p = (node *)malloc(sizeof(node));
    if(!p) exit(0);
    p->data = e;
    if(q.front == q.rear)
        q.front->next = NULL;
    p->next = q.front->next;
    q.front->next = p;
    tail = q.front;
    while(tail->next != NULL)
    {
        tail = tail->next;
    }
    q.rear = tail;

}
void enqueuer(queue &q,int e)
{
    node *p;
    p = (node *)malloc(sizeof(node));
    if(!p) exit(0);
    p->data = e;
    p->next = NULL;
    q.rear->next = p;
    q.rear = p;
}
void dequeuel(queue &q)
{
    if(q.front == q.rear) return ;
    node *p;
    p = q.front->next;
    q.front->next = p->next;
    if(q.rear == p)
        q.rear = q.front;
    free(p);

}
void dequeuer(queue &q)
{
    if(q.front == q.rear) return ;
    node *p,*tail;
    p = q.rear;
    tail = q.front;
    while(tail->next != q.rear)
        tail = tail->next;
    q.rear = tail;
    q.rear->next = NULL;
    free(p);
}
int main()
{
    int n,num;
    queue q;
    char str[100];
    scanf("%d",&n);
    int k = 0,a[10050];
    initqueue(q);
    for(int i = 1;i <= n;i++)
    {
        scanf("%s",str);
        if(strcmp(str,"LIN") == 0)
        {
            scanf("%d",&num);
            enqueuel(q,num);
        }
        else if(strcmp(str,"RIN") == 0)
        {
            scanf("%d",&num);
            enqueuer(q,num);
        }
        else if(strcmp(str,"LOUT") == 0)
        {
            if(q.front == q.rear)
                a[k++] = i;
            else
                dequeuel(q);
        }
        else if(strcmp(str,"ROUT") == 0)
        {
            if(q.front == q.rear)
                a[k++] = i;
            else
                dequeuer(q);
        }
    }
    node *p;
    p = q.front->next;
    while(p)
    {
        printf("%d",p->data);
        p = p->next;
        if(p)
            printf(" ");
        else
            printf("\n");
    }
    if(k)
    {
        for(int i = 0;i < k;i++)
            printf("%d ERROR\n",a[i]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值