[Solved] Not understanding Linked List implementation please help.C++


In my experience even long time programmers doesn’t seem to really grasp linked lists and most try to avoid the afford to get into it since they are part of the standard library and – the fact that some need a full linked list example to understand your question, might reflect that.

It took me 17 books and a few youtube videos to really understand what is going on – 16 were explaining it similar and the last one got different approaches to it and bottom line it is really simple and it really is worth to dig into it.

To get to the point:

void createnode(int value) //Function for adding a new node to the list
node *temp=new node;//Standard code dynamically allocating a new address stored in "temp" to a new struct "node"
temp->data=value;//This writes the new value passed over to the function in the new created struct
temp->next=NULL;//This writes "NULL" in the "next" variable of the new created struct to mark it as the last node when you search through the whole linked list.

(Since cpp11 you should use nullptr instead of NULL for several reasons.)

I recommend working through “Jump to C++ from Alex Allain” if you really want to grasp the topic since that was book no 17 for me and pointers got 6 chapters. He also claims that about 40% of his c++ projects need self created maps and linked lists where the std lib can’t or shouldn’t be used for several reasons. (He was teaching c++ introductory courses at Harvard if someone is after “dog tags/credentials”).

What really helped me understanding linked lists is the following:

  • find new ways to represent data of your code (a)
  • work through programs in small chunks – chop up the code (b)

One unconventional visual example for (a) I came up with that also helped me understanding search and sort algorithms putting some “party drink plastic cups” up that represents variables and some “post it’s” that represent the data that goes through it.
Than I went through the code, put a variable name with a “post it” on the plastic cup, put the data (ints, chars, strings etc.) on different colored “post it’s” and put it from one cup to an other as the code said. (Maybe a fun new year nerd party game doing this with different liqueurs instead of post its 😉 )

Another would be a more common one just writing up the stack and data like explained here.

(b) should be self explaining – don’t try to grasp the whole linked list at once. Pick one module after another – the smaller the better. Meaning: First creating logical chunks like “creating new node” “reading nodes” “deleting nodes” than smaller logical chunks like “first node in an empty list” “second node” and so on.

0

solved Not understanding Linked List implementation please help.C++