数组顺序表
数组顺序表
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<malloc.h> 4 #include<stdbool.h> 5 6 struct Arr 7 { 8 int *pBase; //存储数组第一个元素的地址 9 int len; //数组所能容纳的最大元素个数 10 int cnt; //当前数组有效元素 11 }; 12 13 void init_arr(struct Arr * pArr,int length); 14 bool append_arr(struct Arr * pArr,int val); //追加 15 bool insert_arr(struct Arr * pArr,int pos,int val); 16 bool delet_arr(struct Arr * pArr,int pos,int *pVal); 17 bool is_empty(struct Arr * pArr); 18 bool is_full(struct Arr * pArr); 19 void sort_arr(struct Arr * pArr); 20 void show_arr(struct Arr * pArr); 21 void inversion_arr(struct Arr * pArr); 22 23 int main() 24 { 25 struct Arr arr; 26 int val; 27 28 printf("--------顺序表初始化--------\n"); 29 init_arr(&arr,6); 30 show_arr(&arr); 31 printf("\n\n"); 32 33 printf("--------追加元素--------\n"); 34 append_arr(&arr,1); 35 append_arr(&arr,2); 36 append_arr(&arr,3); 37 append_arr(&arr,4); 38 append_arr(&arr,5); 39 show_arr(&arr); 40 printf("\n\n"); 41 42 printf("--------插入元素--------\n"); 43 insert_arr(&arr,6,99); 44 show_arr(&arr); 45 printf("\n\n"); 46 47 printf("--------删除元素--------\n"); 48 if( delet_arr(&arr,1,&val) ) 49 { 50 printf("删除成功!\n"); 51 printf("您删除的元素是:%d\n",val); 52 } 53 else 54 { 55 printf("删除失败!\n"); 56 } 57 show_arr(&arr); 58 printf("\n\n"); 59 60 printf("--------倒置元素--------\n"); 61 inversion_arr(&arr); 62 printf("倒置之后的数组内容是:\n"); 63 show_arr(&arr); 64 printf("\n\n"); 65 66 printf("--------排序--------\n"); 67 sort_arr(&arr); 68 show_arr(&arr); 69 printf("\n\n"); 70 return 0; 71 } 72 73 void init_arr(struct Arr * pArr,int length) 74 { 75 pArr->pBase = (int *)malloc(sizeof(int) * length); 76 if(NULL == pArr->pBase) 77 { 78 printf("动态内存分配失败!\n"); 79 exit(-1); //终止整个程序 80 } 81 else 82 { 83 pArr->len = length; 84 pArr->cnt = 0; 85 } 86 return; 87 } 88 89 bool is_empty(struct Arr * pArr) 90 { 91 if(0 == pArr->cnt) 92 return true; 93 else 94 return false; 95 } 96 97 bool is_full(struct Arr * pArr) 98 { 99 if(pArr->cnt == pArr->len) 100 return true; 101 else 102 return false; 103 } 104 105 void show_arr(struct Arr * pArr) 106 { 107 if( is_empty(pArr) ) 108 { 109 printf("数组为空!\n"); 110 } 111 else 112 { 113 for(int i=0;i<pArr->cnt;i++) 114 printf("%d ",pArr->pBase[i]); 115 printf("\n"); 116 } 117 } 118 119 bool append_arr(struct Arr * pArr,int val) 120 { 121 //满是返回false 122 if( is_full(pArr) ) 123 return false; 124 pArr->pBase[pArr->cnt] = val; 125 pArr->cnt++; 126 return true; 127 } 128 129 bool insert_arr(struct Arr * pArr,int pos,int val) 130 { 131 132 if(is_empty(pArr) ) 133 return false; 134 if(pos<1 || pos>pArr->len) 135 return false; 136 for(int i=pArr->cnt-1;i>=pos-1;--i) 137 { 138 pArr->pBase[i+1] = pArr->pBase[i]; 139 } 140 pArr->pBase[pos-1] = val; 141 (pArr->cnt)++; 142 return true; 143 } 144 145 bool delet_arr(struct Arr * pArr,int pos,int *pVal) 146 { 147 if( is_empty(pArr) ) 148 return false; 149 if(pos<1 || pos>pArr->cnt) 150 return false; 151 *pVal = pArr->pBase[pos-1]; 152 for(int i=pos;i<pArr->cnt;++i) 153 { 154 pArr->pBase[i-1] = pArr->pBase[i]; 155 } 156 pArr->cnt--; 157 return true; 158 } 159 160 void inversion_arr(struct Arr * pArr) 161 { 162 int i = 0; 163 int j = pArr->cnt-1; 164 int t; 165 while(i<j) 166 { 167 t = pArr->pBase[i]; 168 pArr->pBase[i] = pArr->pBase[j]; 169 pArr->pBase[j] = t; 170 ++i; 171 --j; 172 } 173 return; 174 } 175 176 void sort_arr(struct Arr * pArr) 177 { 178 int i,j,t; 179 for(i=0;i< pArr->cnt ;++i) 180 { 181 for(j=i+1;i< pArr->cnt ;++j) 182 { 183 if(pArr->pBase[i] > pArr->pBase[j]) 184 { 185 t = pArr->pBase[i]; 186 pArr->pBase[i] = pArr->pBase[j]; 187 pArr->pBase[j] = t; 188 } 189 } 190 } 191 }
更多精彩