C 链表在末尾添加项

标签 c singly-linked-list

我正在编写一个程序,在现有链表的末尾添加一个节点。 问题是它似乎没有分配给变量 nrstruct node链表最后一个元素的硬编码值 7 。 这是代码:

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

struct node {
    int nr;
    struct node *next;
};

void addNodes(struct node **head, int n);
void displayList(struct node *head);
void addItemLast(struct node *head);

int main() {
    struct node* head = NULL;
    int n;

    printf("Introduceti numarul de noduri: ");
    scanf("%d", &n);

    addNodes(&head, n);
    displayList(head);
    addItemLast(head);
    displayList(head);

    return 0;
}

void addNodes(struct node **head, int n) {
    *head = (struct node*)malloc(sizeof(struct node));
    struct node *current = *head;

    printf("\nIntroduceti %d noduri:\n", n);
    for(int i = 0; i < n; i++) {
        printf("Element %d = ", i+1);
        scanf("%d", &(current -> nr));

        current->next = (struct node*)malloc(sizeof(struct node));
        current = current->next;
    }
    current->next = NULL;
}

void displayList(struct node *head) {
    struct node *current = head;
    int i = 1;

    printf("\nElementele introduse sunt:\n");
    while(current->next != NULL) {
        printf("Elementul %d = %d\n", i, current->nr);

        current = current->next;
        i++;
    }
}

void addItemLast(struct node *head) {
    struct node *temp = head, *last;
    last = (struct node*)malloc(sizeof(struct node));

    if(last == NULL) {
        printf("\nMemory cannot be allocated!\n");
    } else {
        last->nr = 7;
        last->next = NULL;

        while(1) {
            if(temp->next == NULL) {
                temp->next = last;
                break;
            }
            temp = temp->next;
        }
    }
}

最后一个函数,addItemLast() ,未按预期工作。

这是输出:

Introduceti numarul de noduri: 3

Introduceti 3 noduri:
Element 1 = 1
Element 2 = 2
Element 3 = 3

Elementele introduse sunt:
Elementul 1 = 1
Elementul 2 = 2
Elementul 3 = 3

有问题的函数运行后,我得到以下输出:

Elementele introduse sunt:
Elementul 1 = 1
Elementul 2 = 2
Elementul 3 = 3
Elementul 4 = 13383248

元素 4 不包含硬编码值 7 ,但有一个垃圾值,我不明白为什么。

最佳答案

您的 addNodes 实现不正确,假设我输入 n = 2 ,创建了 3 个节点,但只调用了两次 scanf 函数,因此,您在某些节点中有垃圾值(nr 未设置)。

重新实现它(如果 addItemLast 函数正确实现,它就可以工作):

void addNodes(struct node **head, int n){
    printf("\nIntroduceti %d noduri:\n", n);
    for(int i = 0; i < n; i++){
        struct node* last = addItemLast(*head);
        if (*head == NULL)
            *head = last;

        printf("Element %d = ", i);
        scanf("%d", &(last -> nr));
    }
}
<小时/>

当您想要调用此函数但 headNULL 时,您需要更改 addItemLast 来处理这种情况(无需测试您的程序的这种情况)为空列表调用 addItemLast 时崩溃):

struct node* addItemLast(struct node *head){
    struct node *temp = head, *last;

    last = (struct node*)malloc(sizeof(struct node));
    if (head == NULL)  // <---------
    {
        last->nr = 7;
        last->next = NULL;
        return last;
    }

    if(last == NULL){
        printf("\nMemory cannot be allocated!\n");
    }else{
        last->nr = 7;
        last->next = NULL;

        while(1){
            if(temp->next == NULL){
                temp->next = last;
                break;
            }
            temp = temp->next;
        }
    }
    return last;
}
<小时/>

最后显示列表的函数应该是:

   void displayList(struct node *head){
        struct node *current = head;
        int i = 1;

        printf("\nElementele introduse sunt:\n");
        while(current != NULL){  // <---------
            printf("Elementul %d = %d\n", i, current->nr);

            current = current->next;
            i++;
        }
    }

关于C 链表在末尾添加项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52442765/

相关文章:

java - 对于 addHead 方法为什么要检查 tail == null ?

c - 指向链表开头的指针

java - 我需要帮助制作一个必须通过某些测试的 toString 方法,Java

c - 在已排序的链表中插入一个元素

c - 如何将对象添加到单向链表

C语言for循环创建线程

Petzold 的编程窗口中的代码(制作窗口错误)

c - 在子窗口中记录/打印

c - 获取文件扩展名 C 错误

c - pthreads 传递参数 3 警告