不了解链表的实现请帮助.C++ [英] Not understanding Linked List implementation please help.C++

查看:71
本文介绍了不了解链表的实现请帮助.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;//

以下代码是从OP的评论中添加的:

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; 
   } 
}

推荐答案

根据我的经验,即使是很长一段时间的程序员也似乎并没有真正掌握链表,并且大多数人都尽量避免花钱买它,因为它们是链表的一部分.标准库,而且-有些人需要完整的链接列表示例才能理解您的问题,这一事实可能反映了这一点.

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.

我花了17本书和一些youtube视频来真正了解正在发生的事情-16在解释上是相似的,最后一个对它的用法有所不同,底线非常简单,这确实值得深入研究它.

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.

要切入重点:

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.

(由于cpp11,出于多种原因,您应使用nullptr而不是NULL .)

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

如果您真的想抓住这个话题,我建议您通读从Alex Allain跳到C ++",因为那对我来说是第17本书,指针有6章.他还声称,大约40%的c ++项目需要自创建的映射和链接列表,出于某些原因,不能或不应该使用std lib.(如果有人追求狗牌/证书",他将在哈佛教授c ++入门课程).

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:

  • 找到表示代码数据的新方法(a)
  • 一小段地遍历程序-砍掉代码(b)

关于(a)的一个非常规视觉示例,这也帮助我理解了搜索和排序算法,其中放了一些代表变量的派对酒杯"和一些代表通过它的数据的张贴".然后我完成了代码,在塑料杯上放了一个带有post it"的变量名,将数据(整数、字符、字符串等)放在不同颜色的post it's"上,然后将它从一个杯子放到另一个杯子如代码所示.(也许是一个有趣的新年书呆子派对游戏,用不同的利口酒来做这件事,而不是发布它;))

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

另一种可能是更常见的一种,就是像在此处那样写栈和数据.a>.

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

(b)应该自我解释-不要试图立即掌握整个链表.选择一个接一个的模块-越小越好.含义:首先创建较小的逻辑块(例如创建新节点",读取节点",删除节点"),而不是创建较小的逻辑块,例如空列表中的第一个节点",第二个节点",等等.

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

这篇关于不了解链表的实现请帮助.C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆