题目描述
输入
输出
示例输入
2 6 Add 18353364208 Add 18353365550 Add 18353365558 Add 18353365559 Del Out
示例输出
1835336555818353364208
#include <stdio.h> #include <stdlib.h> #include <string.h> #define stackmax 10000 #define stacknum 11111 typedef long long int ElenType; typedef struct { ElenType *base; ElenType *top; int stacksize; }SqStack; int InitStack(SqStack &S)//栈的初始化; { S.base=(ElenType *)malloc(sizeof(ElenType)*stacknum); if(!S.base) exit(0); S.top=S.base; S.stacksize=stacknum; return 1; } void push(SqStack &S,ElenType &e)//进栈; { if(S.top-S.base>=S.stacksize) { S.base=(ElenType *)realloc(S.base,sizeof(ElenType)*(stacknum+stackmax)); if(!S.base) exit(0); S.top=S.base+S.stacksize; S.stacksize+=stackmax; } *S.top++=e; } int pop(SqStack &S)//出栈; { if(S.top==S.base) return 0; S.top--; return 1; } int StackEmpty(SqStack &S)//判断栈是否为空栈; { if(S.top==S.base) return 1; else return 0; } void print(SqStack &S)//栈的元素的输出; { while(!StackEmpty(S)) { S.top--; printf("%lld\n",*S.top); } } typedef long long int QElemType; typedef long long 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; } Status DeQueuel(LinkQueue &Q,QElemType &e)//出队; { Queueptr p; if(Q.front==Q.rear) return 0; p=Q.front->next; e=p->data; Q.front->next=p->next; if(Q.rear==p) Q.rear=Q.front; free(p); return 1; } int EmptyQueue(LinkQueue q)//判断是否为空队; { if(q.front==q.rear) return 1; else return 0; } QElemType Queuelength(LinkQueue q)//队的长度‘ { QElemType i=0; Queueptr p; p=q.front; while(p!=q.rear) { i++; p=p->next; } return i; } int main() { long int i,n,m; long long int num; char c[4]; while(~scanf("%ld%ld",&n,&m)) { SqStack S; InitStack(S); LinkQueue Q; InitQueue(Q); int flag=1; for(i=1; i<=m; i++) { scanf("%s", c); if(strcmp(c,"Add")==0) { scanf("%lld",&num); if(S.top-S.base<n)//判断栈停车位是否已满, push(S, num); else EnQueuer(Q,num); } if(strcmp(c,"Del")==0) { if(StackEmpty(S)) flag=0;//标记不合法的命令; else { pop(S); DeQueuel(Q,Q.front->data); push(S,Q.front->data); } } if(strcmp(c,"Out")==0) { if(EmptyQueue(Q)) flag=0;//标记不合法的命令; else { DeQueuel(Q,Q.front->data); } } } if(flag==0) printf("Error\n"); else print(S);//栈内元素的输出; } } #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #include <stack> using namespace std; int main() { int N,M,x,t; while(~scanf("%d %d",&N,&M)) { char minglin[10],num[100]; queue<string>qd;//队列的定义; stack<string>sz;//栈的定义; t=0; while(M--) { scanf("%s",minglin); if(minglin[0]=='A') { scanf("%s",num); x=sz.size();//栈的长度; if(x<N) sz.push(num);//进栈; else qd.push(num);//进队; } else if(minglin[0]=='D') { if(sz.empty())//空栈; t=1; else { sz.pop();//出栈; if(!qd.empty()) { sz.push(qd.front());//进栈; qd.pop();//出队; } } } else if(minglin[0]=='O') { if(qd.empty()) t=1; else qd.pop(); } } if(t==1) printf("Error\n"); else { while(!sz.empty()) { cout<<sz.top()<<endl; sz.pop(); } } } return 0; }