|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct student
{
int ornum;
double score;
struct student *next;
};
#define LEN sizeof(struct student)
struct student *create();
void print(struct student *head);
struct student *del(struct student *p,int n);
int n=0;
void main()
{
int num;
struct student *stu , *p;
stu = create();
p = stu ;
print(p);
printf("please input the order number you want to delete:");
scanf("%d",&num);
print(del(stu , num));
printf("\n\n");
system("pause");
}
struct student *create()
{
struct student *p1,*p2,*head;
p1 = p2 = (struct student *)malloc(LEN);
printf("请输入第%d个学生的学号:",n+1);
scanf("%d", &p1->ornum);
printf("请输入第%d个学生的成绩:",n+1);
scanf("%lf",&p1->score);
while(p1->ornum)
{
if(!n)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
n++;
p1 = p2 = (struct student *)malloc(LEN);
printf("请输入第%d个学生的学号:",n+1);
scanf("%d", &p1->ornum);
printf("请输入第%d个学生的成绩:",n+1);
scanf("%lf",&p1->score);
}
p2->next = NULL;
return head;
}
struct student *del(struct student *head,int n)
{
struct student *p1,*p2;
if(!head)
{
printf("该链表为空表\n");
goto end;
}
p1 = head ;
while(p1->ornum != n&&p1->next !=NULL)
{
p2 = p1;
p1 = p1->next ;
}
if(p1->ornum ==n)
{
if(p1 == head)
{
head = p1->next ;
}
else
{
p2->next = p1->next ;
}
printf("\ndelete No.%d succeed!\n");
n--;
}
else
{
printf("找不到!");
}
end:
return head;
}
void print(struct student *head)
{
struct student *p;
printf("there are %d student in total!\n",n);
p = head;
if(head)
{
do
{
printf("学生的学号为%d,成绩为%lf\n",p->ornum ,p->score );
p=p->next ;
}while(p);
}
}求助大家,这段代码怎么也不对,print函数只能打印出第一个学生的信息 |
|