Comment

Comment by kccqzy

This is old-fashioned (uses C89 instead of C99) and some of its "idioms" are questionable, like this one:

    list *p;
    list l = (list) malloc(sizeof(struct list));
    l -> val = val;
    for(p = &the_list; *p && (*p) -> val < val; p = &((*p) -> next))
      ;
    l -> next = *p; *p = l;
Here p is a doubly indirect pointer and I would personally think this is an unwarranted cleverness.

Furthermore this is the year 2019. Why would you still manually traverse a linked list? Oh right because C doesn't have parametric polymorphism (genericity) so you can't abstract out the traversal in a standard library.

Consider the C++ equivalent (although I chose to use a doubly linked list for better reusability):

    list.insert(std::lower_bound(list.begin(), list.end(), val), val);
See how the search for the location to insert is done by the library, and the insertion is also done by the library. This, to me, is truly reusable and maintainable code.
parent kccqzy 6 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton