将代码放置在“if”条件之间陈述
我目前有一个 if 语句,从函数内执行,看起来像这样,但它无法编译,尽管我知道这是因为我在条件 2 和 3 之间执行的代码所致
。要做的是创建一个函数,将新节点插入到整数排序链表中的正确位置。这样做,我需要测试三个条件。第一个是列表是否为空。如果是,则满足条件1
,一切都很好。第二个条件是当前列表中是否只有一个节点。如果是这种情况,则满足条件2
,一切都很好。
现在我们来解决问题了。如果不满足前两个条件,则唯一的其他可能性是列表包含至少两个节点。这种情况下,需要初始化两个临时指针,一个指向Head
,一个指向Head ->
以便跟踪列表中的当前位置并方便将新节点插入到列表中。Head
。 Next
这些是使用放置在 condition2
和 condition3
之间的代码进行初始化的。必须创建它们,因为 condition3
依赖于它们,但在 condition1
之前创建它们会导致分段错误。
谁能告诉我如何实施这样的声明,或者是否可能?我想让代码尽可能简单,而我现在拥有的功能齐全的 LinkedList :: Insert()
函数是一堆 if
语句,我在执行某些代码时遇到困难。
int NewElement;
Node *NewNode;
NewNode = new Node;
NewNode -> Element = NewElement;
Node *TempPrevious;
Node *TempNext;
if (ListIsEmpty) // condition1
{
// some code
return true;
}
else if (ListContainsOnlyOneNode) // condition2
{
// some code
return false;
}
TempPrevious = Head;
TempNext = Head -> Next;
else if (NewNode -> Element > TempNext -> Element) // condition3
{
// some code
return true;
}
I currently have an if statement, executing from within a function, that looks like this, but which doesn't compile, although I know it's because of the code I'm executing in between conditions 2 and 3.
What I'm trying to do is create a function that will inserted a new node into a sorted linked list of integers at the correct position. Do do this, I need to test for three conditions. The first is whether or not the list is empty. If it is then condition1
is satisfied and all is good. The second condition is whether or not there is only a single node currently in the list. If this is the case, then condition2
is satisfied and again all is good.
Now we come to the problem. If the first two conditions are not satisfied, then the only other possibility is that the list contains at least two nodes. In this case, the two temporary pointers need to be initialised, one pointing to Head
and one to Head -> Next
in order to keep track of the current position in the list and facilitate the insertion of the new node into the list.
These are initialised using the code the is placed between condition2
and condition3
. These have to be created because condition3
relies on them, but creating them before the condition1
would result in a segmentation fault.
Can anyone advise me on how to go about implementing such a statement, or if it's even possible? I want to keep the code as simple as possible, and the fully functioning LinkedList :: Insert()
function I have right now is a mess of if
statements and I'm having trouble following some of the code.
int NewElement;
Node *NewNode;
NewNode = new Node;
NewNode -> Element = NewElement;
Node *TempPrevious;
Node *TempNext;
if (ListIsEmpty) // condition1
{
// some code
return true;
}
else if (ListContainsOnlyOneNode) // condition2
{
// some code
return false;
}
TempPrevious = Head;
TempNext = Head -> Next;
else if (NewNode -> Element > TempNext -> Element) // condition3
{
// some code
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这……其实很简单。因为您要从每个块
返回
,所以根本不需要else
!This is... actually really easy. Because you're
return
ing from each block, you don't needelse
at all!删除最后一个
else
,我认为它会按照你的意愿工作。Remove the last
else
and I think it will work as you like.