#include<stdio.h> #include<stdlib.h> #include<time.h> #define OK 1 #define 
ERROR -1 #define LISTSIZEMAX 10 #define LISTSIZE 5 typedef int ElemType; 
typedef struct { ElemType *elem; int length; int size; } List; //初始化 int 
CreatList(List *L) { L->elem = (ElemType*)malloc(sizeof(ElemType)*LISTSIZE); 
if(!(L->elem)) { return ERROR; } L->length = 0; L->size=LISTSIZEMAX; return OK; 
} //打印 int PrintfList(List *L) { if(L->length==0) { printf("顺序表为空!"); return 
ERROR; } int i; for(i=0; i<L->length; i++) { printf("%d ",L->elem[i]); } 
printf("\n"); } //添加元素 void AddListDate(List *L,int n) { int i; srand(time(0)); 
for(i=0; i<n; i++) { L->elem[i]=rand()%1000; L->length++; } PrintfList(L); } 
//插入元素 int ListInsert(List *L,int i,ElemType e) { int j,newsize; 
if(i<1||i>L->length+1) return ERROR; if(L->length>=L->size) { ElemType 
*newbase; 
newsize=(L->size+LISTSIZEMAX)*sizeof(ElemType);//(L.size+LISTSIZEMAX)个ElemType 
空间 
newbase=(ElemType*)realloc(L->elem,newsize);//指针名=(数据类型*)realloc(要改变内存大小的指针名,新的大小)。 
if(!(newsize)) return ERROR; L->elem=newbase; L->size+=LISTSIZEMAX; } 
for(j=L->length-1; i-1<=j; j--) L->elem[j+1]=L->elem[j]; L->elem[i-1]=e; 
L->length++; return OK; } //删除元素 int ListDelete(List*L,int i,int &e1){ int j; 
if((i<1)||(i>L->length)) return ERROR; e1=L->elem[i-1]; 
for(j=i;L->length-1>=j;j++) L->elem[j-1]=L->elem[j]; L->length--; return OK; } 
int main(void) { //初始化 int n; printf("请输入顺序表初始化数据个数:"); scanf("%d",&n); List L; 
CreatList(&L); AddListDate(&L,n); PrintfList(&L); //插入 int i; ElemType e; 
printf("请输入顺序表插入位置及插入数据:"); scanf("%d%d",&i,&e); ListInsert(&L,i,e); 
PrintfList(&L); //删除 int j,e1; printf("请输入顺序表删除位置:"); scanf("%d",&j); 
ListDelete(&L,j,e1); printf("删除数据为:%d\n",e1); PrintfList(&L); return 0; } 
插入算法:
int ListInsert(List *L,int i,ElemType e) { int j,newsize; 
if(i<1||i>L->length+1) return ERROR; if(L->length>=L->size) { ElemType 
*newbase; 
newsize=(L->size+LISTSIZEMAX)*sizeof(ElemType);//(L.size+LISTSIZEMAX)个ElemType 
空间 
newbase=(ElemType*)realloc(L->elem,newsize);//指针名=(数据类型*)realloc(要改变内存大小的指针名,新的大小)。 
if(!(newsize)) return ERROR; L->elem=newbase; L->size+=LISTSIZEMAX; } 
for(j=L->length-1; i-1<=j; j--) L->elem[j+1]=L->elem[j]; L->elem[i-1]=e; 
L->length++; return OK; } 
删除算法:
int ListDelete(List*L,int i,int &e1){ int j; if((i<1)||(i>L->length)) return 
ERROR; e1=L->elem[i-1]; for(j=i;L->length-1>=j;j++) L->elem[j-1]=L->elem[j]; 
L->length--; return OK;; } 
 打印算法:
int PrintfList(List *L) { if(L->length==0) { printf("顺序表为空!"); return ERROR; } 
int i; for(i=0; i<L->length; i++) { printf("%d ",L->elem[i]); } printf("\n"); } 
添加元素算法:
void AddListDate(List *L,int n) { int i; srand(time(0)); for(i=0; i<n; i++) { 
L->elem[i]=rand()%1000; L->length++; } PrintfList(L); }