在C中的链表末尾插入节点

时间:2015-04-11 13:03:52

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

我正在开发一个代码,将一个节点插入到链表的末尾,而它只是不起作用。它给了我与以前相同的链表,没有附加任何节点。

结果

the old list is :
9 8 7 6 5 4 3 2 1 0 
the new list is :
9 8 7 6 5 4 3 2 1 0 

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

//make a new type structure called node
typedef struct nodes{
    int n;
    struct nodes* next;
}node;
//assigning the first node of the linked list
node* head=NULL;
//append function
void append(int number){
    node* tail=malloc(sizeof(node));
    if(tail==NULL){
        printf("unable to allocate");
        exit(1);
    }
    tail->n=number;
    tail->next=NULL;
    if(head->next==NULL){
        tail->next=head;
        printf("added successfully");
    }
    else{
        for(node* current=head;current->next==NULL;current=current->next){
            current->next=tail;
            printf("Added successfully");
            break;
        }
    }
}
//main function
int main(int argc,char* argv[]){
    //checking that the commmand is correct
    if(argc!=2){
        printf("Please type ./append and then type the number you want to add to the list");
    }
    //accept numbers in second argument
    int newnumber=atoi(argv[1]);
    //make the list
    for(int i=0;i<10;i++){
        node* newnode=malloc(sizeof(node));
        //checking
        if(new==NULL){
            exit(1);
        }
        newnode->n=i;
        newnode->next=head;
        head=newnode;
    }
    //printing the old list
    printf("the old list is :\n");
    for(node* conductor=head;conductor!=NULL;conductor=conductor->next){
        printf("%i ",conductor->n);
    }
    //append the number given to the start of the linked list 
    append(newnumber);
    //printing the new list
    printf("\nthe new list is :\n");
    for(node* conductor=head;conductor!=NULL;conductor=conductor->next){
        printf("%i ",conductor->n);
    }
    printf("\n");
    return 0;
}   

所以功能似乎根本没有影响。我看不出bug在哪里。

2 个答案:

答案 0 :(得分:2)

node* tail=malloc(sizeof(node));

您创建了一个名为tail的新节点*。它还没有任何关联。

首先,如评论中所述,如果列表为空,您的代码可能会取消引用NULL指针。例如,您可以在开头添加以下检查:

if(head==NULL) {
    head=tail;
    printf("Added successfully\n");
    return;
}

现在让我们来看看你的代码:

if(head->next==NULL){
        tail->next=head;
        printf("added successfully");
    }

在这里你指定tail->next而不是head->next,所以你的尾巴仍然不在列表中,这是一个错误。

else{
        for(node* current=head;current->next==NULL;current=current->next){
            current->next=tail;
            printf("Added successfully");
            break;
        }
    }

这里你的循环条件是错误的。对于初学者来说,==应该是!=。现在你的循环根本没有执行。

然后你仍然需要将循环体取出循环:

else {
    node* current=head;
    while (current->next!=NULL)
        current=current->next;
    current->next=tail;
    printf("Added successfully");
}

但实际上这些都是非常简单的错误,你应该能够通过仔细观察来捕捉它们。

答案 1 :(得分:1)

append函数中的逻辑在两种情况下都是假的:如果列表为空,则您的测试不正确,您只需修改tail->next而不是head,如果列表是不是空的,你完全没有做任何事情,因为current {&gt; next NULL循环开始时不是for

请查看此更正版本:

void append(int number) {
    node *tail = malloc(sizeof(node));
    if (tail == NULL) {
        printf("unable to allocate");
        exit(1);
    }
    tail->n = number;
    tail->next = NULL;
    if (head == NULL) {
        head = tail;
    } else {
        node *current = head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = tail;
    }
    printf("Added successfully");
}
相关问题