数据结构之自制数组及数组操作(二)


#include <stdio.h>
#include <stdbool.h>

/**定义一个数组*/
struct ArrayList{
    int currentNode;//从1开始(如果从0开始,数组为空的时currentNode等于负整数)
    int arrayLenght;
    int *pBase;//pBase指向第一个元素的地址
};

/**初始化数组*/
void initArray(struct ArrayList *pArray);
bool insertArray(struct ArrayList *pArray);//插入
bool appendArray(struct ArrayList *pArray, int num);//最后面追加
bool isFull(struct ArrayList *pArray);
bool isEmpty(struct ArrayList *pArray);
void showArray(struct ArrayList *pArray);
int getArray(struct ArrayList *pArray);
bool deleteArray(struct ArrayList *pArray, int *pNum);
bool sortArray(struct ArrayList *pArray);//排序数组
/**倒置数组*/
bool inversionArray(struct ArrayList *pArray);

int main(void) {
    
    struct ArrayList arrary;
    initArray(&arrary);
    
//    int num;
//    deleteArray(&arrary, &num);
//    printf("删除的元素为%d\n", num);
    
    
    int num = getArray(&arrary);
    showArray(&arrary);
    
    return 0;
}

void initArray(struct ArrayList *pArray)
{
    printf("请输入数组长度:");
    scanf("%d",&pArray->arrayLenght);
    
    printf("请输入数组个数:");
    scanf("%d",&pArray->currentNode);
    
    pArray->pBase = (int *)malloc(sizeof(int) * pArray->arrayLenght);
    if (NULL == pArray->pBase) {
        printf("分配空间失败!");
        exit(-1);
    }else{
        printf("请输入数组元素:\n");
        for (int i = 0; i<pArray->currentNode; i++) {
            scanf("%d", (pArray->pBase + i));
        }
    }
    return;
}

bool isEmpty(struct ArrayList *pArray)
{
    if (pArray->currentNode) {
        return false;
    }else{
        return true;
    }
}

bool isFull(struct ArrayList *pArray)
{
    if (pArray->arrayLenght == pArray->currentNode) {
        return true;
    }else{
        return false;
    }
}

void showArray(struct ArrayList *pArray)
{
    if (isEmpty(pArray)) {
        printf("这是一个空数组\n");
        return;
    }
    
    printf("数组长度为%d,数组个数为%d,元素分别为:", pArray->arrayLenght, pArray->currentNode);
    for (int i = 0; i<pArray->currentNode; i++) {
        printf("%d ", *(pArray->pBase + i));
    }
    printf("\n");
}

bool appendArray(struct ArrayList *pArray, int num)
{
    if (isFull(pArray)) {
        printf("数组已满,插入失败\n");
        return false;
    }
    
    *(pArray->pBase + pArray->currentNode) = num;
    pArray->currentNode++;
    
    return true;
}

bool insertArray(struct ArrayList *pArray)
{
    if (isFull(pArray)) {
        printf("数组已满,插入失败\n");
        return false;
    }
    
    int insertLocation = 1;//位置从1开始
    printf("插入的数组位置为(从1开始):");
    scanf("%d", &insertLocation);
    
    if (insertLocation > pArray->currentNode + 1 || insertLocation < 1) {
        printf("插入的数组位置不正确\n");
        return false;
    }
    
    int num = 0;
    printf("插入的数组元素为:");
    scanf("%d", &num);
    for (int i = pArray->currentNode; i>=insertLocation; i--) {
        *(pArray->pBase + i) = *(pArray->pBase + i - 1);
    }
    
    *(pArray->pBase + insertLocation - 1) = num;
    pArray->currentNode++;
    
    return true;
}

bool deleteArray(struct ArrayList *pArray, int *pNum)
{
    if (isEmpty(pArray)) {
        printf("数组为空,插入失败\n");
        return false;
    }
    
    int deleteLocation = 1;//位置从1开始
    printf("删除的数组位置为(从1开始):");
    scanf("%d", &deleteLocation);
    
    if (deleteLocation > pArray->currentNode || deleteLocation < 1) {
        printf("删除的数组位置不正确\n");
        return false;
    }
    
    *pNum = *(pArray->pBase + deleteLocation - 1);
    for (int i = deleteLocation; i<=pArray->currentNode; i++) {
        *(pArray->pBase + i - 1) = *(pArray->pBase + i);
    }
    pArray->currentNode--;
    
    return true;
}


bool sortArray(struct ArrayList *pArray)
{

    for (int i = 0; i < pArray->currentNode-1; i++) {
        for (int j = 0; j < pArray->currentNode - 1 - i; j++) {
            if (*(pArray->pBase + j) < *(pArray->pBase + j + 1)) {
                int temp = *(pArray->pBase + j);
                *(pArray->pBase + j) = *(pArray->pBase + j + 1);
                *(pArray->pBase + j + 1) = temp;
            }
        }
    }
    return true;
}

bool inversionArray(struct ArrayList *pArray)
{
    //方法一:
    int i = 0;
    while (i < pArray->currentNode - 1 - i) {
        int temp = *(pArray->pBase + i);
        *(pArray->pBase + i) = *(pArray->pBase + pArray->currentNode - 1 - i);
        *(pArray->pBase + pArray->currentNode - 1 - i) = temp;
        i++;
    }
    
    //方法二:
//    for (int i = 0; i<pArray->currentNode; i++) {
//        int temp = *(pArray->pBase + i);
//        *(pArray->pBase + i) = *(pArray->pBase + pArray->currentNode - 1 - i);
//        *(pArray->pBase + pArray->currentNode - 1 - i) = temp;
//        if (i >= pArray->currentNode - 1 - i) {
//            break;
//            return true;
//        }
//    }
    return true;
}

int getArray(struct ArrayList *pArray)
{
    if (isEmpty(pArray)) {
        printf("数组为空,查找失败\n");
        return -1;
    }
    
    int findLocation = 1;//位置从1开始
    printf("数组位置为(从1开始):");
    scanf("%d", &findLocation);
    
    if (findLocation > pArray->currentNode || findLocation < 1) {
        printf("查找数组位置不正确\n");
        return -1;
    }
    
    
    return *(pArray->pBase + findLocation - 1);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值