简体   繁体   中英

Sorted Insertion in linked list

I'm trying to create a function that does sorted insertion based on two variables, level and name . Apparently I'm having some logic and syntax errors.

My linked list structure:

struct node {
    struct node *next;
    int level;
    char name;
};

My string compare function:

int compare(struct node *one, struct node *two)
{
    return strcmp(one->name, two->name);
}

My insertion function:

void insert(struct node **head, const int level, const char name, int(*cmp)(struct node *l, struct node *r))
{
    struct node *new =NULL;
    /* Find the insertion point */
    for (; *head; head = &(*head)->next)
    {
        if ((*head)->level > level) {     // I think this is what is causing the issue
            if (compare(*head, new) > 0)
            break;
        }
    }

    new = malloc(sizeof *new);
    new->level = level;
    new->name = name;
    new->next = *head;
    *head = new;
}

and this is the call stack:

insert(node **head, const int level, const char name, int(*)(node *, node *))

您正在将NULL值传递给cmp函数(?!?可能正确的函数是int compare(...) 。在将变量的值传递给函数之前,尝试对其进行初始化。

Your syntax error is this line:

return strcmp(one->name, two->name);

The function strcmp expect two char* (aka char pointers) but you give it two char .

The problem is... Do you want

char name;

or

char* name;

That is important in order to get compare right.

Further you need to rearrange your insert function so that you create the new node before using it. Something like:

void insert(struct node **head, const int level, const char name, int(*cmp)(struct node *l, struct node *r))
{
    struct node *new =NULL;

    // Create and initialize new....
    new = malloc(sizeof *new);
    new->level = level;
    new->name = name;

    /* Find the insertion point */
    for (; *head; head = &(*head)->next)
    {
        if ((*head)->level > level) {     // I think this is what is causing the issue
            if (cmp(*head, new) > 0)
                        // ^^^ So that you can use it here

            break;
        }
    }

    new->next = *head;
    *head = new;
}

You declare node.name to be of type char , but your comparison function is written as if they were null-terminated arrays of char or pointers into such arrays (ie C strings). You appear to want this:

struct node {
    struct node *next;
    int level;
    char *name;
};

or maybe this:

struct node {
    struct node *next;
    int level;
    char name[MY_MAXIMUM_NAME_LENGTH_PLUS_ONE];
};

Furthermore, your insert() function passes a NULL pointer to the comparison function as its second argument, because you never allocate any memory for pointer new , and, of course, never assign values to the non-existent members. That doesn't even make sense. What do you think you're comparing to? You seem to want something like this:

struct node *new = malloc(sizeof *new);

if (!new) {
    // allocation failure -- abort ...
}

new->level = level;
new->name = /* hmmmm ... */;

Of course, the problem with the type of your names crops up here, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM