Originally Posted by
JorgeChemE
This is a hell!
This is only a Hell if you are new in the technique of linked list.
If you have programmed some weeks (or months) with it, you collect practice and linked lists will be a absolutly normal thing for you.
Here is the logic of linked list (and also double linked).
Linked List:
You can allways start at 'head' and jump from struct to struct (with '*next') until you find what you searching for.
'tail' is only a helper for append new struct, but you can not go backward.
Code:
. ––––––– ––––––– ––––––– ––––––– –––––––
*head-->| data | | data | | data | | data | | data |<--*tail
| … | | … | | … | | … | | … |
| *next |-->| *next |-->| *next |-->| *next |-->| *next |--> NULL
––––––– ––––––– ––––––– ––––––– –––––––
Double Linked List:
You can start at 'head' and jump forward (with '*next') or you can start at 'tail' and jump backward (with '*prev') until you find what you searching for.
Code:
. ––––––– ––––––– ––––––– ––––––– –––––––
*head-->| data | | data | | data | | data | | data |<--*tail
| … | | … | | … | | … | | … |
| *next |-->| *next |-->| *next |-->| *next |-->| *next |--> NULL
NULL <--| *prev |<--| *prev |<--| *prev |<--| *prev |<--| *prev |
––––––– ––––––– ––––––– ––––––– –––––––
It is very important that you never lose a pointer! If you lose a pointer you can never free the allocated memory!
A lost pointer is automaticly a memory leak!