【问题标题】:insert to sorted position linked list插入到排序位置链表
【发布时间】:2010-10-23 17:46:25
【问题描述】:

我的问题与我不久前问的这个问题非常相关

place a value in the sorted position immediately

我想知道您是否可以使用相同的方法,即您在链表中后退以找到应该插入的位置。

如果可能的话,如何向后循环链表?我无法弄清楚,因为它似乎不可能,因为它应该是一个双链接列表,那么如果我没记错的话?无论如何,我正在使用单链表。

编辑

我想我会采用前瞻性方法,这就是我目前所做的。我被困在我应该如何保存前一个(键,值)的问题上。这是到目前为止所做的代码。 for 循环用于查找我要插入的位置。而且我已经向前看,它会在它到达尽头时打破。

到目前为止,好的,现在我想将值插入到正确的位置。在这里我被困住了。应该怎么做?现在当我插入键:2, 1, 0, 3时,它只会打印出1, 3

struct my_list
{
  /* a pointer to the first element of the list */
  struct list_link* first;
};

struct list_link
{
   int key;                // identifies the data
   double value;           // the data stored
   struct list_link* next; // a pointer to the next data
};

struct list_link* create(int key, double value, struct list_link* next)
{
   // creates the node;
   struct list_link * new_link;
   new_link = new struct list_link;

   // add values to the node;
   new_link->key = key;
   new_link->value = value;
   new_link->next = next;

   return new_link; // Replace this, it is just to be able to compile this file
}

void list_insert(struct my_list* my_this, int key, double value)
{
   if(my_this->first == NULL)   // add if list empty
      my_this->first = create(key, value, my_this->first);   
   else
   {      
      struct my_list* curr;
      struct my_list* prev;         
      struct my_list start;

      start.first = my_this->first;
      curr = my_this;

      cout << "Too be appended: ";
      cout << key << " " << value << endl;
      for(curr->first = my_this->first; 
          key > curr->first->key; 
          curr->first = curr->first->next)
      {
         if(curr->first->next == NULL) //peek at front if empty
            break;
      }
      cout << "append here " << key << " > " << 
          curr->first->key << endl << endl;
      //perform some surgery
      if(curr->first->next == NULL)
      {     
         curr->first->next = create(key, value, my_this->first->next);
      }
      else
      {       
         curr->first = start.first; //move back to start of list
         my_this->first = create(key, value, my_this->first);
      }      
   }  
}

【问题讨论】:

    标签: c++ linked-list sorted


    【解决方案1】:

    你不能向后遍历单链表,但你可以保留一个指向你看到的最后两个元素的指针,而不仅仅是一个。

    所以,从前面遍历列表,并保留两个指针:当前和上一个。如果您要插入的元素小于当前元素,则更新之前的元素以指向它。

    【讨论】:

      【解决方案2】:

      没有一个单链表不能倒退。

      【讨论】:

        【解决方案3】:

        以下是我对您所问问题的理解:当您在单链表中搜索插入位置时,您会发现您已经将一个节点走得太远了,那么,如何返回?

        嗯,有两个主要的解决方案:

        • 在搜索时先查看一个节点(同一想法的不同观点是保留“尾随”指针),或者

        • 确保有一个人为的尾节点(这样你总是有一个真正的下一个节点),并且除了搜索指针之外没有其他“外部”指针进入列表。在具有更高值的节点之后插入新节点。交换两个节点的内容。

        第二个解决方案有点脆弱,因为假设没有其他“外部”指针进入列表。但这有点巧妙。我是从 Donald Knuth 的“计算机编程艺术”中学到的。

        除了这些单链表解决方案之外,您还可以对列表进行双链接。

        干杯,

        【讨论】:

          【解决方案4】:

          您可以使用递归来向后遍历单链表。

          void traverse_backwards(NODE node)
          {
              if (node->next != null && !node->is_marked)
              {
                  node->is_marked = 1;
                  traverse_backwards(node->next);
              }
              else
              {
                  // logic goes here
              }
          }
          

          【讨论】:

          • 这是一种创建一组临时反向链接的方法。
          【解决方案5】:

          举个例子比较容易

          10 -> 20 -> 30 -> NULL
          

          使用两个指针遍历列表:(i) currentNode 和 (ii) nextNode

          currentNode = NULLnextNode = &lt;10&gt; 开头。只要nextNode-&gt;key &lt; insertkey 为真,就将它们两个循环向前移动。

          退出循环时(例如:insertKey == 25, currentNode = &lt;20&gt;, nextNode = &lt;30&gt;):

          1. newNode-&gt;key = insertKeynewNode-&gt;value = insertValue创建newNode

          2. currentNodenextNode之间“插入”newNode

            2.1currentNode-&gt;next = newNode

            2.2newNode-&gt;next = nextNode

          【讨论】:

            猜你喜欢
            • 2015-10-16
            • 2023-03-10
            • 2011-04-23
            • 1970-01-01
            • 1970-01-01
            • 2016-08-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多