Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Implement a program to add to the end of an element and delete the last element to the dynes of the array
- // Implement the extension, deletion, and compression routines to the minimum
- // necessary volume of one-dimensional din. array of integers with adaptive increase.
- // In a situation where you need to request memory, request 20% more than necessary.
- #include <stdio.h>
- #include <stdlib.h>
- int *my_malloc(int size)
- {
- int *array;
- array = (int*)malloc(size * sizeof(int));
- if (array == NULL)
- return NULL;
- return array;
- }
- void add(int **array, int *size, int new_value)
- {
- *array = (int*)realloc(*array, (*size + 1) * sizeof(int));
- *size = *size + 1;
- (*array)[*size - 1] = new_value;
- }
- void delete(int **array, int *size)
- {
- *array = (int*)realloc(*array, (*size - 1) * sizeof(int));
- *size = *size - 1;
- }
- void print_array(int *array,int size)
- {
- for (int i = 0; i < size; i++)
- printf("%d ", array[i]);
- printf("\n");
- }
- int *expand(int *arr, int *new_size, int *max_size)
- {
- int *tmp;
- *max_size = (int) *new_size * 1.2;
- tmp = realloc(arr, sizeof(int) * (*max_size));
- if (!tmp)
- return NULL;
- return tmp;
- }
- void remove_arr(int *arr)
- {
- free(arr);
- }
- int *fit(int *arr, int *actual_size)
- {
- int *tmp;
- tmp = realloc(arr, sizeof(int) * (*actual_size));
- if (!tmp)
- return NULL;
- return tmp;
- }
- int main()
- {
- int *array;
- int size = 5;
- array = my_malloc(size);
- for (int i = 0; i < size; i++)
- array[i] = i;
- print_array(array, size);
- add(&array, &size, 5);
- print_array(array, size);
- delete(&array, &size);
- print_array(array, size);
- delete(&array, &size);
- print_array(array, size);
- free(array);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement