【问题标题】:Adding an element to the end of linkedlist将元素添加到链表的末尾
【发布时间】:2014-02-03 22:38:09
【问题描述】:

我有一个工作正常的函数 headinsert。这在“头部”添加了元素。 但我正在尝试制作一个函数 endinsert 在链表的末尾添加一个元素。 到目前为止我的代码是:

void IntList::endInsert(int the_number)
{
    if (head == NULL)//if list is empty
    {
        head = new IntNode; //create new dynamic variable
        head -> data = the_number; //add value to new variable
        head -> link = NULL; 
    }
    else
    {
        NodePtr temp = head; //initialize
        //Now we want to insert in the back of list. Use for loop to get to the last element of list
        for(temp = head; temp-> link != NULL ; temp = temp -> link)
        {
            temp->link = new IntNode; //create a new var
            temp = temp ->link; //give it a "position"
            temp ->data = the_number; //Give it a value
            temp ->link = NULL;  //The new variable will be the last element and therefore points to NULL
        }
    }
}

但由于某种原因它不起作用:(。有什么提示吗?

提前致谢!

【问题讨论】:

    标签: c++ linked-list


    【解决方案1】:
    for(temp = head; temp->link != NULL ; temp = temp->link);
    
    // now temp is the last one
    
    temp->link = new IntNode;
    temp = temp->link;
    temp->data = the_number;
    temp->link = NULL;
    

    注意; 位于for 循环的末尾。

    【讨论】:

    • 现在可以了 :)。谢谢!在这种情况下,为什么在 for 循环之后必须有一个分号?
    • 在这种情况下,分号被视为空代码块,例如:for(...){ /* do nothing */ } 因为temp = temp->link 已经足够了,您不需要在循环中执行任何其他操作。跨度>
    【解决方案2】:

    当列表不为空时,for() 应该循环到最后一个节点,然后创建并附加一个新节点,如下所示,

    for(temp = head; temp-> link != NULL ; temp = temp -> link) ; // at the end you 
                                                                    are at last node
            // Now form a link from last node to newly created one
            temp->link = new IntNode; 
            temp = temp ->link; 
            temp ->data = the_number; 
            temp ->link = NULL;  
    

    【讨论】:

      【解决方案3】:

      修改这部分代码

      else
      {
          NodePtr temp = head; //initialize
          //Now we want to insert in the back of list. Use for loop to get to the last element of list
          for(temp = head; temp-> link != NULL ; temp = temp -> link)
          {
              temp->link = new IntNode; //create a new var
              temp = temp ->link; //give it a "position"
              temp ->data = the_number; //Give it a value
              temp ->link = NULL;  //The new variable will be the last element and therefore points to NULL
          }
      }
      

      到下面

      else
      {
          NodePtr temp = head; //initialize
          //Now we want to insert in the back of list. Use for loop to get to the last element of list
          while ( temp-> link != NULL ) temp = temp -> link;
      
          temp->link = new IntNode;
          temp->link->link = NULL;
          temp->link->data = the_number; //Give it a value
      }
      

      【讨论】:

        猜你喜欢
        • 2013-12-21
        • 2020-09-13
        • 2020-09-11
        • 2012-09-12
        • 1970-01-01
        • 2013-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多