栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【数据结构】01顺序表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【数据结构】01顺序表

语言:C语言 IED:VS Code

#include
#include

#define TRUE 1
#define FALSE 0
#define INIT_SIZE 10
#define INCREMENT_SIZE 5
#define OK 1
#define ERROR 0

typedef int Elemtype;
typedef int Status;


typedef struct{
    Elemtype *elem;     //存储空间基地址
    int length; //当前长度
    int size;   // 当前分配的表长大小
}SqList;


Status InitList(SqList *L){
    L->elem = (Elemtype *) malloc(INIT_SIZE * sizeof(Elemtype));
    if(!L->elem){
        return ERROR;
    }
    L->length = 0;
    L->size = INIT_SIZE;
    return OK;
}


Status DestroyList(SqList *L){
    free(L->elem);
    L->length = 0;
    L->size = 0;
    return OK;
}



Status ClearList(SqList *L){
    L->length = 0;
    return OK;
}


Status IsEmpty(const SqList L){
    if(0 == L.length){
        return TRUE;
    }
    else{
        return FALSE;
    }
}


int GetLength(const SqList L){
    return L.length;
}


Status compare(Elemtype e1, Elemtype e2){
    if(e1 == e2){
        return 0;
    }
    else if(e1 < e2){
        return -1;
    }
    else return 1;
}



Status InsertElem(SqList *L, int i, Elemtype e){
    Elemtype *new;
    if(i < 1 || i > L->length + 1){
        return ERROR;
    }
    //如果存储空间不足,增加空间,使用realloc(*ptr, new_size)函数重新分配空间
    //ptr是需要被重新分配的内存块,它是之前被malloc或calloc分配的,如果是空指针则会分配一个新内存并返回指针
    if(L->length >= L->size){
        new = (Elemtype*) realloc(L->elem, (L->size + INCREMENT_SIZE) * sizeof(Elemtype));
        if(!new){
            return ERROR;
        }
        L->elem = new;
        L->size +=INCREMENT_SIZE;
    }
    
    //插入元素前,利用两个指针将后面的元素后移;
    Elemtype *p = &L->elem[i-1];
    Elemtype *q = &L->elem[L->length-1];
    for(;q >= p; q--){
        *(q + 1) = *q;
    }
    *p = e;
    ++L->length;
    return OK;
}





Status DeleteElem(SqList *L, int i, Elemtype *e){
    if(i < 1 || i > L->length ) {
        return ERROR;
    }
    Elemtype *p = &L->elem[i-1];
    *e = *p;
    for(; p < &L->elem[i-1]; p++){
        *(p) = *(p+1);
    }
    --L->length;
    return OK;
}



Status GetElem(const SqList L, int i, Elemtype *e){
    if(i < 1 || i > L.length-1){
        return ERROR;
    }
    *e = L.elem[i-1];
    return OK;
}


Status FindElem(const SqList L, Elemtype e, Status(*compare)(Elemtype, Elemtype)){
    int i;
    for(i = 0; i < L.length; i++){
        if(!(*compare)(L.elem[i], e)){
            return i + 1;
        }
    }
    if(i >= L.length){
        return ERROR;
    }
}


Status PreElem(const SqList L,Elemtype cur_e,Elemtype *pre_e){
    int i;
    for(i = 0; i < L.length; i++){ //按位置迭代查找
        if(cur_e == L.elem[i]){
            if(i != 0){
                *pre_e = L.elem[i-1];
            }
            else return ERROR;
        }
    }
    if(i >= L.length) { //元素无此前驱元素
        return ERROR;
    }
    //return OK;//自己加的这行
}


Status NextElem(const SqList L, Elemtype cur_e, Elemtype *next_e){
    int i;
    for(i = 0; i < L.length; i++){
        if(cur_e == L.elem[i]){
            if(i < L.length - 1){ 
                *next_e = L.elem[i+1];
                return OK;
            }
            else return ERROR;
        }
    }
    if(i >= L.length - 1){
        return ERROR;
    }
}


void visit(Elemtype e){
    printf("%d",e);
}


Status TraverseList(const SqList L, void (*visit)(Elemtype)){
    int i;
    for(i = 0; i 
 运行结果: 
shiyanlou:project/ $ gcc -o 2.1 2.1.c                                                      [18:29:32]
shiyanlou:project/ $ ./2.1                                                                 [18:45:14]
init_success
length is 10
The first element is 0
list is not empty
The 5 at 6
The 6's previous element is 5
The 6's next element is 7
delete first element is 0
list:1 2 3 4 5 6 7 8 9 
destroy_success
shiyanlou:project/ $      

练习题目(待补充。。。):
//练习题目||||||||||||

void move(SqList &L){
    int i = 0, j = L.length-1, k;
    Elemtype temp;
    while(i <= j){
        while(L.elem[i]%2 == 1){
            i++;//i指向一个奇数
        }
        while(L.elem[j]%2 == 0){
            j--;//j指向一个偶数
        }
        if(i < j){
            temp = L.elem[i];//
            L.elem[i] =L.elem[j];
            L.elem[j] = temp;
        }
    }
}


void reverse(SqList &L){
    int i;
    Elemtype x;
    for(i = 0;i 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1005052.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号