数据结构验证性实验报告三

上传人:世*** 文档编号:173197092 上传时间:2021-03-12 格式:DOC 页数:15 大小:288.50KB
返回 下载 相关 举报
数据结构验证性实验报告三_第1页
第1页 / 共15页
数据结构验证性实验报告三_第2页
第2页 / 共15页
数据结构验证性实验报告三_第3页
第3页 / 共15页
数据结构验证性实验报告三_第4页
第4页 / 共15页
数据结构验证性实验报告三_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《数据结构验证性实验报告三》由会员分享,可在线阅读,更多相关《数据结构验证性实验报告三(15页珍藏版)》请在金锄头文库上搜索。

1、验证性实验三 栈、队列的实现及应用实验课程名:数据结构专业班级: 11级专升本 学号: 1 姓名: 实验时间: 2012.5.7 实验地点: K4-207 指导教师: 一、 实验目的1、掌握栈和队列的顺序存储结构和链式存储结构,以便在实际背景下灵活运用。2、掌握栈和队列的特点,即先进后出与先进先出的原则。3、掌握栈和队列的基本操作实现方法。二、 实验任务1. 实现栈的顺序存储2. 利用栈实现数制转换实验内容:任务一:实现栈的顺序存储代码如下: #include stdio.h# define MAXSIZE 100#includemalloc.h typedef int ElemType;ty

2、pedef struct ElemType dataMAXSIZE; int top;SeqStack;/构造一个空栈int InitStack(SeqStack *s)s-top=0; return 1;/判断栈是否为空int StackEmpty(SeqStack *s) if(s-top=0) return 1; else return 0;int StackFull(SeqStack *s) if(s-top=MAXSIZE-1) return 1; else return 0;/插入新元素x为栈顶元素int Push(SeqStack *s,int x) if (StackFull(

3、s) printf(the stack is overflow!n);return 0; else s-datas-top=x; s-top+;void display(SeqStack *s)if(s-top=0) printf(the stack is empty!n); else while(s-top!=0) printf(%d-,s-datas-top-1); s-top=s-top-1; ElemType Pop(SeqStack *s) if(StackEmpty(s) return 0; else return s-data-s-top; ElemType StackTop(S

4、eqStack *s) int i;if(StackEmpty(s) return 0; else i=s-top-1;return s-datai; /*返回栈顶元素的值,但不改变栈顶指针*/ void main() int n,i,k,h,x1,x2,select; SeqStack *p = (SeqStack *)malloc(sizeof(SeqStack); printf(create a empty stack!n); InitStack(p); printf(input a stack length:n); scanf(%d,&n); for(i=0;i%dn,x1); dis

5、play(p); break; case 4: x2=StackTop(p);printf(x2-%d,x2);break; 实现栈的顺序存储,完成以下功能:1.构建一个空栈2. 依次输入入栈的值3. 显示输入的值4. 弹出栈顶值:任务二 利用栈实现数制转换代码如下:#include stdio.h#include malloc.h# define MAXSIZE 100typedef int ElemType; /*将顺序栈的元素定义为整型*/typedef struct ElemType dataMAXSIZE; int top;SeqStack; int InitStack(SeqSta

6、ck *s) s-top=0; return 1;int StackEmpty(SeqStack *s) if(s-top=0) return 1; else return 0;int StackFull(SeqStack *s) if(s-top=MAXSIZE-1) return 1; else return 0;int Push(SeqStack *s,int x) if (StackFull(s) printf(the stack is overflow!n); return 0; else s-datas-top=x; s-top+; ElemType Pop(SeqStack *s

7、) ElemType y; if(StackEmpty(s) printf(the stack is empty!n); return 0; else y=s-datas-top-1; s-top=s-top-1; return y; ElemType StackTop(SeqStack *s) if(StackEmpty(s) return 0; else return s-datas-top;void Dec_to_Ocx (int N) /* n是非负的十进制整数,输出等值的八进制数*/ /*定义一个顺序栈*/SeqStack *S = (SeqStack *)malloc(sizeof

8、(SeqStack);ElemType x; InitStack(S); /*初始化栈*/if(N0) printf(n错误,输入的数必须大于0); return; if(!N) Push(S,0);while(N) /*自右向左产生八进制的各位数字,并将其进栈*/ Push(S,N%8); /*余数入栈 */ N=N/8; /*商作为被除数*/ while(!StackEmpty(S) /*栈非空时退栈输出*/ x=Pop(S); printf(%d,x); printf(n); void main( ) int n; printf(请输入要转换为8进制的数:n);scanf(%d,&n);Dec_to_Ocx (n);实验结果:输入十进制的数67转换为八进制后的结果为103实验总结:通过本次实验,我掌握了栈和队列的顺序存储和链式存储,栈的主要特点是先进后出,而队列是先进先出。实验中我们掌握了空栈的建立,以及如何向空栈中压入值,弹出值等操作,通过这些练习让我对栈的特性有了更深刻的体会.四、教师批阅及成绩:

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号