数据结构实验之栈与队列十一:refresh的停车场
Problem Descriptionrefresh最近发了一笔横财开了一家停车场。由于土地有限停车场内停车数量有限但是要求进停车场的车辆过多。当停车场满时要进入的车辆会进入便道等待最先进入便道的车辆会优先进入停车场而且停车场的结构要求只出去的车辆必须是停车场中最后进去的车辆。现告诉你停车场容量N以及命令数M以及一些命令Add num 表示车牌号为num的车辆要进入停车场或便道Del 表示停车场中出去了一辆车Out 表示便道最前面的车辆不再等待放弃进入停车场。假设便道内的车辆不超过1000000.Input输入为多组数据每组数据首先输入N和M0 200000接下来输入M条命令。Output输入结束后如果出现停车场内无车辆而出现Del或者便道内无车辆而出现Out,则输出Error,否则输出停车场内的车辆最后进入的最先输出无车辆不输出。Sample Input2 6 Add 18353364208 Add 18353365550 Add 18353365558 Add 18353365559 Del OutSample Output18353365558 18353364208HintSource#includestdio.h #includestdlib.h #includestring.h char a[200005][25] ; //栈 int top ; char b[1000001][25] ; //队列 int top1 ,front ; int main() { int n , m ; int i ; char o[6]; char num[25] ; while(~scanf(%d %d,n,m)){ int flag 1 ; top -1 ; front top1 -1 ; while(m--){ scanf(%s,o) ; if(strcmp(o,Add) 0){ scanf(%s,num) ; if(topn-1){ strcpy(a[top],num) ; } else { if(top1 front ){ front ; top1 ; } strcpy(b[top1],num) ; } } else if(strcmp(o,Del) 0){ if(top-1){ flag 0 ; } else { top-- ; if(top1!front){ strcpy(a[top],b[front]) ; } } } else if(strcmp(o,Out) 0){ if(front top1){ flag 0 ; } else { front ; } } } if(flag){ for(i top ; i0 ; i--){ printf(%s\n,a[i]) ; } } else printf(Error\n) ; } return 0 ; }代码2#includestdio.h #includestring.h #includestdlib.h //定义队列 typedef struct node { char num[100] ; struct node *next ; } NODE; //定义栈 char a[200000][100] ; int top ; int main() { int n, m; int i ; char ch[100] ; char str[100] ; NODE *rear, *front ; //定义队列的头结点和尾结点 int tag ; while(~scanf(%d %d,n,m)) { NODE *head (NODE*)malloc(sizeof(NODE)) ; NODE *p ; head - next NULL ; rear head ; front head ; tag 1 ; top -1 ; while(m--) { scanf(%s,ch) ; if(strcmp(ch,Add) 0 ) { scanf(%s,str) ; if(topn-1) { strcpy(a[top],str) ; } else { p (NODE*)malloc(sizeof(NODE)) ; strcpy(p-num,str) ; rear-next p ; rear p ; } } else if(strcmp(ch,Del) 0) { if(top-1) { tag 0 ; } else { top-- ; if(rear!front) { p front-next ; front-next p-next ; if(p-next NULL) { rear front ; } strcpy(a[top],p-num) ; free(p) ; } } } else { if(front rear) { tag 0 ; } else { p front - next ; front-next p-next ; if(p-next NULL) { rear front ; } free(p) ; } } } if(tag) { for(i top ; i 0 ; i--) { printf(%s\n,a[i]) ; } } else printf(Error\n) ; } return 0 ; }#includestdio.h #includestring.h #includestdlib.h //构建队列 typedef struct node { char num[100] ; struct node *next ; }NODE ; typedef struct { NODE *rear; NODE *front ; }Link ; //构建栈 char a[1000001][100] ; int top ; int main() { int n , m ; int i ; NODE *p ; Link l ; l.front (NODE*)malloc(sizeof(NODE)) ; l.rear l.front ; char num[100] ; char op[10] ; while(~scanf(%d %d,n,m)) { int tag 1 ; top -1 ; while(m--) { scanf(%s,op) ; if(strcmp(op,Add) 0) { scanf(%s,num) ; if(top n-1) { strcpy(a[top],num) ; } else { p (NODE *)malloc(sizeof(NODE)) ; strcpy(p-num,num) ; p-next NULL ; l.rear-next p ; l.rear p ; } } else if(strcmp(op,Del) 0) { if(top0) { top-- ; if(l.frontl.rear) { tag 0 ; } else { strcmp(a[top],l.front-next-num) ; p l.front-next ; l.front-next p ; if(p-next NULL) { l.front l.rear ; } free(p) ; } } } else { if(l.frontl.rear) { tag 0 ; } else { p l.front-next ; l.front-next p ; if(p-next NULL) { l.front l.rear ; } free(p) ; } } } if(tag){ for(itop ; i0 ;i--) { printf(%s\n,a[i]) ; } } else printf(Error\n) ; } return 0 ; }