Earlier quoted context omitted.
Not everyone went to university, and people have to learn from somewhere. It is also only an introduction, there will be more complicated data structures shown soon, and algorithms. No point in jumping into the heavier stuff. Trying to help people who are new to C.
Is it appropriate for Hacker News though? It fits better on http://www.reddit.com/r/learnprogramming
Linked lists in C, with open source ADT
11–20 of 43 posts
Re: Linked lists in C, with open source ADT
#12On the front page, "test.c" is listed twice. Should it be "list.c" ?
Re: Linked lists in C, with open source ADT
#13This isn't how I'd do a linked list in C. Having to malloc separate storage for the next/prev pointers imposes a big cost in terms of performance (it's hardly O(1)) and robustness. Your linked list operations can all fail now - sooner or later, that's going to put you in a very tight spot. I prefer to store next/prev pointers with the data itself, and to use offsetof/containerof to find a pointer to the data struct w…
Re: Linked lists in C, with open source ADT
#14The book _C Interfaces and Implementation_ is nothing but library interfaces like this and their implementations. You are, for whatever it's worth, better off defaulting to a variable-length array than to a linked list.
Someone wants to know about linked lists, and your comment is to use some other data structure that has a different purpose?
Re: Linked lists in C, with open source ADT
#15This isn't how I'd do a linked list in C. Having to malloc separate storage for the next/prev pointers imposes a big cost in terms of performance (it's hardly O(1)) and robustness. Your linked list operations can all fail now - sooner or later, that's going to put you in a very tight spot. I prefer to store next/prev pointers with the data itself, and to use offsetof/containerof to find a pointer to the data struct w…
This problem is why you're better off with something like a C++ vector (a variable-length array).
Re: Linked lists in C, with open source ADT
#16Earlier quoted context omitted.
Someone wants to know about linked lists, and your comment is to use some other data structure that has a different purpose?
Different purpose?
Re: Linked lists in C, with open source ADT
#17Earlier quoted context omitted.
This problem is why you're better off with something like a C++ vector (a variable-length array).
It really does depend on what you need to be doing in order to make the assumption of what data structure is needed. A vector is all well and good for certain tasks, but sometimes, a list is what you need. I will cover vectors too, but for now I am focused on C. not C++. Thanks though.
Re: Linked lists in C, with open source ADT
#18Earlier quoted context omitted.
Different purpose?
Linked lists are good for adding and removing elements. Variable length arrays are good for allocating a known amount of space at runtime.
Re: Linked lists in C, with open source ADT
#19Thanks.
Re: Linked lists in C, with open source ADT
#20The book _C Interfaces and Implementation_ is nothing but library interfaces like this and their implementations. You are, for whatever it's worth, better off defaulting to a variable-length array than to a linked list.
really tptacek? its O(1) ammortized over time, and when N grows it becomes really slow because it involves reallocation and a linear copy of all elements.