02-linear structure 4 Pop Sequence, 02-sequence

Source: Internet
Author: User

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 ;}
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.