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<stdio.h>
#include<string>
#include<stdlib.h>
using namespace std;
typedef struct st
{
    int data;
    struct st *next,*last;
} tree;
int kk[10050];
int main()
{
    int t1;
    int k=0;
    cin>>t1;
    tree *head,*tail,*p,*t;
    head=NULL;
    tail=NULL;
    string s;
    for(int i=1; i<=t1; i++)
    {
        cin>>s;
        if(s=="LIN")
        {
            int x;
            cin>>x;
            p=new tree;
            p->data=x;
            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")
        {
            int x;
            cin>>x;
            p=new tree;
            p->data=x;
            if(tail)
            {
                p->last=tail;
                p->next=NULL;
                tail->next=p;
                tail=p;
            }
            else
            {
                p->next=NULL;
                p->last=NULL;
                head=p;
                tail=p;
            }
        }
        else if(s=="LOUT")
        {
            p=head;
            if(p)
            {
                t=p->next;
                if(t!=NULL)
                {
                    t->last=NULL;
                    head=t;
                }
                else
                {
                    head=NULL;
                    tail=NULL;
                }
            }
            else kk[k++]=i;
        }
        else if(s=="ROUT")
        {
            p=tail;
            if(p)
            {
                t=p->last;
                if(t)
                {
t->next=NULL;
                            tail=t;
                }
                else
                {
head=NULL;
                         tail=NULL;
                }
            }
            else kk[k++]=i;
        }

    }
    p=head;
    while(p)
    {
        if(p->next)printf("%d ",p->data);
        else printf("%d\n",p->data);
        p=p->next;
    }
    for(int i=0; i<k; i++)
        printf("%d ERROR\n",kk[i]);

    return 0;
}

猜你喜欢

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