【数据结构与算法】(一) c 语言实现数组的简单操作

来源:互联网 发布:mac装win10 wifi不稳定 编辑:程序博客网 时间:2024/05/16 18:12
////  main.c//  TestArray////  Created by lan on 16/2/28.//  Copyright © 2016年 lan. All rights reserved.//#include <stdio.h>#include <stdbool.h>#include <malloc/malloc.h>#include <stdlib.h>struct Arr {    int *pBase;  // 存储的是数组第一个元素的地址    int len;    // 数组所容纳的最大元素的个数    int cnt;    // 当前数组有效元素的个数};void init_arr(struct Arr *pArr, int length);bool append_arr(struct Arr *pArr, int a);bool insert_arr(struct Arr *pArr, int index, int a);bool delete_arr(struct Arr *pArr, int index, int *pValue);int get(struct Arr *pArr, int index);bool is_empty(struct Arr *pArr);bool is_full(struct Arr *pArr);void sort_arr(struct Arr *pArr);void show_arr(struct Arr *pArr);void inversion_arr(struct Arr *pArr);void clear_arr(struct Arr *pArr);int main(int argc, const char * argv[]) {    int val;    struct Arr arr;    init_arr(&arr, 6);    append_arr(&arr, 1);    append_arr(&arr, 2);    append_arr(&arr, 3);    append_arr(&arr, 4);    append_arr(&arr, 5);    show_arr(&arr);        printf("在第 5 个位置插入 9:\n");    insert_arr(&arr, 5, 9);    show_arr(&arr);        delete_arr(&arr, 2, &val);    printf("删除的值: %d \n", val);    show_arr(&arr);        int a = get(&arr, 2);    printf("取出的值: %d \n",a);        printf("逆序:\n");    inversion_arr(&arr);    show_arr(&arr);        printf("升序:\n");    sort_arr(&arr);    show_arr(&arr);        printf("清空数组:\n");    clear_arr(&arr);    show_arr(&arr);        printf("添加:\n");    append_arr(&arr, 9);    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;}bool append_arr(struct Arr *pArr, int a){    if (is_full(pArr)) {        printf("数组已经满,无法添加");        return false;    } else {        pArr->pBase[pArr->cnt] = a;        pArr->cnt++;        return true;    }}bool insert_arr(struct Arr *pArr, int index, int a){    if (index > pArr->cnt+1 || index < 1 || is_full(pArr)) {        printf("超出数组范围");        return false;    } else {                for (int i = pArr->cnt-1; i >= index-1; --i) {            pArr->pBase[i+1] = pArr->pBase[i];        }        pArr->pBase[index-1] = a;        pArr->cnt++;        return true;    }}bool delete_arr(struct Arr *pArr, int index, int * pVal) {    if (index < 1 || index > pArr->cnt || is_empty(pArr)) {        printf("删除失败");        return false;    } else {                * pVal = pArr->pBase[index-1];        for (int i = index; i < pArr->cnt; i++) {            pArr->pBase[i-1] = pArr->pBase[i];        }        pArr->cnt--;        return true;    }}int get(struct Arr * pArr, int index) {    if (index > pArr->cnt || index < 1 || is_empty(pArr)) {        printf("取出失败");        exit(-1);    } else {        return pArr->pBase[index - 1];    }}bool is_empty(struct Arr *pArr){    if (0 == pArr->cnt) {        return true;    }else {        return false;    }}bool is_full(struct Arr *pArr) {    if (pArr->cnt == pArr->len) {        return true;    }else {        return false;    }}void show_arr(struct Arr * pArr) {    printf("显示数组: ");    if (is_empty(pArr)) {        printf("数组为空\n");    }else {        for (int i = 0; i < pArr->cnt; ++i) {            printf("%d ", pArr->pBase[i]);        }        printf("\r\n");    }}void inversion_arr(struct Arr *pArr) {    int i = 0;    int j = pArr->cnt - 1;    while (i < j) {        int t = pArr->pBase[i];        pArr->pBase[i] = pArr->pBase[j];        pArr->pBase[j] = t;        i++;        j--;    }    return;}void sort_arr(struct Arr *pArr) {    for (int i = 0; i < pArr->cnt - 1; i++) {        for (int j = i+1; j<pArr->cnt; j++) {            if (pArr->pBase[i] > pArr->pBase[j]) {                int t = pArr->pBase[i];                pArr->pBase[i] = pArr->pBase[j];                pArr->pBase[j] = t;            }        }    }}void clear_arr(struct Arr *pArr) {    pArr->cnt = 0;}


输出结果:

显示数组: 1 2 3 4 5 在第 5个位置插入 9:显示数组: 1 2 3 4 9 5 删除的值: 2 显示数组: 1 3 4 9 5 取出的值: 3 逆序:显示数组: 5 9 4 3 1 升序:显示数组: 1 3 4 5 9 清空数组:显示数组:数组为空添加:显示数组: 9 Program ended with exit code: 0



0 0
原创粉丝点击