数组实现线性表--王晓东《数据结构与算法设计》

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值