原始线性结构数组的实现以及操作

不多说直接上代码:

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

struct Arr {
    int *pBase;
    int len;
    int cnt;
};
void init_arr(struct Arr *pArr,int length);//初始化一个数组
void show_arr(struct Arr *pArr);//显示某一个数组
bool isEmpty(struct Arr *pArr);//判断一个数组是不是空的
bool append(struct Arr *pArr,int val);//追加
bool is_full(struct Arr *pArr);
bool inserst(struct Arr *pArr,int pos,int val);
bool delete_arr(struct Arr *pArr,int pos,int *pVal);
bool getElement(struct Arr *pArr,int pos,int *pVal);
void sort_arr(struct Arr *pArr);
void inversion_arr(struct Arr *pArr);

int main(void) {
    int val;
    struct Arr arr;
    init_arr(&arr,9);
    show_arr(&arr);
    append(&arr,1);
    append(&arr,2);
    show_arr(&arr);
    inserst(&arr,1,8);
    inserst(&arr,2,9);
    show_arr(&arr);
    delete_arr(&arr,2,&val);
    printf("删除的是第2个位置的值为%d
",val);
    show_arr(&arr);
    getElement(&arr,2,&val);
    printf("得到的值是:%d
",val);
    sort_arr(&arr);
    printf("排序
");
    show_arr(&arr);
    printf("倒置
");
    inversion_arr(&arr);
    show_arr(&arr);
    return 0;
}
//初始化一个数组
void init_arr(struct Arr *pArr,int length) {
    pArr->pBase = (int *)malloc(sizeof(int)*length);
    if(NULL == pArr->pBase) {
        printf("动态分配内存失败
");
        exit(-1);
    } else {
        pArr->len = length;
        pArr->cnt = 0;
    }
    return;
}
//显示数组中的具体内容
void show_arr(struct Arr *pArr) {
    if(isEmpty(pArr)) {
        printf("数组为空
");
    } else {
        for(int i=0; i<pArr->cnt; i++) {
            printf("%d   ",pArr->pBase[i]);
        }
        printf("
");
    }


}
//判断一个数组是不是空的
bool isEmpty(struct Arr *pArr) {
    if( 0 == pArr->cnt) {
        return true;
    } else {
        return false;
    }
}

//追加参数
bool append(struct Arr *pArr,int val) {
    if(is_full(pArr)) {
        printf("数组已满,不宜插入
");
        return false;
    } else {
        pArr->pBase[pArr->cnt] = val;
        pArr->cnt++;
        return true;
    }
}

//判断数组是不是已经满了
bool is_full(struct Arr *pArr) {
    if(pArr->len == pArr->cnt) {
        return true;
    } else {
        return false;
    }
}
//向数组中插入数据
bool inserst(struct Arr *pArr,int pos,int val) {
    if(pos<1 || pos>pArr->cnt+1) {
        printf("插入位置有误
");
        return false;
    }
    if(is_full(pArr)) {
        printf("数组已满
");
        return false;
    }
    for(int i=pArr->cnt-1; i>=pos-1; i--) {
        pArr->pBase[i+1] = pArr->pBase[i];
    }
    pArr->pBase[pos-1] = val;
    pArr->cnt++;
    return true;
}
//删除指定位置的值
bool delete_arr(struct Arr *pArr,int pos,int *pVal) {

    if(isEmpty(pArr)) {
        printf("数组为空!
");
        return false;
    }

    if(pos<1 || pos>pArr->cnt) {
        printf("要删除位置的值不存在
");
        return false;
    }

    *pVal = pArr->pBase[pos-1];
    for(int i =pos; i<pArr->cnt; i++) {
        pArr->pBase[i-1] = pArr->pBase[i];
    }
    pArr->cnt--;
    return true;
}
//得到相对应位置的参数
bool getElement(struct Arr *pArr,int pos,int *pVal) {
    if(isEmpty(pArr)) {
        printf("数组为空!
");
        return false;
    }

    if(pos<1 || pos>pArr->cnt) {
        printf("位置的值不存在
");
        return false;
    }
    *pVal = pArr->pBase[pos-1];
    return true;
}
//将相对应的数组进行一定规模的排序
void sort_arr(struct Arr *pArr) {
    int t;
    if(isEmpty(pArr)) {
        return;
    }
    for(int i=0; i<pArr->cnt; i++) {
        for(int j=i+1; j<pArr->cnt; j++) {
            if(pArr->pBase[i]>pArr->pBase[j]) {
                t = pArr->pBase[i];
                pArr->pBase[i] = pArr->pBase[j];
                pArr->pBase[j] = t;
            }
        }
    }
}

//将数组倒置
void inversion_arr(struct Arr *pArr) {
    int t;
    if(isEmpty(pArr)) {
        return;
    }
    int i = 0;
    int j = pArr->cnt-1;
    while(i<j) {
        t = pArr->pBase[i];
        pArr->pBase[i] = pArr->pBase[j];
        pArr->pBase[j] = t;
        i++;
        j--;
    }
}

这一个主要就是和上面的顺序表相互补充,可以相互借鉴,这个应该是最原始的版本了.

如果有问题请@楼主。

原文地址:https://www.cnblogs.com/strator/p/7227634.html