创建链表然后输出都可以,但删除一个结点就不行,会显示是空链。
请问怎么解决
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{
long num;
float score;
struct Student *next;
};
int n;
int main()
{
struct Student *creat(void);
struct Student *del(struct Student *head, long num);
struct Student *pt, *pt1;
long num;
pt = creat();
do
{
printf("\nnum:%ld\nscore:%5.1f\n", pt->num, pt->score);
pt = pt->next;
} while (pt != NULL);
printf("删除的编号:");
scanf("%ld", &num);
pt1 = del(pt, num);
do
{
printf("\nnum:%ld\nscore:%5.1f\n", pt1->num, pt1->score);
pt1 = pt1->next;
} while (pt1 != NULL);
return 0;
}
struct Student *creat(void)
{
struct Student *head, *p1, *p2;
p1 = p2 = (struct Student*)malloc(LEN);
scanf("%ld,%f", &p1->num, &p1->score);
n = 0;
head = NULL;
while (p1->num != 0)
{
n = n + 1;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct Student*)malloc(LEN);
scanf("%ld,%f", &p1->num, &p1->score);
}
p2->next = NULL;
return head;
}
struct Student *del(struct Student *head, long num)
{
struct Student *p1, *p2;
if (head == NULL)
{
printf("\nlist null!\n");
return head;
}
p1 = head;
while (num != p1->num&&p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (num == p1->num)
{
if (p1 == head)
head = p1->next;
else
p2->next = p1->next;
printf("delete:%d\n", num);
n = n - 1;
}
else
printf("%ld not been found!\n", num);
return head;
}