Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // list - list of integers
- // size - size of the list
- // target - number to put at the end
- void move_to_back(int *list, size_t size, int target) {
- unsigned int index = 0;
- unsigned int index_target = 0;
- int solved_list[size];
- // Sort array
- for (unsigned int i = 0; i < size; i++) {
- if (list[i] != target) {
- solved_list[index++] = list[i];
- } else {
- solved_list[size-1 - index_target++] = target;
- }
- }
- // Copy solved_list to list (Do I need a free() here?)
- memcpy(list, solved_list, sizeof(solved_list));
- }
- int main() {
- int list[9] = {1,9,7,4,9,3,2,6,9};
- move_to_back(list, 9, 9);
- for (int i = 0; i < 9; i++)
- printf("%d ", list[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement