双向队列

双向队列

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

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

Input

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

Output

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

Example Input
8
LIN 5
RIN 6
LIN 3
LOUT
ROUT
ROUT
ROUT
LIN 3
Example Output
3

7 ERROR

代码1 链表模拟

#include<iostream>
using namespace std;
#include<cstdlib>
#include<string>
struct node
{
    int data;
    struct node*next;
};
int main()
{
    int a[10010] ,k = 0;
    string num;
    int i ,n ,t;
    struct node*head,*tail,*p,*q;
    head = (struct node*)malloc(sizeof(struct node));
    head->next = NULL;
    tail = head;
    cin >> n;
    for(int i = 1;i<=n;i++)
    {
        cin >> num;
        if(num == "LIN")
        {
            cin >> t;
            p = (struct node*)malloc(sizeof(struct node));
            p->next = NULL;
            if(head->next==NULL)//插入的第一个要移动尾指针
            {
                tail->next = p;
                tail = p;
            }
            else
            {
                p->next = head->next;
                head->next = p;
            }
            p->data = t;
        }
        else if(num=="RIN")
        {
            cin >> t;
            p = (struct node*)malloc(sizeof(struct node));
            p->next = NULL;
            tail->next = p;
            tail = p;
            p->data = t;
        }
        else if(num=="LOUT")
        {
            if(head->next!=NULL)
            {
             p = head->next;
             head->next = p->next;
              free(p);
              if(head->next==NULL)//如果删除之后为空,尾指针要继续指向头指针,防止被删除
                tail = head;
            }
            else a[++k] = i;
        }
        else if(num == "ROUT")
        {
            if(head->next!=NULL)
            {
            p = head->next;
            q = head;
            while(p->next!=NULL)
               {
                   q  = q->next;
                   p = p->next;
               }
               free(p);
               tail = q;
               tail->next = NULL;
            }
            else  a[++k] = i;
        }
        else a[++k] = i;//别的命令也是错的
    }
    p = head->next;
    while(p)
    {
        cout << p->data ;
        if(p->next!=NULL)
            cout << " ";
        else cout << endl;
        p = p->next;
    }
        for(i = 1;i<=k;i++)
            cout << a[i] << " ERROR" <<endl;
    return 0;
}

代码2 用数组,从中间开始存
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int a[10010];
int s[20010];
int main()
{
    int n ,i , k = 0,t;
    int  mid = 10010, fr = mid ,rear = mid;
    string num;
    cin >> n;
    for(i = 1;i<=n;i++)
    {
        cin >> num;
        if(num == "LIN")
        {
            cin >> t;
            s[--fr] = t;
        }
        else if(num=="RIN")
        {
            cin >> t;
            s[rear++] = t;
        }
        else if(num == "LOUT")
        {
            if(fr>=rear) a[++k] = i,fr = rear = mid;//指向越界要指回去
            else  fr++;
        }
        else if(num == "ROUT")
        {
            if(fr>=rear) a[++k] = i,fr = rear = mid;
            else  rear--;
        }
        else a[++k] = i;
    }
    if(fr<rear)
    {
        for(i = fr ;i<rear-1 ;i++)
            cout << s[i]  << " ";
        cout <<s[i] << endl;
    }
    for(int i = 1;i<=k;i++)
        cout << a[i] << " ERROR" << endl;
        return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值