实验报告第三章1211102车红岫Word下载.docx
- 文档编号:21155367
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:24
- 大小:918.92KB
实验报告第三章1211102车红岫Word下载.docx
《实验报告第三章1211102车红岫Word下载.docx》由会员分享,可在线阅读,更多相关《实验报告第三章1211102车红岫Word下载.docx(24页珍藏版)》请在冰豆网上搜索。
//尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;
4)链队列
typedefstructQNode
QElemTypedata;
structQNode*next;
}QNode,*QueuePtr;
3.算法设计
4.界面设计
1)顺序栈
2)链栈
3)循环队列
4)链队列
5.运行、测试
(1)运行程序,显示菜单
(2)输入所选操作
(3)输出结果
(2)输入所选操作
(1)运行程序,显示菜单
(2)输入n
(3)输出结果
6.实验收获及思考
选用合适的数据结构可以简化问题。
附录:
源代码
#include<
stdio.h>
conio.h>
malloc.h>
stdlib.h>
iomanip>
string>
#defineMAXSIZE100
voidPushStack(SqStack*s,intx)//入栈
{if(s->
top==MAXSIZE-1)
printf("
栈已满,不能入栈\n"
);
else
{s->
data[++s->
top]=x;
printf("
入栈已完成\n"
}
}
intEmpty(SqStack*s)//判断栈空
{return(s->
top==-1?
1:
0);
intPop(SqStack*s,intx)//删除栈顶元素,并用x返回其值
{if(Empty(s))
{printf("
空栈\n"
return0;
s->
top--;
出栈成功\n"
return1;
voidprintstruct(SqStack*s,intx)//输出栈
栈空\n"
exit(0);
{x=s->
data[s->
top];
%d"
x);
//打印
s->
//修改栈顶指针
intGetStackTop(SqStacks)//取栈顶
{returns.data[s.top];
intmain()
{intx;
SqStacknewStack;
newStack.top=-1;
1置空栈\n"
2进栈\n"
3出栈\n"
4取栈顶元素\n"
5显示栈内元素\n"
6判断栈空\n"
0退出\n"
while
(1)
{switch(getch())
{case'
1'
:
break;
case'
2'
请输入要进栈的元素\n"
scanf("
&
x);
PushStack(&
newStack,x);
break;
3'
Pop(&
break;
4'
if(Empty(&
newStack))
{printf("
}
else
printf("
栈顶元素是%d\n"
GetStackTop(newStack));
5'
printstruct(&
6'
非空\n"
0'
exit(0);
default:
操作出错,请重新输入!
\n"
}
}return0;
#include<
typedefstructSNode
LinkStacktop;
LinkStackPushStack(LinkStacktop,intx)//入栈
LinkStacks;
s=(LinkStack)malloc(sizeof(SNode));
data=x;
next=top;
top=s;
returntop;
LinkStackPopStack(LinkStacktop)//退栈
LinkStackp;
if(top!
=NULL)
{
p=top;
top=top->
next;
free(p);
退栈已完成\n"
returntop;
栈是空的,无法退栈!
intGetStackTop(LinkStacktop)//取栈顶元素
returntop->
data;
boolIsEmpty()//bool取值false和true,是0和1的区别,bool只有一个字节,BOOL为int型,bool为布尔型
returntop==NULL?
true:
false;
intPrint()//输出栈
SNode*p;
if(IsEmpty())
空栈!
while(p)
%d"
p->
data);
p=p->
voidmain()
{
intx,a,b;
charm;
do
{
1.置空栈\n"
2.进栈\n"
3.退栈\n"
4.取栈顶元素\n"
5.退出程序\n"
\n请选择一个操作:
"
scanf("
%c"
m);
switch(m)
{
case'
top=NULL;
printf("
\n栈已置空!
break;
printf("
\n请输入要进栈的元素个数是:
scanf("
a);
\n请输入要进栈的%d个元素:
a);
for(b=0;
b<
a;
b++)
{
scanf("
top=PushStack(top,x);
}
进栈已完成!
\n输出栈为:
Print();
case'
\n操作之前的输出栈为:
Print();
top=PopStack(top);
\n操作过后的输出栈为:
if(top!
=NULL)
\n栈顶元素是:
%d\n"
GetStackTop(top));
else
\n栈是空的,没有元素!
default:
\n输入的操作不对,请重新输入!
getchar();
while(m!
='
#defineOK1
#defineERROR0
typedefintStatus;
//Status是函数的类型,其值是函数结果状态代码,如OK等
typedefintQElemType;
#defineMAXQSIZE100//最大队列长度(对于循环队列,最大队列长度要减1)
typedefstruct
StatusInitQueue(SqQueue&
Q)//构造一个空队列Q,该队列预定义大小为MAXQSIZE
//请补全代码
{Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
if(!
Q.base)returnERROR;
Q.front=Q.rear=0;
returnOK;
StatusEnQueue(SqQueue&
Q,QElemTypee)//插入元素e为Q的新的队尾元素
{if((Q.rear+1)%MAXQSIZE==Q.front)returnERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
StatusDeQueue(SqQueue&
Q,QElemType&
e)//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;
否则返回ERROR
{if(Q.front==Q.rear)returnERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
StatusGetHead(SqQueueQ,QElemType&
e)//若队列不空,则用e返回队头元素,并返回OK,否则返回ERROR
StatusQueueTraverse(SqQueueQ)//若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;
否则返回ERROR.
{inti;
i=Q.front;
if(Q.front==Q.rear)printf("
空队列!
else{
printf("
队列为:
"
while(i<
Q.rear)
Q.base[i]);
i=i+1;
inta;
SqQueueS;
QElemTypex,e;
if(InitQueue(S))//判断顺序表是否创建成功
队列已建立.\n"
while
(1)
输入\n2:
删除\n3:
读队列元素\n4:
输出队列\n0:
退出\n请选择:
switch(a)
case1:
&
if(!
EnQueue(S,x))printf("
错误!
//判断入队是否合法
elseprintf("
数据%d输入成功!
x);
case2:
DeQueue(S,e))printf("
//判断出队是否合法
数据%d删除成功!
e);
case3:
GetHead(S,e))printf("
//判断GetHead是否合法
队列头元素为%d!
case4:
QueueTraverse(S);
case0:
return1;
#defineNULL0
typedefstructQNode
QueuePtrfront;
QueuePtrrear;
}LinkQueue;
intInitQueue(LinkQueue&
Q)//初始化队列
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!
Q.front)
return0;
Q.front->
next=NULL;
intQueueEmpty(LinkQueueQ)//判断队列空
if(Q.front==Q.rear)return1;
intDestroyQueue(LinkQueue&
Q)
while(Q.front)
Q.rear=Q.front->
free(Q.front);
Q.front=Q.rear;
intEnQueue(LinkQueue&
Q,QElemTypee)//入队列
QNode*p;
p=(QueuePtr)malloc(sizeof(QNode));
p)
p->
data=e;
Q.rear->
next=p;
Q.rear=p;
intDeQueue(LinkQueue&
Q,QElemType&
e)//出队列
if(QueueEmpty(Q))
p=Q.front->
e=p->
next=p->
if(Q.rear==p)
Q.rear=Q.front;
free(p);
intGetHead(LinkQueueQ,QElemType&
e)//去队列头元素
e=Q.front->
next->
intPrintQueue(LinkQueueQ)//输出队列
QNode*p=Q.front->
if(Q.front==Q.rear)
空队列\n"
输出链队列:
while(Q.front)
p->
if(p==Q.rear)
intn;
QElemTypee=1;
inti;
LinkQueueQ;
InitQueue(Q);
请输入队列的初始数据(按0结束):
while(e)
e);
e)break;
EnQueue(Q,e);
for(i=0;
i<
20;
i++)
请输入以下数字执行相应程序:
元素进链队列\n"
2:
输出队头元素\n"
3:
出队列\n"
4:
输出链队列\n"
5:
判队列空\n"
6:
置空队列\n"
0:
结束程序\n"
n);
switch(n)
case1:
请输入要的进链队列的数(用空格隔开):
case2:
队头元素为:
GetHead(Q,e);
e);
case3:
出队列:
DeQueue(Q,e);
case4:
PrintQueue(Q);
case5:
空队列"
else
非空队列"
case6:
Q.front=Q.rear;
已置空"
case0:
输入错误!
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 报告 第三 1211102 车红岫
![提示](https://static.bdocx.com/images/bang_tan.gif)