数据结构实验报告
想必学计算机专业的同学都知道数据结构是一门比较重要的课程,那么,下面是本站小编给大家整理收集的数据结构实验报告,供大家阅读参考。
数据结构实验报告1
一、实验目的及要求
1)掌握栈和队列这两种特殊的线性表,熟悉它们的特性,在实际问题背景下灵活运用它们。
本实验训练的要点是“栈”和“队列”的观点;
二、实验内容
1) 利用栈,实现数制转换。
2) 利用栈,实现任一个表达式中的语法检查(选做)。
3) 编程实现队列在两种存储结构中的基本操作(队列的初始化、判队列空、入队列、出队列);
三、实验流程、操作步骤或核心代码、算法片段
顺序栈:
Status InitStack(SqStack &S)
{
=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!)
return ERROR;
=;
ksize=STACK_INIT_SIZE;
return OK;
}
Status DestoryStack(SqStack &S)
{
free();
return OK;
}
Status ClearStack(SqStack &S)
{
=;
return OK;
}
Status StackEmpty(SqStack S)
{
if(==)
return OK;
return ERROR;
}
int StackLength(SqStack S)
{
return ;
}
Status GetTop(SqStack S,ElemType &e)
{
if(>=ksize)
{
=(ElemType *)realloc(,(ksize+STACKINCREMENT)*sizeof(ElemType));
if(!) return ERROR;
=+ksize;
ksize+=STACKINCREMENT;
}
*++=e;
return OK;
}
Status Push(SqStack &S,ElemType e)
{
if(>=ksize)
{
=(ElemType *)realloc(,(ksize+STACKINCREMENT)*sizeof(ElemType));
if(!)
return ERROR;
=+ksize;
ksize+=STACKINCREMENT;
}
*++=e;
return OK;
}
Status Pop(SqStack &S,ElemType &e)
{
if(==)
return ERROR;
e=*;
return OK;
}
Status StackTraverse(SqStack S)
{
ElemType *p;
p=(ElemType *)malloc(sizeof(ElemType));
if(!p) return ERROR;
p=;
while(p!=)//上面一个...
{
p--;
printf("%d ",*p);
}
return OK;
}
Status Compare(SqStack &S)
{
int flag,TURE=OK,FALSE=ERROR;
ElemType e,x;
InitStack(S);
flag=OK;
printf("请输入要进栈或出栈的元素:");
while((x= getchar())!='#'&&flag)
{
switch (x)
{
case '(':
case '[':
case '{':
if(Push(S,x)==OK)
printf("括号匹配成功!nn");
break;
case ')':
if(Pop(S,e)==ERROR || e!='(')
{
printf("没有满足条件n");
flag=FALSE;
}
break;
case ']':
if ( Pop(S,e)==ERROR || e!='[')
flag=FALSE;
break;
case '}':
if ( Pop(S,e)==ERROR || e!='{')
flag=FALSE;
break;
}
}
if (flag && x=='#' && StackEmpty(S))
return OK;
else
return ERROR;
}
链队列:
Status InitQueue(LinkQueue &Q)
{
t ==
(QueuePtr)malloc(sizeof(QNode));
if (!t) return ERROR;
t->next = NULL;
return OK;
}
Status DestoryQueue(LinkQueue &Q)
{
while(t)
{
=t->next;
free(t);
t=;
}
return OK;
}
Status QueueEmpty(LinkQueue &Q)
{
if(t->next==NULL)
return OK;
return ERROR;
}
Status QueueLength(LinkQueue Q)
{
int i=0;
QueuePtr p,q;
p=t;
while(p->next)
{
i++;
p=t;
q=p->next;
p=q;
}
return i;
}
Status GetHead(LinkQueue Q,ElemType &e)
{
QueuePtr p;
p=t->next;
if(!p)
return ERROR;
e=p->data;
return e;
}
Status ClearQueue(LinkQueue &Q)
{
QueuePtr p;
while(t->next )
{
p=t->next;
free(t);
t=p;
}
t->next=NULL;
->next=NULL;
return OK;
}
Status EnQueue(LinkQueue &Q,ElemType e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof (QNode));
if(!p)
return ERROR;
p->data=e;
p->next=NULL;
->next = p;
=p; //p->next 为空
return OK;
}
Status DeQueue(LinkQueue &Q,ElemType &e)