不知道是链表建立出错还是其他问题,在遍历链表的时候会漏数据且崩溃
#include<stdio.h>
#include<stdlib.h>
typedef struct arr
{
int nums;
char names[40];
struct arr* next;
}*Node;
int main()
{
Node head, body, tail,p;
head = (Node)malloc(sizeof(struct arr));
tail = (Node)malloc(sizeof(struct arr));
tail->next = NULL;
head->next = tail;
int n;
printf("输入链表长度:\n");
scanf_s("%d", &n);
for (int i = 0; i < n; i++)
{
body = (Node)malloc(sizeof(struct arr));
printf("依次输入编号,姓名;\n");
scanf_s("%d", &body->nums);
getchar();
fgets(body->names, 40, stdin);
if (i == 0)
{
head = body;
tail = head;
}
head->next = body->next;
head->next = body;
}
while (head) //遍历链表
{
printf("%d ", head->nums);
fputs(head->names, stdout);
head = head->next;
}
}