繁体   English   中英

排序插入到链接列表

[英]Sorted insert into Linked List

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

struct声明:

  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;
    }
}

顺便说一句:甚至有可能使用此功能...有关此插入的所有教程,信息等均带有对next-data的递归调用

有很多错误:

l->next = new_list;

该行擦除指针l->next先前值。

没有代码寻找适当的元素在之后插入新元素。

您的while没有意义,因为您的l指针在迭代之间不会改变。

最好只在函数编写的纸质基本步骤中写出,然后再用C ++对它们进行编码。

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

//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关键字。

上面的示例应接近您的期望。 所指出的与您的问题有关。

该结构看起来不错。 我试图使insert的结构尽可能接近您的结构。 我希望这些评论可以帮助您了解自己做错了什么。

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;
   }
}

我可以看到你是怎么被困住的。 您的insert函数尝试执行多个任务(检查重复项,找到要在其中插入新元素的位置并进行实际插入),并且使每个任务所需的步骤弄混了。

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

  1. 生成检查元素是否已经存在的函数(将其称为bool find_element(List l, int data) )。
  2. 创建一个在现有列表的List insert_front(List l, int data)插入新元素并返回新列表的List insert_front(List l, int data) )。 此功能可以利用以下事实: List每个元素也可以视为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; } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM