【问题标题】:Why linked list are more memory efficient than arrays? [closed]为什么链表比数组更节省内存? [关闭]
【发布时间】:2020-04-16 19:32:18
【问题描述】:

我们知道链表不仅存储数据,还存储节点的地址,那么它们如何比数组更节省内存呢?

【问题讨论】:

  • 这太模糊了。使用逻辑来决定条件是什么并采取适当的行动。
  • 你必须使用if/else if 构造。

标签: arrays algorithm linked-list


【解决方案1】:

你想要这样的东西吗?

伪代码:

if (condition1 && condition2)
  do_task1;   // both conditions are true
else if (condition1)
  do_task1;   // only ondition 1 is true
else if (condition2)
  do_task2;   // only condition 2 is true

// if neither condition1 nor condition2 is true do nothing

读者练习:进一步简化。

【讨论】:

    【解决方案2】:

    您的答案是使用else if

    考虑这个通用函数:

    int SetResetLatch(int input, int set, int reset) {
      if (reset)
        return 1;
      else if (set)
        return 0;
      else
        return input;
    }
    

    在这种情况下,reset 具有优先权。如果resetset 都设置了,则reset 具有优先权。

    【讨论】:

      猜你喜欢
      • 2017-11-20
      • 2021-12-16
      • 1970-01-01
      • 2013-04-03
      • 2011-07-23
      • 2016-08-29
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      相关资源
      最近更新 更多