//顺序表的基本操作代码如下:
#include <malloc.h>
#include <stdio.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int
typedef int Status;
typedef struct
{
int *elem;
int length;
int listsize;
} SqList;
Status InitList_Sq(SqList &L)
{ // 算法2.3
// 构造一个空的线性表L。
L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem)
return OK; // 存储分配失败
L.length = 0; // 空表长度为0
L.listsize = LIST_INIT_SIZE; // 初始存储容量
return OK;
} // InitList_Sq
Status ListInsert_Sq(SqList &L, int i, ElemType e)
{ // 算法2.4
// 在顺序线性表L的第i个元素之前插入新的元素e,
// i的合法值为1≤i≤ListLength_Sq(L)+1
ElemType *p;
if (i < 1 || i > L.length + 1)
return ERROR; // i值不合法
if (L.length >= L.listsize)
{ // 当前存储空间已满,增加容量
ElemType *newbase = (ElemType *)realloc(L.elem,
8578 顺序表逆置 SCAU 华南农业大学 数据结构实验(个人笔记 勿喷)
最新推荐文章于 2023-02-28 13:50:04 发布