Analysis of outbound stack and inbound Stack

Source: Internet
Author: User

As a result of Data Structure jobs, I have a serious understanding of the inbound stack, but it is actually very well grasped. The following code is successfully debugged on vs08, which contains detailed procedures for Stack-to-stack operations. I use the stack data type as an example, because it seems that the volume type is not found on the internet... I simply modified the stack of the integer data type ~

# Include <stdlib. h> # include "stdio. H "# include <string. h> typedef char selemtype; // stack data element type # define stack_init_size 10 // initial allocation of stack storage space # define stackincrement 10 // incremental typestrudef CT {selemtype * base for Stack storage space allocation; // before and after construction, the base value is null selemtype * Top; // The stack top pointer int stacksize; // The currently allocated storage capacity (with sizeof (selemtype) unit)} stack; char initstack (stack & S) {S. base = (selemtype *) malloc (stack_init_size * sizeof (selemtype) ); // Use the malloc function to allocate space if (! S. base) return 0; // if the allocation fails, 0 s is returned. top = S. base; // set the stack top pointer to the stack bottom address S. stacksize = stack_init_size; // return 1;} Char stackempty (stack s) {If (S. base) {If (S. base = S. top) // If the stack is empty, 1 (true) is returned; otherwise, 0 (false) {return 1 ;}} return 0 ;} char push (stack & S, selemtype e) {If (S. base) {If (S. top-S.base)> = S. stacksize) // stack full, append storage space {S. base = (selemtype *) realloc (S. base, (S. stacksize + stackincrement) * sizeof (SEL Emtype); If (S. base = NULL) return 0; // If Space reallocation fails, 0 (false) is returned, indicating that the insertion is unsuccessful. top = S. base + S. stacksize; // set the new address s of the array. stacksize + = stackincrement; // Add new space to the allocated space} * s. top ++ = E; return 1;} return 0;} // * s. top ++ = E // after the top position of the stack ++, this operation S. the top stack space remains unchanged, so the S stack top Pointer Points to E. After the calculation, the stack space increases by 1 // * ++ S. top = E // before the top position of the stack ++, this operation S. the top stack is increased by 1, so the new stack is pointed to E. Char POP (stack & S, selemtype & E) {If (S. base) {If (S. top> S. base) // If the stack is not empty {e = * -- S. top; return 1 ;}}return 0 ;}char stacktraverse (stack & S) // output each element from the bottom of the stack to the top of the stack in sequence {int I; If (S. base) {printf ("\ n") sequentially outputs each element from the bottom of the stack to the top of the stack: \ n "); for (I = 0; I <S. top-S.base; I ++) {printf ("% C", S. base [I]); // print data elements in sequence} printf ("\ n"); return 1;} return 0;} void main () {stack s; char X, y; initstack (s); X = 'C'; y = 'K'; push (S, x); stacktr Averse (s); push (S, 'A'); stacktraverse (s); push (S, Y); stacktraverse (s); POP (S, X ); stacktraverse (s); push (S, 'T'); stacktraverse (s); push (S, x); stacktraverse (s); POP (S, X ); stacktraverse (s); push (S,'s '); stacktraverse (s); While (! Stackempty (s) {Pop (S, Y); stacktraverse (s); printf ("% C", Y);} printf ("% C \ n ", x );}

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.