将代码放置在“if”条件之间陈述

发布于 2024-10-23 11:38:15 字数 1226 浏览 0 评论 0原文

我目前有一个 if 语句,从函数内执行,看起来像这样,但它无法编译,尽管我知道这是因为我在条件 2 和 3 之间执行的代码所致

。要做的是创建一个函数,将新节点插入到整数排序链表中的正确位置。这样做,我需要测试三个条件。第一个是列表是否为空。如果是,则满足条件1,一切都很好。第二个条件是当前列表中是否只有一个节点。如果是这种情况,则满足条件2,一切都很好。

现在我们来解决问题了。如果不满足前两个条件,则唯一的其他可能性是列表包含至少两个节点。这种情况下,需要初始化两个临时指针,一个指向Head,一个指向Head -> Head。 Next 以便跟踪列表中的当前位置并方便将新节点插入到列表中。

这些是使用放置在 condition2condition3 之间的代码进行初始化的。必须创建它们,因为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〗斷ホ乔殘χμё〖 2024-10-30 11:38:15

这……其实很简单。因为您要从每个块返回,所以根本不需要else

if (ListIsEmpty) // condition1
{
    // some code
    return true;
}

// you don't have anything that needs to happen here, but you *could*
// since if condition1 is met control leaves the function immediately

if (ListContainsOnlyOneNode) // condition2
{
     // some code
     return false;
}

// if either of the previous conditions are met, 
// control will never reach this point! So put whatever setup you need for 
// the final test here

TempPrevious = Head;
TempNext = Head -> Next;

if (NewNode -> Element > TempNext -> Element) // condition3
{
      // some code
      return true;
}

This is... actually really easy. Because you're returning from each block, you don't need else at all!

if (ListIsEmpty) // condition1
{
    // some code
    return true;
}

// you don't have anything that needs to happen here, but you *could*
// since if condition1 is met control leaves the function immediately

if (ListContainsOnlyOneNode) // condition2
{
     // some code
     return false;
}

// if either of the previous conditions are met, 
// control will never reach this point! So put whatever setup you need for 
// the final test here

TempPrevious = Head;
TempNext = Head -> Next;

if (NewNode -> Element > TempNext -> Element) // condition3
{
      // some code
      return true;
}
相对绾红妆 2024-10-30 11:38:15

删除最后一个else,我认为它会按照你的意愿工作。

Remove the last else and I think it will work as you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文