急 C语言 链表输出结果 但是不知道哪里出问题如图一直不停输出


void print(struct int_list *head)
{
    struct int_list *p;
    printf("out\n");
    p=head;
    if(head!=NULL)
        do
        {
            printf("Output:");
            printf("%d",p->value);
        }while(p!=NULL);
}

int main ()
{
    struct int_list *head;
    head = creat();
    print(head);
    return 0;
}

img

这个代码只是我输出和主函数的代码 我觉得应该是这里的问题吧

你的输出函数里,p一直都指向头结点,你得在循环里面加一句p=p–>next !如果有用请点击采纳


#include <stdio.h>
//#include <stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct int_list)
struct int_list
{
    int value;
    struct int_list *next;
};
typedef struct int_list IntList;
int n;
struct int_list *creat()
{
    struct int_list *head;
    struct int_list *p1,*p2;
    n=0;
    p1=p2=(struct int_list *)malloc(LEN);
    printf("Input:");
    scanf("%d", &p1->value);
    head=NULL;
    while(p1->value!=0)
    {
        n=n+1;
        if(n==1)head=p1;
        else p2->next = p1;
        p2=p1;
        p1=(struct int_list*)malloc(LEN);
        printf("Input:");
        scanf("%d", &p1->value);
    }
    p2->next=NULL;
    return(head);
}

void print(struct int_list *head)
{
    struct int_list *p;
    printf("out\n");
    p=head;
    if(head!=NULL)
        do
        {
            printf("Output:");
            printf("%d",p->value);
        }while(p!=NULL);
}

int main ()
{
    struct int_list *head;
    head = creat();
    print(head);
    return 0;
}

这是完整代码 请大佬帮忙看看 急!!!