【问题标题】:A function in C that adds a node at the end of a singly linked list is not workingC 中在单链表末尾添加节点的函数不起作用
【发布时间】:2021-08-22 21:55:48
【问题描述】:

我刚刚用 C 语言编写了一个函数,它应该在链表的末尾添加一个节点,但是当我编译程序时,控制台中什么也没有。这是函数的主体:

void addAtend(node *head, int val){
node *temp;
node *tempVal;
temp =head;
tempVal= (node*)malloc(sizeof(node));
if(tempVal==NULL){
    printf("Error! Memory was not allocated!");
    exit(-1);
}
tempVal ->data=val;
tempVal->next=NULL;
while (temp!=NULL)
{
    temp = temp->next;

}
temp->next = tempVal;

}

这是完整的 C 程序:

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

typedef struct linked {
    int data;
    struct linked *next;


} node;

//function to ietrate through a linked list
void printer(node *head){
    node *temp = head;
    while (temp!=NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    
}

//function to add a value to the end of the linked list
void addAtend(node *head, int val){
    node *temp;
    node *tempVal;
    temp =head;
    tempVal= (node*)malloc(sizeof(node));
    if(tempVal==NULL){
        printf("Error! Memory was not allocated!");
        exit(-1);
    }
    tempVal ->data=val;
    tempVal->next=NULL;
    while (temp!=NULL)
    {
        temp = temp->next;

    }
    temp->next = tempVal;
    


}

int main(){

    node *ptr = (node*)malloc(sizeof(node));
    if(ptr==NULL){
        printf("Error!");
        exit(-1);
    }
    node *head;
    head = ptr;
    
    ptr->data = 30;
    ptr->next = (node*)malloc(sizeof(node));
    ptr->next->data =50;
    ptr->next->next = NULL;


    addAtend(head, 40);
    printer(head);

}

输出如下所示: enter image description here

谁能看看这段代码,告诉我函数有什么问题?

【问题讨论】:

  • while (temp!=NULL)。当该循环结束时temp 将是NULL,因此循环后面的行将取消引用 NULL 指针,这(在大多数系统上)会导致段错误。

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


【解决方案1】:

在这个循环之后

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

}

指针temp 等于NULL。所以这个空指针在这个语句中是用来访问内存的

temp->next = tempVal;

这会导致未定义的行为。

函数可以通过以下方式定义

int addAtend( node **head, int val )
{
    node *new_node = malloc( sizeof( node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = val;
        new_node->next = NULL;

        while ( *head != NULL ) head = &( *head )->next;

        *head = new_node;
    }

    return success;
}

并且可以像这样调用该函数

addAtend( &head, 40 );

这是一个演示程序。

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

typedef struct linked {
    int data;
    struct linked *next;
} node;

int addAtend( node **head, int val )
{
    node *new_node = malloc( sizeof( node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = val;
        new_node->next = NULL;

        while ( *head != NULL ) head = &( *head )->next;

        *head = new_node;
    }

    return success;
}

FILE * printer( const node *head, FILE *fp )
{
    for ( ; head != NULL; head = head->next )
    {
        fprintf( fp, "%d -> ", head->data );
    }
    
    fputs( "null", fp );
    
    return fp;
}

int main(void) 
{
    node *head = NULL;
    
    addAtend( &head, 30 );
    addAtend( &head, 40 );
    addAtend( &head, 50 );
    
    fputc( '\n', printer( head, stdout ) );

    return 0;
}

程序输出是

30 -> 40 -> 50 -> null

【讨论】:

  • 感谢您抽出宝贵时间回复。我有一个简单的问题:在 addAtend() 中,您写道:node *new_node = malloc( sizeof( node ) ); int success = new_node != NULL;``` Doesn't int success = new_node != NULL;` 分配一个指向 int 类型变量的指针?
  • @Owais 这个变量成功的声明可以等效地重写为 int success = ( new_node != NULL );因此,如果 new_node != NULL 则变量 success 的值为 1,否则为 0。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
相关资源
最近更新 更多