为什么以下代码在单链表的末尾插入节点不起作用?

时间:2016-06-27 10:57:35

标签: c data-structures linked-list singly-linked-list

以下是C语言中的代码:

函数调用:

insert(&head,value);

void insert(struct node** headref,int value)
{
    struct node* head = (*headref); 

    while( head!=NULL )
     {

        head= head->link;
     }

    struct node* new_node=(struct node*)malloc( sizeof(struct node) );

    new_node->data=value;
    new_node->link=NULL;

    head=new_node;  
}

5 个答案:

答案 0 :(得分:0)

您需要将列表的最后一个元素链接到new_node,否则您将松开列表中的链接(如果有这样的话:))。你需要在循环中存储2个指针 - head,你已经拥有它和指向前一个元素的指针(head之前的元素)。如果您有空列表,请特别注意!

答案 1 :(得分:0)

  while( head!=NULL )
 {
    head= head->link;
 }

在此循环结束时,head将为NULL。您可能希望停在最后一个节点

  while( head->link!=NULL )
 {
    head= head->link;
 }

然后执行类似

的操作
head->link = new_node;

现在,如果列表为空,则必须格外小心,因为如果head->link head=NULL将抛出错误。 您可以像

一样在开头进行检查
//allocate new_node here
if(head==NULL)
{
   *headref = new_node; //Note only in this case would headref ever change
}

答案 2 :(得分:0)

您正在用<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> div { background: red; margin: 5px; } table { border: 2px solid black; } td { padding: 10px; border: 1px solid lightgrey; } </style> <script> function createTable() { var a; a = document.getElementById('tb1').value; var length = document.getElementById('table').rows.length; if (a == "") { alert("Please enter some numeric value"); } else { if(length > 1){ var table = document.getElementById("table"); // Create an empty <tr> element and add it to the 1st position of the table: var row = table.insertRow(length); // Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element: var cell1 = row.insertCell(0); var cell2 = row.insertCell(1); var cell3 = row.insertCell(2); var cell4 = row.insertCell(3); cell1.innerHTML = "<input type='text' name='" + "name".concat(length+1) + "'>"; cell2.innerHTML = "<input type='text' name='" + "name".concat(length+1) + "'>"; cell3.innerHTML = "<input type='text' name='" + "name".concat(length+1) + "'>"; cell4.innerHTML = "<input type='text' name='" + "name".concat(length+1) + "'>"; } else { var rows = "<tr><th>Item Name</th><th>Quantity</th><th>QuantityType</th><th>Amount</th></tr>"; for (var i = 0; i < a; i++) { rows += "<tr><td><input type='text' name='" + "name".concat(i+1) + "'></td><td><input type='text' name='" + "quantity".concat(i+1) + "'></td><td><input type='text' name='" + "qtype".concat(i+1) + "'></td><td id='amt'><input type='text' id='sum' onkeyup='myfunction(this.value);' name='" + "total".concat(i+1) + "'></td></tr>"; } document.getElementById("table").innerHTML = rows; } } } </script> </head> <body> <input type="text" id="tb1"/> <button type="button" onclick='createTable()'>Click Me!</button> <table id="table" class="order-table table" name="table1" required> </table> </body> </html>替换最后一个节点head,这是不期望的。循环到最后一个节点,并使new_node指向head->link,如下所示:

new_node

答案 3 :(得分:0)

逻辑错误。试试这个。

struct node* new_node=(struct node*)malloc( sizeof(struct node) );
new_node->data=value;
new_node->link=NULL;

if (!head)
{
   head = new_node;
   return;
}

// Use for or while loop as below.
// 1. for(;head->link;head=head->link); 
// or  
/* 2. */
while (head->link)
{
    head = head->link;
}
head->link = new_node;

最好用一些有意义的名字重命名头部!

答案 4 :(得分:0)

在函数中,局部变量head被分配给新创建的节点。退出函数后,将破坏此局部变量,并且不会更改原始列表。

此外这个循环

while ( head != NULL )
{
    head = head->link;
}

没有意义。实际上它可以替代

head = NULL;

该功能可以如以下说明性程序所示。

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *link;
};

int insert( struct node **head, int value )
{
    struct node *tmp = malloc( sizeof( struct node ) );
    int success = tmp != NULL;

    if ( success )
    {
        tmp->data = value;
        tmp->link = NULL;

        while ( *head ) head = &( *head )->link;

        *head = tmp;
    }

    return success;
}

void display( struct node*head )
{
    for ( ; head != NULL; head = head->link ) printf( " %d", head->data );
}

int main( void ) 
{
    const int N = 10;
    struct node *head = NULL;

    int i = 0;
    while ( i < N && insert( &head, i ) ) i++;

    display( head );

    return 0;
}

它的输出是

 0 1 2 3 4 5 6 7 8 9