1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| #include<stdio.h> #include<malloc.h> #include<stdlib.h>
struct Arr { int *pBase; int len; int cnt; }; void init_arr(struct Arr *pArr, int length); bool append_arr(struct Arr * pArr, int val); bool insert_arr(struct Arr *pArr, int pos, int val); bool delete_arr(struct Arr *pArr, int pos, int *pVal); int get(); bool is_empty(struct Arr *pArr); bool is_full(struct Arr *pArr); void sort_arr(struct Arr *pArr); void show_arr(struct Arr *pArr); void inversion_arr(struct Arr *pArr); int main(void) { struct Arr arr; int val; init_arr(&arr, 6); show_arr(&arr); append_arr(&arr, 1); append_arr(&arr, 2); append_arr(&arr, -3); append_arr(&arr, 4); append_arr(&arr, 10); append_arr(&arr, 3); insert_arr(&arr,3,2); if ( delete_arr(&arr,1,&val)) { printf("删除成功!\n"); printf("您删除的元素是:%d\n", val); } else { printf("删除失败!\n"); } show_arr(&arr); inversion_arr(&arr); printf("倒置之后:"); show_arr(&arr); sort_arr(&arr); printf("\n排序之后:"); show_arr(&arr); return 0; }
void init_arr(struct Arr *pArr, int length) { pArr->pBase = (int *)malloc(sizeof(int)*length); if (NULL == pArr->pBase) { printf("分配失败!"); exit(-1); } else { pArr->len = length; pArr->cnt = 0; } return; }
bool is_empty(struct Arr *pArr) { if (0 == pArr->cnt) { return true; } else { return false; } }
void show_arr(struct Arr * pArr) { if (is_empty(pArr)) { printf("数组为空!\n"); } else { for (int i = 0; i < pArr->cnt; ++i) { printf("%d ", pArr->pBase[i]); } printf("\n"); } }
bool is_full(struct Arr *pArr) { if (pArr->cnt == pArr->len) { return true; } else { return false; } }
bool append_arr(struct Arr * pArr, int val) { if (is_full(pArr)) { printf("数组已满!"); return false; } else { pArr->pBase[pArr->cnt] = val; (pArr->cnt)++; return true; } }
bool insert_arr(struct Arr *pArr, int pos, int val) { int i; if (is_full(pArr)) { return false; } if (pos<1 pos > pArr->len+1) { return false; } for(i = pArr->cnt-1; i >= pos -1; i--) { pArr->pBase[i+1] = pArr->pBase[i]; } pArr->pBase[pos-1] = val; (pArr->cnt)++; return true; }
bool delete_arr(struct Arr *pArr, int pos, int *pVal) { int i; if (is_empty(pArr)) { return false; } if (pos<1 pos > pArr->len+1) { return false; } *pVal = pArr->pBase[pos-1]; for (int i = pos; i < pArr->cnt; ++i) { pArr->pBase[i-1] = pArr->pBase[i]; } (pArr->cnt)--; return true; }
void inversion_arr(struct Arr * pArr) { int i = 0; int j = pArr->cnt-1; int t; while (i < j) { t = pArr->pBase[i]; pArr->pBase[i] = pArr->pBase[j]; pArr->pBase[j] = t; i++; j--; } }
void sort_arr(struct Arr * pArr) { int i, j, t; for (i = 0; i < pArr->cnt; ++i) { for (j = i+1; j < pArr->cnt; ++j) { if (pArr->pBase[i] > pArr->pBase[j]) { t = pArr->pBase[i]; pArr->pBase[i] = pArr->pBase[j]; pArr->pBase[j] = t; } } } }
|