SDUTOJ 1466--双向队列

这道题可以用双向链表来做,左边进队是逆序建链表,右边进队是顺序建链表。

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int f[10050];

struct node
{
    int data;
    struct node *next,*last;
}*head;

int main()
{
    int n,i,num,e=0;
    string st;
    struct node *tail,*p,*q,*r;
    head=NULL;
    tail=NULL;                  //初始化;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>st;
        if(st=="LIN")
        {
            p=new node;
            cin>>num;
            p->data=num;
            if(head)
            {
                p->next=head;
                p->last=NULL;
                head->last=p;
                head=p;
            }
            else
            {
                p->next=NULL;
                p->last=NULL;
                head=p;
                tail=p;
            }                           //逆序建链表
        }else if(st=="RIN")
        {
            p=new node;
            cin>>num;
            p->data=num;
            if(tail)
            {
                p->next=NULL;
                p->last=tail;
                tail->next=p;
                tail=p;
            }else
            {
                p->next=NULL;
                p->last=NULL;
                head=p;
                tail=p;
            }                           //顺序建链表;
        }else if(st=="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(st=="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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值