简体   繁体   中英

My teacher made this function but i don't understand what's the use of it

I'm practising linked lists. Our teacher showed us the standard functions to build new nodes and lists .

In the following function i dont understand what's the use of the secondary function infoGreater.

This is how the function infoGreater is built :

int infoGreater (TInfo info1, TInfo info2) {
    return  info1 > info2;
}

I mean what is that return . I don't get it . What does it return ? And why it says info1>info2 ? What's that

This is the primary function instead:

TList listInsert(TList list, TInfo info) {
    TNode *node = nodeCreate(info);
    assert (node != NULL);
    TNode *prec = NULL, *succ = list;
    while (succ != NULL && infoGreater(info, succ->info)) {
        prec = succ;
        succ = succ->link;
    }
    node->link = succ;
    if (prec == NULL) 
        list = node;
    else
        prec->link = node;
    return list;
}

The main doubt is what return info1>info2; does? And what it returns ? Why is it necessary in the primary function listInsert ?

The function infoGreater is trivial. It is a bit confusing since some implicit type casts are missing. The return type of the function is int which is pretty typical for storing a boolean value.

the function returns a boolean value. 1 (or theoretically any other non-zero) if info1 is bigger or 0 otherwise.

the code is equivalent to:

int infoGreater (TInfo info1, TInfo info2) {
    if (info1 > info2)
        return 1;
    else
        return 0;
}

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