Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

Not understanding Linked List implementation please help.C++

Can you please explain to me the lines that i have marked with '/'.It would mean the world to me!

void createnode(int value)
node *temp=new node;//
temp->data=value;//
temp->next=NULL;//

Code below added from comment from OP:

void nodecreate(int value)
{
   node *temp=new node;// 
   temp->data=value;/// 
   temp->next=NULL;/// 
   if(head==NULL) 
   { 
      head=temp; 
      tail=temp; 
      temp=NULL; 
   } 
   else 
   { 
      tail->next=temp; 
      tail=temp; 
   } 
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...