【发布时间】:2020-04-16 19:32:18
【问题描述】:
我们知道链表不仅存储数据,还存储节点的地址,那么它们如何比数组更节省内存呢?
【问题讨论】:
-
这太模糊了。使用逻辑来决定条件是什么并采取适当的行动。
-
你必须使用
if/else if构造。
标签: arrays algorithm linked-list
我们知道链表不仅存储数据,还存储节点的地址,那么它们如何比数组更节省内存呢?
【问题讨论】:
if/else if 构造。
标签: arrays algorithm linked-list
你想要这样的东西吗?
伪代码:
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
读者练习:进一步简化。
【讨论】:
您的答案是使用else if。
考虑这个通用函数:
int SetResetLatch(int input, int set, int reset) {
if (reset)
return 1;
else if (set)
return 0;
else
return input;
}
在这种情况下,reset 具有优先权。如果reset 和set 都设置了,则reset 具有优先权。
【讨论】: