栈与队列的顺序表示和实现实验报告

上传人:夏** 文档编号:464464369 上传时间:2022-12-13 格式:DOC 页数:11 大小:450KB
返回 下载 相关 举报
栈与队列的顺序表示和实现实验报告_第1页
第1页 / 共11页
栈与队列的顺序表示和实现实验报告_第2页
第2页 / 共11页
栈与队列的顺序表示和实现实验报告_第3页
第3页 / 共11页
栈与队列的顺序表示和实现实验报告_第4页
第4页 / 共11页
栈与队列的顺序表示和实现实验报告_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《栈与队列的顺序表示和实现实验报告》由会员分享,可在线阅读,更多相关《栈与队列的顺序表示和实现实验报告(11页珍藏版)》请在金锄头文库上搜索。

1、姓名刘茂 学号222012315220062实验项目栈与队列的顺序表示和实现实验报告实验内容编写一个程序实现顺序栈和顺序队列的各种基本运算,并完成以下操作:1、 初始化顺序队列;2、 建立顺序队列;3、 入队;4、 出队;5、 判断队列是否为空;6、 取队头元素;7、 遍历队列;8、 初始化顺序栈;9、 插入元素;10、 删除栈顶元素;11、 取栈顶元素;12、 遍历顺序栈; 13、置空顺序栈;算法设计与程序实现:算法分析 1、对于顺序栈和顺序队列,都是采用一组地址连续的存储单元一次存放自栈底到栈顶或从队列头到队列尾的数据元素。 2、对于栈,设栈顶指针为top,栈底指针为base。用top=0

2、或top=base表示栈空。对于队列,设指针front为队列头指针,设指针rear表示队列尾指针。用front=rear=0表示空队列。 3、初始化栈和队列时,令top=0或front=rear=0,将栈或队列置空。 4、每入栈一个数据元素,指针top增加1,出栈时,指针top减小1。每当插入新的队尾数据元素时,指针rear增加1,每当删除一个队头数据元素时,指针front减小1。核心程序/顺序队列#include#include#include#define MAXNUM 100#define ElemType int #define TRUE 1#define FALSE 0 /定义队列的

3、顺序存储结构typedef structElemType queueMAXNUM;int front;int rear;sqqueue;/初始化顺序队列int initQueue(sqqueue *q)if(!q)return FALSE;q-front=-1;q-rear=-1;return TRUE;/入队int append(sqqueue *q,ElemType x)if(q-rear=MAXNUM-1)return FALSEq-rear+;q-queueq-rear=x;return TRUE;/出队ElemType Delete(sqqueue *q)ElemType x;if(

4、q-front=q-rear)printf(队列空!n);return 0;x=q-queue+q-front;printf(队头元素%d出队!n,x);return x;/判断队列是否为空int Empty(sqqueue *q)if(q-front=q-rear)return TRUE;return FALSE;/获取队头元素int gethead(sqqueue *q)ElemType x;if(q-front=q-rear)printf(队列空!n);return 0;x=q-queueq-front+1;printf(n队头元素:%d出队n,x);return x;/遍历队void

5、display(sqqueue *q)int s;s=q-front;if(q-front=q-rear)printf(队列为空!n);elseprintf(n顺序队列依次为:);while(srear)s=s+1;printf(%dqueues);printf(n);printf(顺序队列队尾元素所在位置为:rear=%dn,q-rear);printf(书序队列队头元素所在位置为:front=%dn,q-front);/建立顺序队列void Setsqqueue(sqqueue *q)int n,i,m;printf(n请输入顺序队列的长度:);scanf(%d,&n);printf(n请

6、依次输入顺序队列的元素值:n);for(i=0;in;i+)scanf(%d,&m);append(q,m);/主函数void main()sqqueue *head;int x,select;head=(squeue*)malloc(sizeof(squeue);printf(n第一次使用必须初始化!n);doprintf(n*主菜单*n);printf(n 1 初始化队列 n);printf(n 2 建立顺序队列 n);printf(n 3 入队 n);printf(n 4 出队 n);printf(n 5 判断队列是否为空 n);printf(n 6 取队头元素 n);printf(n

7、7 遍历队列 n);printf(n 0 退出程序 n);printf(n*n);printf(n请选择操作:n);scanf(%d,&select);switch(select)case 1:initQueue(head);printf(已经初始化队列!n);break;case 2:Setsqqueue(head)printf(n已经建立队列!n);dispaly(head);break;case 3:printf(请输入队的值:n);scanf(%d,&x);append(head,x);dispaly(head);break;case 4:Delete(head);diapaly(he

8、ad);break;case 5:if(Empty(head)printf(队列空!n);elseprintf(队列非空!n);break;case 6:gethead(head);break;case 7:dispaly(head);break;case 0:exit(0);while(select=7);/顺序栈#include#include#define MAXNUM 100#define ElemType int /定义栈的顺序存储结构typedef structElemType stackMAXNUM;int top;SqStack;/初始化顺序栈void InitStack(Sq

9、Stack *p)if(!p)printf(内存分配失败!);p-top= -1;/入栈void Push(SqStack *p,ElemType x)if(p-toptop=p-top+1;p-stackp-top=x;elseprintf(Overflow!n);/出栈ElemType Pop(SqStack *p)ElemType x;if(p-top=0)x=p-stackp-top;printf(栈顶数据元素%d已经被删除!n,p-stackp-top);p-top=p-top-1;return(x);elseprintf(Underflow!n);return(0);/获取栈顶元素

10、ElemType GetTop(SqStack *p)ElemType x;if(p-top=0)x=p-stackp-top;printf(n栈顶元素为:%dn,x);return(x);elseprintf(Underflow!n);return(0);/遍历顺序栈void OutStack(SqStack *p)int i;printf(n);if(p-toptop;i=0;i-);printf(第%d个数据元素为:%6dn,i,p-stacki);/置空顺序栈void setEmpty(SqStack *p)p-top=-1;/主函数void main()SqStack *q;int

11、cord;ElemType a;printf(第一次使用必须初始化!n);doprintf(n*主菜单*n);printf(n 1 初始化顺序栈 n);printf(n 2 插入一个元素 n);printf(n 3 删除栈顶元素 n);printf(n 4 区栈顶元素 n);printf(n 5 置空顺序栈 n);printf(n 6 结束程序运行 n);printf(n*n);printf(你选择的序号是(1,2,3,4,5,6);scanf(%d,&cord);switch(cord)case 1:q = (SqStack * )malloc(sizeof(SqStack);InitStack(q);OutStack(q);break;case 2:

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 工作计划

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