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
| #include <stdio.h> #include <stdbool.h> #include <stdlib.h>
struct Arr { int *pBase; int len; int cnt; };
void init(struct Arr *pArr, int length); bool append(struct Arr *pArr, int val); bool insert(struct Arr *pArr, int pos, int val); bool delete(struct Arr *pArr, int pos, int *pVal); bool get(); bool is_empty(struct Arr *pArr); bool is_full(struct Arr *pArr); void sort(struct Arr *pArr); void show(struct Arr *pArr); void inversion(struct Arr *pArr);
int main(void) { struct Arr arr; init(&arr, 4); append(&arr, 1); insert(&arr, 1, 2); int tmp; delete(&arr, 1, &tmp); append(&arr, 5); append(&arr, 4); inversion(&arr); sort(&arr); show(&arr); return 0; }
void init(struct Arr *pArr, int length) { pArr->pBase = (int *) malloc(sizeof(int) * length); if (pArr->pBase == NULL) { printf("动态内存分配失败\n"); exit(-1); } else { pArr->len = length; pArr->cnt = 0; } }
bool is_empty(struct Arr *pArr) { if (pArr->len == 0) { return true; } else { return false; } }
bool is_full(struct Arr *pArr) { if (pArr->cnt == pArr->len) { return true; } else { return false; } }
void show(struct Arr *pArr) { if (is_empty(pArr)) { printf("数组为空\n"); } else { for (int i = 0; i < pArr->cnt; ++i) { printf("%d\n", *(pArr->pBase + i)); } } }
bool append(struct Arr *pArr, int val) { if (is_full(pArr)) { printf("数组已满\n"); return false; } else { *(pArr->pBase + pArr->cnt) = val; pArr->cnt += 1; return true; } }
bool insert(struct Arr *pArr, int pos, int val) { if (is_full(pArr)) { printf("数组已满\n"); return false; } if (pos + 1 > pArr->cnt) { printf("不满足插入条件\n"); return false; } int i = 0; for (i = pArr->cnt; i > pos; --i) { *(pArr->pBase + i) = *(pArr->pBase + i - 1); } *(pArr->pBase + i) = val; pArr->cnt += 1; return true; }
bool delete(struct Arr *pArr, int pos, int *pVal) { if (is_empty(pArr)) { printf("数组为空\n"); return false; } if(pos > pArr->cnt - 1){ printf("不满足删除条件\n"); return false; } *pVal = *(pArr->pBase + pos); int i = 0; for (i = pos; i < pArr->cnt; i++) { *(pArr->pBase + i) = *(pArr->pBase + i + 1); } pArr->cnt -= 1; return true; }
void inversion(struct Arr *pArr) { int t = (pArr->cnt) / 2; int tmp = 0; for (int i = 0; i < t; i++) { tmp = *(pArr->pBase + i); *(pArr->pBase + i) = *(pArr->pBase + pArr->cnt - 1 - i); *(pArr->pBase + pArr->cnt - 1 - i) = tmp; } }
void sort(struct Arr *pArr) { int tmp = 0; for (int i = 0; i < pArr->cnt; i++) { for (int j = pArr->cnt - 1; j - 1 >= i; j--) { if (*(pArr->pBase + j) < *(pArr->pBase + j - 1)) { tmp = *(pArr->pBase + j); *(pArr->pBase + j) = *(pArr->pBase + j - 1); *(pArr->pBase + j - 1) = tmp; } } } }
|