1、 数据结构教程 第十七课 实验三:栈的表示与实现及栈的应用数据结构教程 第十七课 实验三:栈的表示与实现及栈的应用教学目的: 掌握栈的存储表示方式和栈基本操作的实现方法教学重点: 栈的基本操作实现方法,栈的应用教学难点: 栈的存储表示实验内容:一、栈的实现实现栈的顺序存储。栈实现示例#include#include#include#define ERROR 0#define TRUE 1#define FALSE 0#define OK 1#define EQUAL 1#define OVERFLOW -1#define STACK_INIT_SIZE 100#define STACKINCR
2、EMENT 10typedef int Status ;struct STUchar name20;char stuno10;int age;int score;typedef struct STU SElemType;struct STACKSElemType *base;SElemType *top;int stacksize;typedef struct STACK SqStack;typedef struct STACK *pSqstack;Status InitStack(SqStack *S);Status DestroyStack(SqStack *S);Status Clear
3、Stack(SqStack *S);Status StackEmpty(SqStack S);int StackLength(SqStack S);Status GetTop(SqStack S,SElemType *e);Status Push(SqStack *S,SElemType e);Status Pop(SqStack *S,SElemType *e);Status StackTraverse(SqStack S,Status (*visit)();Status InitStack(SqStack *S)(*S)=(SqStack *) malloc(sizeof(SqStack)
4、;(*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;Status DestroyStack(SqStack *S)free(S-base);free(S);Status ClearStack(SqStack *S)S-top=S-base;Status StackEmpty(SqStack S)if(S.top=S.base) return
5、 TRUE;elsereturn FALSE;int StackLength(SqStack S)int i;SElemType *p;i=0;p=S.top;while(p!=S.base)p+;i+;Status GetTop(SqStack S,SElemType *e)if(S.top=S.base) return ERROR;*e=*(S.top-1);return OK;Status Push(SqStack *S,SElemType e)/*if(S-top - S-base=S-stacksize)S-base=(SElemType *) realloc(S-base,(S-s
6、tacksize + STACKINCREMENT) * sizeof(SElemType);if(!S-base)exit(OVERFLOW);S-top=S-base+S-stacksize;S-stacksize += STACKINCREMENT;*/*(S-top+)=e;return OK;Status Pop(SqStack *S,SElemType *e)if(S-top=S-base) return ERROR;*e=*-S-top;return OK;Status StackPrintElem(SElemType * e)printf(“%s %s %d %dn“,e-na
7、me,e-stuno,e-age,e-score);Status StackTraverse(SqStack S,Status (*visit)()while(S.top!=S.base)visit(-S.top);main()SElemType e;SqStack *Sa;clrscr();printf(“nn-SqStack Demo is running.-nn“);printf(“First is Push function.n“);InitStack(strcpy(e.name,“stu1“);strcpy(e.stuno,“100001“);e.age=80;e.score=100
8、0;printf(“ Now Stack is Empty.n“);StackTraverse(*Sa,StackPrintElem);Push(Sa,e);printf(“ Now Stack has one element.n“);StackTraverse(*Sa,StackPrintElem);strcpy(e.name,“stu3“);strcpy(e.stuno,“100002“);e.age=80;e.score=1000;Push(Sa,e);printf(“ Now Stack has another element.n“);StackTraverse(*Sa,StackPr
9、intElem);printf(“ Now Pop Stack,the top elem put into variable e.n“);Pop(Sa,printf(“%sn%sn%dn%dn“,e.name,e.stuno,e.age,e.score);printf(“ Lets see the left of Stacks elem:n“);StackTraverse(*Sa,StackPrintElem);getch();printf(“nnnWelcom to visit http:/nn“);二、栈的应用、利用栈实现数制转换 、利用栈实现单行编辑以上任选一题。数制转换示例#inclu
10、de#include#includetypedef int SElemType;#include “stack.h“Status visit(SElemType * e)printf(“ %d “, *e);void conversion()pSqStack S;SElemType e;int n;InitStack(printf(“Input a number to convert to OCT:n“);scanf(“%d“,if(n#include#include#include#define EOFILE typedef char SElemType;#include “stack.h“
11、Status visit(SElemType * e)printf(“%c“, *e);char OP10=+,-,*,/,(,),#;int precede77=1,1,2,2,2,1,1,1,1,2,2,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,2,2,2,2,2,3,0,1,1,1,1,0,1,1,2,2,2,2,2,0,3;int In(char c,char *op)int i=0;while(i;case 2: return :Pop(OPTR,Pop(OPND, Pop(OPND,Push(OPND,Operate(a,theta,b);break;c=
12、GetTop(*OPND);DestroyStack(OPTR);DestroyStack(OPND);return c;main()char i;printf(“nnnnOnly within 09 evaluation,input a expression end with symbol #:n“);i=EvaluateExpression();printf(“nThis expressions result is: “);printf(“%dnnnn“,i-0);printf(“nnWelcome to visit http:/ !“);这里是实现栈的头文件#include “simuc
13、.h“#define OVERFLOW -1#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef int Status;struct STACKSElemType *base;SElemType *top;int stacksize;typedef struct STACK SqStack;typedef struct STACK *pSqStack;Status InitStack(SqStack *S);Status DestroyStack(SqStack *S);Status ClearStack(SqStack *S)
14、;Status StackEmpty(SqStack S);int StackLength(SqStack S);SElemType GetTop(SqStack S);Status Push(SqStack *S,SElemType e);Status Pop(SqStack *S,SElemType *e);Status StackTraverse(SqStack S,Status (*visit)();Status InitStack(SqStack *S)(*S)=(SqStack *)malloc(sizeof(SqStack);(*S)-base=(SElemType *)mall
15、oc(STACK_INIT_SIZE *sizeof(SElemType);if(!(*S)-base)exit(OVERFLOW);(*S)-top=(*S)-base;(*S)-stacksize=STACK_INIT_SIZE;return OK;Status DestroyStack(SqStack *S)free(S-base);free(S);Status ClearStack(SqStack *S)S-top=S-base;Status StackEmpty(SqStack S)if(S.top=S.base) return TRUE;elsereturn FALSE;int S
16、tackLength(SqStack S)int i;SElemType *p;i=0;p=S.top;while(p!=S.base)p+;i+;SElemType GetTop(SqStack S)if(S.top=S.base) return ERROR;return *(S.top-1);Status 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;return OK;Status Pop(SqStack *S,SElemType *e)if(S-top=S-base) return ERROR;*e=*(-(S-top);return OK;Status StackTraverse(SqStack S,Status (*visit)()while(S.topS.base)visit(-S.top);