學過鏈表的大家都知道,它是一種動態的數據結構,但是它理解起來是比較困難的,但是用鏈表存儲數據是非常好用的,可以使我們的程序更加具有健壯性。為了初學者理解單項鏈表更加快,今天就用鏈表和文件操作寫了一控制台的學生管理系統,希望可以幫助大家更好的理解鏈表。
程序實現的功能:
源代碼如下
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void AppendNode(long long studentID, char studentName[10], char studentSex[4], int score[4]);//添加數據
void DisplayNode(struct link *head);//顯示數據
void InsertNode(long long studentID, char studentName[10], char studentSex[4], int score[4]);//插入一條數據;按總分的從大到小排序
void InsertNodeNumber(long long studentID, char studentName[10], char studentSex[4], int score[4]);//按學號的從小到大排序
void NumberSorting(int flag);//按從大到小對數據進行排序;1、對總分進行排序,2、對學號進行排序
void DeleteNodeID();//輸入學號刪除
void DeleteNodeName();//輸入學號刪除
void DeleteMemory(struct link *head);//刪除聊表所占用的內存;
void Save();//保存數據
void Open();//打開數據
void FindID();//按學號查找學生
void FindName();//按姓名查找學生;
void Menu();
typedef struct student
{
long long studentID;
char studentName[10];
char studentSex[4];
int score[4];
}STU;
struct link
{
STU student;
struct link *next;
};
struct link *head = NULL;//保存輸入的學生信息數據
struct link *head1 = NULL;//保存排序後的學生信息數據
int main()
{
long long studentID;
char studentName[10];
char studentSex[4];
int score[4];//定義要輸入學生信息的變量;
char c;
int menu;//保存要進行的選項;
Open();
while (1)
{
system("pause");
Menu();
printf("請輸入要進行的操作:");
scanf("%d", &menu);
switch (menu)
{
case 0:exit(0);
break;
case 1:
printf("請輸入Y或y來添加數據\n");
scanf(" %c", &c);
while (c == 'y' || c == 'Y')
{
printf("請輸入學生學號:");
scanf("%lld", &studentID);
printf("請輸入學生姓名:");
scanf("%s", &studentName);
printf("請輸入學生性別:");
scanf("%s", &studentSex);
int i = 0;
printf("請分別輸入學生四門課的成績:");
for (i = 0; i < 4; i++)
{
scanf("%d", &score[i]);
}
AppendNode(studentID, studentName, studentSex, score);
printf("請輸入Y或y來添加數據\n");
scanf(" %c", &c);
}
DisplayNode(head);
break;
case 2:
FindID();
break;
case 3:
FindName();
break;
case 4:
DeleteNodeID();
break;
case 5:
DeleteNodeName();
case 6:
NumberSorting(1);
DeleteMemory(head1);
head1 = NULL;
break;
case 7:
NumberSorting(2);
DeleteMemory(head1);
head1 = NULL;
break;
default:
printf("輸入有誤!請重新輸入");
}
}
Save();
DeleteMemory(head);
DeleteMemory(head1);
system("pause");
}
void AppendNode(long long studentID, char studentName[10], char studentSex[4], int score[4])
{
struct link *p = NULL, *pr = head;
p = (struct link *) malloc(sizeof(struct link));
if (p == NULL)
{
printf("申請內存失敗");
return;
}
if (head == NULL)
{
head = p;
}
else {
while (pr->next != NULL)
{
pr = pr->next;
}
pr->next = p;
}
p->student.studentID = studentID;
strcpy(p->student.studentName, studentName);
strcpy(p->student.studentSex, studentSex);
p->student.score[0] = score[0];
p->student.score[1] = score[1];
p->student.score[2] = score[2];
p->student.score[3] = score[3];
p->next = NULL;
return;
}
void NumberSorting(int flag)
{
struct link *p = head;
struct link *p1 = head1;
int sum = 0;
if(p == NULL)
{
printf("沒有數據,無法排序");
return;
}
while (p!=NULL)
{
switch (flag)
{
case 1:
InsertNode(p->student.studentID, p->student.studentName, p->student.studentSex, p->student.score);
break;
case 2 :
InsertNodeNumber(p->student.studentID, p->student.studentName, p->student.studentSex, p->student.score);
break;
default:
printf("程序異常,請重試!");
break;
}
p = p->next;
}
DisplayNode(head1);
}
void DisplayNode(struct link *head)
{
struct link *p = head;
if (p == NULL)
{
return;
}
printf("%lld", p->student.studentID);
printf("%10s", p->student.studentName);
printf("%5s", p->student.studentSex);
{
int i = 0;
for (i = 0; i < 4; i++)
{
printf("%4d", p->student.score[i]);
}
}
printf("\n");
DisplayNode(p->next);
}
void DeleteMemory(struct link *head)
{
struct link *p = head, *pr = NULL;
while (p != NULL)
{
pr = p;
p = p->next;
free(pr);
}
}
void InsertNodeNumber(long long studentID, char studentName[10], char studentSex[4], int score[4])
{
struct link *pr = head1, *p = head1, *temp = NULL;
p = (struct link *)malloc(sizeof(struct link));
if (p == NULL)
{
printf("內存申請失敗");
return;
}
p->next = NULL;
p->student.studentID = studentID;
strcpy(p->student.studentName, studentName);
strcpy(p->student.studentSex, studentSex);
p->student.score[0] = score[0];
p->student.score[1] = score[1];
p->student.score[2] = score[2];
p->student.score[3] = score[3];
if (head1 == NULL)
{
head1 = p;
}
else
{
while (pr->student.studentID < studentID&&pr->next != NULL)
{
temp = pr;
pr = pr->next;
}
if (pr->student.studentID >= studentID)
{
if (pr == head1)
{
p->next = head1;
head1 = p;
}
else
{
pr = temp;
p->next = pr->next;
pr->next = p;
}
}
else
{
pr->next = p;
}
}
return;
}
void InsertNode(long long studentID, char studentName[10], char studentSex[4], int score[4])
{
struct link *pr = head1, *p = head1, *temp = NULL;
int sum, sum1;
p = (struct link *)malloc(sizeof(struct link));
if (p == NULL)
{
printf("內存申請失敗");
return;
}
p->next = NULL;
p->student.studentID = studentID;
strcpy(p->student.studentName, studentName);
strcpy(p->student.studentSex, studentSex);
p->student.score[0] = score[0];
p->student.score[1] = score[1];
p->student.score[2] = score[2];
p->student.score[3] = score[3];
sum = p->student.score[0] + p->student.score[1] + p->student.score[2] + p->student.score[3];
if (head1 == NULL)
{
head1 = p;
}
else
{
sum1 = pr->student.score[0] + pr->student.score[1] + pr->student.score[2] + pr->student.score[3];
while (sum1 > sum&&pr->next != NULL)
{
temp = pr;
pr = pr->next;
sum1 = pr->student.score[0] + pr->student.score[1] + pr->student.score[2] + pr->student.score[3];
}
if (sum1 <= sum)
{
if (pr == head1)
{
p->next = head1;
head1 = p;
}
else
{
pr = temp;
p->next = pr->next;
pr->next = p;
}
}
else
{
pr->next = p;
}
}
return;
}
void DeleteNodeID()//輸入學號刪除
{
struct link *p = head, *pr = head;
long long studentID = 0;
if (head == NULL) {
printf("對不起,沒有數據可以刪除");
return;
}
printf("請輸入你要刪除的學生的學號");
scanf("%s", studentID);
while (studentID != p->student.studentID&&p->next != NULL)
{
pr = p;
p = p->next;
}
if (studentID == p->student.studentID)
{
if (p == head) {
head = p->next;
}
else
{
pr->next = p->next;
}
free(p);
}
else
{
printf("你要查找的這個數據未找到");
}
return;
}
void DeleteNodeName()//輸入姓名刪除
{
struct link *p = head, *pr = head;
char studentName[10] = "";
if (head == NULL) {
printf("對不起,沒有數據可以刪除");
return;
}
printf("請輸入你要刪除的學生的姓名:");
scanf("%s", studentName);
while (0!=strcmp(studentName,p->student.studentName)&&p->next != NULL)
{
pr = p;
p = p->next;
}
if (0==strcmp(studentName, p->student.studentName))
{
if (p == head) {
head = p->next;
}
else
{
pr->next = p->next;
}
free(p);
}
else
{
printf("你要查找的這個數據未找到");
}
return;
}
void Save()//保存鏈表中的數據
{
FILE *fp;
struct link *p = head;
if ((fp = fopen("demo.txt", "w")) == NULL)
{
printf("打開文件失敗");
return;
}
while (p != NULL)
{
fprintf(fp, "%20lld%10s%5s%4d%4d%4d%4d", p->student.studentID, p->student.studentName, p->student.studentSex,
p->student.score[0],
p->student.score[1],
p->student.score[2],
p->student.score[3]);
p = p->next;
}
fclose(fp);
return;
}
void Open()//將文件中獲得的數據寫入到鏈表中
{
long long studentID;
char studentName[10];
char studentSex[4];
int score[4];
FILE *fp;
char c;
if ((fp = fopen("demo.txt", "r")) == NULL)
{
printf("文件打開失敗");
return;
}
while ((c = fgetc(fp))!=EOF)
{
fscanf(fp, "%20lld", &studentID);
fscanf(fp, "%10s", studentName);
fscanf(fp, "%5s", studentSex);
fscanf(fp, "%4d", &score[0]);
fscanf(fp, "%4d", &score[1]);
fscanf(fp, "%4d", &score[2]);
fscanf(fp, "%4d", &score[3]);
AppendNode(studentID, studentName, studentSex, score);
}
fclose(fp);
}
void FindID()
{
struct link *p = head;
long long studentID=0;
if (head == NULL)
{
printf("沒有數據查找");
return;
}
printf("請輸入你要查找的學生的學號:");
scanf("%lld", &studentID);
while (studentID != p->student.studentID&&p->next != NULL)
{
p = p->next;
}
if (studentID == studentID)
{
printf("%lld", p->student.studentID);
printf("%10s", p->student.studentName);
printf("%5s", p->student.studentSex);
{
int i = 0;
for (i = 0; i < 4; i++)
{
printf("%4d", p->student.score[i]);
}
}
}
else{
printf("沒有你要查找的數據");
}
return;
}
void FindName()
{
struct link *p = head;
char studentName[10] = "";
if (head == NULL)
{
printf("沒有數據查找");
return;
}
printf("請輸入你要查找的學生的姓名:");
scanf("%s", studentName);
while (0!=strcmp(studentName,p->student.studentName)&&p->next != NULL)
{
p = p->next;
}
if (0==strcmp(studentName,p->student.studentName))
{
printf("%lld", p->student.studentID);
printf("%10s", p->student.studentName);
printf("%5s", p->student.studentSex);
{
int i = 0;
for (i = 0; i < 4; i++)
{
printf("%4d", p->student.score[i]);
}
}
}
else{
printf("沒有你要查找的數據");
}
return;
}
void Menu()
{
system("cls");//清平操作;
printf("\n\n\n\n\n");//輸入回車,形成格式;
printf("\t\t|...........學生管理系統..............|\n");//
printf("\t\t|\t 0.退出。 |\n");//“/t”水平制表,相當於一個tab鍵;
printf("\t\t|\t 1.添加學生成績信息! |\n");
printf("\t\t|\t 2.查找學生(按學號)信息! |\n");
printf("\t\t|\t 3.查找學生(按姓名)信息! |\n");
printf("\t\t|\t 4.刪除學生成績(按學號)信息! |\n");
printf("\t\t|\t 5.刪除學生成績(按姓名)信息! |\n");
printf("\t\t|\t 6.按總分排序! |\n");
printf("\t\t|\t 7.按學號排序! |\n");
printf("\t\t|...........學生管理系統..............|\n");//
}
如對程序有什麼好的建議歡迎評論指教。。。。。。