第1个回答 2010-12-10
#include <stdio.h>
#include <malloc.h>
struct stu
{
int num;
char name[16];
char sex;
int age;
float grade;
struct stu *next;
}a[5]=,,,
,};
struct stu *createlist() //建立链表
{
stu *head,*p1;
int i;
head=(stu *)malloc(sizeof(stu));
head->next=NULL;
if(head!=NULL)
{
for(i=0;i<5;i++)
{
p1=(stu *)malloc(sizeof(stu));
*p1=a[i];
p1->next=head->next->next;
head->next=p1;
}
}
else
printf("out of space");
return head;
}
int deletenode(stu *head,int age) //删除年龄为age的结点
{
stu * t,*k;
t=(stu *)malloc(sizeof(stu));
if(t!=NULL)
{
t=head;
while(t->next!=NULL&&t->next->age!=age)
t=t->next;
if(t->next==NULL)
{
printf("Not exist");
return 0;
}
else
{
k=t->next;
t->next=k->next;
free(k);
return 1;
}
}
}
void output(stu *head) //输出链表
{
stu *p;
p=(stu*)malloc(sizeof(stu));
for(p=head;head->next!=NULL;head=head->next)
{
printf("学号:%d 姓名:%s 性别:%c 年龄:%d 分数:%f",
p->num,p->name,p->sex,p->age,p->grade);
}
}
void main()
{
int n;
stu * T;
T=createlist();
output(T);
printf("输出要删除学生的年龄:");
scanf("%d",n);
deletenode(T,n);
output(T);
}
新手请多包含,希望对你帮助