C链表末尾添加项

时间:2018-09-21 11:27:45

标签: c singly-linked-list

我正在编写一个程序,该程序在现有链表的末尾添加一个节点。 问题是它似乎没有将硬编码值nr分配给链表最后一个元素struct 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,但是具有垃圾值,我不知道为什么。

2 个答案:

答案 0 :(得分:0)

访问current->next != NULL的元素时不必检查current

将while循环更改为:

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

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

答案 1 :(得分:0)

您对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));
    }
}

当您要调用此函数但addItemLasthead时,您需要更改NULL以处理大小写(如果不测试这种情况,您的程序在调用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++;
        }
    }
相关问题