#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
#define Status int
typedef int ElemType ;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList(SqList &L){
L.elem=(ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!L.elem) exit(1);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return 1;
}
Status Destory(SqList &L){
free(L.elem);
return 1;
}
Status ClearList(SqList &L){
L.elem=NULL;
L.length=0;
return 1;
}
Status IsListEmpty(SqList &L){
if(L.length=0) return 1;
else return 0;
}
Status ListLength(SqList &L){
return L.length;
}
Status GetElem(SqList &L,int i,ElemType &e){
if(i>=1&&i<=L.length){
e=L.elem[i-1];
return 1;
}
else
return 0;
}
//返回第一个与e相同的元素的位置,不存在返回0
Status LocateElem(SqList &L,ElemType e){
for(int i=0;i<L.length;i++){
if(L.elem[i]==e)
return (i+1);
}
return 0;
}
Status PrioElem(SqList &L,ElemType cur_e,ElemType &pre_e){
for(int i=1;i<L.length;i++){
if(L.elem[i]==cur_e){
pre_e=L.elem[i-1];
return 1;
}
}
return 0;
}
Status NextElem(SqList &L,ElemType cur_e,ElemType &next_e){
for(int i=0;i<L.length-1;i++){
if(L.elem[i]==cur_e){
next_e=L.elem[i+1];
return 1;
}
}
return 0;
}
Status ListInsert(SqList &L,int i,ElemType e){
ElemType *p,*q;
if(i<1||i>L.length+1) return 0;
if(L.length>=L.listsize){
L.elem=(ElemType *)realloc(L.elem,(L.listsize+LIST_INCREMENT)*sizeof(ElemType));
L.listsize+=LIST_INCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;p--) *(p+1)=*p;
*q=e;
L.length++;
return 1;
}
Status printList(SqList &L){
for(int i=0;i<L.length;i++)
printf("%d\n",L.elem[i]);
return 1;
}
int main(){
SqList l;
InitList(l);
ElemType e1,e2,e3,e4,e5;
e1=e2=e3=e4=e5=100;
ListInsert(l,1,e1);
ListInsert(l,2,e2);
ListInsert(l,3,e3);
ListInsert(l,4,e4);
ListInsert(l,5,e5);
printList(l);
return 0;
}
纯新手 有误请指出