|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
int n;
struct student
{
int num;
float score;
struct student *next;
} ;
int main()
{
struct student * creat(void);
struct student * del(struct student *pstu, struct student *p_del_stu);
struct student * insert(struct student *pstu, struct student *p_in_stu);
void print(struct student *pstu);
struct student *stu,*stu_x;
struct student student_X;
stu_x = &student_X;
stu = creat();
print(stu);
printf("请输入需要删除的学生的学号(0结束):");
scanf_s("%d", &stu_x->num);
while (stu_x->num)
{
stu=del(stu, stu_x);
print(stu);
printf("请输入需要删除的学生的学号(0结束):");
scanf_s("%d", &stu_x->num);
}
printf("\n");
printf("请输入需要插入的学生的学号(0结束):");
scanf_s("%d", &stu_x->num);
printf("请输入需要插入的学生的成绩:");
scanf_s("%f", &stu_x->score);
stu_x->next = NULL;
while (stu_x->num)
{
stu = insert(stu, stu_x);
print(stu);
printf("请输入需要插入的学生的学号(0结束):");
scanf_s("%d", &stu_x->num);
printf("请输入需要插入的学生的成绩:");
scanf_s("%f", &stu_x->score);
stu_x->next = NULL;
}
system("pause");
return 0;
}
//生成链表函数
struct student * creat(void)
{
struct student *p1, *p2, *head;
n = 0;
p1 = p2 = (struct student *)malloc(sizeof(struct student));
printf("please input stuent number(0 to terminate):");
scanf_s("%d", &p1->num, 1);
printf("please input stuent score:");
scanf_s("%f", &p1->score, 2);
head = NULL;
while (p1->num)
{
n = n + 1;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(sizeof(struct student));
printf("please input stuent number(0 to terminate):");
scanf_s("%d", &p1->num, 1);
printf("please input stuent score:");
scanf_s("%f", &p1->score, 2);
}
p2->next = NULL;
return head;
}
//链表节点删除函数
struct student * del(struct student *pstu, struct student *p_del_stu)
{
struct student *head,*p1,*p2,*p0;
p0 = p_del_stu;
head = p1 = p2 = NULL;
if (pstu)
{
head = pstu;
p1 = head;
while (p1->num != p0->num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (p1->num == p0->num)
{
if (p1 == head)
{
head = p1->next;
}
else
p2->next = p1->next;
n--;
}
else
printf("没有该学生记录\n");
}
else
printf("此表为空链表!!!\n\n");
return head;
}
//链表插入
struct student * insert(struct student *pstu, struct student *p_in_stu )
{
struct student *head,*p0, *p1, *p2;
head = p1 = pstu;
p2 = NULL;
p0 = p_in_stu;
if (head)
{
while (p0->num > p1->num &&p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (p0->num <= p1->num)
{
if (p1 == head)
{
head = p0;
p0->next = p1;
}
else
{
p2->next = p0;
p0->next = p1;
}
}
else
{
p1->next = p0;
p0->next = NULL;
}
n++;
}
return head;
}
//打印函数
void print(struct student *pstu)
{
struct student *head;
head = pstu;
printf("There are %d records!!!\n", n);
while (head)
{
printf("student number = %d\n", head->num);
printf("student score = %5.2f\n\n", head->score);
head = head->next;
}
} |
|