数据结构__栈与队列

上传人:子 文档编号:43080681 上传时间:2018-06-04 格式:DOC 页数:6 大小:339KB
返回 下载 相关 举报
数据结构__栈与队列_第1页
第1页 / 共6页
数据结构__栈与队列_第2页
第2页 / 共6页
数据结构__栈与队列_第3页
第3页 / 共6页
数据结构__栈与队列_第4页
第4页 / 共6页
数据结构__栈与队列_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《数据结构__栈与队列》由会员分享,可在线阅读,更多相关《数据结构__栈与队列(6页珍藏版)》请在金锄头文库上搜索。

1、第 1 页 共 6 页 仲恺农业工程学院实验报告纸仲恺农业工程学院实验报告纸(院、系) 专业 班 组 课学号 姓名 实验日期 教师评定 实验一实验一 栈和队列的操作及应用栈和队列的操作及应用第一部分 栈的操作及应用 一、实验目的1、理解栈的定义、特征及在其所定义的基本运算2、掌握在两种存储结构上对栈所施加的基本运算的实现,特别是判断栈满和栈空的条件3、掌握利用栈解决简单实际问题,如数制转换、表达式求值等4、理解递归算法执行过程中栈的状态变化过程 二、实验要求编写程序实现栈的各种运算,并在此基础上设计主函数,使其完成如下功能:(1)初始化栈(2)元素a,b,c,d,e依次进栈(3)输出栈顶元素(

2、4)栈顶元素出栈(5)输出栈序列三、程序运算结果截图四、程序源代码 #include #include using namespace std;第 2 页 共 6 页 #define maxsize 10 / typedef struct Stack int *top; int *buttom; /栈 int length; int size; stack; / void Init(stack* a) a-top=(int*)malloc(sizeof(int)*maxsize); if(!a-top) exit(0); a-buttom=a-top; /初始化栈 a-length=0; a-

3、size=maxsize; / void Push(stack* a,int d) if(a-top)-(a-buttom)=a-size) (a-size)+=maxsize; a-top=(int*)realloc(a-top,sizeof(int)*(a-size); if(!a-top) exit(0); /入栈 *(a-top)=d; (a-top)+; a-length+; / int Pop(stack* a) int tmp; tmp=*(a-top)-1); if(a-top)-(a-buttom)=0) /出栈 (a-top)-; return tmp; / void De

4、stroy(stack* a)第 3 页 共 6 页 int i; for(i=0;ilength;i+) free(a-buttom); /销毁栈 a-buttom+; a-buttom=a-top=NULL; a-size=a-length=0; / void main() stack a;/栈 int d;/数据 int start=1;/是否循环进栈 Init(/初始化 while(start) coutd; Push( cout:“; cinstart; cout #include using namespace std; / typedef struct Qnode int dat

5、a;第 5 页 共 6 页 struct Qnode* next; /队节点 node,*qnode; / typedef struct Queue qnode front; qnode rear; /队列 int length; queue; / void Init(queue* a) a-front=a-rear=(qnode)malloc(sizeof(node); if(!a-front) /初始化队列 exit(0); a-front-next=NULL; / void Enqueue(queue* a,int d) qnode tmp; tmp=(qnode)malloc(size

6、of(node); if(!a-front) /入队 exit(0); a-length+; tmp-data=d; tmp-next=NULL; a-rear-next=tmp; a-rear=tmp; / int Dequeue(queue* a) int d; qnode tmp; tmp=a-front-next; d=tmp-data; /出队 a-front-next=tmp-next; if(a-rear=tmp) a-rear=a-front; free(tmp); a-length-; return d; 第 6 页 共 6 页 / bool Empty(queue* a)

7、if(a-rear=a-front) return true; /判断队列是否为空 return false; / void Destory(queue* a) while(a-front) a-rear=a-front-next; /销毁队列 free(a-front); a-front=a-rear; / void main() int d;/数据 int start=1;/是否循环进队 queue a;/队列 Init(/初始化 /循环操作 while(start) coutd; Enqueue( cout:“; cinstart; cout“队列序列:“; while(!Empty( coutendl; Destory(

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

最新文档


当前位置:首页 > 生活休闲 > 科普知识

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