|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
/*************************************************
******这是一个动态链接程序**************************/
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student *creat(); //创建链表 当NUM为0 时 结束
void print(struct student *head);//打印函数
struct student *delet(struct student *head); //s删除结点
struct student *insert(struct student *head); //插入结点
struct student
{
int num ;
float score ;
struct student *next;
};
int n;
void main()
{
struct student *stu;
stu = creat();
delet(stu);
print(stu);
insert(stu);
print(stu);
printf("\n\n");
}
struct student *creat()
{
struct student *head ;
struct student *p1 , *p2;
int s;
p1 = p2 = malloc(LEN);
printf("当输入学生号为 0 时 结束输入\n");
again: printf("请输入学生的学号 :");
if(!(scanf("%d" , &p1->num))) //进行输入判定
{
printf("input error please input again\n");
getchar();
goto again;
}
again2: printf("请输入学生的分数 :"); //进行输入判定
if(!(scanf("%f", &p1->score)));
{
printf("input error please input again\n");
getchar();
goto again2;
}
head = NULL;
n = 0 ;
while(p1->num)
{
n++;
if(n == 1)
{
head = p1 ;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
again1: printf("请输入学生的学号 :");
if(!(scanf("%d" , &p1->num))); //进行输入判定
{
printf("input error please input again\n");
getchar();
goto again1;
}
printf("请输入学生的分数 :");
again3: if(!(scanf("%f", &p1->score))); //进行输入判定
{
printf("input error please input again\n");
getchar();
goto again3;
}
}
p2 ->next = NULL;
return head;
}
void print(struct student *head)
{
struct student *p ;
p = head ;
printf("\t\t\t你输入的学生数是: %d\n\n", n);
while(p)
{
printf("学生的学号是:%d \t学生的分数是:%f \n",p->num , p->score);
p = p->next;
}
}
struct student *delet(struct student *head)
{
struct student *p1 , *p2;
int j ;
p1 = head ;
printf("请输入你要删除的学号:");
scanf("%d" , &j);
while(p1->num != j && p1->next != NULL)
{
if(p1->num != j)
{
p2 = p1 ;
p1 = p1->next;
}
}
if(p1->num == j)
{
if(p1 == head)
{
head = NULL;
head = p1->next;
n -= 1 ;
}
else
{
p2->next = p1->next;
n -= 1;
}
}
else
{
printf("\t\t\t没有你要删除的学号\n");
}
return head;
}
struct student *insert(struct student *head)
{
struct student *the_new , *p1 , *p2 ;
the_new = (struct student *)malloc(LEN);
printf("请输入你要插入学生的学号:");
scanf("%d",&the_new->num);
printf("请输入你要插入学生的成绩:");
scanf("%f",&the_new->score);
p1 = head;
while((the_new->num > p1->num) &&(p1->next != NULL) )
{
p2 = p1 ;
p1 = p1->next ;
}
if(the_new->num <= p1->num)
{
if(p1 == head)
{
head = the_new ;
}
else
{
p2->next = the_new ;
}
the_new->next = p1 ;
}
else
{
p1->next = the_new ;
the_new->next = NULL;
}
n = n+1 ;
return head ;
}
求教 为什么头结点无法删除!
|
|