GoodtimeGood 2023-11-18 22:45 采纳率: 0%
浏览 0

链表头插法实现打印有问题

我在学习链表的头插法,我写的代码在notepade++上面编译通过后,输入值后,无限打印,不知道是什么原因?有知道的兄弟吗

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

struct Test {
    int data;
    struct Test *next;
};

void printLink(struct Test *head) {
    struct Test *point = head;
    while (point != NULL) {
        printf("%d ", point->data);
        point = point->next;
    }
    printf("\n");
}

struct Test* insertFromHead(struct Test *head) {
    int newData;
    struct Test *newNode;

    while (1) {
        printf("please input your new node data:\n");
        if (scanf("%d", &newData) != 1) {
            printf("Invalid input\n");
            return head;
        }

        if (newData == 0) {
            printf("0 quit!!\n");
            return head;
        }

        newNode = (struct Test *)malloc(sizeof(struct Test));
        if (newNode == NULL) {
            printf("Memory allocation failed\n");
            return head;
        }

        newNode->data = newData;
        newNode->next = head;
        head = newNode;
    }
    return head;
}

void freeList(struct Test *head) {
    struct Test *current = head;
    while (current != NULL) {
        struct Test *temp = current;
        current = current->next;
        free(temp);
    }
}

int main() {
    struct Test *head = NULL;

    head = insertFromHead(head);

    printLink(head);

    freeList(head);

    return 0;
}


![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/206566813007193.png "#left")


img

  • 写回答

2条回答 默认 最新

  • GoodtimeGood 2023-11-18 22:49
    关注
    
    #include <stdio.h>
    #include <stdlib.h>
    struct Test
    {
        int data;
        struct Test *next;
    };
    
    void printLink(struct Test *head)
    {
        struct Test *point = head;
        while (point != NULL)
        {
            printf("%d ", point->data);
            point = point->next;
        }
        printf("\n");
    }
    struct Test* insertFromHead(struct Test *head)
    {
        struct Test *new;
        while(1){
                new = (struct Test *)malloc(sizeof(struct Test));
                printf("please input your new note data:\n");
                scanf("%d",&(new->data));
                if(new -> data == 0){
                    printf("0 quit!!\n");
                    return head;
                }
                if(head ==NULL){
                    head = new;
                    
                }else{
                    new ->next = head;
                    head = new;
        }
        }
        return head;
    }
    int main()
    {
      
        struct Test *head = NULL;
    
        head = insertFromHead(head);
    
       printLink(head);
           
        return 0;
    }
    

    下面的这个才是我写的又问题的代码

    评论

报告相同问题?

问题事件

  • 创建了问题 11月18日

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)