该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
struct student
{
long num;
char *name;
char *sex;
int age;
struct student *next;
};
struct student *creat()
{
struct student a,b,c;
struct student *head;
a.num=1001,a.name="Xiao Ming",a.sex="man",a.age=20;
b.num=1002,b.name="Xiao Hong",b.sex="woman",b.age=30;
c.num=1003,c.name="Xiao Wang",c.sex="man",c.age=40;
head=&a;
a.next=&b;
b.next=&c;
c.next=NULL;
return head;
}//建立链表函数
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
p1=head;
while(num!=p1->age&&p1->next!=NULL)
{p2=p1;p1=p1->next;}//不是所要找的结点,则移动到下一个结点,并用p2保存上个结点
if(num==p1->age)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
}
return head;
}//建立删除结点函数
void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
do
{ printf("%4ld %s %s %d",p->num,p->name,p->sex,p->age);
p->next;}
while(p!=NULL);
}//建立输出函数
void main()
{
struct student *head;
int del_num;
head=creat();
scanf("%d",&del_num);
head=del(head,del_num);
print(head);
}