建立一个存储正整数的、10个节点的顺序表(一维数组),实现顺序表的输出、插入节点、删除节点的操作。
来源:百度知道 编辑:UC知道 时间:2024/06/12 22:49:00
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{
// 构造一个空的线性表L。
L.elem = new ElemType[LIST_INIT_SIZE];
if (!L.elem) return OVERFLOW; // 存储分配失败
L.length = 0; // 长度为0
L.listsize = LIST_INIT_SIZE; // 初始存储容量
return OK;
} // InitList_Sq
Status ListInsert_Sq(SqList &L, int i, ElemType e)
{
ElemType *p,*q;
if (i < 1 || i > L.length+1) return ERROR;
q = &(L.elem[i-1]); // q指示插入位置
for (p = &(L.elem[L.length-1]); p >= q; --p)
*(p+1) = *p;
// 插入位置及之后的元素右移
*q = e; // 插入e
++L.length; // 表长增1
return OK;
} // ListInser