实验二栈队列的实现及应用Word文档下载推荐.docx
- 文档编号:16030354
- 上传时间:2022-11-17
- 格式:DOCX
- 页数:20
- 大小:150.66KB
实验二栈队列的实现及应用Word文档下载推荐.docx
《实验二栈队列的实现及应用Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《实验二栈队列的实现及应用Word文档下载推荐.docx(20页珍藏版)》请在冰豆网上搜索。
"
);
e=getchar();
*S->
top++=e;
return(OK);
}//Push()end
/*将元素弹出顺序栈*/
intPop(SqStack*S,SElemType*e)//Pop()sub-function
{
if(S->
top==S->
base)
{
printf("
栈为空!
\n"
return(ERROR);
*e=*--S->
top;
}//Pop()end
voiddisplay(SqStack*s)
if(s->
top==s->
else{
while(s->
top!
=s->
{
s->
top=s->
top-1;
printf("
%c->
*(s->
top));
}
}
intmain()
intchoice;
SElemTypee;
SqStacks;
do
===============================\n"
0:
退出\n"
1:
初始化栈\n"
2:
入栈\n"
3:
出栈\n"
4:
读取栈顶元素\n"
5:
显示栈中元素\n"
输入操作选择代码(0-5):
scanf("
%d"
&
choice);
while(choice<
0||choice>
5){printf("
输入有误,请重新输入(0-5):
scanf("
switch(choice)
case0:
exit
(1);
case1:
InitStack(&
s);
break;
case2:
printf("
2\n"
Push(&
case3:
Pop(&
s,&
e);
出栈元素的值是:
%c\n"
e);
case4:
GetTop(&
栈顶元素的值是:
case5:
栈中元素的值是为:
display(&
}while(choice);
return0;
(2)运行结果
(3)结果分析
顺序表通过设置栈顶运用线性结构实现先进先出功能。
2.任务一
(2):
完成下列程序,该程序实现栈的链式存储结构,构建链栈(栈中的元素依次为China,Japan,France,India,Australia),依次进行进栈和出栈操作,判断栈空和栈满操作,返回栈顶元素操作。
要求生成链栈时,从键盘上读取数据元素。
(1)源代码:
#include<
stdio.h>
stdlib.h>
string.h>
#defineOK1
#defineERROR0
typedefcharDataType;
/*链式栈的存储类型*/
typedefstructSNode//definestructureLinkStack
{DataTypedata[20];
structSNode*next;
}SNode,*LinkStack;
voidInitStack_L(LinkStack*top)
top=(LinkStack)malloc(sizeof(SNode));
top->
next=NULL;
\n\n栈初始化成功!
\n\n"
/*取链式栈顶元素*/
intGetTop_L(LinkStack*top,DataTypee[])//GetTop_L()sub-function
{if(!
next)
{printf("
链栈为空!
return(ERROR);
else
{strcpy(e,top->
next->
data);
}//GetTop_L()end
/*将元素压入链式栈*/
intPush_L(LinkStack*top)//Push_L()sub-function
{SNode*q;
DataTypee[20];
q=(LinkStack)malloc(sizeof(SNode));
if(!
q)
存储空间分配失败!
\n"
//清除输入缓冲区,否则原来的输入会默认送给变量e
\n请输入要入栈的元素的值:
gets(e);
strcpy(q->
data,e);
q->
next=top->
next;
top->
next=q;
}//Push_L()end
/*将元素弹出链式栈*/
intPop_L(LinkStack*top,DataTypee[])//Pop_L()sub-function
\n"
strcpy(e,top->
q=top->
next=q->
free(q);
}//Pop_L()end
voiddisplay(LinkStack*top)
LinkStackp=top->
p)
while(p)
{
%s->
p->
p=p->
^\n"
}
charchoice;
DataTypee[20]="
;
LinkStacks=NULL;
do
%c"
&
while(choice<
'
0'
||choice>
5'
)
switch(choice)
case'
:
1'
InitStack_L(&
break;
2'
Push_L(&
3'
Pop_L(&
s,e);
4'
GetTop_L(&
%s\n"
e);
栈中元素的值是:
"
display(&
}while(choice);
return0;
链表通过设置栈顶运用指针实现先进先出功能
3.任务二:
完成下列程序,该程序实现循环队列的存储和基本操作,构建循环队列,完成键盘缓冲区的功能,每输入一个字符,链入缓冲区队列中;
每输出一个字符,将该字符从缓冲区中删除。
#defineMAXQSIZE100
/*定义QElemType为int或别的自定义类型*/
typedefcharQElemType;
/*顺序队列的存储类型*/
typedefstructSqQueue//definestructureSqQueue
{QElemType*base;
intfront;
intrear;
}SqQueue;
/*构造空顺序队列*/
intInitQueue(SqQueue*Q)//InitQueue()sub-function
{Q->
base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
Q->
分配空间失败!
front=Q->
rear=0;
队列初始化成功!
}//InitQueue()end
/*求顺序队列长度*/
intQueueLength(SqQueue*Q)//QueueLength()sub-function
{return((Q->
rear-Q->
front+MAXQSIZE)%MAXQSIZE);
/*在顺序队列尾插入新元素*/
intEnQueue(SqQueue*Q,QElemTypee)//EnQueue()sub-function
{if((Q->
rear+1)%MAXQSIZE==Q->
front)
队列已满!
return(ERROR);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 队列 实现 应用
![提示](https://static.bdocx.com/images/bang_tan.gif)