Sequential stack Creation

Source: Internet
Author: User
1   /*  2   Create an ordered Stack  3   4   Levi.  5   */  6   7 # Include <stdio. h> 8   # Define Datatype char 9   # Define Stacksize 100 10  11   12 Typedef Struct  {  13   Datatype stack [stacksize];  14       Int  Top;  15   } Seqstack;  16   17   18   Void Initstack (seqstack * S ){ 19 S-> Top = 0  ;  20   }  21   22   Int  Stackempty (seqstack s ){  23       If (S. Top = 0  )  24                 Return   1  ; 25       Else  26                 Return   0  ;  27   }  28   29   Int Gettop (seqstack S, datatype * E ){  30       If (S. Top <= 0  ){  31 Printf ( "  Null! \ N  "  );  32                 Return   0  ;  33   }  34       Else  {  35 * E = S. Stack [S. Top- 1  ];  36          Return   1  ;  37   }  38   }  39   40   Int Pushstack (seqstack * S, datatype e ){  41       If (S-> top> = Stacksize ){  42 Printf ( " Fail! \ N  "  );  43           Return   0  ;  44   }  45       Else  {  46 S-> stack [S-> top] = E;  47 S-> top ++ ;  48          Return   1  ;  49   }  50   }  51   52   Int Popstack (seqstack * s, datatype * E ){  53       If (S-> top <= 0  ){  54 Printf ("  Fail! \ N  "  );  55           Return   0  ;  56   }  57       Else  {  58 S-> top -- ;  59 * E = s-> stack [S-> Top]; 60           Return   1  ;  61           62   }  63   }  64   65   Int  Stacklength (seqstack s ){  66       Return  S. Top;  67   } 68   69   Void Clearstack (seqstack * S ){  70 S-> Top = 0  ;  71   }  72   73   Void  Conversion (){  74   Seqstack S;  75  Unsigned N;  76       Int  E;  77 Initstack (& S );  78 Printf ( "  N converse % d Jinzhi, please input: n (> = 0) =  " , 2  );  79 Scanf ( "  % U " ,& N );  80       While  (N ){  81 Pushstack (& S, N % 2  );  82 N = N/ 2  ;  83   }  84   85       While (!Stackempty (s )){  86 Popstack (& S ,& E );  87 Printf ( "  % D  "  , E );  88   }  89 Printf ( "  \ N  "  );  90  }  91   92   Void  Print (seqstack s ){  93       Int   Base = 0  ;  94       While ( Base < S. Top ){  95 Printf ( " % C  " , S. Stack [ Base  ]);  96           Base ++ ;  97   }  98 }

 

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.