简体   繁体   English

C 中的链表不打印

[英]Linked list in C is not printing

I think it is not printing because I am not inserting into the list correctly.我认为它没有打印,因为我没有正确插入列表。 I see most people allocate space outside of the loop, but I am not sure that is the case.我看到大多数人在循环外分配空间,但我不确定情况是否如此。

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

typedef struct intList {
    int number;
    struct intList *next;
} INT_LIST;

int main() {
    INT_LIST *start = NULL, *temp;
    INT_LIST *trvPtr = start;
    /* Insert into list */
    while (temp->number != 0) {
        temp = malloc(sizeof(INT_LIST));
        printf("Enter your integer: ");
        scanf("%d", &(temp->number));
        temp->next = start;
        start = temp;
    }
    printf("List is: \n");
    while (trvPtr != NULL) {
        printf("%d", trvPtr->number);
        trvPtr = trvPtr->next;
    }
    return 0;
}

temp is uninitialized and points to garbage. temp未初始化并指向垃圾。 Checking temp->number != 0 is undefined behavior.检查temp->number != 0是未定义的行为。

You can solve this with a do/while loop.您可以使用 do/while 循环解决此问题。 The loop will always run once.循环将始终运行一次。 Allocate temp , set it, then check.分配temp ,设置它,然后检查。

    INT_LIST *start = NULL, *temp;
    do {
        temp = malloc(sizeof(INT_LIST));
        printf("Enter your integer: ");
        scanf("%d", &(temp->number));
        temp->next = start;
        start = temp;
    } while(temp->number != 0);

Note that your list will always start with a value of 0. To avoid this, check the input before doing anything else.请注意,您的列表将始终以 0 值开头。为避免这种情况,请在执行任何其他操作之前检查输入。 Use while(1) for an infinite loop and use break to end the loop.使用while(1)进行无限循环,使用break结束循环。

    INT_LIST *start = NULL;
    while(1) {
        int input;
        printf("Enter your integer: ");
        scanf("%d", &input);
        if(input == 0) {
            break;
        }

        INT_LIST *temp = malloc(sizeof(INT_LIST));
        temp->number = input;
        temp->next = start;

        start = temp;
    }

When you print, trvPtr will always be NULL;打印时, trvPtr永远是 NULL; it will not track changes to start .它不会跟踪更改以start You have to assign it to the new value of start .您必须将其分配给start的新值。

    printf("List is: ");
    INT_LIST *trvPtr = start;
    while (trvPtr != NULL) {
        printf("%d ", trvPtr->number);
        trvPtr = trvPtr->next;
    }
    puts("");

Note that you're not limited to declaring all the variables at the start of the function.请注意,您不限于在 function 的开头声明所有变量。

I have made some modifications/corrections in your code and the following should work -我对您的代码进行了一些修改/更正,以下应该有效 -

Correct Code -正确的代码 -

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

typedef struct intList {
    int number;
    struct intList *next;
} INT_LIST;

int main() {
    INT_LIST *start = NULL, *temp;
    temp = (INT_LIST*)malloc(sizeof(INT_LIST));
    INT_LIST *trvPtr = temp;
    /* Insert into list */
    do {
        
        printf("Enter your integer: ");
        scanf("%d", &(temp->number));
        start = temp;
        temp->next = (INT_LIST*)malloc(sizeof(INT_LIST));
        temp = temp->next;
        
    }while (start->number != 0);
    temp->next=NULL;

    printf("List is: \n");
    while (trvPtr->next != NULL) {
        printf("%d", trvPtr->number);
        trvPtr = trvPtr->next;
    }
    return 0;
}

SAMPLE OUTPUT:样本 OUTPUT:

Enter your integer: 1
Enter your integer: 2
Enter your integer: 3
Enter your integer: 0
List is:
1230

If you don't want the 0 to be printed, include temp = start , right after the first while loop ends.如果您不想打印 0,请在第一个 while 循环结束后立即包含temp = start


Explanation:解释:

  1. Firstly, in your code, you hadn't assigned temp anything.首先,在您的代码中,您没有为temp分配任何东西。 It should have been allocated space and assigned something first or else the statement temp->number != 0 is just undefined behavior.它应该已经分配了空间并首先分配了一些东西,否则语句temp->number != 0只是未定义的行为。

  2. Nextly, I have used start to check for the last value entered by the user.接下来,我使用start来检查用户输入的最后一个值。 If it is 0, the loop will break.如果为 0,循环将中断。 Do note I have converted the loop into do-while loop, since start is NULL at the beginning.请注意,我已将循环转换为do-while循环,因为start是 NULL。

  3. Lastly, once the loop ends, I have set the next of temp to null so that the printing loop will execute correctly.最后,一旦循环结束,我将 next of temp 设置为 null 以便打印循环正确执行。 In your code, this part also had a mistake since you had assigned NULL to trvPtr but never changed the value.在您的代码中,这部分也有错误,因为您已将 NULL 分配给trvPtr但从未更改该值。 So, in your case, the loop would never have executed.因此,在您的情况下,循环永远不会执行。

NOTE: It's always better to mention the cast-type explicitly while using malloc. Some compiler would give warnings/errors.注意:在使用 malloc 时,最好明确提及转换类型。某些编译器会给出警告/错误。 So, I have modified that as well wherever you have used malloc()所以,无论你在哪里使用malloc() ,我都修改了它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM