本帖最后由 Ъγ:_小ツ雨oο 于 2014-2-12 16:43 编辑 #include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct stuednt)
int n; //全局变量
struct stuednt
{
int num;
float score;
struct stuednt *next;
};
struct stuednt *creat(); //创建链表
struct stuednt *del(struct stuednt *head, int num); // 传递的是 头和要删除的数
void print(struct stuednt *head); //打印出来
void main()
{
struct student *stu, *p;
int n;
stu = creat();
p = stu;
print(p);
printf("Please enter the delete num : ");
scanf("%d", &n);
print(del(p, n));
printf("\n\n");
system("pause");
}
struct stuednt creat()
{
struct stuednt *head, *p1, *p2;
p1 = p2 = (struct stuednt *)malloc(LEN);
printf("Please enter the num : ");
scanf("%d", &p1->num);
printf("Please enter the score : ");
scanf("%f", &p1->score);
head = NULL;
n = 0;
while(p1->num)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct stuednt *)malloc(LEN);
printf("Please enter the num :");
scanf("%d", &p1->num);
printf("Please enter the score :");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
void print(struct stuednt *head)
{
struct student *p;
p = head;
if(head)
{
do
{
printf("%d号学生的成绩是: %5.1f\n", p->num, p->score);
p = p->next;
}while(p);
}
}
struct stuednt *del(struct stuednt *head, int num)
{
struct stuednt *p1, *p2;
if(head == NULL)
{
printf("在忽悠人!\n");
goto end;
}
p1 = head;
while(p1->num != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(p1->num == num)
{
if(head == p1)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
printf("\nDelete No: %d succeed!\n",num);
n = n-1;
}
else
{
printf("%d not been found!\n", num);
}
end :
return head;
}
求分析错误, 不晓得这个错误是什么意思, |