【问题标题】:Sorted insert into Linked List排序插入到链表中
【发布时间】:2023-03-10 12:06:01
【问题描述】:

自周日以来我一直在使用此迭代函数,但没有成功,我想为排序插入创建迭代函数,但经过数小时的节点绘图后,我想我需要一些有关该函数的帮助:

结构声明:

  typedef struct E_Type * List;

  struct E_Type
  {
      int data;
      struct E_Type* next;
  };

功能:

bool insert(List & l, int data) {
    while (l != 0) {
        for (List p = l; p; p = p->next) {
            if (p->data == data)
                return false;
        }

        if (l->data > data) {
            List new_list = new E_Type;
            new_list->data = data;
            new_list->next = l;
            l = new_list;
            return true;
        } else if (l->data < data) {

            List new_list = new E_Type;
            new_list->data = data;
            l->next = new_list;
            l = new_list;
            return true;
        }
    }

    if (l == 0) {
        List new_list = new E_Type;
        new_list->data = data;
        new_list->next = l;
        l = new_list;
        return true;
    }
}

顺便说一句:这个功能甚至可能吗...有关此插入的所有教程,信息等都带有对下一个数据的递归调用

【问题讨论】:

  • 请描述您遇到的问题。
  • 为什么你需要在while循环里面放那个for循环。一次性检查还不够吗?
  • 在插入大于当前值的值后代码崩溃
  • 你的while 没有意义。如果data 小于或大于当前节点,则创建new E_Type 并返回...
  • @tj 我正在检查列表中已经存在的值,如果有的话。返回 false。

标签: c++ linked-list


【解决方案1】:

有很多错误:

l->next = new_list;

这一行删除了指针l-&gt;next的先前值。

没有代码可以寻找合适的元素在之前或之后插入新元素。

您的 while 没有任何意义,因为您的 l 指针不会随着迭代而变化。

您最好只在纸上写下您的函数应该执行的基本步骤,并且仅在使用 C++ 对其进行编码之后。

(或者只使用来自邻居答案的工作样本;-))

【讨论】:

    【解决方案2】:

    结构看起来不错。我试图让insert 的结构尽可能接近你的结构。我希望cmets可以帮助你看看你做错了什么。

    bool insert( List & l, int data ) {
        // We have to look sequentially at all members in the *ordered* list
        while ( l != 0 ) {
             // We already have this data, we exit
             if ( l->data == data ) return false;
             // If this element's data is bigger we shift it by one spot
             // so we can insert the new one
             else if ( l->data > data ) {
                  List new_element = new E_Type;
                  // Save current element in the new one ( that will shift )
                  new_element->data = l->data;
                  new_element->next = l->next;
                  // "Insert" our new element
                  l->data = data;
                  l->next = new_element;
                  return true;
             }
             // If we didn't return before, we skip to the next element
             // but only if it's not the end
             if ( l->next )
                 l = l->next;
             else
                 break;
        }
        if ( l ) {
            // If we are here it means that all elements are lower than the new data.
            // l currently holds the last element of the list, so we only need to add
            // a new element to the list
            List new_element = new E_Type;
            new_element->data = data;
            new_element->next = 0;
            l->next = new_element;
            return true;
       }
       else {
            // If we are here it means that there was no list to begin with.
            // Thus, we have to create a first element.
            l = new E_Type;
            l->data = data;
            l->next = 0;
            return true;
       }
    }
    

    【讨论】:

      【解决方案3】:
      //find the closest Node that is not > data and return it
      List* find(List* l, int data) {
          // l != NULL
          List* current = l;
          List* previous = NULL;
      
          while(current != NULL && current->data > data) {
              previous = current;
              current = current->next;
          }
      
          return previous;
      }
      
      List* insert(List* l, int data) {
          //l != NULL
          List* current = new List();
          current->data = data;
      
          if(l->data > data) {
              List* insert_position = find(l, data);
              current->next = insert_position->next;
              insert_position->next = current;
          } else {
              current->next = l;
              return current;
          }
          return l;
      }
      

      struct List
      {
          int data;
          List* next;
      };
      

      您实际上并不需要 typedefstruct 关键字。

      上面的示例应该与您要查找的内容相近。正如您所指出的那样,存在一些问题。

      【讨论】:

      • 我认为给学生一些更正比只是一个工作代码更好......
      • 好吧,我肯定不会使用代码,但是有这样的代码指导,这样我就可以重建我的功能了。
      【解决方案4】:

      我可以看到你是如何陷入困境的。您的 insert 函数尝试执行多项任务(检查重复项、查找插入新元素的位置并进行实际插入),并且每项任务所需的步骤都变得混乱。

      我最好的建议是退一步写三个函数:

      1. 制作检查元素是否已存在的函数(我们称之为bool find_element(List l, int data))。
      2. 创建一个函数,在现有列表的前面插入一个新元素并返回新列表 (List insert_front(List l, int data))。这个函数可以利用List的每个元素也可以被视为列表的头部这一事实。
      3. 创建一个函数来确定在何处插入新元素 (List locate_insertion_point(List l, int data))。
      4. 根据三个新函数编写您的 insert 函数。

        bool insert(List& l, int data)
        {
            if (find_element(l, data))
                return false;
        
            List insert = locate_insertion_point(l, data);
            if (insert == NULL)
            { /* Can't insert after any point. Insert at the front */
                List new_list = new E_Type;
                new_list->data = data;
                new_list->next = l;
                l = new_list;
            }
            else
            { /* insert after insert */
                List next = insert->next;
                insert->next = insert_front(next, data);
            }
            return true;
        }
        

      【讨论】:

      • 谢谢,我也认为像 find 这样的帮助功能会大大降低我对这个功能的复杂性和困惑
      猜你喜欢
      • 2015-10-16
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      相关资源
      最近更新 更多