堆栈及其应用.ppt
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
int key ; /* other fields */ } element ; element stack [ MAX_STACK_SIZE ] ; int top = 1 ;
Boolean IsEmpty ( stack ) ::=
top < 0 ;
Boolean IsFull ( stack ) ::=
操作数
操作符
1. 计算后缀表达式 62342 = 8
Get token: 6 ( operand )
Get token: 2 ( operand )
Get token: ( operator )
2
top
Get token: 3 ( operand )
2348
ttoopp
Get token: ( operator )
} /* end while-loop */ return pop ( &top ); /* return result */ }
top >= MAX_STACK_SIZE 1 ;
Stack Push ( stack, item ) ::=
void push ( int *top, element item ) { /* add an item to the global stack */
if ( *top >= MAX_STACK_SIZE 1 ) { stack_full ( ) ; /* print an error message and more ... */ return ;
precedence token; char symbol; /* original character of token */ int op1, op2; /* operands */ int n = 0; /* counter for the expression string */ int top = 1; token = get_token( &symbol, &n ); /* get one symbol and token type from expr [ n ] */
while ( token != eos ) { /* while it’s not the end of expr */ if ( token == operand ) push (&top, symbol ’0’); /* push the number into stack */ else { op2 = pop ( &top ); /* pop two operands */ op1 = pop ( &top ); switch ( token ) { /* perform operation and push result */ case plus: push ( &top, op1 + op2 ); break; case minus: push ( &top, op1 op2 ); break; case times: push ( &top, op1 op2 ); break; case divide: push ( &top, op1 / op2); break; case mod: push ( &top, op1 % op2 ); } /* end switch */ } /* end else */ token = get_token (&symbol, &n); /* get the next token */
mod, eos, operand } precedence; int stack [ MAX_STACK_SIZE ]; /* global stack */ char expr [ MAX_EXPR_SIZE ]; /* input string */
int eval ( void ) { /* evaluate a postfix expression*/
堆栈及其应用
堆栈
56
6
5
4
3
5
2
6
1
堆栈
堆栈是一个后进先出 (Last-In-First-Out ,LIFO) 的线性表 , 也就是说,插入和删除只在线性表的一端(即顶端)进行
Stack CreateS ( max_stack_size ) ::=
#define MAX_STACK_SIZE 100 /* maximum stack size */ typedef struct {
} /* end if */ stack [ ++*top ] = item ; }
Element Pop ( stack ) ::=
element pop ( int *top ) { /* return the top element from the stack */
if ( *top == 1 ) return stack_empty ( ) ; /* return an error key */
Get token: 4 ( operand )
6308
ttoopp
Get to6ken: 2( ope2rand )= 3 3 3 =0
Get token: ( operator )
top
Get token: ( oper4ator ) 2 P=op8: 8 0 8 = 8
程序实现
#define MAX_STACK_SIZE 100 /* maximum stack size */ #define MAX_EXPR_SIZE 100 /* max size of expression*/ typedef enum { lparen, rparen, plus, minus, times, divide,
return stack [ ( *top ) ] ; }
堆栈应用实例 表达式的计算
2346/2= ?
概念: 中缀表达式:An infix expression: a b c d e 前缀表达式:A prefix expression: a b c d e 后缀表达式:A postfix expression: a b c d e
Boolean IsEmpty ( stack ) ::=
top < 0 ;
Boolean IsFull ( stack ) ::=
操作数
操作符
1. 计算后缀表达式 62342 = 8
Get token: 6 ( operand )
Get token: 2 ( operand )
Get token: ( operator )
2
top
Get token: 3 ( operand )
2348
ttoopp
Get token: ( operator )
} /* end while-loop */ return pop ( &top ); /* return result */ }
top >= MAX_STACK_SIZE 1 ;
Stack Push ( stack, item ) ::=
void push ( int *top, element item ) { /* add an item to the global stack */
if ( *top >= MAX_STACK_SIZE 1 ) { stack_full ( ) ; /* print an error message and more ... */ return ;
precedence token; char symbol; /* original character of token */ int op1, op2; /* operands */ int n = 0; /* counter for the expression string */ int top = 1; token = get_token( &symbol, &n ); /* get one symbol and token type from expr [ n ] */
while ( token != eos ) { /* while it’s not the end of expr */ if ( token == operand ) push (&top, symbol ’0’); /* push the number into stack */ else { op2 = pop ( &top ); /* pop two operands */ op1 = pop ( &top ); switch ( token ) { /* perform operation and push result */ case plus: push ( &top, op1 + op2 ); break; case minus: push ( &top, op1 op2 ); break; case times: push ( &top, op1 op2 ); break; case divide: push ( &top, op1 / op2); break; case mod: push ( &top, op1 % op2 ); } /* end switch */ } /* end else */ token = get_token (&symbol, &n); /* get the next token */
mod, eos, operand } precedence; int stack [ MAX_STACK_SIZE ]; /* global stack */ char expr [ MAX_EXPR_SIZE ]; /* input string */
int eval ( void ) { /* evaluate a postfix expression*/
堆栈及其应用
堆栈
56
6
5
4
3
5
2
6
1
堆栈
堆栈是一个后进先出 (Last-In-First-Out ,LIFO) 的线性表 , 也就是说,插入和删除只在线性表的一端(即顶端)进行
Stack CreateS ( max_stack_size ) ::=
#define MAX_STACK_SIZE 100 /* maximum stack size */ typedef struct {
} /* end if */ stack [ ++*top ] = item ; }
Element Pop ( stack ) ::=
element pop ( int *top ) { /* return the top element from the stack */
if ( *top == 1 ) return stack_empty ( ) ; /* return an error key */
Get token: 4 ( operand )
6308
ttoopp
Get to6ken: 2( ope2rand )= 3 3 3 =0
Get token: ( operator )
top
Get token: ( oper4ator ) 2 P=op8: 8 0 8 = 8
程序实现
#define MAX_STACK_SIZE 100 /* maximum stack size */ #define MAX_EXPR_SIZE 100 /* max size of expression*/ typedef enum { lparen, rparen, plus, minus, times, divide,
return stack [ ( *top ) ] ; }
堆栈应用实例 表达式的计算
2346/2= ?
概念: 中缀表达式:An infix expression: a b c d e 前缀表达式:A prefix expression: a b c d e 后缀表达式:A postfix expression: a b c d e