02-线性结构4 Pop Sequence
这道题是2013年PAT春季考试真题,考察队堆栈的基本概念的掌握。在保证输入正确后,关键在于对"Pop"序列的判断,我用isPopOrder函数进行了判断,代码如下:
#include <stdio.h> #include <stdlib.h> #define MaxSize 1001 typedef struct SNode *Stack; struct SNode{ int Data[MaxSize]; int MaxCap; //Maximum capacity of this stack int Top; }; Stack CreateStack(int M) { Stack PtrS; PtrS = (Stack)malloc(sizeof(struct SNode)); PtrS->Top = -; PtrS->MaxCap = M; return PtrS; } /*堆栈基本操作此处略去*/ int isPopOrder(int *CheckOrder, int M, int N, int K) { int i, ci = ; Stack S; S = CreateStack(M); for(i = ; i < N + ; i++){ Push(i, S); //Push in the order of 1, 2, ..., N while(CheckOrder[ci] == GetTop(S)){ Pop(S); ci++; } } if(GetTop(S) == -) //堆栈为空 return ; else return ; } int main(int argc, char const *argv[]) { int M, N, K; //M is the maximum capacity of the stack; Push N numbers; K sequences to be checked int i, j; int CheckOrder[MaxSize], res[MaxSize]; scanf("%d %d %d", &M, &N, &K); for(i = ; i < K; i++){ for(j = ; j < N; j++){ scanf("%d", &CheckOrder[j]); } res[i] = isPopOrder(CheckOrder, M, N, K); } for(i = ; i < K; i++){ if(res[i]) printf("YES\n"); else printf("NO\n"); } return ; }
相关推荐
lickylin 2020-02-23
hanyujianke 2019-11-09
mingrixing 2019-06-28
lickylin 2019-06-28
freedomfanye 2012-04-28