- 6
- 0
- 约6.09千字
- 约 21页
- 2019-02-26 发布于广东
- 举报
2011 2012学年第一学期
合肥学院数理系
实验报告
课程名称: 数据结构
实验项目: 链表的基本运算
实验类别:综合性口 设计性口 验证性口
专业班级: 09数学(2)
姓 名: 张琼
学号: 0907022040
实验地点: 7#606
实验时间: 2011.10.28
指导教师:李旭 成 绩:
一.实验目的:练习关于链表的一些基本算法
二?实验内容:关于单链表的建立、置空、求表长、按序号取元素、 插入、删除等基本算法。
三.实验方案(程序设计说明)
按值查找程序
#include stdio.h
#include malloc.h
#define null 0;
typedef struct node{
int data;
struct node *next;
} linklist;
linklist *creatlisth()
{
linklist *l,*head,*s;
char ch;
l=(linklist *)malloc(sizeof(linklist));head=l;
l-next=NULL;
ch=getchar();
while(ch!=,\n)
{
s=(linklist *)malloc(sizeof(linklist)); s-data=ch;
s-next=l-next;
l-next=s;
ch=getchar();
}
return head;
}
linklist *lllocate (linklist x)
{
linklist *p;
p=l-next;
while(p!二NULL)
if(p-data!=x) p=p-next;
else
break;
return p;
void main()
linklist *l,*s;
l=creatlisth();
int x;
printfC请输入一个数\n”);
scanf(n%d*\x);
s=lllocate(l,x);
if(s二二NULL)
print查找成功J;
else
printfC喳找失败);
}
按序号取元素
#include stdio.h
#include malloc.h
#define null 0;
typedef struct node{
int data;
struct node *next;
} linklist;
linklist *creatlisth()
linklist *l,*head,*s;
char ch;
1 二(linklist *)malloc(sizeof(linklist));head=l;
l-next=NULL;
ch=getchar();
while(ch!=,\n,)
{
s=(linklist *)malloc(sizeof(linklist));
s-data=ch;
s-next=l-next;
l-next=s;
ch=getchar();
}
return head;
}
linklist *get(linklist *l,int i)
intj;
linklist *p;
p=l-next;
j=l;
while(p!=NULLji)
{
p=p-next;
j++;
}
return (p);
else
return 0;
}
void main()
{
linklist *l,*s;
l=creatlisth();
int i;
printf(n请输入一个序号\n”);
scanf(n%dn,i);
s=get(l,i);//s表示序号i在链表中的位置
printf(H%dn,s);
I空表及求表长程序
#include stdio.h #include malloc.h #define null 0; typedef struct node{ int data; struct node *next;
} linklist;
linklist *creatlisth()
{
linklist *l,*head,*s;
char ch;
1 二(linklist *)malloc(sizeof(linklist));head=l;
l-next=NULL;
ch=getchar();
while(ch!=,\n,)
{
s=(linklist *)malloc(sizeof(linklist));
s-data=ch;
s-next=l-next;
l-next=s;
ch=getchar();
}
return head;
}
linklist *setnull(linklist *1)
{
l-next=NULL;
return 1;
}
int lllength(linklist *1)
{
linklist *p;
int n二
原创力文档

文档评论(0)