I cannot get my code to work! I am trying to implement the quick sort algorithm with linked lists. I keep getting memory leaks problems and cannot resolve them.
#include <stdio.h>
#include "linked_list.h"
#include <stdlib.h>
#include "memcheck.h"
#include <string.h>
#include <assert.h>
node *quicksort(node *list);
int ListLength (node *list);
int main(int argc, char *argv[]) {
node *list;
node *sorted_list;
int i;
int intArg = 0; /* number of integer arguments */
int print = 1;
/* if -q is found anywhere then we are going
* to change the behavior of the program */
if (argc == 1) {
fprintf(stderr, "usage: %s [-q] number1 number2 ... \
(must enter at least one argument)\n", argv[0]);
exit(1); }
for ( i = 1 ; i < argc; i++) {
if (strcmp(argv[i], "-q") == 0) {
print = 0; }
else {
list = create_node(atoi(argv[i]), list); /* memory allocation is taken care of */
intArg++; } }
if (intArg == 0) {
fprintf(stderr, "usage: %s [-q] number1 number2 ...\
(at least one of the input arguments must be an integer)\n", argv[0]);
exit(1); }
sorted_list = quicksort(list);
free_list(list);
list = sorted_list;
if (print == 1) {
print_list(list); }
print_memory_leaks();
return 0; }
node *quicksort(node *list) {
node *less=NULL, *more=NULL, *next, *temp=NULL, *end, *listCopy;
node *pivot = list;
listCopy = copy_list(list);
if (ListLength(list) <= 1) {
return listCopy; }
else {
next = listCopy->next;
listCopy = next;
/* split into two */
temp = listCopy;
while(temp != NULL) {
next = temp->next;
if (temp->data < pivot->data) {
temp->next = less;
less = temp; }
else {
temp->next = more;
more = temp; }
temp = next; }
less = quicksort(less);
more = quicksort(more); }
/* appending the results */
if (less != NULL) {
end = less;
while (end->next != NULL) {
end = end->next; }
pivot->next = more;
end->next = pivot;
return less; }
else {
pivot->next = more;
return pivot; } }
int ListLength (node *list) {
node *temp = list;
int i=0;
while(temp!=NULL) {
i++;
temp=temp->next; }
return i; }
/*Code from the libraries */
node *
create_node(int data, node *n) {
node *result = (node *)malloc(sizeof(node));
if (result == NULL) {
fprintf(stderr, "Fatal error: out of memory. "
"Terminating program.\n");
exit(1); }
result->data = data; /* Fill in the new node with the given value. */
result->next = n;
return result; }
void
free_list(node *list) {
node *n; /* a single node of the list */
while (list != NULL) {
n = list;
list = list->next;
/*
* 'n' now points to the first element of the list, while
* 'list' now points to everything but the first element.
* Since nothing points to 'n', it can be freed.
*/
free(n); } }
node *
copy_list(node *list) {
if (list == NULL) {
return list; }
else {
node *new_list;
new_list = (node *)malloc(sizeof(node));
if (new_list == NULL) {
fprintf(stderr, "Fatal error: out of memory. "
"Terminating program.\n");
exit(1); }
new_list->data = list->data;
new_list->next = copy_list(list->next);
return new_list; } }
/* other available methods are append_lists, reverse_list */
As a general answer to serve you in all such circumstances: you say you keep getting memory leaks but cannot resolve them. Have you tried using a tool to attempt to catch memory errors, though? Valgrind, for example, will catch most failures to free memory, use after free errors, etc. It is well worth learning how to use it.
(As an aside, you say that you know you’re getting memory leaks, but you do not explain how you know you are getting them.)