線性表的順序表示和實現:sqlist完整代碼

#include <stdio.h>
#include <stdlib.h>
#include <time.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;
}
//初始條件:線性表L已存在,1<=i<=ListLength(L)
//操作結果:用e改寫L中第i個數據元素的值
Status SetElem(SqList L,int i,ElemType e){
    if(i<1 || i>ListLength(L)) return ERROR;
    L.elem[i-1] = e;
    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);
}

void Union(SqList *La,SqList Lb){
    int La_len = ListLength(*La);
    int Lb_len = ListLength(Lb);
    ElemType e;
    for(int i=1;i<=Lb_len;i++){
        GetElem(Lb,i,&e);
        if(!LocateElem(*La,e,my_compare))
            ListInsert(La,++La_len,e);
    }
}

//將鏈表按升序進行排序
void sortAscList(SqList L){
    ElemType e,f,min;
    int min_pos;
    int len = L.length;
    for(int i=1;i<=len;i++){
        GetElem(L,i,&e);
        min=e;
        for(int j=i+1;j<=len;j++){
            GetElem(L,j,&f);
            if(min>f){
                min=f;
                min_pos=j;
            }
        }

        if(min!=e){
            SetElem(L,i,min);
            SetElem(L,min_pos,e);
        }

    }
}

//已知線性表La和Lb中的數據元素按值非遞減排列
//歸併La和Lb得到新的線性表Lc,Lc的數據元素也按值非遞減排列
void MergeList(SqList La,SqList Lb,SqList *Lc){
    InitList(Lc);
    int alen=La.length;
    int blen=Lb.length;
    int i=1,j=1,k=0;
    ElemType ai,bj;
    while(i<=alen && j<=blen){
        GetElem(La,i,&ai);
        GetElem(Lb,j,&bj);

        if(ai<bj){
            ListInsert(Lc,++k,ai);
            i++;
        }else{
            ListInsert(Lc,++k,bj);
            j++;
        }
    }

    while(i<=alen){
        GetElem(La,i,&ai);
        ListInsert(Lc,++k,ai);
        i++;
    }

    while(j<=blen){
        GetElem(Lb,j,&bj);
        ListInsert(Lc,++k,bj);
        j++;
    }
}

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


    SqList La;
    InitList(&La);
    for(int i=1;i<11;i++){
        ListInsert(&La,La.length+1,i*10);
    }
    printf("打印La:");
    ListTraverse(La,my_print);
    printf("共%d個\n",ListLength(La));

    SqList Lb;
    InitList(&Lb);
    for(int i=1;i<11;i++){
        ListInsert(&Lb,Lb.length+1,i);
    }
    printf("打印Lb:");
    ListTraverse(Lb,my_print);
    printf("共%d個\n",ListLength(Lb));

    printf("合併La、Lb\n");
    Union(&La,Lb);
    printf("打印La:");
    ListTraverse(La,my_print);
    printf("共%d個\n",ListLength(La));


    srand(time(NULL));
    SqList Le;
    InitList(&Le);
    for(int i=1;i<11;i++){
        ListInsert(&Le,Le.length+1,rand()%11);
    }
    printf("打印Le:");
    ListTraverse(Le,my_print);
    printf("共%d個\n",ListLength(Le));

    SqList Lf;
    InitList(&Lf);
    for(int i=1;i<11;i++){
        ListInsert(&Lf,Lf.length+1,rand()%11*10);
    }
    printf("打印Lf:");
    ListTraverse(Lf,my_print);
    printf("共%d個\n",ListLength(Lf));


    printf("排序Le\n");
    sortAscList(Le);
    printf("打印Le:");
    ListTraverse(Le,my_print);
    printf("共%d個\n",ListLength(Le));

    printf("排序Lf\n");
    sortAscList(Lf);
    printf("打印Lf:");
    ListTraverse(Lf,my_print);
    printf("共%d個\n",ListLength(Lf));

    SqList Lg;
    printf("歸併Le,Lf到Lg:\n");
    MergeList(Le,Lf,&Lg);

    printf("打印Lg:");
    ListTraverse(Lg,my_print);
    printf("共%d個\n",ListLength(Lg));

    return 0;
}

 

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