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

//
//  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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值