写了个小甲鱼老师留的作业 学生数据管理
#include<stdio.h>#include<stdlib.h>
#define LEN sizeof(struct student)
struct student
{
int num;
int scoer;
struct student *next;
};
int n;
int main()
{
struct student *found();
void print(struct student *);
struct student *del(struct student *);
struct student *insert(struct student *,struct student *stu);
struct student *linked_list=NULL;
struct student *stu,stud;
int temp=0;
char temp1;
stu = &stud;
li:
printf("*********************************************************************\n\n\n");
printf(" 学生管理系统 \n\n\n");
printf("*********************************************************************\n");
printf("创建数据/F 输出数据/P 插入数据/L 删除数据/D\n");
scanf_s("%c", &temp1, 1);
getchar();
if (temp1 == 'F')
{
printf("学号输入为0时创建结束\n");
linked_list = found();
temp++;
printf("数据创建完毕\n");
goto li;
}
else if (temp != 0)
{
switch (temp1)
{
case 'P':print(linked_list); break;
case 'L':linked_list = insert(linked_list, stu); break;
case 'D':linked_list = del(linked_list); break;
default:printf("您输入的不正确。\n");
}
goto li;
}
else
{
printf("请先创建数据\n\n\n\n");
goto li;
}
}
struct student *found()
{
struct student *head, *p1, *p2;
int back;
head = NULL;
n=1;
p1 = (struct student *)malloc(LEN);
head = p2 = p1;
do
{
printf("please input number :\n");
back = scanf_s("%d", &p1->num);
getchar();
} while (back == 0);
do
{
printf("please input scoer :\n");
back = scanf_s("%d", &p1->scoer);
getchar();
} while (back == 0);
while (p1->num != 0)
{
n = n + 1;
p1 = (struct student *)malloc(LEN);
do
{
printf("please input number :\n");
back = scanf_s("%d", &p1->num);
getchar();
} while (back == 0);
do
{
printf("please input scoer :\n");
back = scanf_s("%d", &p1->scoer);
getchar();
} while (back == 0);
if (p1->num == 0)
{
break;
}
else
{
p2->next = p1;
p2 = p1;
}
p2->next = NULL;
}
return head;
}
void print(struct student *head)
{
struct student *p1, *p2 = NULL;
p1 = head;
if (head == NULL)
{
printf("The linked list is NULL");
}
else
{
do
{
printf("number is %d\n",p1->num);
printf("scoer is %d\n",p1->scoer);
p2 = p1;
p1 = p1->next;
} while (p2->next != NULL);
}
}
struct student *del(struct student *head)
{
struct student *p1, *p2 = NULL;
int num;
int back;
p1 = head;
printf("please input delete number:\n");
do
{
back = scanf_s("%d", &num);
getchar();
} while (back == 0);
while (p1->num != num && p1->next!=NULL)
{
p2 = p1;
p1 = p1->next;
}
if (p1 == head)
{
head = p1->next;
}
else if (p1->num == num)
{
if (p1->next == NULL)
{
p2->next= NULL;
}
else
{
p2->next = p1->next;
}
}
else
{
printf("NO data found\n");
}
return head;
}
struct student *insert(struct student *head,struct student *stu)
{
struct student *p0, *p1, *p2=NULL;
int back;
p0 = stu;
p1 = head;
do
{
printf("please input number:\n");
back = scanf_s("%d", &p0->num);
getchar();
} while (back == 0);
do
{
printf("please input scoer:\n");
back = scanf_s("%d", &p0->scoer);
getchar();
} while (back == 0);
if (head == NULL)
{
head = p0;
}
else
{
while (p0->num > p1->num&&p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}if (p1 == head)
{
head = p0;
p0->next = p1;
}
else if (p0->num <= p1->num)
{
p2->next = p0;
p0->next = p1;
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
return head;
}
页:
[1]