验证性试验三.docVIP

  • 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;

文档评论(0)

1亿VIP精品文档

相关文档