arrList.h
自己的笔记,不喜勿喷,欢迎大佬指导
#include <malloc.h>
#define Error(module) fprintf(stderr,"error:"#module"\n")
typedef struct alist *List;
typedef struct alist{
int n;
int maxsize;
ListItem *table; //ListItem 为自己定义的数据类型,可在实现函数中定义 typedef int ListItem;
}Alist;
List ListInit(int size){
List L = malloc(sizeof(*L));
L->table = malloc(size*sizeof(ListItem));
L->maxsize = size;
L->n = 0;
return L;
}
int ListEmpty(List L) {
return L->n == 0;
}
int ListLength(List L){
return L->n;
}
ListItem ListRetrieve(int k, List L){ //查找在k处数据
if( k<1 || k>L->n ) Error("out of bounds.");
return L->table[k-1];
}
int ListLocate(ListItem x, List L) { //查找在x数据
int i;
for( i=0; i<L->n; i++ ) {
if( L->table[i]==x )
return ++i;
}
return 0;
}
void ListInsert(int k, ListItem x, List L) { //此处 k[0,L->n]
int i;
if(k<0 || k>L->n ) Error("out of bounds.");
if( L->n == L->maxsize ) Error("out of memory.");
for( i=L->n-1; i>=k; i++ ) {
L->table[i+1] = L->table[i];
}
L->table[k] = x;
L->n ++;
}
ListItem ListDelete(int k, List L) { //此处 k[1,L->n] 删除位置为k数据
int i;
ListItem x;
if( k<1 || k>L->n ) Error("out of bounds.");
x = L->table[k-1];
for( i=k; i<L->n; i++ )
L->table[i-1] = L->table[i];
L->n --;
return x;
}