顺序线性表 ListInsert 插入法实现代码
// sdsdf.cpp : Defines the entry point for the console application.
//
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <malloc.h> //malloc(),realloc()
#include <process.h> //exit()
#define LIST_INIT_SIZE 10 //线性表存储空间的初始分配量
#define LIST_INCREMENT 2 //线性表的存储空间分配增量
typedef int ElemType;
typedef int Status;
//线性表的动态分配顺序存储结构
struct SqList
{
ElemType *elem; //存储空间基地址
int length; //当前长度
int listsize; //当前分配的存储容量(以sizeof(ElemType)为单位)
};
//构造一个空的顺序线性表L
void InitList(SqList &L)
{
L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)
{
exit(OVERFLOW); //分配失败
}
L.length = 0; //当前长度
L.listsize = LIST_INIT_SIZE; //初始容量
}
Status ListInsert(SqList &L,int i,ElemType e)
{
//初始条件:顺序线性表已存在,1<=i<=ListLength(L)+1
//操作结果:在L中第一个位置前插入新的元素e,L的长度加 1
ElemType *newbase,*q,*p;
if(i<1||