双重链接列表和树:使用或不使用? [等候接听]

时间:2022-03-17 19:55:09

I've always been taught that the more pointers you need in a piece of code, the less elegant it is. Also, I know that the only data structure that you 'need' is a singly linked tree. As a result, I've always tried my best to avoid such atrocities as doubly linked lists and doubly linked trees.

我总是被告知,在一段代码中你需要的指针越多,它就越不优雅。此外,我知道您需要的唯一数据结构是单链接树。因此,我总是尽力避免像双重链接列表和双重链接树这样的暴行。

Is this true? Is code really inelegant if you use pointers and doubly linked structures where fewer pointers or a singly linked structure is sufficient?

这是真的?如果你使用指针和双重链接结构,而较少的指针或单个链接结构就足够了,代码是否真的不优雅?

EDIT: Turns out there IS a clever way to avoid the doubly linked tree, by the way.

编辑:顺便说一下,有一个聪明的方法可以避免双重链接的树。

3 个解决方案

#1


5  

Your question has a semantic flaw: excessive X is too much. Too much is always a bad thing.

你的问题有一个语义缺陷:过多的X太多了。太多总是坏事。

Any operation which can be done with pointers can also be done with arrays. That doesn't mean it should be. Lists, trees, queues: all have elegance when applied to suitable problems. Just because pointers were the last and most advanced topic in a CS class doesn't mean they should be used in all programs.

任何可以用指针完成的操作也可以用数组完成。这并不意味着它应该是。列表,树木,队列:应用于合适的问题时都具有优雅。仅仅因为指针是CS类中最后一个也是*的主题并不意味着它们应该在所有程序中使用。

While pointers are a frequent source of programming errors, so are integer assignments. That doesn't mean we should avoid using either of them. Just write code which uses them in a straightforward and understandable manner.

虽然指针是编程错误的常见来源,但整数赋值也是如此。这并不意味着我们应该避免使用它们中的任何一个。只需编写以简单易懂的方式使用它们的代码。

#2


4  

I'd say that your code is inelegant if it's got pointers sprinkled throughout.

我会说你的代码是不优雅的,如果它的指针遍布各处。

However, a well written data structure will abstract the fact that it's using pointers away from you, the client. As long as YOUR code isn't littered with pointers, and the data structure's API is minimal yet complete, I would have no qualms about using them.

但是,一个编写良好的数据结构将抽象出它使用指针远离你的客户端这一事实。只要你的代码没有乱七八糟的指针,并且数据结构的API很小而且完整,我对使用它们没有任何疑虑。

As a matter of fact, I'd criticize anybody who eschewed data structures for a reason such as "too many pointers". I don't know who taught you that, but I'd unlearn it quickly. Use the right data structure for the task at hand and don't worry about pointers.

事实上,我批评任何因为“太多指针”而避开数据结构的人。我不知道是谁教过你的,但我很快就会忘掉它。为正在进行的任务使用正确的数据结构,不要担心指针。

#3


2  

The elegance is not not from the layout of the code, but also from the underlying functionality in the code.

优雅不是来自代码的布局,而是来自代码中的底层功能。

Tree structures(e.g. a lot of famous binary search trees, disjoint sets) are actually very elegant and efficient.

树结构(例如,许多着名的二叉搜索树,不相交的集合)实际上非常优雅和高效。

#1


5  

Your question has a semantic flaw: excessive X is too much. Too much is always a bad thing.

你的问题有一个语义缺陷:过多的X太多了。太多总是坏事。

Any operation which can be done with pointers can also be done with arrays. That doesn't mean it should be. Lists, trees, queues: all have elegance when applied to suitable problems. Just because pointers were the last and most advanced topic in a CS class doesn't mean they should be used in all programs.

任何可以用指针完成的操作也可以用数组完成。这并不意味着它应该是。列表,树木,队列:应用于合适的问题时都具有优雅。仅仅因为指针是CS类中最后一个也是*的主题并不意味着它们应该在所有程序中使用。

While pointers are a frequent source of programming errors, so are integer assignments. That doesn't mean we should avoid using either of them. Just write code which uses them in a straightforward and understandable manner.

虽然指针是编程错误的常见来源,但整数赋值也是如此。这并不意味着我们应该避免使用它们中的任何一个。只需编写以简单易懂的方式使用它们的代码。

#2


4  

I'd say that your code is inelegant if it's got pointers sprinkled throughout.

我会说你的代码是不优雅的,如果它的指针遍布各处。

However, a well written data structure will abstract the fact that it's using pointers away from you, the client. As long as YOUR code isn't littered with pointers, and the data structure's API is minimal yet complete, I would have no qualms about using them.

但是,一个编写良好的数据结构将抽象出它使用指针远离你的客户端这一事实。只要你的代码没有乱七八糟的指针,并且数据结构的API很小而且完整,我对使用它们没有任何疑虑。

As a matter of fact, I'd criticize anybody who eschewed data structures for a reason such as "too many pointers". I don't know who taught you that, but I'd unlearn it quickly. Use the right data structure for the task at hand and don't worry about pointers.

事实上,我批评任何因为“太多指针”而避开数据结构的人。我不知道是谁教过你的,但我很快就会忘掉它。为正在进行的任务使用正确的数据结构,不要担心指针。

#3


2  

The elegance is not not from the layout of the code, but also from the underlying functionality in the code.

优雅不是来自代码的布局,而是来自代码中的底层功能。

Tree structures(e.g. a lot of famous binary search trees, disjoint sets) are actually very elegant and efficient.

树结构(例如,许多着名的二叉搜索树,不相交的集合)实际上非常优雅和高效。