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 3308320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:30:29+00:00 2026-05-17T21:30:29+00:00

I have a linked List c file/head as an independent library i am using

  • 0

I have a linked List c file/head as an independent library i am using for a project. I dont yet have a add ordered method in the library. My problem is writing a compare function because i want to compare on different items for different projects. How do i create a compare function in my main for whatever project i am using then pass and use that function into the add_ordered method in my linked list library? I cant seem to find a workable solution to passing in the function and using it within my linked list.

here is a uncompiled version of my add_ordered and compareto methods (compare to method will be different for each file):

void ll_add_ordered(ll_node *head, void *d){
 ll_node *cur;
 ll_node *temp;

 if(head->size == 0){
  ll_add_first(head, d);
 }else{
  temp = (ll_node *)malloc(sizeof(ll_node));
  temp->data = d;

  for(cur = head->next; cur->data != NULL && compareTo(temp->data, cur->data); cur = cur->next)
   ;

  temp->next = cur;
  temp->prev = cur->prev;
  cur->prev->next = temp;
  cur->prev = temp;

  head->size++;
 }
}

int compareTo(proc *first, proc *second){
 if(first->arrival < second->arrival)
  return -1;
 else if(first->arrival > second->arrival)
  return 1;
 else
  return 0;
}
  • 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-17T21:30:29+00:00Added an answer on May 17, 2026 at 9:30 pm

    A simple solution would be to use a function pointer, and pass the pointer to the function.

    I would recommend you declare an alias for the function signature as well, lest you get crazy after a while 🙂

    I’m assuming ll_node.data is of type void *. Thus your linked list requires two things:

    1. The comparison function takes two void pointers, compares them (using insider knowledge). The comparison function must know how to deal with the data. This requirement is of course implicitly enforced on the person/code/evil co-worker calling ll_add_ordered.
    2. The comparison function returns 1, 0 or -1.

    The type alias for such a function pointer would be:

    typedef int (*ll_comp_func)(void *, void*);
    

    If you think this looks crazy, you’re totally right. It serves to make you crazyhomicidial late nights when nothing works. Anyhow, what it really does is create a typedef for a function pointer and it calls the alias ll_comp_func.

    You would then alter your ll_add_ordered into the following form:

    void ll_add_ordered(ll_node *head, void *d, ll_comp_func comparison){
       // Do stuff.
       int order = comparison(temp->data, cur->data);
       // Do even more stuff.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of Linked list pointers. Each LinkedList has a head pointer
I have a problem either adding to or traversing a linked list. The main
I have an application that reads a linked list 1 from a file when
I have a project where I get a list of file location strings that
I have a file with a double-linked-list that contains a set of process identifiers
I'm having a problem with a doubly linked list so I have two questions.
I have a Linked List and I want to implement a function: Random_Shuffle_List (struct
I have a linked list, which stores groups of settings for my application: typedef
Say you have a linked list structure in Java. It's made up of Nodes:
I have a singly-linked list containing 1 value in each element. struct listElement {

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.