码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构之C语言模拟整数数组实现

时间:2015-05-06 19:37:54      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

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


typedef
struct Arr {
    int *pBase; //数组第一个元素地址
    int len;    //数组长度
    int cnt;    //当前有效元素数量
} Array;


void init_array(Array *, int); //初始化数组

void show_array(Array *); //遍历打印数组

bool is_empty(Array *);   //判断数组是否空

bool insert_array(Array *, int pos, int); //插入元素 pos表示在第几个元素前面插入

bool append_array(Array *, int); //添加元素

bool del_array(Array *, int pos, int * val); //删除第几个元素

bool is_full(Array *); //判断数组是否已满

void sort_arr(Array * pArr); //默认升序

void inversion_arr(struct Arr * pArr);//反转数组

int main() {
    Array a;
    init_array(&a, 10);

    append_array(&a, 0);
    append_array(&a, 1);
    append_array(&a, 2);
    append_array(&a, 3);
    append_array(&a, 4);
    append_array(&a, 5);

    inversion_arr(&a);

    show_array(&a);


    getchar();
    free((&a)->pBase);
}


void inversion_arr(struct Arr * pArr){
    int i = 0;
    int j = pArr->cnt-1;
    int temp = 0;

    while (i < j){
        temp = pArr->pBase[i];
        pArr->pBase[i] = pArr->pBase[j];
        pArr->pBase[j] = temp;
        ++i;
        --j;
    }

}


void sort_arr(Array * pArr){

    int temp = 0;

    for (int i = 0; i < pArr->cnt; i++){
        for (int j = 0; j < pArr->cnt-1 - i; j++){
            if (pArr->pBase[j] > pArr->pBase[j + 1]) {
                temp = pArr->pBase[j];
                pArr->pBase[j] = pArr->pBase[j + 1];
                pArr->pBase[j + 1] = temp;
            }
        }
    }
}


bool del_array(Array * arr, int pos, int * val){
    if (is_empty(arr)) {
        printf("array is empty\n");
        return false;
    }

    if (pos < 1 || pos>arr->cnt){
        printf("position is error \n");
        return false;
    }

    *val = arr->pBase[pos - 1];

    for (int i = pos; i < arr->cnt; i++) {
        arr->pBase[i - 1] = arr->pBase[i];
    }

    arr->cnt--;
    return true;



}


bool append_array(Array * arr, int val){
    if (is_full(arr)){
        printf("array is full");
        return false;
    }
    arr->pBase[arr->cnt] = val;
    arr->cnt++;
    return true;
}

bool is_full(Array *arr) {
    return arr->cnt == arr->len;
}

bool insert_array(Array *arr, int pos, int val) {
    if (is_full(arr)) {
        printf("array is full");
        return false;
    }

    if (pos < 1 || pos > arr->cnt + 1) {
        printf("position is error\n");
        return false;
    }

    for (int i = arr->cnt; i >= pos - 1; i--) {
        arr->pBase[i] = arr->pBase[i - 1];
    }
    arr->pBase[pos - 1] = val;
    arr->cnt++;

    return true;

}


void init_array(Array *arr, int len) {
    arr->pBase = (int *)malloc(sizeof(int) * len);
    if (NULL == arr->pBase) {
        printf("内存分配失败");
        exit(-1);
    }
    else {
        arr->cnt = 0;
        arr->len = len;
    }
}

bool is_empty(Array *arr) {
    return 0 == arr->cnt;
}

void show_array(Array *arr) {
    if (is_empty(arr)) {
        printf("数组为空不能打印");
    }
    else {
        for (int i = 0; i < arr->cnt; i++) {
            printf("%d ", arr->pBase[i]);
        }
    }
}

 

数据结构之C语言模拟整数数组实现

标签:

原文地址:http://www.cnblogs.com/or2-/p/4482819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!