链接列表,在C ++末尾插入

时间:2013-11-21 15:47:10

标签: c++ linked-list

我正在编写一个简单的函数来插入C ++上链表的末尾,但最后它只显示了第一个数据。我无法弄清楚什么是错的。这是功能:

void InsertAtEnd (node* &firstNode, string name){

        node* temp=firstNode;

        while(temp!=NULL) temp=temp->next;

            temp = new node;
        temp->data=name;
        temp->next=NULL;

        if(firstNode==NULL) firstNode=temp;

}

8 个答案:

答案 0 :(得分:12)

你写的是:

  • 如果firstNode为空,则将其替换为单个节点temp 没有下一个节点(没有人nexttemp

  • 否则,如果firstNode不为空,则除了temp之外没有任何反应 节点被分配和泄露。

以下是更正确的代码:

void insertAtEnd(node* &first, string name) {
    // create node
    node* temp = new node;
    temp->data = name;
    temp->next = NULL;

    if(!first) { // empty list becomes the new node
        first = temp;
        return;
    } else { // find last and link the new node
        node* last = first;
        while(last->next) last=last->next;
        last->next = temp;
    }
}

另外,我建议将构造函数添加到node

struct node {
    std::string data;
    node* next;
    node(const std::string & val, node* n = 0) : data(val), next(n) {}
    node(node* n = 0) : next(n) {}
};

这使您可以像这样创建temp节点:

node* temp = new node(name);

答案 1 :(得分:1)

列表中的最后一个元素永远不会将next指针设置为列表中的新元素。

答案 2 :(得分:1)

问题是您正在用新元素替换链表的头部,并在此过程中丢失对实际列表的引用。

要在最后插入,您要将while条件更改为:

while(temp->next != null)

循环后,temp将指向列表中的最后一个元素。然后创建一个新节点:

node* newNode = new node;
newNode->data = name;
newNode->next = NULL;

然后更改此新节点旁边的temp

temp->next = newNode;

您也不需要将firstNode作为参考传递,除非您希望NULL被视为长度为0的链接列表。在这种情况下,您需要显着修改您的方法因此它可以单独处理firstNodeNULL的情况,因为在这种情况下,如果没有分段错误,则无法评估firstNode->next

答案 3 :(得分:0)

你犯了两个基本错误:

  1. 滚动列表时,滚动最后一个元素并开始构建它后面的空白。在最后一个元素之后找到第一个NULL是没用的。您必须找到最后一个元素(其中一个'next'等于NULL)。迭代temp->next,而不是temp

  2. 如果要在末尾附加元素,则必须用其地址覆盖最后一个指针的NULL。而是在列表的开头写下新元素。

    void InsertAtEnd(node *& firstNode,string name) {    node * newnode = new node;    newnode->数据=名;    newnode->接着= NULL;

    if(firstNode == NULL)    {         firstNode = newnode;    }    其他    {         node * last = firstNode;         while(last-> next!= NULL)last = last-> next;         last-> next = newnode;    } }

  3. 请注意,如果您确保永远不会提供NULL但所有列表始终使用至少一个元素进行初始化,则会更加整洁。此外,在列表的开头插入比在最后添加更容易:newnode->next=firstNode; firstNode=newnode

答案 4 :(得分:0)

如果您不想使用引用指针,可以使用指向指针的指针。我的完整代码如下:

void insertAtEnd(struct node **p,int new_data)
{
    struct node *new_node=(struct node *)malloc(sizeof(struct node));
    new_node->data=new_data;
    new_node->next=NULL;
    if((*p)==NULL)//if list is empty
    {
        *p=new_node;
        return;
    }
    struct node* last=*p;//initailly points to the 1st node
    while((last)->next != NULL)//traverse till the last node
        last=last->next;
    last->next=new_node;
}
void printlist(struct node *node)
{
    while(node != NULL);
    {
        printf("%d->",node->data);
        node=node->next;
    }
}
int main()
{
    struct node *root=NULL;
    insertAtEnd(&root,1);
    insertAtEnd(&root,2);
    insertAtEnd(&root,3);
    insertAtEnd(&root,4);
    insertAtEnd(&root,5);
    printlist(root);
return 0;
}    

了解以下两个变量的需要是理解问题的关键:

  1. struct node ** p:因为我们需要从主要创建的根节点链接它。
  2. struct node * last:因为如果不使用,原始内容将随着while循环中下一个节点的内容而改变。最后只打印2个元素,最后2个节点,这是不可取的。

答案 5 :(得分:0)

void addlast ( int a)
{

    node* temp = new node;
    temp->data = a;
    temp->next = NULL;
    temp->prev=NULL;
    if(count == maxnum)
    { 
        top = temp;
        count++; 
    } 
    else
    { 
        node* last = top;
        while(last->next)
            last=last->next;
        last->next = temp;
    }
}

答案 6 :(得分:-1)

void InsertAtEnd (node* &firstNode, string name){

        node* temp=firstNode;

        while(temp && temp->next!=NULL) temp=temp->next;

       node * temp1 = new node;
        temp1->data=name;
        temp1->next=NULL;
       if(temp==NULL)
           firstNode=temp1;
       else
           temp->next= temp1;


}
  

while循环将在代码中返回temp == null而不是像这样需要从while循环返回最后一个节点指针

while(temp && temp->next!=NULL) temp=temp->next;

并将新节点分配给返回的临时节点的下一个指针,将数据添加到链表的尾部。

答案 7 :(得分:-1)

您可以使用此代码:

void  insertAtEnd(Node* firstNode, string name)
{
    Node* newn = new Node;              //create new  node
    while( firstNode->next != NULL )    //find the last element in yur list
        firstNode = firstNode->next;    //he is the one that points to NULL 
    firstNode->next = newn;             //make it to point to the new element
    newn->next = NULL;      //make your new element to be the last (NULL)
    newn->data = name;      //assign data.
}
相关问题