双向队列
来源:互联网 发布:朱苏力 知乎 编辑:程序博客网 时间:2024/06/11 19:22
题目描述
想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首;两头都可以做出队,入队的操作。
现在给你一系列的操作,请输出最后队列的状态;
命令格式:
LIN X X表示一个整数,命令代表左边进队操作;
RIN X 表示右边进队操作;
ROUT
LOUT 表示出队操作;
输入
第一行包含一个整数M(M<=10000),表示有M个操作;
以下M行每行包含一条命令;
命令可能不合法,对于不合法的命令,请在输出中处理;
输出
输出的第一行包含队列进行了M次操作后的状态,从左往右输出,每两个之间用空格隔开;
以下若干行处理不合法的命令(如果存在);
对于不合法的命令,请输出一行X ERROR
其中X表示是第几条命令;
示例输入
8LIN 5RIN 6LIN 3LOUTROUTROUTROUTLIN 3
示例输出
37 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;}
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- 双向队列
- merge sort
- 代码整洁之道-函数
- 去掉使用百度编辑器生成的p标签及生成的实体符号
- scu 4440 Rectangle
- apache Commons-beanutils的使用
- 双向队列
- Redis持久化
- 让Eclipse 安装FindBugs插件
- POJ----3061
- VS2015自定义编程背景
- HDU 1078 FatMouse and Cheese(dp+记忆化搜索)史上最详细题解
- sleep()和wait()方法的区别
- Android中AutoCompleteTextView控件的使用
- 6个变态的C语言写的Hello World【转】