02-线性结构3 Pop Sequence
2015-09-22 17:15
495 查看
#include <stdio.h> #include <stdlib.h> #define MaxSize 1001 typedef struct node{ int top; int cap; int data[MaxSize]; } Stack; Stack* CreateStack(); void Push(Stack *PtrS, int e); void Pop(Stack *PtrS); int CheckStack(int tmp[], int M, int N); int main(int argc, char const *argv[]) { int M, N, K;//M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). scanf("%d %d %d", &M, &N, &K); int *tmp = (int*)malloc(sizeof(int)*N); for( ; K > 0; K--){ for(int i = 0; i < N; i++){ scanf("%d", tmp+i); } if(CheckStack(tmp, M, N)) printf("YES\n"); else printf("NO\n"); } return 0; } Stack* CreateStack() { Stack *PtrS = (Stack*)malloc(sizeof(Stack)); PtrS->cap = MaxSize; PtrS->top = -1; return PtrS; } void Push(Stack *PtrS, int e) { if(PtrS->top == PtrS->cap - 1) return; PtrS->top++; PtrS->data[PtrS->top] = e; } void Pop(Stack *PtrS) { if(PtrS->top == -1) return; PtrS->top--; } int CheckStack(int tmp[], int M, int N) { Stack *ps = CreateStack(); Push(ps, 0);//让top指向data数组的第一个位置data[0] int idx = 0; int num = 1; while(idx < N){ while(ps->data[ps->top] < tmp[idx] && idx < N && ps->top < M) Push(ps, num++); if(ps->data[ps->top] == tmp[idx]){ Pop(ps); idx++; } else return 0; } return 1; }
相关文章推荐
- 系统管理数据库分析,Easyui树形操作
- easyui select验证必填项
- versions and build in xcode
- Android ROM定制——界面美化基础(framework-res、SystemUI修改)
- Caused by: java.lang.IllegalArgumentException: addChild: Child name 'localhost' is not unique
- UITableView判断reloadData结束(iOS)
- MMD_2c_FrequentItemsets
- 出现Field 'ssl_cipher' doesn't have a default value错误怎么解决???
- UIFont & 测量NSString大小
- NSFetchedResultController与UITableView
- iOS8 用UITableViewRowAction实现Cell自定义滑动操作
- Bluetooth SIG profiles
- UIActivityIndicatorView的详细使用
- Building System之编译前的准备工作
- Android设置android:clipChildren达到的特殊UI设计效果
- QMap的key的使用 value的使用
- Google Guice结合模式
- UILabel自适应高、宽
- 擦除UIImageView
- java基于AbstractQueuedSynchronizer实现资源共享锁,限制并发线程数目