realloc
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 | #include <stdio.h> #include <stdlib.h> char** Create_Array(int horizon, int vertical); void Delete_Array(char** _Array, int vertical); int Optimize_Vertical_Size(char*** _Array, int vertical); void Display_Array(char** _Array); int Input_Array(char** _Array, int vertical); void bubble_sort(char** array, int size); void swapping(char** current, char** next); int main(void) { int horizon = 30, vertical = 10; char** _Array = (char**)Create_Array(horizon, vertical); // Dynamic memory allocation int Number_Of_Input = Input_Array(_Array, vertical); if (Number_Of_Input < 0) return -1; bubble_sort(_Array, Number_Of_Input); Display_Array(_Array); Delete_Array(_Array, Number_Of_Input + 1); // free memory return 0; } void bubble_sort(char** array, int size) { int allElement = 0, partialElement = 0; for (allElement = 0; allElement < size; allElement++) { for (partialElement = 0; partialElement < size - 1; partialElement++) { if (strcmp(*(array + partialElement), *(array + partialElement + 1)) > 0) { swapping(&(*(array + partialElement)), &(*(array + partialElement + 1))); } } partialElement++; } return; } void swapping(char** current, char** next) { char* temporary = (char)0; temporary = *current; *current = *next; *next = temporary; return; } int Input_Array(char** _Array, int vertical) { if (_Array == (char)0) { printf("Error! The array is null. Input_Array()\n"); return -1; } int index = 0; for (index = 0; index < vertical; index++) { scanf("%s", *(_Array + index)); getchar(); if (*(*(_Array + index) + 0) == '0') break; } if (index == vertical) return index; *(*(_Array + index) + 0) = (char)0; int result = Optimize_Vertical_Size(&_Array, index); if (result < 0) return -2; return index; } void Display_Array(char** _Array) { int index = 0; for (index = 0; *(*(_Array + index) + 0) != (char)0; index++) { printf("%s\n", *(_Array + index)); } return; } void Delete_Array(char** _Array, int vertical) { int index = 0; for (index = 0; index < vertical; index++) { free(*(_Array + index)); } free(_Array); return; } int Optimize_Vertical_Size(char*** _Array, int vertical) { if (*_Array == (char)0) { printf("Error! The array is null. Optimize_Vertical_Size()\n"); return -1; } *_Array = (char**)realloc(*_Array, (vertical + 1) * sizeof(char*)); if (_Array == (char)0) { printf("Error! Dynamic memory allocation. Optimize_Vertical_Size()\n"); return -2; } return 1; } char** Create_Array(int horizon, int vertical) { if (horizon < 1 || vertical < 1) { printf("Error! Input zero or more. Create_Array()\n"); return (char)0; } vertical++; char** Return_Array = (char**)calloc(vertical, sizeof(char*)); if (Return_Array == (char)0) { printf("Error! Dynamic memory allocation(1). Create_Array()\n"); return (char)0; } int index = 0; for (index = 0; index < vertical; index++) { *(Return_Array + index) = (char*)calloc(horizon, sizeof(char)); if (*(Return_Array + index) == NULL) { printf("Error! Dynamic memory allocation(2). Create_Array()\n"); return (char)0; } } return (char**)Return_Array; } | cs |
0 개의 댓글:
댓글 쓰기