栈数据结构的应用:中序表达式后续表达式和前序表达式的计算,中序表达式转换为后续表达式,做回溯控制
1中序表达式求值
1 //计算中序四则表达式的值,输入的表达式内的每一个字符 2 //代表一个操作数或运算符,而且中间不可有空格,换句话 3 //说,操作数的范围只有数字0到9 4 #include <stdlib.h> 5 #include <stdio.h> 6 7 struct stack_node 8 { 9 int data; 10 struct stack_node * next; 11 }; 12 13 typedef struct stack_node stack_list; 14 typedef stack_list * link; 15 16 link operator = NULL; 17 link operand = NULL; 18 19 link push(link stack,int value) 20 { 21 link new_node; 22 23 new_node = (link)malloc(sizeof(stack_list)); 24 if(!new_node) 25 { 26 printf("内存分配失败! \n"); 27 return NULL; 28 } 29 new_node->data = value; 30 new_node->next = stack; 31 stack = new_node; 32 return stack; 33 } 34 35 36 link pop(link stack,int *value) 37 { 38 link top; 39 40 if(stack != NULL) 41 { 42 top = stack; 43 stack = stack->next; 44 *value = top->data; 45 free(top); 46 return stack; 47 } 48 else 49 *value = -1; 50 } 51 52 53 int empty(link stack) 54 { 55 if(stack == NULL) 56 return 1; 57 else 58 return 0; 59 } 60 61 62 int isoperator(char op) 63 { 64 switch(op) 65 { 66 case '+': 67 case '-': 68 case '*': 69 case '/': return 1; 70 default: return 0; 71 } 72 } 73 74 75 int priority(char op) 76 { 77 switch(op) 78 { 79 case '*': 80 case '/': return 2; 81 case '+': 82 case '-': return 1; 83 default: return 0; 84 } 85 } 86 87 88 int get_value(int op,int operand1,int operand2) 89 { 90 switch((char)op) 91 { 92 case '*': return (operand2 * operand1); 93 case '/': return (operand2 / operand1); 94 case '+': return (operand2 + operand1); 95 case '-': return (operand2 - operand1); 96 } 97 } 98 99 100 int main() 101 { 102 char exp[100]; 103 int op = 0; 104 int operand1 = 0; 105 int operand2 = 0; 106 int result = 0; 107 int pos = 0; 108 109 printf("请输入中序表达式 ==> "); 110 gets(exp); 111 112 while(exp[pos] != '\0' && exp[pos] != '\n') 113 { 114 if(isoperator(exp[pos])) 115 { 116 if(!empty(operator)) 117 while(!empty(operator) && priority(exp[pos]) <= priority(operator->data)) 118 { 119 operator = pop(operator,&op); 120 operand = pop(operand,&operand1); 121 operand = pop(operand,&operand2); 122 123 operand = push(operand,get_value(op,operand1,operand2)); 124 } 125 operator = push(operator,exp[pos]); 126 } 127 else 128 operand = push(operand,exp[pos] - 48); 129 pos++; 130 } 131 132 while(!empty(operator)) 133 { 134 operator = pop(operator,&op); 135 operand = pop(operand,&operand1); 136 operand = pop(operand,&operand2); 137 138 operand = push(operand,get_value(op,operand1,operand2)); 139 } 140 operand = pop(operand,&result); 141 printf("表达式[%s]的结果是 %d\n",exp,result); 142 }

前序表达式求职
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 struct stack_node 5 { 6 int data; 7 struct stack_node * next; 8 }; 9 10 typedef struct stack_node stack_list; 11 typedef stack_list * link; 12 13 link prefix = NULL; //表达式栈指针 14 link operand = NULL; 15 16 link push(link stack,int value) 17 { 18 link new_node; 19 20 new_node = (link)malloc(sizeof(stack_list)); 21 if(!new_node) 22 { 23 printf("内存分配失败! \n"); 24 return NULL; 25 } 26 new_node->data = value; 27 new_node->next = stack; 28 stack = new_node; 29 return stack; 30 } 31 32 33 link pop(link stack,int *value) 34 { 35 link top; 36 37 if(stack != NULL) 38 { 39 top = stack; 40 stack = stack->next; 41 *value = top->data; 42 free(top); 43 return stack; 44 } 45 else 46 *value = -1; 47 } 48 49 50 int empty(link stack) 51 { 52 if(stack == NULL) 53 return 1; 54 else 55 return 0; 56 } 57 58 59 int isoperator(char op) 60 { 61 switch(op) 62 { 63 case '+': 64 case '-': 65 case '*': 66 case '/': return 1; 67 default: return 0; 68 } 69 } 70 71 72 int get_value(int op,int operand1,int operand2) 73 { 74 switch((char)op) 75 { 76 case '*': return (operand2 * operand1); 77 case '/': return (operand2 / operand1); 78 case '+': return (operand2 + operand1); 79 case '-': return (operand2 - operand1); 80 } 81 } 82 83 84 int main() 85 { 86 char exp[100]; 87 int operand1 = 0; 88 int operand2 = 0; 89 int result = 0; 90 int pos = 0; 91 int token = 0; 92 93 printf("请输入前序表达式 ==> "); 94 gets(exp); 95 printf("前序表达式[%s]的结果是 ",exp); 96 97 while(exp[pos] != '\0' && exp[pos] != '\n') 98 { 99 prefix = push(prefix,exp[pos]); 100 pos++; 101 } 102 103 while(!empty(prefix)) 104 { 105 prefix = pop(prefix,&token); 106 if(isoperator(token)) 107 { 108 operand = pop(operand,&operand1); 109 operand = pop(operand,&operand2); 110 operand = push(operand,get_value(token,operand1,operand2)); 111 } 112 else 113 operand = push(operand,token-48); 114 115 } 116 operand = pop(operand,&result); 117 printf(" %d\n",result); 118 119 }

后序表达式求值
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 struct stack_node 5 { 6 int data; 7 struct stack_node * next; 8 }; 9 10 typedef struct stack_node stack_list; 11 typedef stack_list * link; 12 13 link operand = NULL; 14 15 link push(link stack,int value) 16 { 17 link new_node; 18 19 new_node = (link)malloc(sizeof(stack_list)); 20 if(!new_node) 21 { 22 printf("内存分配失败! \n"); 23 return NULL; 24 } 25 new_node->data = value; 26 new_node->next = stack; 27 stack = new_node; 28 return stack; 29 } 30 31 32 link pop(link stack,int *value) 33 { 34 link top; 35 36 if(stack != NULL) 37 { 38 top = stack; 39 stack = stack->next; 40 *value = top->data; 41 free(top); 42 return stack; 43 } 44 else 45 *value = -1; 46 } 47 48 49 int empty(link stack) 50 { 51 if(stack == NULL) 52 return 1; 53 else 54 return 0; 55 } 56 57 58 int isoperator(char op) 59 { 60 switch(op) 61 { 62 case '+': 63 case '-': 64 case '*': 65 case '/': return 1; 66 default: return 0; 67 } 68 } 69 70 71 int get_value(int op,int operand1,int operand2) 72 { 73 switch((char)op) 74 { 75 case '*': return (operand2 * operand1); 76 case '/': return (operand2 / operand1); 77 case '+': return (operand2 + operand1); 78 case '-': return (operand2 - operand1); 79 } 80 } 81 82 83 int main() 84 { 85 char exp[100]; 86 int operand1 = 0; 87 int operand2 = 0; 88 int result = 0; 89 int pos = 0; 90 91 printf("请输入后序表达式 ==> "); 92 gets(exp); 93 printf("后序表达式[%s]的结果是 ",exp); 94 95 while(exp[pos] != '\0' && exp[pos] != '\n') 96 { 97 if(isoperator(exp[pos])) 98 { 99 operand = pop(operand,&operand1); 100 operand = pop(operand,&operand2); 101 operand = push(operand,get_value(exp[pos],operand1,operand2)); 102 } 103 else 104 operand = push(operand,exp[pos]-48); 105 pos++; 106 } 107 108 operand = pop(operand,&result); 109 printf(" %d\n",result); 110 111 }

中序表达式转换为后序表达式
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 struct stack_node 5 { 6 int data; 7 struct stack_node * next; 8 }; 9 10 typedef struct stack_node stack_list; 11 typedef stack_list * link; 12 13 link operator = NULL; 14 15 link push(link stack,int value) 16 { 17 link new_node; 18 19 new_node = (link)malloc(sizeof(stack_list)); 20 if(!new_node) 21 { 22 printf("内存分配失败! \n"); 23 return NULL; 24 } 25 new_node->data = value; 26 new_node->next = stack; 27 stack = new_node; 28 return stack; 29 } 30 31 32 link pop(link stack,int *value) 33 { 34 link top; 35 36 if(stack != NULL) 37 { 38 top = stack; 39 stack = stack->next; 40 *value = top->data; 41 free(top); 42 return stack; 43 } 44 else 45 *value = -1; 46 } 47 48 49 int empty(link stack) 50 { 51 if(stack == NULL) 52 return 1; 53 else 54 return 0; 55 } 56 57 58 int isoperator(char op) 59 { 60 switch(op) 61 { 62 case '(': 63 case ')': 64 case '+': 65 case '-': 66 case '*': 67 case '/': return 1; 68 default: return 0; 69 } 70 } 71 72 int priority(char op) 73 { 74 switch(op) 75 { 76 case '*': 77 case '/': return 3; 78 case '+': 79 case '-': return 2; 80 case '(': return 1; 81 default: return 0; 82 } 83 } 84 85 int main() 86 { 87 char infix[100]; 88 char result[100]; 89 int op = 0; 90 int pos = 0; 91 int rpos = 0; 92 93 printf("请输入中序表达式 ==> "); 94 gets(infix); 95 96 97 while(infix[pos] != '\0' && infix[pos] != '\n') 98 { 99 if(isoperator(infix[pos])) 100 { 101 if(empty(operator) || infix[pos] == '(') 102 operator = push(operator,infix[pos]); 103 else 104 { 105 if(infix[pos] == ')') 106 { 107 while(operator->data != '(') 108 { 109 operator = pop(operator,&op); 110 result[rpos++] = op; 111 } 112 operator = pop(operator,&op); 113 } 114 else 115 { 116 while(priority(infix[pos]) <= 117 priority(operator->data) && 118 !empty(operator)) 119 { 120 operator = pop(operator,&op); 121 result[rpos++] = op; 122 } 123 operator = push(operator,infix[pos]); 124 } 125 } 126 } 127 else 128 result[rpos++] = infix[pos]; 129 pos++; 130 } 131 while(!empty(operator)) 132 { 133 operator = pop(operator,&op); 134 result[rpos++] = op; 135 } 136 137 result[rpos] = '\0'; 138 printf("后序表达式是 %s \n",result); 139 }

用栈做回溯控制
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct stack_node 5 { 6 int x; 7 int y; 8 struct stack_node * next; 9 }; 10 11 typedef struct stack_node stack_list; 12 typedef stack_list * link; 13 14 link path = NULL; 15 16 link push(link stack,int x,int y) 17 { 18 link new_node; 19 20 new_node = (link)malloc(sizeof(stack_list)); 21 if(!new_node) 22 { 23 printf("内存分配失败! \n"); 24 return NULL; 25 } 26 new_node->x = x; 27 new_node->y = y; 28 new_node->next = stack; 29 stack = new_node; 30 return stack; 31 } 32 33 link pop(link stack,int *x,int *y) 34 { 35 link top; 36 37 if(stack != NULL) 38 { 39 top = stack; 40 stack = stack->next; 41 *x = stack->x; 42 *y = stack->y; 43 free(top); 44 return stack; 45 } 46 else 47 *x = -1; 48 } 49 50 51 int main() 52 { 53 int maze[7][10]={ 54 1,1,1,1,1,1,1,1,1,1, 55 1,0,1,0,1,0,0,0,0,1, 56 1,0,1,0,1,0,1,1,0,1, 57 1,0,1,0,1,1,1,0,0,1, 58 1,0,1,0,0,0,0,0,1,1, 59 1,0,0,0,1,1,1,0,0,1, 60 1,1,1,1,1,1,1,1,1,1, 61 }; 62 63 int i,j; 64 int x = 5; 65 int y = 8; 66 67 while(x != 1 || y!= 1) 68 { 69 maze[x][y] = 2; 70 if(maze[x-1][y] <= 0) 71 { 72 x = x -1; 73 path = push(path,x,y); 74 } 75 else 76 if(maze[x+1][y] <= 0) 77 { 78 x = x + 1; 79 path = push(path,x,y); 80 } 81 else 82 if(maze[x][y-1] <= 0) 83 { 84 y = y - 1; 85 path = push(path,x,y); 86 } 87 else 88 if(maze[x][y] <= 0) 89 { 90 y = y + 1; 91 path = push(path,x,y); 92 } 93 else 94 { 95 maze[x][y] = 3; 96 path = pop(path,&x,&y); 97 } 98 } 99 maze[y][y] = 2; 100 printf("迷宫的路径如下图所示:\n"); 101 for(i = 1;i < 6;i++) 102 { 103 for(j = 1;j < 9;j++) 104 printf("%d",maze[i][j]); 105 printf("\n"); 106 } 107 108 return 0; 109 }


浙公网安备 33010602011771号