線性表的順序表示和實現: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);

    for(int k=L->length;k>i-1;k--){
        L->elem[k]=L->elem[k-1];
    }
    L->elem[i-1]=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;

    *e=L->elem[i-1];
    for(int k=i;k<L->length;k++){
        L->elem[k-1]=L->elem[k];
    }
    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));

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章