肇庆学院 计算机学院/软件学院
实 验 报 告
专业_软件工程_班级14软件2班 姓名_黎福喜_学号201424133225课程名称 数据结构 学年2015—2016 学期 1? / 2□ 课程类别 专业必修? 限选□ 任选□ 实践□ 评分: 批阅老师: 2015年 月 日
实验1 线性表的基本操作
? 实验目的
1、掌握线性表的基本一算。
2、掌握顺序存储的概念,学会对顺序存储数据结构进行操作。
3、加深对顺序存储数据结构的理解,逐步培养解决实际问题的编程能力。
? 实验内容
1、编写线性表举报操作函数
(1)InitList(LIST *L,int ms)初始化线性表;
(2)InsertList(LIST *L,int item,int rc)向线性表指定位置插入元素; (3)DeleteList1(LIST *L,int item)删除指定元素的线性表记录; (4)DeleteList2(LIST *L,int rc)删除删除指定位置的线性表记录; (5)FindList(LIST *L,int item)查找线性表中的元素; (6)OutputList(LIST *L)输出线性表元素。 2、调用上述函数实现下列操作: (1)初始化线性表;
(2)调用插入函数建立一个线性表; (3)在线性表中寻找指定的元素; (4)在线性表中删除指定值的元素; (5)在线性表中删除指定位置的元素; (6)遍历并输出线性表。
? 实验结果
1、流程图
添加元素 查找元素 按位置删除元素 按值删除元素 退出 返回主主菜单 显示线性表 2、程序运行主要结果截图
2、程序源代码
#include
typedef struct LinearList LIST;
void InitList(LIST *L,int ms)//线性表初始化 { if((L->list=(int *)malloc(ms*sizeof(int)))==NULL) { printf(\内存申请错误!\\n\ exit(1); } L->size=0; L->Maxsize=ms; }
int InsertList(LIST *L,int item,int rc)//插入 //item-记录值;rc-插入位置 {
int i;
if(L->size==L->Maxsize)
return -1; if(rc<0) rc=0; if(rc>L->size) rc=L->size;
for(i=L->size-1;i>=rc;i--) L->list[i+1]=L->list[i]; L->list[rc]=item; L->size++; return 0; }
void OutputList(LIST *L)//输出 {
int i;
for(i=0;i
printf(\ printf(\}
int DeleteList1(LIST *L,int item)//删除元素值的线性表记录 {
int i,n;
for(i=0;i
for(n=i;n
return -1; }
int DeleteList2(LIST *L,int rc)//删除指定位置的线性表记录 {
int n;
if(rc<0||rc>=L->size) return -1;
for(n=rc;n
int FindList(LIST *L,int item)//查找 {
int i;
for(i=0;i
int main() {
LIST LL; int i,r,choice;
printf(\ InitList(&LL,100);
printf(\ do{
printf(\ printf(\添加元素----------\\n\ printf(\按元素值查找元素--\\n\ printf(\按元素值删除元素--\\n\ printf(\按位置删除元素----\\n\ printf(\退出--------------\\n\ printf(\
printf(\请输入数字(0~4)选择你要进行的操作:\ fflush(stdin);
scanf(\ switch(choice){ case 1:
while(1)//添加元素
{
printf(\请输入元素值,输入0结束插入操作:\ fflush(stdin);//清空标准输入缓冲区 scanf(\ if(i==0) break;
printf(\请输入插入位置:\ scanf(\
InsertList(&LL,i,r-1); printf(\线性表为:\ OutputList(&LL); }
break; case 2:
while(1)//按元素值查找元素 {
printf(\请输入查找元素值,输入0结束查找操作:\ fflush(stdin); scanf(\ if(i==0) break;
r=FindList(&LL,i); if(r<0)
printf(\没找到\\n\ else
printf(\有符合条件的元素,位置为:%d\\n\ }
break; case 3:
while(1)//按元素值删除元素 {
printf(\请输入删除元素值,输入0结束操作:\ fflush(stdin); scanf(\ if(i==0) break;
r=DeleteList1(&LL,i); if(r<0)
printf(\没找到\\n\ else {
printf(\有符合条件的元素,位置为:%d\\n\ printf(\线性表为:\ OutputList(&LL); } }
break; case 4:
while(1)//按位置删除元素 {
printf(\请输入删除元素位置,输入0结束查找操作:\ fflush(stdin); scanf(\ if(r==0) break;
i=DeleteList2(&LL,r-1); if(i<0)
printf(\位置越界\\n\ else {
printf(\线性表为:\ OutputList(&LL); } }
break; case 0:break; }
}while(choice!=0);
printf(\感谢使用!再见!\ return 0; }
实验2 链表的基本操作
? 实验目的
1、掌握链表的概念,学会对链表进行操作。
2、加深对链式存储数据结构的理解,逐步培养解决实际问题的编程能力。 ? 实验内容
1、编写链表几把操作函数:
(1)InitList(LIST **p)初始化链表;
(2)InsertList1(LIST **p,int item,int rc)向链表的指定位置插入元素; (3)InsertList2(LIST **p,int item)向有序链表插入元素;
(4)DeleteList(LIST **p,int item)删除指定元素值的链表记录; (5)FindList(LIST *p,int item)查找链表中的元素; (6)OutputList(LIST *p)输出链表元素。 2、调用上述函数实现下列操作: (1)初始化链表;
(2)调用插入函数建立一个链表; (3)在链表中寻找指定的元素; (4)在链表中删除指定值的元素; (5)遍历并输出链表。 ? 实验结果
1、流程图
主菜单 指定位置追加 升序追加 查找结点 删除结点 输出链表 清空链表 退出
2、程序运行主要结果截图