Data Structure-stack and Data Structure
Note: Yan Weimin's "data structure" (C language version) takes notes and records them for later viewing.
As shown in, the base pointer and top pointer both point to the low stack. When the stack is pressed, the top pointer moves up until the stack is full and the top Pointer Points to the off-stack address, in this case, we need to allocate a new space.
# Include <stdio. h> # include <malloc. h> # include <stdlib. h> # define STACK_INIT_SIZE 100 // storage space initial allocation # define STACKINCREMENT 10 // storage space allocation increment 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 Sta Tus 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.