求大神帮看下我的链表哪儿出问题了。。。

#include <stdio.h>#include <stdlib.h>#include "malloc.h"struct student{ int num; int age; struct student *next;};struct student *creat(){ struct student *head,*p,*q; int first=0; p=q=(struct student *)malloc(sizeof(struct student)); printf("enter num,age:"); scanf("%d%d",&p->num,&p->age); while(p!=NULL){ if(first==0) { head=p; q=p; first=1; } else { q->next=p; q=p; } p=(struct student *)malloc(sizeof(struct student)); printf("enter num,age:"); scanf("%d%d",&p->num,&p->age); if(p->num==0){ p=NULL; } } return head;}int main(int argc, char* argv[]){ struct student *stu; stu=creat(); while(stu!=NULL) {printf("%d,%d",stu->num,stu->age); stu=stu->next;} return 0;}输的出来但后面还会不停的输出奇怪的数,就像死循环一样,求大神们帮看看谢谢了啊
2026年09月24日 16:31
有1个网友回答
网友(1):

这个静态链表就是用下标来模仿指针的功能的
这段代码就是在可用链的链头插入回收的结点的,下标为0的结点是空闲结点链的头结点
过程为当前结点指向头结点的后继,头结点再指向本结点,这样完成了链表的头插入