///功能:线性表的顺序表示和实现
///版本:V1.0
///作者:苏嗣典
///日期:2011-12-1
#include<stdio.h>
#include<malloc.h> //注意这个分配地址的头文件必须得加
#define INITIAL_SIZE 100 //定义线性表的初始大小
#define INCREACEMENT 10 //定义线性表的分配增量
//定义一个线性表结构
struct List
{
int *Elem; //线性表的基址
int Length; //当前长度
int Allocate_size; //当前分配的容量
}*L;
//初始化线性表
void Init_List(List &L)
{
L.Elem=(int *)malloc(INITIAL_SIZE* sizeof(int)); //分配基地址
if (!L.Elem ) return;
L.Length =0; //初始长度为 0
L.Allocate_size =INITIAL_SIZE; //初始分配的大小为 100
}
//向线性表中插入一个数
void InsertInto_List(List &L,int i,int e)
{
int *p,*q;
if (i>L.Length+1 || i<1)return; //先判断要插入的序号在不在线性表长度之内
if(L.Length>=L.Allocate_size) //如果线性表的长度大于等于分配的大小时,需重新分
配线性表大小
{
int *newbase=(int *)realloc(L.Elem ,(L.Length +INCREACEMENT)*sizeof(int));
if (!newbase)return;
L.Elem =newbase; //基地址改变
L.Allocate_size +=INCREACEMENT; //分配的大小加倍
}
q=&(L.Elem [i-1]); //要插入的位序
for (p=&(L.Elem [L.Length-1]);p>=q;p--) //循环实现要插入位置以后的数都要后