STACK: a linear table that can be inserted or deleted only at one end. Inserting an element into the stack is called the stack, and deleting the data element from the stack is called the stack.
Stack storage includes sequential storage and chained storage. Sequential storage requires the stack memory space to be allocated in advance. Here we only talk about chained storage.
In international practice, we should first go to the source code and talk about each module.
1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <malloc. h> 4 5 // stack definition 6 typedef struct node {7 int data; 8 struct node * Next; 9} stack; 10 11 // stack initialization 12 Void stackinit (stack * Top) 13 {14 // set the stack top pointer to null 15 top-> next = NULL; 16} 17 18 // check whether the stack is empty: If it is null, 1 is returned. If it is not null, 019 int isempty (stack * Top) 20 {21 // whether the stack top is empty, stack top blank: Stack blank 22 if (top-> next = NULL) 23 {24 return 1; 25} 26 return 0; 27} 28 29 // Stack: push element into stack. 1 is returned successfully, and 030 int push (stack * Top, int element) 31 {32 // create a node and store element element33 stack * P; 34 p = (stack *) malloc (sizeof (stack); 35 if (P = NULL) 36 {37 return 0; 38} 39 p-> DATA = element; 40 // the top of the stack remains unchanged, and new nodes are placed under the top of the stack. that is, the top pointer of the stack moves 41 P-> next = Top-> next; 42 top-> next = P; 43 return 1; 44} 45 46 // out of the stack: the data element of the stack is sent to element47 int POP (stack * Top, int * element) 48 {49 If (isempty (top) 50 {51 return 0; 52} 53 stack * P; 54 p = Top-> next; // The first node at the top of the stack, that is, 55 * element = p-> data; 56 top-> next = p-> next; // stack top forward 57 free (p); // release memory space 58 return 1; 59} 60 61 int main () 62 {63 int X; 64 stack * Top = (stack *) malloc (sizeof (stack); // stack top 65 stackinit (top ); // stack initialization 66 printf ("input some positive integers: \ n"); 67 scanf ("% d", & X); 68 while (x> 0) 69 {70 push (top, x); // stack 71 scanf ("% d", & X); 72} 73 stack * P = top; 74 // elements in the output stack (starting from the top of the stack) 75 printf ("elemet in stack: \ n"); 76 while (P) 77 {78 printf ("% d", p-> data); 79 p = p-> next; 80} 81 p = NULL; 82 printf ("\ n "); 83 // output stack element 84 printf ("Pop stack element: \ n"); 85 while (POP (top, & X )) 86 {87 printf ("% d", x); 88} 89 printf ("\ n"); 90 // note: the memory space in the stack has been released 91 free (top); 92 93 return 0; 94}
Stack is a single-chain table, but different from the single-chain table, stack operations only require the top of the stack, that is, no access to other positions in the stack.
1. Stack initialization: Set the top pointer of the stack to null.
2. Check whether the stack is empty: Check whether the top pointer of the stack is empty, instead of checking whether the top pointer of the stack is empty.
3. Stack import: How do I understand the top pointer of the stack and move it back when it is pushed to the stack? That is, the top of the stack remains unchanged, and the nodes to be added to the stack are placed at the bottom of the stack. That is, the top pointer of the stack only needs to be placed at the top of the stack each time it is pushed to the stack.
4. Out Stack: The top pointer of the stack moves forward. Delete the node at the top of the stack, and delete the node at the top of the stack every time the stack is released.
Note: When the stack is released, the memory space of the stack should be released. After the stack is cleared, the memory space at the top of the stack should also be released.