Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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 counter = 0;
- unsigned int index = 0;
- int solved_list[size];
- // Create a new list without the target number
- for (int i = 0; i < size; i++) {
- if (list[i] == target) {
- counter++;
- } else {
- solved_list[index++] = list[i];
- }
- }
- // Append the appropriate number of targets to the end
- for (int i = index; i < size; i++) {
- solved_list[i] = 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement