美文网首页
连续存储数组(C语言)

连续存储数组(C语言)

作者: 你好667 | 来源:发表于2017-08-25 10:24 被阅读0次
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>

//定义数组结构体
struct arr
{
    int  * pBase;  //数组首地址(数组名)
    int  len;  //数组长度
    int  cnt;  //有效数组长度
};


//初始化数组
void init(struct arr * pArr,int length);
//遍历数组中元素
void show(struct arr * pArr);
//添加一个数组元素
bool add(struct arr * pArr,int val);
//判断数组元素是否为空数组
bool is_empty(struct arr * pArr);
//判断数组元素是否是满的
bool is_full(struct arr * pArr);
//插入数组元素,指定位置
bool add_index(struct arr * pArr,int index,int val);
//倒置数组元素
void reverse(struct arr * pArr);
//删除特定位置的元素
bool delete_index(struct arr * pArr,int index);
//数组排序 
void sort(struct arr * pArr);
//获取特定位置元素
int get(struct arr * pArr,int index);


int main(void)
{
    struct arr array;
    printf("---------------------初始化数组--------------------\n");
    init(&array,5);
    show(&array);
    printf("---------------------初始化数组--------------------\n\n\n");

    printf("--------------添加数组元素数组--------------------\n");
    add(&array,1);
    add(&array,2);
    add(&array,3);
    add(&array,4);
    show(&array);
    printf("--------------添加数组元素数组--------------------\n\n\n");
    
    printf("--------------指定位置插入数组元素数组-------------\n");
    add_index(&array,2,5);
    show(&array);
    printf("--------------指定位置插入数组元素数组-------------\n\n\n");

    printf("---------------------删除数组元素--------------------\n");
    if(delete_index(&array,2)){
        printf("删除成功\n");
        show(&array);
    }else{
        printf("删除失败\n");
    }
    printf("---------------------删除数组元素--------------------\n\n\n");
    
    printf("---------------------倒置数组元素--------------------\n");
    reverse(&array);
    show(&array);
    printf("---------------------倒置数组元素--------------------\n\n\n");

    printf("---------------------升序数组元素--------------------\n");
    sort(&array);
    show(&array);
    printf("---------------------升序数组元素--------------------\n\n\n");

    printf("---------------------获取数组元素指定元素--------------------\n");
    int t = get(&array,3);
    if(t == -1){
        printf("无法获取 \n");
    }else{
        printf("%d \n",t);
    }
    printf("---------------------获取数组元素指定元素--------------------\n\n\n");
    
    return 0;
}

void init(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 is_empty(struct arr * pArr)
{
    if(pArr->cnt == 0)
    {
        return true;
    }else{
        return false;
    }
}

bool is_full(struct arr * pArr)
{
    if(pArr->cnt == pArr->len)
    {  //数组有效长度和数组开辟空间相等
        return true;
    }else{
        return false;
    }
}

void show(struct arr * pArr)
{
    if(is_empty(pArr))
    {
        printf("数组为空\n");
    }else{
        for(int i =0; i < pArr->cnt;i++)
        {
            printf("%d \n",  pArr->pBase[i]);
        }
    }
}

bool add(struct arr * pArr, int val)
{
    //若是满的就不再添加
    if(is_full(pArr))
    {
        return false;
    }

    //添加元素
    pArr->pBase[pArr->cnt] = val;
    (pArr->cnt)++;

        return true;
    }

bool add_index(struct arr * pArr, int index, int val)
{
    //若是满的就不再添加
    if(is_full(pArr))
    {
        return false;
    }
    //若是不在范围内
    if(index <1 || index >= pArr->cnt)
    {
        return false;
    }

    for(int i = pArr->cnt+1; i >= index-1;i--){   //从插入的位置开始,到结束  从最后一个开始
        pArr->pBase[i+1] = pArr->pBase[i];
    }

    //插入位置
    pArr->pBase[index-1] = val;

    return true;
}

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

    while(i < j)
    {
        temp = pArr->pBase[i];    //第一个位置存起来
        pArr->pBase[i] = pArr->pBase[j]; //然后第一个位置存最后一个位置的数
        pArr->pBase[j] = temp;  //最后一个位置存一个位置的数
        i++;
        j--;
    }
    return;
}

bool delete_index(struct arr * pArr,int index)
{
    //若是为空不删除
    if(is_empty(pArr))
    {
        return false;
    }
    //若是不在范围内
    if(index <1 || index >= pArr->cnt)
    {
        return false;
    }
    
    for(int i = index;i<pArr->cnt;i++){
        pArr->pBase[i-1] = pArr->pBase[i];  //后面的值赋值到前面
    }
    
    (pArr->cnt)--;
    return true;
}

void sort(struct arr * pArr)
{   
    int temp,i,j;

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

int get(struct arr * pArr,int index)
{
    //若是为空
    if(is_empty(pArr))
    {
        return -1;
    }
    //若是不在范围内
    if(index <1 || index >= pArr->cnt)
    {
        return -1;
    }
    return pArr->pBase[index];

}

相关文章

网友评论

      本文标题:连续存储数组(C语言)

      本文链接:https://www.haomeiwen.com/subject/hgqudxtx.html