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

  • Home
  • SEARCH
  • 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 6339407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:38:35+00:00 2026-05-24T19:38:35+00:00

I am trying to sort a singly linked list which has been created and

  • 0

I am trying to sort a singly linked list which has been created and all its items,pointers initialized. I am trying to use qsort() C library function as shown below. It doesn’t seem to sort the list. It is giving me compiler error saying:
left of ‘item’ specifies undefined struct/union ‘LINKED_LIST_S’ at line shown below in the code.

struct LINKED_LIST_S
{
         int item;
         struct LINKED_LIST_S * next; 
 } ;

typedef int (*cmpfn)(const void *ptr1, const void *ptr2);

int mylistsort(my_list_t list, cmpfn f1)
{

    qsort ( list , 100, sizeof (struct LINKED_LIST_S), (fn) );
   return -1;
}


int sort_fn_ascend(const void *ptr1, const void *ptr2)
{
    int a = (*(LINKED_LIST_S *)ptr1).item; //Compiler error
   int b = (*(LINKED_LIST_S *)ptr2).item; //Compiler error

  return b - a;

}

int sort_fn_descend(const void *ptr1, const void *ptr2)
{   
   int a = ((struct LINKED_LIST_S *)ptr1)->item; //Compiler error
   int b = ((struct LINKED_LIST_S *)ptr2)->item; //Compiler error

  return a - b;

}

this is how the function mylistsort() is called at 2 places:

mylistsort(list, sort_fn_descend);//where list a properly initialized linked list pointer.

mylistsort(list, sort_fn_ascend);//where list a properly initialized linked list pointer.

1] Doesn’t qsort() work on linked list if the head node poitner is passed as base array(first argument) to qsort.

2] How do I achieve the sorting of this linked list using qsort() in above code?

EDIT: Thanks for the answers. Now I have implemented a way to sort a list as mentioned by @muksie approach 1] he suggested, as code below. Still qsort() does not return a sorted array of ints.
First I want to sort an array of 100 elements, in descending order, but passed array is exact reverse, i.e. the elements are in ascending order of from 1,2,…100. What am I doing wrong? How can I fix it?

int linkedListSort(LINKED_LIST_T list, newCompareFunction fn, void *usr_info)
{
    LINKED_LIST_T tmpptr = list;
    int newitems[N_ITEMS];
    int i=0;

    //Logic to get all the items in the list in a array
    while(tmpptr != NULL)
    {
        newitems[i] = tmpptr->item;
        tmpptr = tmpptr->next;
        i++;
    }
    tmpptr = list;
    i = 0;
    //Sort that array
    //qsort ( list , 100, sizeof (struct LINKED_LIST_S), (fn) );
    qsort ( newitems , 100, sizeof(list->item), (fn) );

    //store the sorted items back into the list.
    while(tmpptr != NULL)
    {
        tmpptr->item = newitems[i];
        tmpptr = tmpptr->next;
        i++;
    }

   return -1;
}

int sort_fn_descend(void *ptr1,void *ptr2)
{   
   int a = *((int*)(ptr1));
   int b = *((int*) (ptr2));

  return a - b;

}


int sort_fn_ascend(const void *ptr1, const void *ptr2)
{
    int a = *((int*)(ptr1));
   int b = *((int*) (ptr2));

  return b - a;

}
  • 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-24T19:38:36+00:00Added an answer on May 24, 2026 at 7:38 pm

    Since I was able to solve the problem, using approach 2] suggested by @muskie in his answer, I am posting the answer here.
    A bug fix in functions sort_fn_descend/ascend, mentioned in the edited OP was needed , and the code is sorting the list jsut fine. Bug fix FYI – the descend function above should return b – a and ascend fn should return a – b.

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

Sidebar

Related Questions

I am trying to sort a dict based on its key and return an
I am trying to sort a list using delegates but I am getting a
I am trying to write a JTextPane which supports some sort of coloring: as
I'm trying to order items based on an attribute value: <xsl:apply-templates select=Question> <xsl:sort order=ascending
Hey all. Im trying to sort out how to get MATLAB running as best
I am trying to run sort example on Hadoop single-node cluster. First of all,
I saw an earlier post which is trying to do a similar sort of
I'm looking at the MySQL docs and trying to sort out the distinction between
I'm somewhat new to the ASP.NET MVC architecture and I'm trying to sort out
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound

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.