2.1 线性表的顺序表示和实现
线性表的顺序表示是指用一组地址连续的存储单元依次存储线性表的数据元素
设线性表每个元素需占用个存储单元
线性表第i+1个数据元素的存储位置和第i个数据元素存储位置关系是
线性表第i个数据元素 的存储位置是
2.1.1 动态数组
数组具有随机存取(即直接访问)的特点,又线性表长度可变,故使用动态数组实现线性表的顺序存储结构
#define LISTINITSIZE 1000 // 线性表存储空间的初始分配量
typedef int Status;
typedef struct Contiguous_List{
ElemType* elem; // 存储空间基址
int length; // 当前线性表长度
int lsize; // 当前分配的存储容量
}SeqList;
2.1.2 线性表的初始化:构造一个空的线性表
伪代码:
def InitList(SeqList& L):
L.elem = new ElemType[LISTINITSIZE]
if !L.elem:
exit(OVERFLOW)
L.length = 0
L.lsize = LISTINITSIZE
2.1.3 线性表的销毁
伪代码:
def DestroyList(SeqList &L):
delete []L.elem
L.elem = nullptr
L.length = 0
L.lsize = 0
2.1.4 线性表的清空
伪代码:
def ClearList(SeqList &L):
L.length = 0
2.1.5 线性表判空
伪代码:
def ListEmpty(L):
return L.length>0 ? 1 : 0
2.1.6 获取线性表长度
伪代码:
def ListLength(L):
return L.length
2.1.7 获取线性表元素
伪代码:
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
// Status是函数的类型,其值是函数结果状态代码
typedef int Status;
typedef struct Contiguous_List{
Status* elem;
}SeqList;