4 ms·
Linked lists in C, with open source ADT
- categorykni 14y agoThis is week-one stuff of a CS degree or on the job training. If you don't know this you shouldn't be programming.
- AncoraImparo 14y agoNot 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.
- deleted 14y ago[deleted]
- stuffihavemade 14y agoIs it appropriate for Hacker News though? It fits better on http://www.reddit.com/r/learnprogramming http://www.reddit.com/r/learnprogramming
- AncoraImparo 14y agoSee, thats more constructive! :) I figured it belonged here, especially since a lot of people are probably still interested in learning things like this, I was not aware of a reddit for this.
- spinlocked 14y agoDid you create a new account solely to bash this guy?
- littlejdu 14y agoI use a Chrome plugin that creates a new account each time I post a comment. I use a proxy that means HN can't track the number of accounts I've created. Keeps my privacy.
- marshray 14y agoThere's nothing wrong with someone taking the time to write a week-one CS degree blogpost.
- afhof 14y agoOn the front page, "test.c" is listed twice. Should it be "list.c" ?
- AncoraImparo 14y agoI will fix, I hadn't realized! Thankyou!
- tptacek 14y agoThe 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.
- afhof 14y agoSomeone wants to know about linked lists, and your comment is to use some other data structure that has a different purpose?
- tptacek 14y agoDifferent purpose?
- afhof 14y agoLinked lists are good for adding and removing elements. Variable length arrays are good for allocating a known amount of space at runtime.
- tptacek 14y agoThe "variable" in "variable length" describes the fact that you don't know how much space you need, and insertions to the end of a variable-length array are very fast. I can see why you thought my comment was weird, now.
- spinlocked 14y ago"insertions to the end of a variable-length array are very fast" 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.
- marshray 14y agoYou have a magic allocator for your list nodes that does better than O(1) amortized?
- chj 14y agoSome simple suggestions: 1. Please align the asterisks properly. It hurts eyes. 2. Access by index? 3. Iterator (callback on each element) 4. use 'init' instead of 'initialize'. Extra typing hurts fingers.
- gosu 14y agoThis 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 which contains a given set of next/prev pointers. One example of such an scheme is the list used in the Linux kernel: http://lxr.linux.no/#linux+v3.7.3/include/linux/list.h http://lxr.linux.no/#linux+v3.7.3/include/linux/list.h http://lwn.net/Articles/336255/ http://lwn.net/Articles/336255/
- tptacek 14y agoThis problem is why you're better off with something like a C++ vector (a variable-length array).
- AncoraImparo 14y agoIt 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.
- tptacek 14y agoYou need a list when you have routine insertions somewhere besides the front or the back of the container, and you don't have routine random-access reads. The vector covers more cases and is generally more performant. Use whatever you like, though.
- spinlocked 14y agoi disagree.
- tptacek 14y ago
- AncoraImparo 14y agoSorry for posting this, it appears that HN is not the place to post content if your only goal is to help people to learn. The hacker ethic is dead I guess, Richard Stallman really was the last man standing then. Remind me to come and post something when I decide to make some silly iPhone / android app when I want to make money rather than sharing knowledge. Thanks.
- spinlocked 14y agoNot at all. Thanks for posting OP. Any technical discussion is welcome here. Ignore the haters who think STL vectors are a drop in replacement for lists.