sdut oj双向队列

双向队列

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

Problem Description

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

Input

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

Output

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

Sample Input

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

Sample Output

3
7 ERROR

Hint

Source

wanglin

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int f[10050];
struct st
{
    struct st *next,*last;
    int data;
};
int main()
{
    struct st *tail,*head,*p,*r,*q;
    head=NULL;
    tail=head;
    int n,i,e=0,tem;
    string s;
    cin>>n;
    for(i=1; i<=n; i++)
    {
        cin>>s;
        if(s=="LIN")
        {
            cin>>tem;
            p=new st;
            p->data=tem;
            if(head)
            {
                p->next=head;
                p->last=NULL;
                head->last=p;
                head=p;
            }
            else
            {
                p->last=NULL;
                p->next=NULL;
                head=p;
                tail=p;
            }
        }
        else if(s=="RIN")
        {
            cin>>tem;
            p=new st;
            p->data=tem;
            if(tail)
            {
                p->last=tail;
                p->next=NULL;
                tail->next=p;
                tail=p;
            }
            else
            {
                p->last=NULL;
                p->next=NULL;
                head=p;
                tail=p;
            }
        }
        else if(s=="LOUT")
        {
            q=head;
            if(q)
            {
                r=q->next;
                if(r)
                {
                    r->last=NULL;
                    head=r;
                }
                else
                {
                    head=NULL;
                    tail=NULL;
                }

            }
            else f[e++]=i;
        }
        else if(s=="ROUT")
        {
            q=tail;
            if(q)
            {
                r=q->last;
                if(r)
                {
                    r->next=NULL;
                    tail=r;
                }
                else
                {
                    head=NULL;
                    tail=NULL;
                }

            }
            else f[e++]=i;
        }
    }
    p=head;
    while(p)
    {
        if(p->next)cout<<p->data<<" ";
        else cout<<p->data<<endl;
        p=p->next;
    }
    for(i=0; i<e; i++)
        cout<<f[i]<<" "<<"ERROR"<<endl;


    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41374539/article/details/81149639