seqlist.cpp
文件
主要包括对顺序表的基本操作,如初始化、插入元素、销毁顺序表、查找元素等。
# include <iostream>
# include "seqlist.h"
using namespace std;
/*
初始化一个空的线性表
*/
Status InitSeqList(Sqlist* L)
{
L->elem = (Elemtype*)malloc(INIT_SIZE * sizeof(Elemtype)); // 分配存储空间
if (!L->elem)
{
return ERROR;
} // 如果分配内存失败,操作失败,否则对结构体的成员变量初始化
L->length = 0;
L->size = INIT_SIZE;
return OK;
}
/*
销毁线性表
*/
Status DestroySeqList(Sqlist* L)
{
free(L->elem); // 释放指针所指的存储空间
L->length = 0; // 结构体内元素置零
L->size = 0;
return OK;
}
/*
清空线性表
*/
Status ClearSeqList(Sqlist *L)
{
L->length = 0; // ???????
return OK;
}
/*
判断线性表是否为空
*/
Status isSeqEmpty(Sqlist L)
{
if (L.length == 0)
return TRUE;
else
return FALSE;
}
/*
获取长度
*/
int getLength(const Sqlist L)
{
return L.length;
}
/*
插入元素
*/
Status InsertElem(Sqlist* L, int i, Elemtype e)
{
Elemtype *newelem;
if (i < 1 || i > L->length + 1)
{
cout << "Wrong position" << endl;
return ERROR;
}
// 如果内存空间不足,需要添加内存
if (L->length >= L->size)
{
newelem = (Elemtype*)realloc(L->elem, (L->size + INC