Creating a linked list; need to know good practices.
-
I'm creating a linked list that stores two strings in each node. I quickly drafted the code as:
struct SongInfo { CString Title; CString Address; SongInfo* next; }; void CreateList() { // not an actual function, just to illustrate my allocation ... if (bINeedAnother) { cur->next = new SongInfo; cur = cur->next; } ... }
I am using CStrings since the length of the strings is unknown. However, since once they are initialized they don't change, I am thinking I should change it to dynamically allocated WCHAR pointers, with the appropriate length for each string. Or is there another practice that would be better? The other question is on the allocation of the nodes. The number of nodes can be as few as 10, and I've seen as much as 300. Is it a bad idea to allocate one node at a time (i.e. can I rely on Windows to keep the memory unfragmented)? If so, what is a good practice here? (Sorry for the noobish questions, my programming professor sucked, and her linked lists ALWAYS crashed, and she ALWAYS blamed the OS.) -
I'm creating a linked list that stores two strings in each node. I quickly drafted the code as:
struct SongInfo { CString Title; CString Address; SongInfo* next; }; void CreateList() { // not an actual function, just to illustrate my allocation ... if (bINeedAnother) { cur->next = new SongInfo; cur = cur->next; } ... }
I am using CStrings since the length of the strings is unknown. However, since once they are initialized they don't change, I am thinking I should change it to dynamically allocated WCHAR pointers, with the appropriate length for each string. Or is there another practice that would be better? The other question is on the allocation of the nodes. The number of nodes can be as few as 10, and I've seen as much as 300. Is it a bad idea to allocate one node at a time (i.e. can I rely on Windows to keep the memory unfragmented)? If so, what is a good practice here? (Sorry for the noobish questions, my programming professor sucked, and her linked lists ALWAYS crashed, and she ALWAYS blamed the OS.)1. using CString is ok. 2. usually, when building linked lists, we (me) allocate one node at a time; Best practice in 2008 would be to use STL (list or vector or dequeue).
Maximilien Lincourt Your Head A Splode - Strong Bad
-
1. using CString is ok. 2. usually, when building linked lists, we (me) allocate one node at a time; Best practice in 2008 would be to use STL (list or vector or dequeue).
Maximilien Lincourt Your Head A Splode - Strong Bad