上机任务指导书2.docx
- 文档编号:25174197
- 上传时间:2023-06-05
- 格式:DOCX
- 页数:7
- 大小:15.47KB
上机任务指导书2.docx
《上机任务指导书2.docx》由会员分享,可在线阅读,更多相关《上机任务指导书2.docx(7页珍藏版)》请在冰豆网上搜索。
上机任务指导书2
上机任务指导书2
一、实验名称:
栈和队列综合应用
二、实验目的:
熟练运用数组、指针和结构体类型完成栈、队列的相关操作,编制相关应用程序,运用模块化方法完成任务,通过菜单作为界面
三、实验环境:
Turboc2.0或VC++6.0
四、实验内容(采用顺序存储或链式存储均可)
栈、队列基本操作
1入栈2出栈3十进制转八进制
4入队5出队6两个栈实现队列
7退出
请输入选择(1—7):
五、源程序清单
/*
实验内容(采用顺序存储或链式存储均可)
栈、队列基本操作
1入栈2出栈3十进制转八进制
4入队5出队6两个栈实现队列
7退出
请输入选择(1—7):
*/
#include
#include
#defineMAX10
typedefstructnode
{inttop;
inta[MAX];
}NODE;
inti=0;
voidPush(NODE*h)//入栈
{
intn;
inti=h->top+1;
printf("请输入一个入栈数:
");
scanf("%d",&n);
if(h->top>=MAX)
printf("栈已经满");
else
{
h->a[i]=n;
i++;
h->top++;
printf("%d:
已经入栈.\n",n);
}
}
voidPop(NODE*h)//出栈
{
if(h->top<=-1)
printf("空栈!
");
else{
printf("%d已经出栈\n\n",h->a[h->top]);
h->top--;
}
}
voidcomersion(NODE*h)//转换
{intx;
printf("请输入10进制数:
");
scanf("%d",&x);
while(x>=8)
{h->top++;
h->a[h->top]=x%8;
x=x/8;
}
if(x>0)
{h->top++;
h->a[h->top]=x;
}
printf("\n8进制数为:
");
while(h->top>-1)
{
printf("%d",h->a[h->top]);
h->top--;
}
printf("\n");
}
voidPushQueue(NODE*h)//入队
{intx;
printf("请输入数:
");
scanf("%d",&x);
printf("\n");
if(h->top>=MAX)
printf("队列已满\n\n");
else{
h->top++;
h->a[h->top]=x;
printf("%d:
已经入队\n\n",x);
}
printf("\n");
}
voidOutQueue(NODE*h)//出队
{inti=0;
if(h->top==-1)
printf("队列为空");
else
{printf("%d",h->a[0]);
printf(":
已经出队\n");
for(;i
h->a[i]=h->a[i+1];
}
h->top--;
}
voidMarger(NODE*h1,NODE*h2)//两个栈实现队列
{intx;
printf("请输入数输入-1结束");
scanf("%d",&x);
while(x!
=-1)
{h1->top++;
h1->a[h1->top]=x;
scanf("%d",&x);
}
while(h1->top!
=-1)
{h2->top++;
h2->a[h1->top]=h1->a[h1->top];
h1->top--;
}
printf("\n以转换成队列输出:
");
while(h2->top!
=-1)
{h1->top++;
printf("%d",h2->a[h1->top]);
h2->top--;
}
printf("\n");
}
main()
{
inta[MAX],x;
inti,n;
NODE*h2=(structnode*)malloc(sizeof(structnode));
NODE*h1=(structnode*)malloc(sizeof(structnode));
NODE*h3=(structnode*)malloc(sizeof(structnode));
NODE*shu=(structnode*)malloc(sizeof(structnode));
NODE*dui=(structnode*)malloc(sizeof(structnode));
h1->top=-1;
h3->top=-1;
h2->top=-1;
shu->top=-1;
dui->top=-1;
while
(1)
{printf("\n\n");
printf("菜单:
\n");
printf("1Push2Pop\n3comersion4PushQueue\n5OutQueue6Marger\n7Quit\n");
printf("Pleaseinputyourselection(1-7)");
scanf("%d",&n);
if(n>=1&&n<=7)
{
printf("\n");
}
else
{
printf("输入错误!
\n请重新输入:
");
scanf("%d",&n);
}
switch(n)
{case1:
Push(h2);break;
case2:
Pop(h2);break;
case3:
comersion(shu);break;
case4:
PushQueue(dui);break;
case5:
OutQueue(dui);break;
case6:
Marger(h1,h3);break;
case7:
system("ClS");exit(0);break;
}
}
}
六、编程过程中遇到的问题及解决的措施
自己没有真正的理解入栈和出栈的具体操作,有许多地方卡住做不下去,最后通过查找资料和课堂笔记把问题解决了。
七、本次实验的收获
本次实验使我知道了自己的不足之处,没有彻底理解书本知识,还存在很多不解之迷需要去探索去学习。
指导教师:
王若民
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 上机 任务 指导书
![提示](https://static.bdocx.com/images/bang_tan.gif)