双向队列

题目描述

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

输入

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

输出

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

示例输入

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

示例输出

3

7 ERROR

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef int QElemType; typedef int Status; typedef struct QNode {     QElemType data;     QNode *next; } QNode, *Queueptr; typedef struct {     Queueptr front;     Queueptr rear; } LinkQueue; Status InitQueue (LinkQueue &q)//队的初始化; {     q.front=q.rear=(Queueptr)malloc(sizeof(QNode));     if(!q.front) exit(0);     q.front->next=NULL;     return 1; } Status EnQueuer(LinkQueue &q, QElemType e)//从队尾进队; {     Queueptr p;     p=(Queueptr)malloc(sizeof(QNode));     if(!p) exit(0);     p->data=e;     p->next = NULL;     q.rear->next=p;     q.rear=p;     return 1; } void EnQueuel(LinkQueue &q, QElemType e)//从左插入元素相当于逆序建链表从对头进队; {     Queueptr p1, p2;     p1=(Queueptr)malloc(sizeof(QNode));     if(!p1) exit(0);     p1->data=e;     if(q.front==q.rear)         q.front->next=NULL;     p1->next = q.front->next;     q.front->next = p1;     p2=q.front;     while(p2->next!=NULL)     {         p2=p2->next;     }     q.rear=p2; } void DeQueuer (LinkQueue &q)//从队尾方向的出队; {     Queueptr p1, p2;     if (q.front == q.rear)         exit(0);     p1=q.rear;     p2=q.front;     while(p2->next!=q.rear)//指向队尾的前一个元素     {         p2=p2->next;     }     p2->next=p1->next;     q.rear = p2;     free(p1); } Status DeQueuel (LinkQueue &q)//从队头出队; {     Queueptr p;     if (q.front == q.rear)         return 0;     p = q.front->next;     q.front->next = p->next;     if (q.rear == p)         q.rear = q.front;     free (p);     return 1; } int main() {     int m, i, num, a[10010], j=0;     char c[5];     LinkQueue q;     InitQueue(q);     scanf("%d", &m);     for(i=1; i<=m; i++)     {         scanf("%s", c);         if(strcmp(c,"LIN")==0)         {             scanf("%d", &num);             EnQueuel(q, num);//队头进队         }         if(strcmp(c,"RIN")==0)         {             scanf("%d", &num);             EnQueuer(q, num);//队尾进队;         }         if(strcmp(c,"LOUT")==0)         {             if(q.front == q.rear)                 a[j++]=i;//计入不合法的命令;             else                 DeQueuel(q);         }         if(strcmp(c,"ROUT")==0)         {             if(q.front == q.rear)                 a[j++]=i;//计入不合法的命令;             else                 DeQueuer(q);         }     }     Queueptr p;     p=q.front->next;     while(p)//队元素的输出;     {         if(p->next!=NULL)             printf("%d ", p->data);         else             printf("%d\n", p->data);         p=p->next;     }     if(j)     {         for(i=0; i<j; i++)             printf("%d ERROR\n",a[i]);     } }

#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; int main() {     int n,i,j,x,l=0,m=0;     int a[100000],b[100000];//a数组是模拟队的数组计入队的元素,b数组计入不合法的命令;     char str[100];     scanf("%d",&n);     for(i=0;i<n;i++)     {         scanf("%s",str);         if(str[0]=='R')         {             if(str[1]=='I')             {                 scanf("%d",&x);                 a[l++]=x;             }             else if(str[1]=='O')             {                 if(l>0)                 l--;                 else                 b[m++]=i+1;             }         }         if(str[0]=='L')         {             if(str[1]=='I')             {                 scanf("%d",&x);                 l++;                 for(j=l-1;j>0;j--)                     a[j]=a[j-1];                     a[0]=x;             }             else if(str[1]=='O')             {                 if(l>0)                 {                     for(j=0;j<l-1;j++)                         a[j]=a[j+1];                         l--;                 }                 else                     b[m++]=i+1;             }         }     }     for(i=0;i<l;i++)     {         if(i==l-1)             printf("%d",a[i]);         else             printf("%d ",a[i]);     }     printf("\n");     for(i=0;i<m;i++)         printf("%d ERROR\n",b[i]);     return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值