- 2
- 0
- 约1.52万字
- 约 22页
- 2018-03-11 发布于河南
- 举报
验证性试验三
实验三 栈的实现及应用
专业班级: 10网络工程班 学号: 32 姓名: 胡超
实验时间: 10.30 实验地点: K4-102 指导教师:祁文青
一、实验目的
1、掌握栈和队列的顺序存储结构和链式存储结构,以便在实际背景下灵活运用。
2、掌握栈和队列的特点,即先进后出与先进先出的原则。
3、掌握栈和队列的基本操作实现方法
二、实验内容
下面是栈和队列的部分基本操作实现的算法,请同学们自己设计主函数和部分算法,调用这些算法,完成下面的实验任务。
1、实现栈的顺序存储#include stdio.h
# include malloc.h
# include iostream.h
# include conio.h
# define STACK_INIT_SIZE 100
# define STACKINCREMENT 10
# define OK 1
# define ERROR 0
typedef int SElemType;
typedef struct //define structure SqStack()
{ SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
int InitStack(SqStack S,int top) //InitStack() sub-function
{ int i,e;
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)
{ coutendlAllocate space failure !;
return (ERROR);
}
S.top=S.base;
cout输入栈中的值:endl;
for(i=0;i3;i++)
{cine;
*S.top++=e;
}
S.stacksize=STACK_INIT_SIZE;
return (top);
} //InitStack() end
int output(SqStack S,int top)
{ int i;int *p;
for(i=0,p=S.top-1;p+1!=S.base;i++) //output the new SqStack
{cout输出栈中的第i+1个值:;
cout*p-- endl;
}
return(0);
}
void main() //main() function
{ SqStack S;
int top;
coutendlendlInitStack.cppendl;
InitStack( S,top);
output(S,top);
}
实验结果:
2.栈的链式存储结构
//Pop_L.cpp
//This program is to pop LinkStack
# include malloc.h
# include iostream.h
# include conio.h
# define Stack_Length 6
# define OK 1
# define ERROR 0
typedef int SElemType;
typedef struct SNode //define structure LinkStack
{ SElemType data;
struct SNode *next;
}SNode,*LinkStack;
int Pop_L(LinkStack top,SElemType e) //Pop_L() sub-function
{ SNode *q;
if(!top-next)
{ coutendlErrer! Its an empty LinkStack !;
return (ERROR);
}
e=top-next-data;
q=top-next;
top-next=q-next;
free(q);
return (OK);
} //Pop_L() end
void main() //main function
{ SElemType e,i;
SNode node[Stack_Length];
SNode *p,*top=node;
top=(LinkStack)malloc(sizeof(SNode));
top-next=NULL;
您可能关注的文档
最近下载
- SY-T 5504.3-2008 油井水泥外加剂评价方法 第3部分:减阻剂.pdf VIP
- 【三年级数学】每日口算 30 天.pdf VIP
- 物资采购应急方案.docx VIP
- 餐饮服务与管中职全套完整教学课件.pptx
- 第四版国际压力性损伤溃疡预防和治疗临床指南解读PPT课件.pptx VIP
- 1209 六年级(上)英语知识点汇总(外研版).docx VIP
- ICP-MS介绍讲课课件.ppt VIP
- 6篇2025年度民主生活会“五个带头”对照检查材料(参考范文).docx VIP
- DBJT_15-138-2018_建筑电气防火检测_技术规程-全国各省建筑标准.pdf VIP
- j12ZG604-全国各省建筑标准.pdf VIP
原创力文档

文档评论(0)