堆栈模拟队列
来源:互联网 发布:zte中兴网络机顶盒密码 编辑:程序博客网 时间:2024/05/01 12:20
点击打开链接
#include<cstdio> #include<algorithm> #include<stack> #include<cstring>using namespace std; //typedef struct SNode{ // int num; // struct SNode * next; //}*STACK; //STACK create() //{ // STACK shead = (STACK)malloc(sizeof(struct SNode)); // shead->next = NULL; // return shead; //} // //void push(STACK S,int number) //{ // STACK snew = (STACK)malloc(sizeof(struct SNode)); // snew->num = number; // snew->next = S->next; // S->next = snew; //} //// //int pop(STACK S) //{ // STACK p; // int number; // p = S->next; // number = p->num; // S->next = p->next; // free(p); // return number; //} int main(void) { //STACK s1 = create(); // STACK s2 = create(); stack<int> s1; stack<int> s2; int n1,n2; scanf("%d%d",&n2,&n1); int count1 = 0,count2 = 0;/*conut1代表容量小的栈中的个数,count2代表容量大的*/ while(1) { char ch; int num; scanf("%c",&ch); if(ch == 'A') { scanf("%d",&num); // getchar(); if(count1 < n1) { // push(s1,num); s1.push(num); count1++; } else { printf("ERROR:Full\n"); } } else if(ch == 'D') { if(!s2.empty()) { int item = s2.top(); s2.pop(); printf("%d\n",item); // printf("%d\n", pop(s2)); //count2--; } else { printf("ERROR:Empty\n"); } } else if(ch =='T') { break; } if(count1 == n1 && s2.empty())/*只要s1栈满,而s2空,就把s1全部转入s2*/ { while(!s1.empty()) { int j = s1.top(); s1.pop(); s2.push(j); // push(s2,pop(s1)); count1--; //此处注意count1要减 //count2++; } } } return 0; }
阅读全文
0 0
- 堆栈与队列模拟
- 堆栈模拟队列
- PAT堆栈模拟队列
- 堆栈模拟队列
- 用堆栈模拟队列,和用队列模拟堆栈
- 用堆栈模拟队列,和用队列模拟堆栈
- 两个队列模拟一个堆栈
- 使用堆栈来模拟队列
- 两个队列模拟一个堆栈
- 两个队列模拟一个堆栈
- 两个队列模拟一个堆栈
- PAT3-08. 堆栈模拟队列
- 两个队列模拟一个堆栈
- LinkedList模拟队列和堆栈
- 3-08. 堆栈模拟队列(25)
- PAT1024. 堆栈模拟队列(25)
- 3-08. 堆栈模拟队列(25)
- 5-22 堆栈模拟队列 (25分)
- 停课总结(三)
- codeforces——875B —— Sorting the Coins
- 分治算法-球队循环赛日程安排-java实现
- AndroidStudio调试smali代码
- mergeTwoSortList
- 堆栈模拟队列
- printListReverse
- opencv2.4.7.2画圆,画十字,画矩形
- elasticsearch 集群
- 好久没来了啊,都改版了
- EA&UML日拱一卒-状态图::延缓(处理)事件
- 第一行代码-Fragment碎片
- 笛卡尔树
- localStorage