此代码会有不足之处或者需要改进的地方!请各位不了手下留情!多多提醒我改进的地方。那么有劳各位费一点时间运行一下我的代码!想交流想法的大神或者尬聊的各位多多欢迎评论。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
//建立结构体
struct Student_n //学生的自然信息
{
int iNumber;
char cName[20];
char sex[5];
int old;
char nation[10];
Student_n* pNext;
};
//建立结构体
struct Student_p //学生的选课信息
{
int iNumber;
char cName[20];
int scnumber;
char scname[20];
Student_p* pNext;
};
//建立结构体
struct Student_s //学生的成绩
{
int iNumber;
char cName[20];
int scnumber;
char scname[20];
int sc;
Student_s* pNext;
};
//建立空表头
Student_n* head_n()
{
Student_n* pHead = (Student_n *)malloc(sizeof(Student_n));
pHead->pNext = NULL;
return pHead;
}
int iCount_n; //记节点的个数
//建立链表
void create_n(Student_n* pHead)
{
Student_n* temp = (Student_n *)malloc(sizeof(Student_n)); //建立可移动的表头
Student_n* pNew;
int i = 1;
iCount_n = 0;
printf("开始输入学生基本信息!\n\n");
while(1)
{
pNew = (Student_n *)malloc(sizeof(Student_n));
pNew->pNext = NULL;
printf("输入第%d个人的信息(输入0终止输入):\n", i);
printf("学号:");
scanf("%d", &pNew->iNumber);
if(pNew->iNumber == 0)
{
break;
}
printf("姓名:");
scanf("%s", pNew->cName);
printf("性别:");
scanf("%s", pNew->sex);
printf("年龄:");
scanf("%d", &pNew->old);
printf("民族:");
scanf("%s", pNew->nation);
if(i == 1)
{
pHead->pNext = pNew;
temp = pNew;
}
else
{
temp->pNext = pNew;
temp = temp->pNext;
temp->pNext = NULL;
}
iCount_n++;
i++;
}
free(pNew);
}
//添加基本信息
void insert_n(Student_n* pHead)
{
int j = 1;
int i = iCount_n;
Student_n* ptemp;
Student_n* p, *temp; //可移动的添加链表
Student_n* head = (Student_n *)malloc(sizeof(Student_n));
while(1)
{
temp = (Student_n *)malloc(sizeof(Student_n));
temp->pNext = NULL;
printf("添加第%d个人的信息:(输入0终止输入)\n", i + 1);
printf("学号:");
scanf("%d", &temp->iNumber);
if(temp->iNumber == 0)
{
break;
}
printf("姓名:");
scanf("%s", temp->cName);
printf("性别:");
scanf("%s", temp->sex);
printf("年龄:");
scanf("%d", &temp->old);
printf("民族:");
scanf("%s", temp->nation);
if(j == 1)
{
head = temp;
p = temp;
}
else
{
p->pNext = temp;
p = p->pNext;
p->pNext = NULL;
}
i++;
iCount_n++;
}
free(temp);
ptemp = pHead;
while(ptemp->pNext != NULL)
{
ptemp = ptemp->pNext;
}
ptemp->pNext = head;
}
//删除基本信息
void Delete_n(Student_n* pHead)
{
int n = 0; //输入要删除人的学号
Student_n* p, *temp; //负责移动的指针
Student_n* goal; //要删除的节点
printf("请输入要删除人的学号:");
DeLeTe:
scanf("%d", &n);
p = pHead;
temp = pHead;
goal = pHead;
while(n != goal->iNumber)
{
p = p->pNext;
goal = goal->pNext;
if(goal == NULL)
{
printf("无此人!请输入正确的学号...\n");
goto DeLeTe;
}
}
if(goal->pNext == NULL)
{
while(temp->pNext != goal)
{
temp = temp->pNext;
}
temp->pNext = NULL;
free(goal);
iCount_n--;
}
else
{
p = p->pNext;
while(temp->pNext != goal)
{
temp = temp->pNext;
}
temp->pNext = p;
iCount_n--;
free(goal);
}
}
//更改基本信息
void change_n(Student_n* pHead)
{
int n = 0; //学号
char name[20];
int old;
Student_n* temp; //负责移动
printf("请输入要更改人的学号:");
change:
scanf("%d", &n);
temp = pHead;
while(n != temp->iNumber)
{
temp = temp->pNext;
if(temp == NULL)
{
printf("无此人!请输入正确的学号...\n");
goto change;
}
}
printf("1,更改姓名\n");
printf("2,更改年龄\n");
errorchange:
scanf("%d", &n);
if(n == 1)
{
printf("请输入新的姓名:");
scanf("%s", name);
strcpy(temp->cName, name);
}
else if(n == 2)
{
printf("请输入新的年龄:");
scanf("%d", &old);
temp->old = old;
}
else
{
printf("输入有误!请重新输入...\n");
goto errorchange;
}
}
//存盘
void Fileoutput_n(Student_n* pHead)
{
pHead = pHead->pNext;
Student_n* head; //临时指针负责移动
head = pHead;
FILE* p = fopen("nature.dat", "wb");
if(p == NULL)
{
printf("指针为空\n");
exit(1);
}
while(head != NULL)
{
fwrite(head, sizeof(Student_n), 1, p);
head = head->pNext;
}
fclose(p);
}
//读盘
void Fileinput_n(Student_n* pHead_n)
{
iCount_n = 0;
int i = 1;
Student_n* head;
Student_n* temp = (Student_n *)malloc(sizeof(Student_n));
Student_n pNew;
head = pHead_n;
FILE* p = fopen("nature.dat", "rb");
if(p == NULL)
{
printf("文件为空!\n");
exit(1);
}
while(fread(&pNew, sizeof(Student_n), 1, p) == 1)
{
temp = (Student_n *)malloc(sizeof(Student_n));
*temp = pNew;
head->pNext = temp;
head = head->pNext;
head->pNext = NULL;
iCount_n++;
}
fclose(p);
}
//遍历
void traversal_n(Student_n* pHead)
{
Student_n* temp; //临时指针负责移动
temp = pHead;
int i = 1;
if(pHead->pNext == NULL)
{
printf("链表为空!\n");
exit(1);
}
temp = temp->pNext;
printf("输出%d个人的信息!\n", iCount_n);
while(temp != NULL)
{
printf("输出第%d个人的信息:\n", i);
printf("学号:%d\n", temp->iNumber);
printf("姓名:%s\n", temp->cName);
printf("性别:%s\n", temp->sex);
printf("年龄:%d\n", temp->old);
printf("民族:%s\n", temp->nation);
temp = temp->pNext;
i++;
}
}
int iCount_p;
//建立选课信息的空表头
Student_p* head_p()
{
Student_p* head = (Student_p *)malloc(sizeof(Student_p));
head->pNext = NULL;
return head;
}
//建立选课信息链表
void create_p(Student_p* pHead)
{
int i = 1;
iCount_p = 0;
printf("开始输入学生选课信息\n");
Student_p* head = pHead;
Student_p* pNew;
while(1)
{
pNew = (Student_p *)malloc(sizeof(Student_p));
printf("第%d个人的选课信息(输入0终止输入):\n", i);
printf("学号:");
scanf("%d", &pNew->iNumber);
if(pNew->iNumber == 0)
{
break;
}
printf("姓名:");
scanf("%s", pNew->cName);
printf("课程号:");
scanf("%d", &pNew->scnumber);
printf("课程名:");
scanf("%s", pNew->scname);
head->pNext = pNew;
head = head->pNext;
head->pNext = NULL;
iCount_p++;
i++;
}
}
//添加选课信息
void insert_p(Student_p* pHead)
{
int j = 1;
int i = iCount_p;
Student_p* temp; //负责在原链表上移动
Student_p* p, *pNew; //负责在新的联表上移动
Student_p* head;
printf("开始添加学生选课信息:\n");
while(1)
{
pNew = (Student_p *)malloc(sizeof(Student_p));
printf("添加第%d个人的信息(输入0终止输入):\n", i + 1);
printf("学号:");
scanf("%d", &pNew->iNumber);
if(pNew->iNumber == 0)
{
break;
}
printf("姓名:");
scanf("%s", pNew->cName);
printf("课程号:");
scanf("%d", &pNew->scnumber);
printf("课程名:");
scanf("%s", pNew->scname);
if(j == 1)
{
head = pNew;
p = pNew;
head->pNext = NULL;
}
else
{
head->pNext = pNew;
head = head->pNext;
head->pNext = NULL;
}
j++;
i++;
iCount_p++;
}
free(pNew);
temp = pHead;
while(temp->pNext != NULL)
{
temp = temp->pNext;
}
temp->pNext = p;
}
//删除选课信息
void Delete_p(Student_p* pHead)
{
Student_p* p, *temp, *goal;
int n = 0;
error123:
printf("请输入要删除人的学号:");
scanf("%d", &n);
temp = pHead;
p = pHead;
goal = pHead;
while(n != goal->iNumber)
{
p = p->pNext;
goal = goal->pNext;
if(goal == NULL)
{
printf("无此人!请输入正确的学号...\n");
goto error123;
}
}
if(goal->pNext == NULL)
{
while(temp->pNext != goal)
{
temp = temp->pNext;
}
temp->pNext = NULL;
free(goal);
iCount_p--;
}
else
{
p = p->pNext;
while(temp->pNext != goal)
{
temp = temp->pNext;
}
temp->pNext = p;
free(goal);
iCount_p--;
}
}
//更改选课信息
void change_p(Student_p* pHead)
{
char name[20]; //scname
int number; //scnumber
int n = 0;
Student_p* goal;
goal = pHead;
errorchangepick:
printf("请输入更改人的学号:");
scanf("%d", &n);
while(n != goal->iNumber)
{
goal = goal->pNext;
if(goal == NULL)
{
printf("无此人!请输入正确的学号...\n");
goto errorchangepick;
}
}
printf("1,更改选课信息\n");
printf("2,更改课程号\n");
printf("3,更改课程名\n");
errorchange123:
scanf("%d", &n);
if(n == 1)
{
system("cls");
printf("请输入新课程号:");
scanf("%d", &goal->scnumber);
printf<