实验一 顺序表、单链表基本操作的实现
l 实验目的
1、顺序表
(1)掌握线性表的基本运算。
(2)掌握顺序存储的概念,学会对顺序存储数据结构进行操作。
(3)加深对顺序存储数据结构的理解,逐步培养解决实际问题的编程能力。
l 实验内容
1、 顺序表
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)遍历并输出线性表;
l 实验结果
1、顺序表
(1)流程图
(2)程序运行主要结果截图
(3)程序源代码
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct LinearList/*定义线性表结构*/
{
int *list; /*存线性表元素*/
int size; /*存线性表长度*/
int Maxsize; /*存list数组元素的个数*/
};
typedef struct LinearList LIST;
void InitList(LIST *L,int ms)/*初始化线性表*/
{
if((L->list=(int*)malloc(ms*sizeof(int)))==NULL)
{
printf("内存申请错误");
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<L->size;i++)
printf("%d",L->list[i]);
printf("\n");
}
int FindList(LIST *L,int item)