| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
 跟大牛学C++学算法数据结构 程序代码：
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student *creat();
struct student *del(struct student *head,int num);
struct student *insert(struct student *head,struct student *stu_2);
void print (struct student *head);
struct student
{
int num;
float score;
struct student *next;
};
int n;
void main()
{
struct student *stu ,*p,*stu_2;
int n;
stu=creat();
stu_2=(struct student *)malloc(LEN);
p=stu;
print(p);
printf("please enter the num to delete:");//删除
scanf("%d",&n);
print(del(p,n));
printf("input the num to insert:");//添加
scanf("%d",&stu_2->num);
printf("input score:");
scanf("%f",&stu_2->score);
p=insert(stu,stu_2);
print(p);
printf("\n\n");
system("pause");
}
struct student *creat()
{
struct student *p1,*p2;
p1=p2=(struct student *)malloc(LEN);
printf("please enter the num:");
scanf("%d",&p1->num);
printf("please enter the score:");
scanf("%f",&p1->score);
n=0;
while(p1->num)
{
n++;
if(n==1)
{
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("please enter the num:");
scanf("%d",&p1->num);
printf("please enter the score:");
scanf("%f",&p1->score);
}
p2->next=NULL;
}
void print(struct student *head)
{
struct student *p;
printf("\nthere are %d records\n",n);
{
do
{
printf("学号为%d的成绩是：%f\n",p->num,p->score);
p=p->next;
}while(p);
}
}
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
{
printf("this is a null\n");
goto END;
}
while(p1->num!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
{
}
else
{
p2->next=p1->next;
}
printf("\ndelete No:%d succeed!\n",num);
n=n-1;
}
else
{
printf("%d 没有",num);
}
}
struct student *insert(struct student *head,struct student *stu_2)
{
struct student *p0,*p1,*p2;
p0=stu_2;
{
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
{
p0->next=p1;
}
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{
p1->next=p0;
p0->next=NULL;
}

}
n=n+1;
}    • 1
• 1/1页
• • 1
• Powered by Discuz, Processed in 0.059468 second(s), 8 queries.