zoukankan      html  css  js  c++  java
  • c语言数组的操作

      1 #include<stdio.h>
      2 #include<malloc.h>
      3 #include<stdlib.h>//exit()
      4 
      5 
      6 struct Arr//定义了一个数据类型,数据类型名字Arr
      7 {
      8 
      9     int *pbase;//基地址指针
     10     int lenth;//数组长度
     11     int cent;//数组实际大小
     12     //int increment;//增战因子
     13 };
     14 void init_arr(struct Arr *array, int lenth);
     15 bool append_arr(struct Arr *array,int data);
     16 bool delete_arr(struct Arr *array,int pos);
     17 bool insert_arr(struct Arr *array,int data,int pos);//插入
     18 void show_arr(struct Arr *array);
     19 bool is_empty(struct Arr *array);
     20 bool is_full(struct Arr *array);
     21 int get();
     22 bool inversion(struct Arr *array);
     23 void sort_bubble(struct Arr *array);//冒泡
     24 void sort_insert(struct Arr *array);//插入排序
     25 
     26 
     27 int main()
     28 {
     29     struct Arr arr;
     30     init_arr(&arr,6);
     31     printf("%d
    ",arr.cent);
     32     show_arr(&arr);
     33     if(append_arr(&arr,45)&&append_arr(&arr,4)&&append_arr(&arr,5)&&append_arr(&arr,1245)&&    printf("%d
    ",arr.cent))
     34     {
     35         printf("插入成功
    ");
     36     }
     37     
     38     show_arr(&arr);
     39     insert_arr(&arr,99,1);
     40     show_arr(&arr);
     41     delete_arr(&arr,3);
     42     show_arr(&arr);
     43     inversion(&arr);
     44     show_arr(&arr);
     45     sort_bubble(&arr);
     46     show_arr(&arr);
     47     sort_insert(&arr);
     48     show_arr(&arr);
     49     return 0;
     50 }
     51 
     52 void init_arr(struct Arr *array,int lenth)//初始化
     53 {
     54     array->lenth=99;
     55     array->pbase=(int *)malloc(sizeof(int)*lenth);
     56     if(NULL==array->pbase)
     57     {
     58         printf("动态内存分配失败
    ");
     59         exit(-1);
     60     }
     61     else
     62     {
     63         array->lenth=lenth;
     64         array->cent=0;
     65     }
     66 }
     67 
     68 void show_arr(struct Arr *array)//展示数组
     69 {
     70     if(is_empty(array))
     71     {
     72         printf("数组为空
    ");
     73     }
     74     else
     75     {
     76         int i;
     77         for(i=0;i<array->cent;i++)
     78         {
     79             printf("%d ",array->pbase[i]);
     80         }
     81         printf("
    ");
     82     }
     83 }
     84 
     85 bool is_empty(struct Arr *array)//判断是否为空
     86 {
     87     if(array->cent==0)
     88         return true;
     89     else
     90         return false;
     91 }
     92 
     93 bool append_arr(struct Arr *array,int data)//在后面插入
     94 {
     95     if(is_full(array))
     96         return false;
     97     else 
     98     {
     99         array->pbase[array->cent++]=data;
    100     }
    101     return true;
    102 }
    103 
    104 bool is_full(struct Arr *array)//判断是否满
    105 {
    106     if(array->cent==array->lenth)
    107         return true;
    108     else 
    109         return false;
    110 }
    111 
    112 bool insert_arr(struct Arr *array,int data,int pos)//数组中插入
    113 {
    114     if(pos>0&&pos<=array->cent)
    115     {
    116             if(is_full(array))
    117             {
    118                 printf("数组已满,无法插入
    ");
    119                 return false;
    120             }
    121             else
    122             {
    123                 for(int i=array->cent;i>=pos;i--)
    124                     array->pbase[i] = array->pbase[i-1];
    125                 array->pbase[pos-1]=data;
    126                 array->cent++;
    127                 return true;
    128             }
    129     }
    130     else
    131     {
    132         printf("pos越界
    ");
    133         return false;
    134     }
    135 
    136 }
    137 
    138 bool delete_arr(struct Arr *array,int pos)//删除摸个位置上的元素
    139 {
    140     if(is_empty(array))
    141     {
    142         printf("数组为空
    ");
    143         return false;
    144     }
    145     if(pos>0&&pos<array->cent)
    146     {
    147         int vol=array->pbase[pos-1];
    148         for(int i=pos;i<array->cent;i++)
    149         {
    150             array->pbase[i-1]=array->pbase[i];
    151         }
    152         array->cent--;
    153         printf("数据已删除
    ");
    154         printf("删除的数据:%d
    ",vol);
    155         return true;
    156     }
    157     else
    158     {
    159         printf("pos越界
    ");
    160         return false;
    161     }
    162 
    163 }
    164 
    165 bool inversion(struct Arr *array)//数组反转
    166 {
    167     int i,t,l;
    168     /*l=array->cent/2;
    169     for(i=0;i<l;i++)
    170     {
    171         if(array->pbase[i]!=array->pbase[array->cent-1-i])
    172         {
    173             t=array->pbase[i];
    174             array->pbase[i]=array->pbase[array->cent-1-i];
    175             array->pbase[array->cent-1-i]=t;
    176         }
    177     }*/
    178     l=array->cent-1;
    179     i=0;
    180     while(i<l)
    181     {
    182         if(array->pbase[i]!=array->pbase[l])
    183         {
    184             t=array->pbase[i];
    185             array->pbase[i]=array->pbase[l];
    186             array->pbase[l]=t;
    187         }
    188         i++;
    189         l--;    
    190     }
    191     return true;
    192 }
    193 
    194 void sort_bubble(struct Arr *array)
    195 {
    196     int i,j,l,t;
    197     l=array->cent;
    198     for(i=0;i<l-1;i++)
    199         for(j=i+1;j<l;j++)
    200         {
    201             if(array->pbase[i]>array->pbase[j])
    202             {
    203                     t=array->pbase[i];
    204                     array->pbase[i]=array->pbase[j];
    205                     array->pbase[j]=t;
    206             }
    207         }
    208 }
    209 
    210 void sort_insert(struct Arr *array)
    211 {
    212     int i,j,l,t;
    213     l=array->cent;
    214     for(i=1;i<l;i++)
    215         for(j=i-1;j>=0;j--)
    216         {
    217             t=array->pbase[i];
    218             if(array->pbase[i]<array->pbase[j])
    219             {
    220                 for(int p=i;p>j;p--)
    221                     array->pbase[p] = array->pbase[p-1];
    222                 array->pbase[j]=t;
    223             }
    224         }
    225 }
  • 相关阅读:
    jvm 致命问题分析
    java 包引入时*会全部加载吗
    JDK压缩指针
    ArrayList && HashMap扩容策略
    数据结构对齐 堆对齐 栈对齐
    TLS Thread Local Storage
    vue2 设置网页title的问题
    error ...项目路径 ode_moduleschromedriver: Command failed.
    Vue打包后出现一些map文件
    'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序
  • 原文地址:https://www.cnblogs.com/weilq/p/3437680.html
Copyright © 2011-2022 走看看