SPOJ ADAQUEUE Ada and Queue

Ada the Ladybug has many things to do. She puts them into her queue. Anyway she is very indecisive, so sometime she uses the top, sometime the back and sometime she decides to reverses it.

Input

The first line consists of 1 ≤ Q ≤ 106, number of queries. Each of them contains one of following commands

back - Print number from back and then erase it

front - Print number from front and then erase it

reverse - Reverses all elements in queue

push_back N - Add element N to back

toFront N - Put element N to front

All numbers will be 0 ≤ N ≤ 100

Output

For each back/front query print appropriate number.

If you would get this type of query and the queue would be empty, print "No job for Ada?" instead.

Example Input

15
toFront 93
front
back
reverse
back
reverse
toFront 80
push_back 53
push_back 50
front
front
reverse
push_back 66
reverse
front

Example Output

93
No job for Ada?
No job for Ada?
80
53

66

和上一篇一样都是双端输入的应用。

代码:

#include <iostream>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<map>
#include<vector>
#include<deque>
#define ll long long
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
char a[1001];
std::deque<int>p,q;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {

        int flag=0;
        for(int i=0; i<n; i++)
        {
            //getchar();
            scanf("%s",a);
            if(a[0]=='t')
            {
                int m;
                scanf("%d",&m);
                if(flag==0)
                    p.push_front(m);
                else
                    p.push_back(m);

            }
            if(a[0]=='f')
            {
                if(p.empty())
                {
                    printf("No job for Ada?\n");
                }
                else
                {
                    //printf("%d\n",flag);
                    if(flag==1)
                    {
                        printf("%d\n",p.back());
                        p.pop_back();
                    }
                    else
                    {
                        printf("%d\n",p.front());
                        p.pop_front();
                    }
                }
            }
            if(a[0]=='b')
            {
                if(p.empty())
                {
                    printf("No job for Ada?\n");
                }
                else
                {
                    if(flag==1)
                    {
                        printf("%d\n",p.front());
                        p.pop_front();
                    }
                    else
                    {
                        printf("%d\n",p.back());
                        p.pop_back();
                    }
                }

            }
            if(a[0]=='r')
            {

                if(flag==1)
                {
                    flag=0;
                }
                else if(flag==0)//之前将这里写成if导致没a出来,这是气死了,哎。。。
                    flag=1;

            }
            if(a[0]=='p')
            {
                int x;
                scanf("%d",&x);
                if(flag==0)
                {
                    p.push_back(x);
                }
                else
                {
                    p.push_front(x);
                }
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值