Note: Yan Weimin's "data structure" (C language version) takes notes and records them for later viewing. As shown in, what are the base pointer and tZ pointer at the beginning? Www.2cto. Large
Note: Yan Weimin's "data structure" (C language version) takes notes and records them for later viewing. As shown in, what are the base pointer and tZ pointer at the beginning? Http://www.2cto.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcNa41eu2vNa4z/lvu7xno6y1sdg51bu1xmqxuvkjrhrvcna41evp8snp0sa2r6os1rg1.
Note: Yan Weimin's "data structure" (C language version) takes notes and records them for later viewing.
As shown in, what are the base pointer and tZ pointer at the beginning? Http://www.2cto.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcNa41eu2vNa4z/placement =" brush: SQL; "> # include # Include # Include # Define STACK_INIT_SIZE 100 // initial storage space allocation # define STACKINCREMENT 10 // incremental storage space allocation const int OK = 1; // The correct definition returns const int ERROR =-1; // return const int OVERFLOW =-2 if an error is defined; // define OVERFLOW // define the element type typedef int SElemType; // define the return type typedef int Status; typedef struct {SElemType * base; // stack bottom pointer. The base value before and after construction is NULL SElemType * top; // stack top pointer int stacksize; // allocated space} SqStack; // initialize the stack Status InitStack (SqStack & S) {S. base = (SElemType *) malloc (STACK_INIT_SIZE * sizeof (SElemType); if (! S. base) exit (OVERFLOW); S. top = S. base; S. stacksize = STACK_INIT_SIZE; return OK;} // gets the top element of the stack Status GetTop (SqStack S, SElemType & e) {if (S. top = S. base) return ERROR; e = * (S. top-1); return OK;} // Push (SqStack & S, SElemType e) {if (S. top-S. base> = S. stacksize) {S. base = (SElemType *) realloc (S. base, (S. stacksize + STACKINCREMENT) * sizeof (SElemType); if (! S. base) exit (OVERFLOW); S. top = S. base + S. stacksize; S. stacksize + = STACKINCREMENT;} * S. top = e; S. top ++; return OK;} // output stack Status Pop (SqStack & S, SElemType & e) {if (S. top = S. base) return ERROR; e = * (-- S. top); return OK;} // determines whether the stack is empty. bool StackEmpty (const SqStack & S) {if (S. top = S. base) return true; else return false;} // convert the decimal number to the octal number void conversion (SqStack & S) {InitStack (S ); printf ("Enter the number in decimal format and return an octal number: \ n" ); Int n; scanf ("% d", & n); while (n) {Push (S, n % 8); n = n/8;} SElemType e; printf ("8-digit number: 0x"); while (! StackEmpty (S) {Pop (S, e); printf ("% d", e);} printf ("\ n");} int main () {SqStack sq; // InitStack (sq); // Push (sq, 1); // Push (sq, 2); // Push (sq, 3 ); // SElemType e3; // Pop (sq, e3); // GetTop (sq, e3); // printf ("% d", e3); conversion (sq ); scanf ("% d"); return 0 ;}
The above conversion function is an example of converting a 10-to-8 system. This is an application of the stack, such as the verification of matching brackets and the solution to the maze.
For example, Hanoi tower problems:
Assume that there are three towers a, B, and c respectively, and a has a diameter from a to a small disc. You can use the B Tower to move the disc on a to c, the order in the movement process remains unchanged.
Void movePic (char a, int n, char B) {printf ("Move the disc numbered % d from % c to % c \ n", n,, b);} void hanuota (int n, char x, char y, char z) {if (n = 1) {movePic (x, 1, z ); // move the disc numbered 1 from x to z} else {hanuota (n-1, x, z, y ); // move the disc numbers 1 to n-1 on x to y, and z serves as the auxiliary tower movePic (x, n, z ); // move the disc numbered n from x to z hanuota (n-1, y, x, z); // move the disc numbered 1 to n-1 on y to z, x auxiliary tower }}int main () {hanuota (3, 'A', 'B', 'C ');}We can simply abstract the problem into recursion.
1. To move n disks to c, first move n-1 disks to B
2. Move the bottom disc on a to c.
3. Finally, N-1 1 disc on B to c.
After these three steps, the movement can be completed. In these three steps, step 1, moving n-1 disks from a to B is the same problem as the problem itself, step 3 moving n-1 disks from B to c is also the same as the problem itself, so we can call these two disks iteratively.