欢迎来到天天文库
浏览记录
ID:52199562
大小:62.00 KB
页数:7页
时间:2020-03-24
《数据结构实验报告5.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、数据结构实验报告——实验5学号:姓名:得分:______________一、实验目的1、复习栈的逻辑结构、存储结构及基本操作;2、掌握顺序栈、链栈。二、实验内容1、(必做题)假设栈中数据元素类型是字符型,请采用顺序栈实现栈的以下基本操作:(1)StatusInitStack(&S)//构造空栈S;(2)StatusPush(&S,e)//元素e入栈S;(3)StatusPop(&S,&e)//栈S出栈,元素为e。2、(必做题)请实现:对于一个可能包括括号{}、[]、()的表达式,判定其中括号是否匹配。三、算法描述(采用自然语言描
2、述)1.构建空栈s,输入元素,将元素依次入栈,遍历打印栈中元素,输出栈顶元素,打印被输出的元素,遍历打印栈中元素。2.构建空栈,输入表达式,使用函数count判断表达式中括号是否匹配,如果匹配输出匹配正确,不匹配则输出匹配错误。四、详细设计1.打印被输出的元素遍历打印栈中元素开始构建空栈s将元素依次入栈输出栈顶元素结束输入元素遍历打印栈中元素72.输出匹配错误使用函数count判断表达式中括号是否匹配开始构建空栈s结束输入表达式输出匹配正确五、程序代码(给出必要注释)1.#include#include3、lib.h>#defineMaxSize100typedefstructnode*SqStack;typedefcharElemType;structnode//栈的数据结构{inttop;ElemTypedata[MaxSize];};voidStatusInitStack(SqStack*L)//构造空栈S{(*L)=(SqStack*)malloc(sizeof(SqStack));(*L)->top=-1;}7voidStatusPush(SqStackL,ElemTypee)//元素e入栈S{if(L->top==Ma4、xSize-1){printf("栈满");}else{L->top++;L->data[L->top]=e;}}voidStatusPop(SqStackL,ElemType*e)//栈S出栈,元素为e{if(L->top==-1){printf("栈空");}else{*e=L->data[L->top];L->top--;}}voidPrint(SqStackL)//遍历输出{inti=0;for(i=0;i<=L->top;i++){printf("%c",L->data[i]);}printf("");}i5、ntmain(){SqStacks;ElemTypee;ElemType*y;y=&e;StatusInitStack(&s);printf("输入入栈数据:");scanf("%c",&e);7while(e!=''){StatusPush(s,e);scanf("%c",&e);}printf("目前栈中元素为:");Print(s);StatusPop(s,y);printf("出栈元素是:%c",*y);printf("栈顶元素出栈后,栈为:");Print(s);}2.#include6、dio.h>#include#include#defineSTACK_INIT_SIZE10#defineSTACK_GROW_SIZE5#defineELEMTYPEchartypedefstruct/*建立一个栈的首结点*/{ELEMTYPE*base;ELEMTYPE*top;intstacksize;}SpStack;intInitStack(SpStack*s)/*建立空的栈并返回首地址*/{s->base=((ELEMTYPE*)malloc(STACK_INIT_SIZE*s7、izeof(ELEMTYPE)));if(!s->base)return0;s->top=s->base;s->stacksize=STACK_INIT_SIZE;return1;}intStackEmpty(SpStack*s)/*判断栈是否为空*/{if(s->top==s->base)return1;elsereturn0;}intPush(SpStack*s,ELEMTYPEe)/*往栈顶插入元素即进栈*/7{if(s->top-s->base>=s->stacksize)/*判断是否栈满*/{s->base=((ELE8、MTYPE*)realloc(s->base,(s->stacksize+STACK_GROW_SIZE)*sizeof(ELEMTYPE)));if(!s->base)return0;s->stacksize+=STACK_GROW_SIZE;s->top=s
3、lib.h>#defineMaxSize100typedefstructnode*SqStack;typedefcharElemType;structnode//栈的数据结构{inttop;ElemTypedata[MaxSize];};voidStatusInitStack(SqStack*L)//构造空栈S{(*L)=(SqStack*)malloc(sizeof(SqStack));(*L)->top=-1;}7voidStatusPush(SqStackL,ElemTypee)//元素e入栈S{if(L->top==Ma
4、xSize-1){printf("栈满");}else{L->top++;L->data[L->top]=e;}}voidStatusPop(SqStackL,ElemType*e)//栈S出栈,元素为e{if(L->top==-1){printf("栈空");}else{*e=L->data[L->top];L->top--;}}voidPrint(SqStackL)//遍历输出{inti=0;for(i=0;i<=L->top;i++){printf("%c",L->data[i]);}printf("");}i
5、ntmain(){SqStacks;ElemTypee;ElemType*y;y=&e;StatusInitStack(&s);printf("输入入栈数据:");scanf("%c",&e);7while(e!=''){StatusPush(s,e);scanf("%c",&e);}printf("目前栈中元素为:");Print(s);StatusPop(s,y);printf("出栈元素是:%c",*y);printf("栈顶元素出栈后,栈为:");Print(s);}2.#include6、dio.h>#include#include#defineSTACK_INIT_SIZE10#defineSTACK_GROW_SIZE5#defineELEMTYPEchartypedefstruct/*建立一个栈的首结点*/{ELEMTYPE*base;ELEMTYPE*top;intstacksize;}SpStack;intInitStack(SpStack*s)/*建立空的栈并返回首地址*/{s->base=((ELEMTYPE*)malloc(STACK_INIT_SIZE*s7、izeof(ELEMTYPE)));if(!s->base)return0;s->top=s->base;s->stacksize=STACK_INIT_SIZE;return1;}intStackEmpty(SpStack*s)/*判断栈是否为空*/{if(s->top==s->base)return1;elsereturn0;}intPush(SpStack*s,ELEMTYPEe)/*往栈顶插入元素即进栈*/7{if(s->top-s->base>=s->stacksize)/*判断是否栈满*/{s->base=((ELE8、MTYPE*)realloc(s->base,(s->stacksize+STACK_GROW_SIZE)*sizeof(ELEMTYPE)));if(!s->base)return0;s->stacksize+=STACK_GROW_SIZE;s->top=s
6、dio.h>#include#include#defineSTACK_INIT_SIZE10#defineSTACK_GROW_SIZE5#defineELEMTYPEchartypedefstruct/*建立一个栈的首结点*/{ELEMTYPE*base;ELEMTYPE*top;intstacksize;}SpStack;intInitStack(SpStack*s)/*建立空的栈并返回首地址*/{s->base=((ELEMTYPE*)malloc(STACK_INIT_SIZE*s
7、izeof(ELEMTYPE)));if(!s->base)return0;s->top=s->base;s->stacksize=STACK_INIT_SIZE;return1;}intStackEmpty(SpStack*s)/*判断栈是否为空*/{if(s->top==s->base)return1;elsereturn0;}intPush(SpStack*s,ELEMTYPEe)/*往栈顶插入元素即进栈*/7{if(s->top-s->base>=s->stacksize)/*判断是否栈满*/{s->base=((ELE
8、MTYPE*)realloc(s->base,(s->stacksize+STACK_GROW_SIZE)*sizeof(ELEMTYPE)));if(!s->base)return0;s->stacksize+=STACK_GROW_SIZE;s->top=s
此文档下载收益归作者所有