【问题标题】:descending ordered Linked list shows does not show it in order降序链表显示不按顺序显示
【发布时间】:2020-08-21 04:20:44
【问题描述】:

我的多项式链表不适用于某些输入,谁能告诉我哪里出错了?

以下输入不起作用,但是

 insert(10, 1);
    insert(20, 2);
    insert(30, 3);
    insert(50, 5);
    insert(60, 6);
    insert(40, 4);

但是这行得通

insert(60, 6);
    insert(20, 2);
    insert(50, 5);
    insert(40, 4);
    insert(10, 1);
    insert(30, 3);
#include <stdio.h>
#include <stdlib.h>
struct Node
{
    int coeff;
    int pow;
    struct Node *next;
};
struct Node *head = NULL;
void insert(int coeff, int pow)
{
    struct Node *link = (struct Node *)(malloc(sizeof(struct Node)));
    link->coeff = coeff;
    link->pow = pow;
    if (head == NULL)
    {
        link->next = NULL;
        head = link;
    }
    else
    {
        if (pow > head->pow)
        {
            link->next = head;
            head = link;
        }
        else
        {
            struct Node *temp = head;
            while ((temp->pow > pow + 1) && (temp->next))
                temp = temp->next;
            link->next = temp->next;
            temp->next = link;
        }
    }
}
void print()
{
    struct Node *temp = head;
    while (temp->next)
    {
        printf("%dx^%d+", temp->coeff, temp->pow);
        temp = temp->next;
    }
    printf("%dx^%d=0", temp->coeff, temp->pow);
}
int main()
{
    insert(10, 1);
    insert(20, 2);
    insert(30, 3);
    insert(50, 5);
    insert(60, 6);
    insert(40, 4);
   

    print();
    return 0;
}

【问题讨论】:

  • 和往常一样,在处理清单之类的事情时,我建议您花点时间坐下来拿一张纸和一支铅笔。在纸上完成所有操作,为列表中的节点绘制小框,为节点之间的链接绘制箭头。当您对列表进行操作时,擦除并重新绘制箭头。确保这一切都按照您的意愿进行,然后使用干净的纸再次进行以真正确保。 然后你坐在电脑前把它变成代码。
  • 如果您已经获得了代码,则使用调试器逐语句逐句执行代码,同时监控变量及其值。当您逐步执行代码时,再次使用纸和铅笔绘制代码执行的操作。希望这应该可以让您更容易看到何时出现问题。
  • 这个方法我想了一会儿,然后实现了
  • 请解释为什么有时你与pow比较,有时与pow+1比较。
  • 请解释一下“不起作用”的确切含义。有什么症状?

标签: c data-structures linked-list


【解决方案1】:
while ((temp->pow > pow + 1) && (temp->next))
    temp = temp->next;

您的迭代逻辑有问题。我认为您正在尝试将 temp 迭代到下一个元素应该是 link (您的新节点)的位置。但是要确定这个位置,你的逻辑是错误的。

5 -> 3 -> 1

如果您尝试在 temp=head - 5>(2+1) && temp->next 处插入 2,这是正确的。所以你最终会在 5 之后插入 2,这是不正确的。

我避免用勺子喂你解决方案。请尝试以此为基础。此外,列表排序似乎与 coeff 无关,您只是在 pow 上进行比较。将来您可能希望从问题中抽象出不必要的细节。

【讨论】:

    【解决方案2】:

    您的程序中有错误,逻辑错误,编译器不会显示,并且在运行时也不会发生。让我告诉你为什么你的代码不适用于第一组。取 insert(10,1) 和 insert(20,2) 。在第一次插入期间,由于第一个 if 和第二次插入 pow 值 2 将与第二个 if 和 value 中的 head.pow 进行比较,您的值将存储在第一个节点中存储在 head.pow 是 1 所以第二个 if 将变为 true 并且您的程序将在执行 if 语句后退出,并且节点将不会连接,而是您的 head 将更新为未连接的新链接,这在 pow 时发生>head.pow 在您的第一组输入中。您需要在那里更正您的逻辑。可能是如果您可以共享两组输入的输出,我将能够为您提供更多帮助,但我说如果您尝试自己纠正逻辑会更好。如果您仍然需要帮助,请告诉我。

    【讨论】:

      【解决方案3】:

      你去给你修好了

      您必须与下一个幂进行比较并将其添加到列表中

      #include <stdio.h>
      #include <stdlib.h>
      struct Node
      {
          int coeff;
          int pow;
          struct Node *next;
      };
      struct Node *head = NULL;
      void insert(int coeff, int pow)
      {
          struct Node *link = (struct Node *)(malloc(sizeof(struct Node)));
          link->coeff = coeff;
          link->pow = pow;
          if (head == NULL)
          {
              link->next = NULL;
              head = link;
          }
          else
          {
              if (pow > head->pow)
              {
                  link->next = head;
                  head = link;
              }
              else
              {
                  struct Node *temp = head;
                  while (temp->next && (temp->next->pow > pow))
                      temp = temp->next;
                  link->next = temp->next;
                  temp->next = link;
              }
          }
      }
      void print()
      {
          struct Node *temp = head;
          while (temp->next)
          {
              printf("%dx^%d+", temp->coeff, temp->pow);
              temp = temp->next;
          }
          printf("%dx^%d=0", temp->coeff, temp->pow);
      }
      int main()
      {
          insert(70, 7);
          insert(50, 5);
          insert(20, 2);
          insert(10, 1);
          insert(80, 8);
          insert(60, 6);
          insert(30, 3);
          insert(40, 4);
          print();
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-03
        • 1970-01-01
        • 1970-01-01
        • 2017-11-16
        • 1970-01-01
        • 1970-01-01
        • 2013-01-20
        • 2020-10-31
        相关资源
        最近更新 更多