大数据结构实验报告材料二Word格式文档下载.docx
《大数据结构实验报告材料二Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《大数据结构实验报告材料二Word格式文档下载.docx(15页珍藏版)》请在冰点文库上搜索。
【二、实验教师对报告的最终评价及处理意见】
实验成绩:
(涂改无效)
指导教师签名:
张振领2016年月日
注:
要将实验项目、实验课程的成绩评定及课程考核办法明确告知学生,并报实验管理中心备案
【三、实验预习】
实验目的和要求:
1、熟练掌握栈和队列的结构,以及这种数据结构的特点;
2、会定义顺序栈、循环队列,能实现栈、队列的基本操作;
3、会利用栈解决典型问题,如数制转换等。
实验内容和原理或涉及的知识点:
用C语言设计实现栈的初始化、入栈、出栈、判空等功能,并利用栈完成数制转换功能;
设计实现循环队列的定义、初始化、入队、出队、求队列长度等功能。
实验条件:
具有C语言集成开发环境的计算机
实验设计方案:
栈设计的算法有:
1、初始化栈;
2、入栈;
3、出栈;
4、判断栈是否为空;
5、十进制转换为八进制。
队列设计的算法有:
1、初始化;
2、入队;
3、出队;
4、求队列长度。
实验预习成绩(涂改无效)
合格□
不合格□
【四、实验过程、数据和实验结果记录】
实验方法、步骤、操作过程的记录描述或程序代码。
实验过程中输入/输出数据、程序运行结果的记录。
(可加附页)
1、根据实验预习阶段的实验设计方案,编写顺序栈的伪C代码如下。
typedefstruct{
SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
StatusInitStack(SqStack&
S){
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!
S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
returnOK;
}//InitStack
StatusPush(SqStack&
S,SElemTypee){
if(S.top-S.base>
=S.stacksize)//栈满
{S.base=(SElemType*)realloc
(S.base,(S.stacksize+STACKINCREMENT)
*sizeof(SElemType));
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}//if
*S.top++=e;
}//Push
StatusPop(SqStack&
S,SElemType&
e){
if(S.top==S.base)returnERROR;
e=*--S.top;
}//Pop
StatusStackEmpty(SqStackS)
{
if(S.base==S.top)
returnTRUE;
returnFALSE;
}
voidconversion(){
InitStack(S);
scanf("
%d"
&
N);
while(N){
Push(S,N%8);
N=N/8;
}
while(!
StackEmpty(S)){
Pop(S,e);
printf("
e);
}//conversion
2、将算法细化为程序代码。
#include<
stdio.h>
stdlib.h>
#defineLIST_INIT_SIZE10
#defineLISTINCREMENT100
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
#defineOVERFLOW-2
typedefintStatus;
typedefintSElemType;
typedefstruct
}SqStack;
StatusInitStack(SqStack*S);
StatusPush(SqStack*S,SElemTypee);
StatusPop(SqStack*S,SElemType*e);
StatusStackEmpty(SqStackS);
voidconversion();
intmain()
printf("
Pleaseinputanumbertoconver:
\n"
);
conversion();
return0;
StatusInitStack(SqStack*S)
S->
base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
S->
base)exit(OVERFLOW);
top=S->
base;
stacksize=STACK_INIT_SIZE;
StatusPush(SqStack*S,SElemTypee)
if(S->
top-S->
base>
=S->
stacksize)//栈满
{
base=(SElemType*)realloc
(S->
base,(S->
stacksize+STACKINCREMENT)
base+S->
stacksize;
stacksize+=STACKINCREMENT;
*S->
top++=e;
}//Push
StatusPop(SqStack*S,SElemType*e)
if(S->
top==S->
base)returnERROR;
*e=*--S->
top;
}//Pop
if(S.base==S.top)
voidconversion()
SqStackS;
intN,e;
InitStack(&
S);
while(N)
Push(&
S,N%8);
StackEmpty(S))
Pop(&
S,&
e);
printf("
e);
3、编译、链接、运行程序。
4、循环队列的伪C代码如下:
#defineMAXQSIZE100//最大队列长度
typedefstruct{
QElemType*base;
//动态分配存储空间
intfront;
//头指针,若队列不空,
//指向队列头元素
intrear;
//尾指针,若队列不空,指向
队列尾元素的下一个位置
}SqQueue;
StatusInitQueue(SqQueue&
Q){
//构造一个空队列Q
Q.base=(ElemType*)malloc
(MAXQSIZE*sizeof(ElemType));
Q.base)exit(OVERFLOW);
//存储分配失败
Q.front=Q.rear=0;
StatusEnQueue(SqQueue&
Q,ElemTypee){
//插入元素e为Q的新的队尾元素
if((Q.rear+1)%MAXQSIZE==Q.front)
returnERROR;
//队列满
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
StatusDeQueue(SqQueue&
Q,ElemType&
e){
//若队列不空,则删除Q的队头元素,
//用e返回其值,并返回OK;
否则返回ERROR
if(Q.front==Q.rear)returnERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
intQueueLength(SqQueueQ)
return(Q.rear-Q.front+MAXSIZE)%MAXSIZE;
5、将算法细化成程序代码:
#definetrue1
#definefalse0
#defineOPSETSIZE7
#defineMAXQSIZE100
typedefintElemType;
typedefintQElemType;
typedefstructQNode
QElemTypedata;
structQNode*next;
}QNode,*QueuePtr;
QueuePtrfront;
QueuePtrrear;
}LinkQueue;
StatusInitQueue(LinkQueue*Q);
StatusDestoryQueue(LinkQueue*Q);
StatusPush(LinkQueue*Q,QElemTypee);
StatusPop(LinkQueue*Q,QElemType*e);
LinkQueueQ;
QElemTypee;
InitQueue(&
Q);
Q,1);
Q,2);
Q,3);
Q,4);
Push(&
\nPush(&
while(Pop(&
Q,&
e))
Pop(&
\ne=%d\n"
DestoryQueue(&
StatusInitQueue(LinkQueue*Q)
Q->
front=Q->
rear=(QueuePtr)malloc(MAXQSIZE*sizeof(QNode));
if(!
Q->
front)
exit(OVERFLOW);
front->
next=NULL;
StatusDestoryQueue(LinkQueue*Q)
while(Q->
rear=Q->
next;
free(Q->
front);
rear;
StatusPush(LinkQueue*Q,QElemTypee)
QueuePtrp=(QueuePtr)malloc(sizeof(QNode));
p)
p->
data=e;
rear->
next=p;
rear=p;
StatusPop(LinkQueue*Q,QElemType*e)
if(Q->
front==Q->
rear)
QueuePtrp=Q->
*e=p->
data;
next=p->
rear==p)
front;
free(p);
6、编译、链接、运行程序。
实验数据和实验结果记录
栈程序的一个运行实例如下。
队列的一个运行实例如下:
记录成绩(涂改无效)
【五、实验结果分析】
1、分析数制转换时后进先出的特点;
2、分析如果将数转换为二进制,conversion函数的修改;
3、分析如果没有初始化栈的操作时程序的运行结果;
3、写出自己的心得体会。