Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Creating a linked list; need to know good practices.

Creating a linked list; need to know good practices.

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresperformance
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    x87Bliss
    wrote on last edited by
    #1

    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.)

    M 1 Reply Last reply
    0
    • X x87Bliss

      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.)

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • M Maximilien

        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

        N Offline
        N Offline
        ninjanear
        wrote on last edited by
        #3

        STL containers are well optimized and you have several types where choose. A good practice is using STL (at least, it's my opinion) Dinamic resizing of vectors was a great help for me some years ago.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups