pouria pouria - 2 months ago 19
C Question

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:

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

/* Find the insertion point */
for (; *head; head = &(*head)->next)
{
if ((*head)->level > level) {
if (cmp(*head, new) > 0)
break;
}
}

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


and output:

'3rd.exe' (Win32): Loaded 'C:\Users\PBF\Documents\Visual Studio 2015\Projects\3rd\Release\3rd.exe'. Symbols loaded.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\kernel32.dll'
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbase.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'3rd.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file.
Exception thrown at 0x00F11072 in 3rd.exe: 0xC0000005: Access violation reading location 0x00000000.

Answer

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;
}
Comments