C链表在开始处打印额外的0

ctehm74n  于 2023-01-08  发布在  其他
关注(0)|答案(1)|浏览(138)

这个程序还没有完全准备好,所以不是所有的功能都编好了。
但是,当涉及到非常基本的功能时,我面临着一个问题:将所述节点添加到所述链表的末尾。
不知何故,我在链表的开头得到了一个神秘的数字"0",但我不知道它来自哪里

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

typedef struct USER{
    int number;
    struct USER *next;
}USER;

USER* allocMem(USER* newUser){
    if ((newUser = (USER*)malloc(sizeof(USER))) == NULL ){
        perror("Memory allocation failed.");
        exit(1);
    }
    newUser->number;
    printf("A list has been created.\n");
    return newUser;
}

int newNode(USER* start){
    int num;
    USER *newUser = NULL; 
    USER *value = NULL;
    if ((newUser = (USER*)malloc(sizeof(USER))) == NULL ){
        perror("Memory allocation failed.");
        exit(1);
    }
    printf("\nGive a new number: ");
    scanf("%d", &num);
    newUser->number = num;
    newUser->next = NULL;
    if (start == NULL){
        start = newUser;
    }else{
        value = start;
        while(value->next != NULL){
            value = value->next;
        }
        value->next = newUser;
    }
    return 0;
}

int newNode_position(USER* start){
    USER *newUser = NULL;
    USER *value = NULL;
    int num;
    int position;
    if ((newUser = (USER*)malloc(sizeof(USER))) == NULL ){
        perror("Memory allocation failed.");
        exit(1);
    }
    printf("\nGive a new number: ");

    scanf("%d", &newUser->number);

    printf("Into which node do you want to place the number: ");
    scanf("%d", &position);
    value = start;
    for(int i = 1; i < position; i++){  
        value = value->next;
        
    }
    newUser->next = value->next; 
    value->next = newUser;

    value = NULL;
    value = start;
    printf("The following numbers are in the list: \n");
    while(value->next != NULL){
        printf("%d ", value->number);
        value = value->next;
    }

    return 0;
}

void print(USER *start){
    USER *value = NULL;
    value = start;
    printf("Following numbers are now in the list: \n");
    while(value->next != NULL){
        printf("%d ", value->number);
        value = value->next;
    }
    printf("\n");
}

void freeMem(USER* start){ 
    USER *value = NULL;
    while (start != NULL){
        value = start;   
        start = value->next;
        free(value); 
    }
}


int main(void){
    int choice;
    USER *start = NULL;
    printf("This program let's you create a linked list and manage it.\n");
    do {
        printf("\n1) Create a list\n");
        printf("2) Insert a new node in the end\n");
        printf("3) Insert a new node in the middle, to a specified position\n");
        printf("4) Clear the list\n");
        printf("5) Delete certain node from the list\n");
        printf("6) Print the list\n");
        printf("0) Quit\n");
        printf("Your choice: ");
        scanf("%d", &choice);
        switch(choice){
            case 1:
            start = allocMem(start);  
            break;
            case 2:
            newNode(start);
            break;
            case 3:
            newNode_position(start);
            case 4:
            freeMem(start);
            break;
            case 6:
            print(start);
            break;
            case 0:
            printf("\nThank you for using the program.");
            break;
            default:
            printf("Unknown choice.");
            break;
        }
    } while(choice != 0);
    
}
    • 输出:**
    • 创建列表并添加编号时:"2"在列表末尾三次,输出为:**

0、2、2

    • 当它应该**

二、二、二
我不明白为什么它在开头打印神秘的0。

vddsk6oq

vddsk6oq1#

函数allocMem创建一个节点,该节点具有未初始化的数据成员numbernext

USER* allocMem(USER* newUser){
    if ((newUser = (USER*)malloc(sizeof(USER))) == NULL ){
        perror("Memory allocation failed.");
        exit(1);
    }
    newUser->number;
    printf("A list has been created.\n");
    return newUser;
}

这导致程序的未定义行为。
函数打印不正确

void print(USER *start){
    USER *value = NULL;
    value = start;
    printf("Following numbers are now in the list: \n");
    while(value->next != NULL){
        printf("%d ", value->number);
        value = value->next;
    }
    printf("\n");
}

输出存储在第一个节点的数据成员number中的这个不确定值,而且由于while语句的条件,它不输出最后一个节点中的值

while(value->next != NULL){

实际上,函数allocMem是多余的,应该删除。

USER *start = NULL;

已创建空列表。
这段代码还有其他的缺点,例如函数freeMem实际上并没有清除列表,因为main中声明的指针start在调用该函数后将保持不变,因此将是无效的。
要么声明函数,如

USER * freeMem(USER* start){ 
    USER *value = NULL;
    while (start != NULL){
        value = start;   
        start = value->next;
        free(value); 
    }

    return start;
}

并称之为

case 4:
start = freeMem(start);
break;

或者你也可以声明为

void freeMem( USER **start){ 
    while ( *start != NULL ){
        USER *value = *start;   
        start = &( *start )->next;
        free(value); 
    }
}

并称之为

case 4:
freeMem( &start);
break;

或者例如函数newNode_position可以再次调用未定义的行为,至少因为在这个for循环中

value = start;
for(int i = 1; i < position; i++){  
    value = value->next;
    
}

不检查指针value是否等于NULL
此外,该函数不会更改main中声明的指针start

相关问题