- 14
- 0
- 约2.32千字
- 约 6页
- 2018-02-06 发布于河南
- 举报
实验报告栈
数据结构实验报告
----链栈
班级:信息管理与信息系统09-3班
教学班:数据结构C0001班
姓名:刘宗明
学号实验时间:2011年4月
实验目的
(1)掌握栈的基本概念。
(2)掌握链栈的建立、入栈和出栈等方法。
(3)根据具体问题的需要,设计出合理的表示数据的结构,并设计相关算法。
实验内容
编写算法实现下列问题的求解。
1初始化一个链栈。
2判断是否空栈。
3入栈
第一组数据:10,13,5,8,20,55
第二组数据:a,b,c,d,e,f,g
4取栈顶元素
5出栈
6将10进制数转换为16进制数
第一组数据:4
第二组数据:11
第三组数据:254
第四组数据:1357
算法实现
(一)实验中,各个程序基本上都要实现结点结构体定义、主函数实现,故首先将除特定程序所涉及到的具体算法外的公共程序部分(具体操作或有不同)写出如下:
(1)结点结构体定义:
typedef struct node
{
int data;
node *next; //链式栈的结点结构体定义
node *top;
} LSnode;
主函数实现:
void main()
{
定义变量;
调用所需函数; //不同实验要求有不同主函数实现
}
具体算法以及部分运行界面截图
1初始化一个链栈。
node *IniStack(node *L)
{
L-top=L;
L-next=NULL; //链式栈的初始化
return L;
}
2判断是否空栈。
int StackEmpty(node *L)
{
if(L-top-next==NULL) //判断栈是否为空的函数实现
return 1;
else return 0;
}
3入栈
第一组数据:10,13,5,8,20,55
第二组数据:a, b, c, d, e, f, g
node *Push_Stack(int i)
{
node *P,*L;
L=new node;
node *u;int x; P=L;
L-next=NULL;
printf(For the %d stack:,i);
puts(End your imput with the number -1!); //头插法创建链栈
cinx;
while(x!=-1)
{
u=new node;
u-data=x;
u-next=L-next;//键入值
L-next=u;
cinx;
}
L-top=P;
return P;
}
4取栈顶元素
void Stack_Top(node *L)
{
if(L-top-next==NULL)
printf(No items to be selected!Please check!\n); //取栈顶元素的实现
else
printf(The top number is:%d\n,L-top-next-data);
}
5出栈
void Pop_Stack(node *L)
{ node *P,*u;P=L;
if(L-next==NULL)
printf(栈为空!无法出栈!);
else
{
u=new node;
u=L-next;
L-next=u-next;
delete u;
}
L=L-next;
puts(操作后的结果为:);
while(L!=NULL)
{
printf(%d ,L-data);
L=L-next;
}
puts();
}
6将10进制数转换为16进制数
第一组数据:4
第二组数据:11
第三组数据:254
第四组数据:1357
void DecToHex(int x)
{
node *P,*u,*t;int y;
P=new node;
u=P;
while(x!=0)
{
t=new node;
y=x%16;
t-data=y;
P-next=t;
P=t;
x=x/16;
}
P-next=NULL;
u=u-next;
puts(The result is(十六进制数):);
while(u!=NULL)
{
print
原创力文档

文档评论(0)