Stack of DSAA (2)

1. Simple stack implementation

#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#define MAXSTACKSIZE 20
#define  handle_error(msg) do{ perror(msg); exit(-1);}while(0);
typedef  int TYPE;

typedef struct stack{
   int stacksize;
   int topofstack;
   TYPE  * arrary;
} STACK;

void destroy_stack(STACK * stack);
TYPE pop(STACK * stack);
int push(TYPE value,STACK * stack);
STACK * creat_stack(int stacksize);

int main(void){
   STACK * ptr;
   ptr=creat_stack(MAXSTACKSIZE);
   TYPE num;
   printf("push :\n");
   scanf("%d",&num);
   if(push(num,ptr) == -1){
        errx(1,"stack full\n");
   }
   printf("pop %d\n",pop(ptr));
   destroy_stack(ptr);
}

STACK * creat_stack(int stacksize){
   STACK * ptr;
   if((ptr=malloc(sizeof(STACK))) == NULL)
      handle_error("malloc");
   ptr->stacksize=stacksize;
   ptr->topofstack=-1;
   if((ptr->arrary=malloc(stacksize*sizeof(TYPE))) == NULL)
      handle_error("malloc");
   return ptr;
}

int push(TYPE value,STACK * stack){
   STACK * ptr;
   if(stack->topofstack== stack->stacksize)
      return -1;
   (stack->arrary)[++stack->topofstack]=value;
   return 0;
}


TYPE pop(STACK * stack){
   STACK * ptr;
   if(stack->topofstack== -1)
      return -1;
   return (stack->arrary)[stack->topofstack--];
}

void destroy_stack(STACK * stack){
   if(stack != NULL){
      free(stack->arrary);
      free(stack);
   }
}

  result:

[root@localhost ~]# ./3_3    
push :
5
pop 5
[root@localhost ~]# 

2. Prefix and postfix expressions

  Specifically, it should be divided into two steps. First, the infix expression, programming prefix or suffix expression, and then calculate the corresponding result according to the prefix and suffix expressions.
  The conversion algorithm for infix-to-prefix expressions is :

  • First construct an operator stack (parentheses can also be placed), and operators (with parentheses as the demarcation point) are arranged in the stack according to the principle that the priority does not decrease as it goes to the top of the stack.大于或者等于
  • Scan infix expressions from right to left , starting from the first character on the right:
    • If the current character is a number, it will parse to the end of the number string and output the number string directly.
    • If it is an operator, it compares the precedence. If the priority of the current operator is greater than or equal to the priority of the operator on the top of the stack (when the top of the stack is a parenthesis, it is directly pushed onto the stack), the operator will be pushed directly onto the stack; otherwise, the operator on the top of the stack will be popped off the stack and output until the current The priority of the operator is greater than or equal to the priority of the operator on the top of the stack (when the top of the stack is a parenthesis, it is directly pushed onto the stack), and then the current operator is pushed onto the stack.
      If it is a parenthesis, it is processed according to the direction of the parenthesis. If it is a right parenthesis, it will be directly pushed to the stack; otherwise, all operators will be popped off the stack and output before the opening parenthesis is encountered, and the left and right parentheses will be deleted together after the right parenthesis is encountered.
  • Repeat the above operation (2) until the end of the scan, pop all the remaining operators in the stack and output, and then invert the output string. Infix expressions are also converted to prefix expressions.

  Prefix expression calculation method:

  • Evaluates a prefix expression, scanning the expression from right to left.
  • First, judge from the first character on the right. If the current character is a number, it will be recorded until the end of the number string. If it is an operator, the two "number strings" that are closest to the right will be operated accordingly. This is recorded as a new "string of numbers";
  • When the scan reaches the leftmost end of the expression, the scan ends, and the value of the last operation is the value of the expression.
      

The conversion method of  infix-to-suffix expressions is as follows:

  • Start scanning ( from left to right ), add suffix expressions when numbers are present;
  • Encountered operator operator:
    • If it is '(', push into the stack;
    • If it is ')', add the operators in the stack to the suffix expression in turn, until '(' appears, delete '(' from the stack;
    • If it is an operator other than parentheses, when its priority is higher than the top-of-the-stack operator except '(', it is directly pushed to the stack. Otherwise, starting from the top of the stack, pop-up operators with higher priority than the currently processed operator and Operators of equal precedence until a lower precedence or an opening parenthesis is encountered.
  • When the scanned infix expression ends, all operators in the stack are popped;

  The calculation method of the postfix expression:

  • Build a stack S. Read the expression from left to right . If the operand is read, it will be pushed into the stack S. If the n-ary operator (that is, the operator that requires the number of parameters is n) is read, it will be taken from the top of the stack. Items are operated according to the operands, and the result of the operation is pushed into the stack S instead of the n items at the top of the original stack. If the suffix expression has not been read, the above process is repeated, and the value at the top of the stack is finally output.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324589870&siteId=291194637