线性表的顺序表示和实现:sqlist的第二种写法

#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef int Status;
typedef int ElemType;
typedef struct {
    ElemType *elem;
    int length;
    int listsize;
}SqList;
//操作结果:构造一个空的线性表L
Status InitList(SqList *L){
    L->elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L->elem) exit(OVERFLOW);
    L->length = 0;
    L->listsize = LIST_INIT_SIZE;
    return OK;
}

Status ResizeList(SqList *L,int increment){
    ElemType *newbase = (ElemType*)realloc(L->elem,(L->listsize+increment)*sizeof(ElemType));
    if(!newbase) exit(OVERFLOW);

    L->elem = newbase;
    L->listsize += increment;
    return OK;
}

//初始条件:线性表L已存在
//操作结果:销毁线性表L
Status DestroyList(SqList *L){
    if(0==L->listsize) return ERROR;
    free(L->elem);
    L->elem = NULL;
    L->length = 0;
    L->listsize = 0;
    return OK;
}
//初始条件:线性表L已存在
//操作结果:将L重置为空表
Status clearList(SqList *L){
    if(0==L->listsize) return ERROR;
    L->length = 0;
    return OK;
}
//初始条件:线性表L已存在
//操作结果:若L为空表,则返回TRUE,否则返回FALSE
Status ListEmpty(SqList L){
    return (L.length>0) ? FALSE : TRUE;
}
//初始条件:线性表L已存在
//操作结果:返回L中数据元素个数
Status ListLength(SqList L){
    return L.length;
}
//初始条件:线性表L已存在,1<=i<=ListLength(L)
//操作结果:用e返回L中第i个数据元素的值
Status GetElem(SqList L,int i,ElemType *e){
    if(i<1 || i>ListLength(L)) return ERROR;
    *e = L.elem[i-1];
    return OK;
}
int my_compare(ElemType a,ElemType b){
    return a==b;
}
//初始条件:线性表L已存在,compare()就数据元素判定函数
//操作结果:返回L中第一个给与e满足关系compare()的数据元素的位序。若这样的数据元素不存在,则返回值为0。
Status LocateElem(SqList L,ElemType e,int (*compare)(ElemType,ElemType)){
    for(int i=1;i<=L.length;i++){
        if(compare(L.elem[i-1],e)) return i;
    }
    return 0;
}

//初始条件:线性表L已存在
//操作结果:若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,否则操作失败,pre_e无定义
Status PriorElem(SqList L,ElemType cur_e,ElemType *pre_e){
    int i = LocateElem(L,cur_e,my_compare);
    if(i>1){
        *pre_e = L.elem[i-2];
        return TRUE;
    }else{
        return FALSE;
    }
}
//初始条件:线性表L已存在
//操作结果:若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败,next_e无定义
Status NextElem(SqList L,ElemType cur_e,ElemType *next_e){
    int i = LocateElem(L,cur_e,my_compare);
    if(i>0 && i<L.length){
        *next_e = L.elem[i];
        return TRUE;
    }else{
        return FALSE;
    }
}
//初始条件:线性表L已存在,1<=i<=ListLength(L)+1
//操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1
Status ListInsert(SqList *L,int i,ElemType e){
    if(i<1 || i>L->length+1) return ERROR;

    //若存储空间已满,需开辟新空间
    if(L->length*sizeof(ElemType)>=L->listsize) ResizeList(L,LISTINCREMENT);

    ElemType *q=&(L->elem[i-1]);
    for(ElemType *p=&(L->elem[L->length-1]);p>=q;p--){
        *(p+1)=*p;
    }
    *q=e;
    L->length++;
    return OK;
}
//初始条件:线性表L已存在
//操作结果:删除L的第i个数据元素,并用e返回其值,L的长度减1
Status ListDelete(SqList *L,int i,ElemType *e){
    if(i<1 || i>L->length) return ERROR;

    ElemType *p =&(L->elem[i-1]);
    *e = *p;
    ElemType *q=&(L->elem[L->length-1]);

    while(p<q){
        *p=*(p+1);
        p++;
    }
    L->length--;
    return OK;
}
//初始条件:线性表L已存在
//操作结果:依次对L的每个数据元素调用函数visit()。一旦visit()失败,则操作失败。
Status ListTraverse(SqList L,void visit(ElemType)){
    for(int i=0;i<L.length;i++) visit(L.elem[i]);
    return OK;
}

void my_print(ElemType e){
    printf("%d,",e);
}

int main()
{
    SqList L;
    ElemType e;
    printf("构造空List\n");
    InitList(&L);
    if(ListEmpty(L)) printf("List为空\n");
        else printf("List不为空\n");
    printf("向List填充数据元素\n");
    for(int i=1;i<11;i++){
        ListInsert(&L,L.length+1,i*10);
        //printf("%d,",i);
    }

    if(ListEmpty(L)) printf("List为空\n");
        else printf("List不为空\n");
    printf("打印List:");
    ListTraverse(L,my_print);
    printf("共%d个\n",ListLength(L));
    GetElem(L,6,&e);
    printf("第%d个元素为%d\n",6,e);


    ListDelete(&L,3,&e);
    printf("删除第%d个元素为%d\n",3,e);
    printf("打印List:");
    ListTraverse(L,my_print);
    printf("共%d个\n",ListLength(L));

    ListInsert(&L,3,33);
    printf("在第%d位插入元素为%d\n",3,33);
    printf("打印List:");
    ListTraverse(L,my_print);
    printf("共%d个\n",ListLength(L));

    e=60;
    int m=LocateElem(L,e,my_compare);
    if(m>0) printf("查找到值为%d的数据,在第%d个\n",e,m);
        else printf("查找不到值为%d的数据\n",e);

    ElemType next_e;
    GetElem(L,1,&e);
    printf("顺序打印List:%d,",e);
    while(NextElem(L,e,&next_e)){
        printf("%d,",next_e);
        e=next_e;
    }
    printf("\n");

    ElemType pre_e;
    GetElem(L,ListLength(L),&e);
    printf("逆序打印List:%d,",e);
    while(PriorElem(L,e,&pre_e)){
        printf("%d,",pre_e);
        e=pre_e;
    }
    printf("\n");

    clearList(&L);
    printf("清空List\n");
    if(ListEmpty(L)) printf("List为空\n");
        else printf("List不为空\n");

    printf("销毁List\n");
    DestroyList(&L);



    return 0;
}
发布了14 篇原创文章 · 获赞 1 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lzdelphi/article/details/104138542