02-linear structure 4 Pop Sequence, 02-sequence
This question is the true question of the PAT Spring Test in 2013, and the basic concept of the inspection team stack is mastered. After the input is correct, the key is to judge the "Pop" sequence. I used the isPopOrder function to judge the sequence. The Code is as follows:
# 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 =-1; PtrS-> MaxCap = M;
return PtrS;}
/* The basic Stack operations are omitted here */
int isPopOrder (int * CheckOrder, int M, int N, int K) {
int I, ci = 0;
Stack S;
S = CreateStack (M);'
for (I = 1; I <N + 1; I ++) {
Push (I, S);
// Push in the order of 1, 2 ,..., N
while (CheckOrder [ci] = GetTop (S) {
Pop (S); ci ++ ;
}}
if (GetTop (S) =-1)
// The stack is empty.
return 1;
else
return 0;
}
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", & M, & N, & K);
for (I = 0; I <K; I ++) {
for (j = 0; j <N; j ++) {
scanf ("% d", & CheckOrder [j]);
}
res [I] = isPopOrder (CheckOrder, m, N, K) ;
}
for (I = 0; I <K; I ++) {
if (res [I])
printf ("YES \ n ");
else printf ("NO \ n");}
return 0 ;}