![]() |
![]() |
![]() |
Linked lists alternatives? I'm a C programmer and I "get" pointers. I like them quite a bit. But I'm really not too fond of is linked lists. Most of the time their complexity isn't needed. Yeah, they're necessary if you have to insert or remove stuff at any point or do lot's of searching or sorting, but if all you want is a linear list of stuff that you'll process in a sequential order, what alternatives are there and what do you prefer?
The Mountain Man of Park Avenue.
I use std::list when I have to insert and remove stuff a lot, and read/sort sometimes. I use std::vector when I have to insert/remove stuff almost never, and read/sort frequently.
Kalani
If you know in advance the number of elements, why not just malloc() a simple array? Actually I prefer calloc() as it will clear the space and so you can be sure that it's really been allocated. Even if the number of elements changes you can always realloc() it, although clearly you don't want to do that too often.
fred
If you know that SOME_STUFF is going to be stored on a list, then you can put a pointer inside the struct itself:
Christopher Wells
Linked lists don't have any more complexity in day-to-day usage than anything else as long as you have a collection class and iterators to hide the complexity. Why people refuse to do this is beyond me.
anon
Mountain man said he was a C programmer, not a C++ programmer.
Glade Warner
In plain flat C, I think I'd probably use arrays of structures and let the compiler worry about the addressing offsets if I didn't care the order in which members of the list were stored.
Simon Lucy
There exist numerous libraries to give C some C-style collections. There is really no good reason not no use them. Unless he's a student.
Anon to protect the guilty
Sorry buddy, but if you think linked lists are full of "complexity", then you're going to be SOL when you learn about more advanced data structures (hashtables, trees, etc). Linked lists are about as simple as it gets--they're basic comp101 stuff!
Sexist
> if you think linked lists are full of "complexity"
The Mountain Man of Park Avenue.
IMHO, C is a terrible language for grinding through a list of strings. Why not use a language known for text processing like Perl? Or at least a language with built-in list/iterator capabilities like Python.
Anony Coward
Because sometimes you can't...
Jack of all
Back in my C days, I rarely used linked lists. Arrays are a lot more convenient. If I didn't know the size in advance, I started with some initial size and reallocated it to be twice is large whenever necessary.
Julian
It's pretty easy to write a generic doubly linked list implementation using macros in C.
SG
Grab list.h from the linux source code tree. If you look at it there are 2 parts: 1 is a set of macros for dorking with doubly linked lists. The other optimizes memory caching. Delete the caching crap and you have some really nifty tools for working with linked lists.
Snotnose
OP: 'a linear list of stuff that you'll process in a sequential order'
Lee
Linked lists have their places.
i like i
|