实验二 顺序表的实现
1、实验目的:
(1)掌握线性表的基本概念。
(2)掌握线性表的顺序存储结构。
(3)掌握顺序表的常用操作的实现。
2、实验环境与设备:
已安装Visual Studio 2010(或其以上版本)集成开发环境的计算机。
3、实验原理:
(1)在理解顺序表的基础上,实现顺序表的常用操作。
(2)测试顺序表,并能够利用实现的顺序表解决实际问题。
4、实验内容:
顺序表包含了初始化、判空、求长、销毁、定位、删除、插入等基本操作。
试完成以下工作:
(1)根据程序的需要定义顺序表结构体(可定义成一个头文件,参考代码如下)。
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define FALSE -1
#define TURE 1
#define ERROR -1
#define OVERFLOW -1
#define INFEASIBLE -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef int ElemType;
typedef struct {
ElemType *elem;
int length;
int listsize;
}Sqlist;
(2)实现顺序表的常见操作。
//1、初始化
Status InitList(Sqlist *L)
//2、销毁
Status Destorylist(Sqlist *L)
//3、清空
Status ClearList(Sqlist *L)
//4、判空
Status EmptyList(Sqlist L)
//5、求表长
int LengthList(Sqlist L)
//6、取元素
Status GetElem(Sqlist L, int i, ElemType *e)
// 7、定位操作
int LocateELem(Sqlist L, ElemType e)
//8、求前驱
Status PriorElem(Sqlist L, ElemType cur_e, ElemType *pre_e)
//9、求后继
Status NextElem(Sqlist L, ElemType cur_e, ElemType *next_e)
//10、插入
Status InsertElem(Sqlist *L, int i, ElemType e)
//11、删除
Status DeleteElem(Sqlist *L, int i, ElemType *e)
//12、遍历
void TraverList(Sqlist L)
//13、比较
Status CompareList(Sqlist L, ElemType e1, ElemType e2)
(3)测试程序
测试顺序表的上述13种操作。
5、实验考核:
(1)完成纸质版实验报告
(2)提交电子版作业
6、执行结果示例如下:
实验源码
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define FALSE -1
#define TURE 1
#define ERROR -1
#define OVERFLOW -1
#define INFEASIBLE -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef int ElemType;
typedef struct {
ElemType *elem;
int length;//当前长度
int listsize;//总长度
}Sqlist;
//1、初始化
Status InitList(Sqlist *L){
L->elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L->elem) exit(OVERFLOW);
//return ERROR; //exit(OVERFLOW) //存储分配失败
//分配成功
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
//2、销毁
Status Destorylist(Sqlist *L){
if (!L->elem) return OK;//无地址
L->length = 0;
L->listsize = 0;
free(L->elem);
}
//3、清空
Status ClearList(Sqlist *L){
if (!L->elem) return ERROR;//无地址
if (L->length == 0)return OK;//列表为空
//不为空
L->length = 0;
}
//4、判空
Status EmptyList(Sqlist L){
if (!L.elem) return ERROR;//无地址
return(L.length == 0);
}
//5、求表长
int LengthList(Sqlist L){
if (!L.elem) return ERROR;
return L.length;
}
//6、取元素
Status GetElem(Sqlist L, int i, ElemType *e){
if (!L.elem) return ERROR;//无地址
if (L.length == 0) //线性表中无元素
return ERROR;
else if (i<1 || i > L.length) // 取的元素不存在
return ERROR;
*e = L.elem[i - 1];
return OK;
}
// 7、定位操作
int LocateELem(Sqlist L, ElemType e){
if (!L.elem) return ERROR;//无地址
if (EmptyList(L))return ERROR;//列表为空
//不为空
int i = 0;
while (i < L.length)
{
if (e == L.elem[i])break;
i++;
}
if (i <= L.length) return i + 1;
else return ERROR; //数据不存在
}
//8、求前驱
Status PriorElem(Sqlist L, ElemType cur_e, ElemType *pre_e){
if (!L.elem) return ERROR;//无地址
if (L.length == 0) return ERROR; //线性表中无元素
int cur = LocateELem(L, cur_e);
if (cur - 1)
{
GetElem(L, cur - 1, pre_e);
return OK;
}
else return FALSE;
}
//9、求后继
Status NextElem(Sqlist L, ElemType cur_e, ElemType *next_e){
if (!L.elem) return ERROR;//无地址
if (L.length == 0) return ERROR; //线性表中无元素
int cur = LocateELem(L, cur_e);
if (cur + 1<L.length)
{
GetElem(L, cur + 1, next_e);
return OK;
}
else return FALSE;
}
//10、插入
Status InsertElem(Sqlist *L, int i, ElemType e){
// i的合法值为 1 <= i <= L.length + 1
if (!L->elem) return ERROR;//无地址
if (i < 1 || i > L->length + 1) return ERROR; //位置i不合法
if (L->length >= L->listsize)
{ //当前存储空间已满,增加分配
ElemType *newbase;
newbase = (ElemType*)realloc(L->elem, (L->listsize + LISTINCREMENT)* sizeof(ElemType));
if (!newbase) exit(OVERFLOW); //return ERROR; //空间分配失败
L->elem = newbase; // 新基址
L->listsize += LISTINCREMENT;
}
ElemType *q, *p;
q = &(L->elem[i - 1]);
for (p = &(L->elem[L->length - 1]); p >= q; --p){
*(p + 1) = *p;
}
*q = e;
++L->length;
return OK;
}
//11、删除第i个元素
Status DeleteElem(Sqlist *L, int i, ElemType *e){
ElemType *q, *p;
if (!L->elem) return ERROR;//无地址
if (L->length == 0) return ERROR; //线性表中无元素
else if (i<1 || i > L->length) // 被删除的元素不存在
return ERROR;
p = &(L->elem[i - 1]);
*e = *p;
q = L->elem + L->length - 1;
for (++p; p <= q; ++p)
*(p - 1) = *p;
--L->length;
return OK;
}
//12、遍历
void TraverList(Sqlist L){
// if (!L.elem) return ERROR;//无地址
//if (L.length == 0) return ERROR; //线性表中无元素
int i;
if (EmptyList(L)) return;
for (i = 0; i < L.length; i++){
if (i != L.length - 1) printf("%d,", L.elem[i]);
else printf("%d", L.elem[i]);
}
printf(">\n");
}
//13、比较
Status CompareList(Sqlist L1, Sqlist L2){
if (!L1.elem&&!L2.elem) return ERROR;
int tra = L1.length - L2.length;
if (tra>0){
for (int i = 0; i < L2.length; i++){
if (L1.elem[i]>L2.elem[i]) return TURE;
if (L1.elem[i]<L2.elem[i]) return FALSE;
}//end for
}//end if
if (tra <= 0){
for (int i = 0; i < L1.length; i++){
if (L1.elem[i]>L2.elem[i]) return TURE;
if (L1.elem[i]<L2.elem[i]) return FALSE;
}//end for
}
}
void bianli(Sqlist L)
{
printf("顺序表遍历结果:<");
TraverList(L);
}
void isEmpty(Sqlist L)
{
if (EmptyList(L))
{
printf("顺序表是:空表\n");
}
else
{
printf("顺序表是:非空表\n");
}
}
int main()
{
Sqlist a;
InitList(&a);
isEmpty(a);
InsertElem(&a, 1, 5); InsertElem(&a, 2, 3); InsertElem(&a, 3, 100);
InsertElem(&a, 4, 1); InsertElem(&a, 5, 2);
InsertElem(&a, 6, 4); InsertElem(&a, 7, 6);
bianli(a);
isEmpty(a);
printf("顺序表长度= %d\n", LengthList(a));
int x1 = 100;
printf("取得的顺序表元素= %d\n", x1);
printf("元素位于%d号位置\n", LocateELem(a, x1));
int x2 = 1;
if (DeleteElem(&a, LocateELem(a, 1), &x2)) printf("删除的元素= %d\n", x2);
bianli(a);
isEmpty(a);
printf("顺序表长度= %d\n", LengthList(a));
int x3 = 6;
ElemType pre, nex;
if (PriorElem(a, x3, &pre) != -1)
{
printf("\t %d的直接前驱元素是:%d\n", x3, pre);
}
else
{
printf("\t %d无直接前驱元素\n", x3);
}
if (NextElem(a, x3, &nex) != -1)
{
printf("\t %d的直接后继元素是:%d\n", x3, nex);
}
else
{
printf("\t %d无直接后继元素\n", x3);
}
return 0;
}```