《数据结构》实验二 栈和队列
更新时间:2024-03-23 22:48:01 阅读量: 综合文库 文档下载
- 数据结构实验二叉树推荐度:
- 相关推荐
《数据结构》实验指导及报告书
2014 / 2015 学年 第 1学期
姓 名: 学 号: 班 级: 指导教师:徐江
计算机科学与工程学院
2014
实验二 栈和队列
一、实验目的
1、掌握栈的结构特性及其入栈,出栈操作;
2、掌握队列的结构特性及其入队、出队的操作,掌握循环队列的特点及其操作。
二、实验内容和要求
1、阅读下面程序,将函数Push和函数Pop补充完整。要求输入元素序列1 2 3 4 5 e,运行结果如下所示。
#include
#define STACK_INT_SIZE 10 /*存储空间初始分配量*/ #define STACKINCREMENT 5 /*存储空间分配增量*/ typedef int ElemType; /*定义元素的类型*/ typedef struct{ ElemType *base; ElemType *top;
int stacksize; /*当前已分配的存储空间*/ }SqStack;
int InitStack(SqStack *S); /*构造空栈*/ int push(SqStack *S,ElemType *e); /*入栈*/ int Pop(SqStack *S,ElemType *e); /*出栈*/ int CreateStack(SqStack *S); /*创建栈*/
void PrintStack(SqStack *S); /*出栈并输出栈中元素*/
int InitStack(SqStack *S){
S->base=(ElemType *)malloc(STACK_INT_SIZE *sizeof(ElemType)); if(!S->base) return ERROR;
S->top=S->base;
S->stacksize=STACK_INT_SIZE; return OK; }/*InitStack*/
int Push(SqStack *S,ElemType e){
}/*Push*/
int Pop(SqStack *S,ElemType *e){
}/*Pop*/
int CreateStack(SqStack *S){ int e;
if(InitStack(S))
printf(\ else{
printf(\ return ERROR; }
printf(\ while(scanf(\ Push(S,e); return OK; }/*CreateStack*/
void PrintStack(SqStack *S){ ElemType e;
while(Pop(S,&e)) printf(\}/*Pop_and_Print*/
int main(){ SqStack ss;
printf(\ CreateStack(&ss);
printf(\ PrintStack(&ss); return 0; }
算法分析:输入元素序列1 2 3 4 5,为什么输出序列为5 4 3 2 1?体现了栈的什么特性?
#include
?
#include
#define STACK_INT_SIZE 10 /*存储空间初始分配量*/ #define STACKINCREMENT 5 /*存储空间分配增量*/ typedef int ElemType; /*定义元素的类型*/ typedef struct{ ElemType *base; ElemType *top;
int stacksize; /*当前已分配的存储空间*/ }SqStack;
int InitStack(SqStack *S); /*构造空栈*/ int Push(SqStack *S,ElemType *e); /*入栈*/ int Pop(SqStack *S,ElemType *e); /*出栈*/ int CreateStack(SqStack *S); /*创建栈*/
void PrintStack(SqStack *S); /*出栈并输出栈中元素*/
int InitStack(SqStack *S){
S->base=(ElemType *)malloc(STACK_INT_SIZE *sizeof(ElemType)); if(!S->base) return ERROR; S->top=S->base;
S->stacksize=STACK_INT_SIZE; return OK; }/*InitStack*/
int Push(SqStack *S,ElemType e){ if(S->top-S->base>=S->stacksize) {
S->base=(ElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!S->base) return ERROR; S->top=S->base+S->stacksize; S->stacksize+=STACKINCREMENT; }
*S->top++=e; return OK; }/*Push*/
int Pop(SqStack *S,ElemType *e){ if(S->top=S->base)return ERROR; e= --S->top; return OK;
}/*Pop*/
int CreateStack(SqStack *S){ int e;
if(InitStack(S))
printf(\ else{
printf(\ return ERROR; }
printf(\ while(scanf(\ Push(S,e); return OK; }/*CreateStack*/
void PrintStack(SqStack *S){ ElemType e;
while(Pop(S,&e)) printf(\}/*Pop_and_Print*/
int main(){ SqStack ss;
printf(\ CreateStack(&ss);
printf(\ Pop(&ss, &e); PrintStack(&ss); return 0; }
2、在第1题的程序中,编写一个十进制转换为二进制的数制转换算法函数(要求利用栈来实现),并验证其正确性。 ? 实现代码
#include
正在阅读:
《数据结构》实验二 栈和队列03-23
人教部编版小学数学五年级上册第5课时 一个数除以小数2 教案03-09
2019年盼望圣诞节作文300字06-14
北师大版九年级数学第一学期期中评价检测试卷(含答案)03-12
奇瑞汽车联合动力站房施工组织设计4.1105-01
精编开展学校素质提升教育心得体会优选例文08-04
优质护理服务文字材料 - 图文12-13
698-最小装量测定USP09-16
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 数据结构
- 队列
- 实验
- 房屋安全鉴定报告 - 图文
- triz描述一个问题,并找出其中的技术冲突 需找出改进的参数和恶
- 江苏省泰兴市洋思中学2014-2015学年八年级政治下学期期中试题 新
- 问题在于改变世界(马克思《关于费尔巴哈的提纲》的读书笔记)
- 炎热天气施工方案
- 磁场的测量
- 新编湖南省娄底市双峰一中等五校高三上学期期中联考数学(文)试
- 初中生语文学习计划
- 34.2014高考领航(文)6-2
- 产前筛查管理制度
- (民主集中制)
- 盘锦装修枫丹白露
- 西方政治论文-上交
- 创意简报
- 2016-2017年最新北师大版三年级语文上册第八单元测试题及答案
- 小学语文论文注重为学生建构学习的智力背景 - 人教版新课标-文档
- 关于想家的句子
- 《风波》读后感
- 社会救助 考试题型及分值
- 中国农业银行经济资本管理办法.doc