双向队列

双向队列

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

提示

这里可以采用数组进行模拟。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>

using namespace std;

const int Max=30000;

int a[Max];

int b[10000];

int L,R,top;

int main()
{
    int n,data;
    char s[10];
    top=0;
    L=Max/2;
    R=Max/2;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%s",s);
        if(strcmp(s,"LIN")==0)
        {
            scanf("%d",&data);
            a[--L]=data;
        }
        else if(strcmp(s,"RIN")==0)
        {
            scanf("%d",&data);
            a[R++]=data;
        }
        else if(strcmp(s,"LOUT")==0)
        {
            if(L<R)//             判断是否合法</span>
            {
                L++;
            }
            else
            {
                b[top++]=i;
            }
        }
        else if(strcmp(s,"ROUT")==0)
        {
            if(L<R)
            {
                R--;
            }
            else
            {
                b[top++]=i;
            }
        }
    }
    if(L<R)
    {
        for(int i=L; i<R; i++)
        {
            if(i!=L)
                cout<<" ";
            cout<<a[i];
        }
        cout<<endl;
    }
    for(int i=0; i<top; i++)
    {
        cout<<b[i]<<" "<<"ERROR"<<endl;
    }
    return 0;
}

也可以采用STL方法。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
using namespace std;
int flag[10010];
int main()
{
    deque<int>q;
    int n,m,i,k;
    char str[10];
    memset(flag,0,sizeof(flag));
    scanf("%d",&n);
    int j=0;
    for(i=1; i<=n; i++)
    {
        scanf("%s",str);
        if(strcmp(str,"LIN")==0)
        {
            cin>>m;
            q.push_front(m);
        }
        else if(strcmp(str,"RIN")==0)
        {
            cin>>m;
            q.push_back(m);
        }
        else if(strcmp(str,"LOUT")==0)
        {
            if(!q.empty())
                q.pop_front();
            else
                flag[j++]=i;
        }
        else if(strcmp(str,"ROUT")==0)
        {
            if(!q.empty())
                q.pop_back();
            else
                flag[j++]=i;
        }
    }
    int tmp=0;
    while(!q.empty())
    {
        if(!tmp)
        {
            cout<<q.front();
            tmp=1;
        }
        else cout<<" "<<q.front();
        q.pop_front();
    }
    cout<<endl;
    for(int i=0; i<j; i++)
        cout<<flag[i]<<" ERROR\n";
    return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值