zoukankan      html  css  js  c++  java
  • 连续存储数组操作

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    
    struct Array {
        int * pBase;
        int length;
        int current;
    };
    
    void initArray(struct Array * pArray, int length);
    void showArray(struct Array * pArray);
    bool is_empty(struct Array * pArray);
    bool append_array(struct Array * pArray, int value);
    bool insert_array(struct Array * pArray, int position, int value);
    bool delete_array(struct Array * pArray, int position, int * val);
    void inversion_array(struct Array * pArray);
    
    int main (void) {
        
        struct Array array;
        int val;
        initArray(&array, 6);
        
        append_array(&array, 1);
        append_array(&array, 2);
        append_array(&array, 3);
        append_array(&array, 4);
    
    //    insert_array(&array, 2, 88);
    //
    //    if ( delete_array(&array, 2, &val) ) {
    //        printf("删除成功
    ");
    //        printf("删除的元素是: %d
    ", val);
    //
    //    }
        
        inversion_array(&array);
        showArray(&array);
    
        
        return 0;
    }
    
    /** 创建数组*/
    void initArray(struct Array * pArray, int length) {
        
        pArray->pBase = (int *)malloc(sizeof(int) * length);
        if (pArray->pBase == NULL) {
            printf("分配内存失败!!!");
            exit(-1);
        }else{
            pArray->length = length;
            pArray->current = 0;
        }
        
    }
    
    /** 判断是否为空*/
    bool is_empty(struct Array * pArray) {
        
        if (pArray->current == 0) {
            return true;
        }else{
            return false;
        }
    }
    
    /** 打印数组*/
    void showArray(struct Array * pArray) {
        if (is_empty(pArray)) {
            printf("数组为空
    ");
        }else{
            for (int i=0; i<pArray->current; i++) {
                printf("%d
    ", pArray->pBase[i]);
                
            }
        }
    }
    
    bool append_array(struct Array * pArray, int value) {
        
        if (pArray->current == pArray->length) {
            return false;
        }
        
        pArray->pBase[pArray->current] = value;
        pArray->current++;
        return true;
    }
    
    
    /** 插入*/
    bool insert_array(struct Array * pArray, int position, int value) {
        
        if (position < 1 || position > pArray->current + 1) {
            return false;
        }
        
        for (int i = pArray->current - 1; i >= position - 1; i--) {
            pArray->pBase[i+1] = pArray->pBase[i];
            
        }
        
        pArray->pBase[position-1] = value;
        pArray->current++;
        
        return true;
    }
    
    /** 删除*/
    bool delete_array(struct Array * pArray, int position, int * val) {
        
        if (position < 1 || position > pArray->current || is_empty(pArray)) {
            return false;
        }
        
        *val = pArray->pBase[position-1];
        for (int i=position; i < pArray->current; i++) {
            pArray->pBase[i-1] = pArray->pBase[i];
        }
        
        pArray->current--;
        
        return true;
    }
    
    /** 倒置*/
    void inversion_array(struct Array * pArray) {
        int i = 0;
        int j = pArray->current-1;
        int t;
        while (i < j) {
            
            t = pArray->pBase[i];
            pArray->pBase[i] = pArray->pBase[j];
            pArray->pBase[j] = t;
            i++;
            j--;
        }
        return;
    }
  • 相关阅读:
    2018.10.11----2018.10.13 计算机网络(写了两天)
    2018.10.10 Java的The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 错误
    2018.10.10 MAC 的Launchpad图标改变大小的设置
    2018.10.8 Hibernate中解决乱码问题---配置一个过滤器
    2018.10.7 理解Hibernate的工作原理及其中的ORM详解
    2018.10.6 Hibernate配置文件详解-------ORM元数据配置 &&& hibernate主配置文件
    2018.10.5 hibernate导入约束,在Eclipse的xml文件实现自动提示
    2018.10.4 AndroidStudio
    2018.10.3 MianShiBaoDian JavaWeb后端部分
    C语言练习之 冒泡排序(二)
  • 原文地址:https://www.cnblogs.com/jiefangzhe/p/10766528.html
Copyright © 2011-2022 走看看