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;
    }
  • 相关阅读:
    i春秋 可恶的黑客
    bugku 变量1
    开源Odoo ERP 13.2版本发行说明(译文+原文)
    Java数学运算
    SET DYNAMICS 365 COLORS AND LOGO USING THEMES
    Use SQL to Query Data from CDS and Dynamics 365 CE
    SAP四代增强实现:VA01销售订单复制项目文本时不需要显示文本框和回车
    ABAP 动态备份自建表数据到新表(自建表有数据的情况下要改字段长度或者其他)
    NTFS ADS(备用数据流)
    Windows RestartManeger重启管理器
  • 原文地址:https://www.cnblogs.com/jiefangzhe/p/10766528.html
Copyright © 2011-2022 走看看