|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# include<stdio.h>
# include<stdlib.h>
# define LEN sizeof(struct student)
struct student
{
int num;
int age;
struct student *next;
};
int n;
struct student *creat()
{
struct student *p1,*p2,*head;
p1=p2=(struct student *)malloc(LEN);
head=NULL;n=0;
printf("input the num: ");
scanf("%d",&p1->num);
printf("input the age: ");
scanf("%d",&p1->age);
while(p1->num)
{
n++;
if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("input the num: ");
scanf("%d",&p1->num);
printf("input the age: ");
scanf("%d",&p1->age);
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head)
{
do
{
printf("%d,%d\n",p->num,p->age);
p=p->next;
}while(p);
}
}
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
p1=head;p2=NULL;
while(p1->num!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==num)
{
if(p1==head)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
}
else
{
printf("没找到");
}
return head;
}
struct student *insert(struct student *head,struct student *stu2)
{
struct student *p1,*p2,*p0;
p1=head;p2=NULL;p0=stu2;
if(NULL==head)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(p1==head)
{
head=p0;
}
else
{
p2->next=p0;
}
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
return head;
}
void main()
{
struct student *stu,*p,stu2;
stu=creat();
p=stu;
print(p);
printf("\n");
printf("你想删除哪一个呢:");
scanf("%d",&n);
print(del(p,n));
printf("\n");
printf("插入num:");
scanf("%d",&stu2.num);
printf("插入age:");
scanf("%d",&stu2.age);
p=insert(stu,&stu2);
print(p);
printf("\n");
}
这个程序运行时,删第一个节点再加上第一个就多出个内容,帮帮忙!! |
|