数据结构作业Word文档格式.docx
- 文档编号:19376086
- 上传时间:2023-01-05
- 格式:DOCX
- 页数:17
- 大小:18.12KB
数据结构作业Word文档格式.docx
《数据结构作业Word文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构作业Word文档格式.docx(17页珍藏版)》请在冰豆网上搜索。
top==-1)
e=s->
top];
top--;
intGetTop(SqStack*s,ElemType&
voidDispStack(SqStack*s)
inti;
for(i=s->
top;
i>
=0;
i--)
printf("
%c"
s->
elem[i]);
printf("
\n"
);
/*文件名:
exp4-1.cpp*/
}tSqStack;
externvoidInitStack(SqStack*&
s);
externvoidClearStack(SqStack*&
externintStackLength(SqStack*s);
externintStackEmpty(SqStack*s);
externintPush(SqStack*&
s,ElemTypee);
externintPop(SqStack*&
e);
externintGetTop(SqStack*s,ElemType&
externvoidDispStack(SqStack*s);
voidmain()
ElemTypee;
SqStack*s;
(1)初始化栈s\n"
InitStack(s);
(2)栈为%s\n"
(StackEmpty(s)?
"
空"
:
非空"
));
(3)依次进栈元素a,b,c,d,e\n"
Push(s,'
a'
b'
c'
d'
e'
(4)栈为%s\n"
(5)栈长度:
%d\n"
StackLength(s));
(6)从栈顶到栈底元素:
DispStack(s);
(7)出栈序列:
while(!
StackEmpty(s))
{
Pop(s,e);
e);
}
(8)栈为%s\n"
(9)释放栈\n"
ClearStack(s);
实验结果:
实验二
#include<
stdlib.h>
#defineOK0
#defineERROR1
#defineOVERFLOW-1
typedefstructnode//定义链栈的数据结构
ElemTypedata;
node*next;
}StackNode,*LinkStack;
voidInitStack(LinkStack&
top)//初始化栈
top=NULL;
voidDestorStack(LinkStack&
top)//销毁栈
LinkStackp,q;
p=top;
while(p)
q=p;
p=p->
next;
free(q);
voidStackEmpty(LinkStacktop)//判断栈是否为空
if(top==NULL)
栈为空!
else
栈不为空!
intPush(LinkStack&
top,ElemTypee)//进栈函数
LinkStackp;
p=newStackNode;
//新开辟内存空间
if(!
p)exit(OVERFLOW);
p->
data=e;
next=top;
top=p;
//将P设置为头结点
returnOK;
intPop(LinkStack&
top,ElemType&
e)//出栈函数
if(top==NULL)
returnERROR;
e=top->
data;
//获取当前节点数据信息
top=top->
deletep;
//释放当前节点
intGetStackLen(LinkStacktop)//获取栈长度
intLength=0;
LinkStackp=top;
++Length;
returnLength;
voidPrintStack(LinkStacktop)//输出链栈信息
%4c"
p->
data);
LinkStacktop;
//生成链栈指针变量
intLength;
InitStack(top);
//初始化链栈
StackEmpty(top);
元素进栈!
//进栈
Push(top,'
Length=GetStackLen(top);
//获取栈长度
栈的长度为:
Length);
栈顶到栈底元素为:
PrintStack(top);
元素出栈:
while(Length)//出栈
Pop(top,e);
%c\n"
--Length;
DestorStack(top);
实验三
iostream>
typedefstructQNode//节点类型
//数据域
QNode*next;
//指针域
}QNode,*QueuePtr;
typedefstruct//链队列类型
QueuePtrfront;
//头指针
QueuePtrrear;
//尾指针
}LinkQueue;
intInitQueue(LinkQueue&
Q)//初始化链队
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
//新建节点
Q.front)exit(OVERFLOW);
Q.front->
next=NULL;
intDestryQueue(LinkQueue&
Q)//销毁
while(Q.front)
Q.rear=Q.front->
free(Q.front);
Q.front=Q.rear;
intEnQueue(LinkQueue&
Q,ElemTypee)//进队
QueuePtrp;
p=(QueuePtr)malloc(sizeof(QNode));
Q.rear->
next=p;
Q.rear=p;
intDeQueue(LinkQueue&
Q,ElemType&
e)//出队
if(Q.rear==Q.front)returnERROR;
//空列队
p=Q.front->
e=p->
next=p->
if(Q.rear==p)Q.rear=Q.front;
//出队节点为最后一个
free(p);
voidQueueEmpty(LinkQueueQ)//判断队列是否为空
if(Q.front==Q.rear)
队列为空\n"
else
队列不为空\n"
intGetQueueLen(LinkQueueQ)//获取队列长度
{LinkQueueQ;
intlength=0;
InitQueue(Q);
//初始化队列
printf("
初始化队列\n"
QueueEmpty(Q);
元素a,b,c,d,e开始进队\n"
EnQueue(Q,'
DeQueue(Q,e);
//一个元素出队
出队一个元素,该元素为:
队列的长度为:
GetQueueLen(Q));
;
;
元素f,g,h进队\n"
f'
//元素进队
g'
h'
GetQueueLen(Q));
length=GetQueueLen(Q);
//获取队列长度
元素出队:
while(length)//全部元素出队
DeQueue(Q,e);
--length;
system("
pause"
释放队列\n"
DestryQueue(Q);
实验四
#defineMaxqsize8
ElemTypeelem[Maxqsize];
intfront,rear;
}SqQueue;
voidInitQueue(SqQueue*&
q)
q=(SqQueue*)malloc(sizeof(SqQueue));
q->
front=q->
rear=0;
voidClearQueue(SqQueue*&
free(q);
intQueueLength(SqQueue*q)
return(q->
rear-q->
front+Maxqsize)%Maxqsize;
intQueueEmpty(SqQueue*q)
return(q->
front==q->
rear);
intenQueue(SqQueue*&
q,ElemTypee)
if((q->
rear+1)%Maxqsize==q->
front)
rear=(q->
rear+1)%Maxqsize;
elem[q->
rear]=e;
intdeQueue(SqQueue*&
q,ElemType&
if(q->
rear)
front=(q->
front+1)%Maxqsize;
e=q->
front];
}
SqQueue*q;
(1)初始化队列Q\n"
InitQueue(q);
(3)队列为%s\n"
(QueueEmpty(q)?
(2)依次进队列元素a,b,c,d,e\n"
if(enQueue(q,'
)==0)printf("
队满,不能进队\n"
if(deQueue(q,e)==0)
队空,不能出队\n"
(4)出队一个元素%c\n"
(5)队列Q的元素个数:
QueueLength(q));
(6)依次进队列元素f,g,h\n"
(7)队列Q的元素个数:
(8)出队列序列:
QueueEmpty(q))
{deQueue(q,e);
(9)释放队列\n"
ClearQueue(q);
实验五
typedefstructqnode
intdata;
structqnode*next;
}QNode;
/*链队结点类型*/
typedefstruct
QNode*front,*rear;
}QuType;
/*链队类型*/
voidseeDoctor(){
intsel,flag=1,find,no;
QuType*qu;
QNode*p;
qu=(QuType*)malloc(sizeof(QuType));
//创建队列
qu->
front=qu->
rear=(QNode*)malloc(sizeof(QNode));
//创建头结点
front->
next=NULL;
//队列为空
while(flag==1)/*循环执行*/
{printf("
1:
排队2:
就诊3:
查看排队4:
不再排队,余下依次就诊5:
下班请选择"
scanf("
%d"
&
sel);
switch(sel)
{case1:
>
>
输入病历号"
do
{
scanf("
no);
find=0;
p=qu->
front;
while(p!
=NULL&
&
!
find)
{
if(p->
data==no)
find=1;
else
p=p->
}
if(find)
printf("
输入的病历号重复,重新输入:
}while(find==1);
//直到输入一个不在队列中的病号才结束循环
p=(QNode*)malloc(sizeof(QNode));
//创建结点
p->
data=no;
p->
qu->
rear->
next=p;
qu->
rear=p;
//将*p结点入队
break;
case2:
if(qu->
next==NULL)/*队空*/
没有排队的病人!
else/*队不空*/
p=qu->
病人%d就诊\n"
qu->
next=p->
free(p);
break;
case3:
if(qu->
next==NULL)//队空
else{//队不空
p=qu->
排队病人"
while(p!
=NULL)
{printf("
printf("
}
case4:
if(qu->
printf("
else{//队不空
病人按以下顺序就诊:
{
p=p->
}
break;
case5:
next!
=NULL)//队不空
请排队的病人明天就医!
flag=0;
/*退出*/
seeDoctor();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 作业