Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7549925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:04:50+00:00 2026-05-30T10:04:50+00:00

I cannot get my code to work! I am trying to implement the quick

  • 0

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 */
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:04:52+00:00Added an answer on May 30, 2026 at 10:04 am

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When running the following code I get no output but I cannot work out
I'm trying to create a piece of code but cannot get it working. The
I have been trying to get the following code to work(everything is defined in
I cannot seem to get my try/catch to work correctly. When you implement a
I've been trying to get this code to work for what feels like an
I have the following code in html, I cannot get the Function call back
I cannot get a two-way bind in WPF to work. I have a string
I cannot get the internet explorer web developer tool bar to work with a
I cannot get Bindable LINQ to work with VB.NET for the life of me.
I am trying to get AutoComplete to work on a website application I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.