头文件:MyList.h
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define sizeof (unsigned int)sizeof
struct student
{
char name[20];
int age;
char sex[10];
char tel[20];
};
//结构体名称是Node
struct Node
{
struct student data;
struct Node* next;
};
//创建链表
struct Node* createList()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//创建表头
//1.sizeof(struct Node)就是求 struct Node 这个结构体占用的字节数。
//2.malloc(sizeof(struct Node))就是申请 struct Node 这个结构体占用字节数大小的空间
//3.(struct Node *) malloc(sizeof(struct Node))将申请的空间的地址强制转化为 struct Node* 指针类型
//4.struct Node* headNode=(struct Node *) malloc(sizeof(struct Node))将那个强制转化的地址赋值给 headNode.
if (headNode != NULL)
{
headNode->next = NULL;
}
return headNode;
}
//创建结点
struct Node* createNode(struct student data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode != NULL) {
newNode->data = data;
newNode->next = NULL;
}
return newNode;
}
//插入节点
void insertNodeByHead(struct Node* headNode, struct student data)
{
struct Node* newNode = createNode(data);
//表头法
newNode->next = headNode->next;
headNode->next = newNode;
}
//修改学生信息
void alterMessage(struct Node* headNode, char* name)
{
struct Node* posNode = headNode->next;
struct Node* posFrontNode = headNode;
if (posNode == NULL)
{
printf("数据为空,无法修改!\n");
return;
}
while (strcmp(posNode->data.name, name))
{//strcmp(str1,str2),若str1=str2,则返回零;若str1<str2,则返回负数;若str1>str2,则返回正数
posFrontNode = posNode;
posNode = posFrontNode->next;
if (posNode == NULL)
{
printf("未找到该学生,无法修改!");
return;
}
}
//找到了posNode
struct student stu;
printf("请输入修改后的学生姓名、年龄、性别、电话:");
scanf_s("%s%u%s%s", stu.name, sizeof(stu.name), &stu.age, stu.sex, sizeof(stu.sex), stu.tel, sizeof(stu.tel));
//char a[100];
//scanf_s("%s", a, sizeof(a));
posNode->data = stu;
}
//指定位置删除
void deleteAppoinNode(struct Node* headNode, char* name)
{
struct Node* posNode = headNode->next;
struct Node* posFrontNode = headNode;
if (posNode == NULL)
{
printf("数据为空,无法删除!\n");
return;
}
while (strcmp(posNode->data.name, name))
{
posFrontNode = posNode;
posNode = posFrontNode->next;
if (posNode == NULL)
{
printf("未找到指定位置无法删除!");
return;
}
}
//找到了
posFrontNode->next = posNode->next;
free(posNode);
}
//查找功能
struct Node* searchInfoByData(struct Node* headNode, char* name)
{
struct Node* pMove = headNode->next;
if (pMove == NULL)
return NULL;
while (pMove != NULL && strcmp(pMove->data.name, name))
{
pMove = pMove->next;
}
return pMove;
}
//文件读操作
void readInfoFromFile(struct Node* headNode, const char* fileName)//把文件信息读到链表中去
{
//1.打开
FILE* fp;
struct student data;
fopen_s(&fp, fileName, "r");//以r打开只读文件,该文件必须存在
if (fp == NULL)//如果fp为空,即打开的文件地址为空,即该文件不存在即打开失败
{
fopen_s(&fp, fileName, "w+");//则重新以w+打开可读写文件,若文件不存在则建立该文件
}
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
//2.读文件
while (fscanf_s(fp, "%s\t%d\t%s\t%s\n", data.name, sizeof(data.name), &data.age, data.sex, sizeof(data.sex), data.tel, sizeof(data.tel)) != EOF)//成功读取文件则返回读入的参数的个数,失败则返回EOF(-1)
{
insertNodeByHead(headNode, data);//读取成功则把文件的信息插入到链表中去
}
//3.关闭文件
fclose(fp);
}
//文件写操作
void writeInfoToFile(struct Node* headNode, const char* fileName)//把链表信息写到文件中去
{
FILE* fp;
fopen_s(&fp, fileName, "w");//以w的方式打开只写文件,若文件不存在则建立该文件。
if (fp == NULL) {
printf_s("文件打开失败!");
return;
}
struct Node* pMove = headNode->next;
while (pMove)
{
fprintf_s(fp, "%s\t%d\t%s\t%s\n", pMove->data.name, pMove->data.age, pMove->data.sex, pMove->data.tel);
pMove = pMove->next;
}
fclose(fp);
}
//打印链表
void printList(struct Node* headNode)
{
struct Node* pMove = headNode->next;
printf("姓名\t年龄\t性别\t电话\n");
while (pMove)
{
printf("%s\t%d\t%s\t%s\n", pMove->data.name, pMove->data.age, pMove->data.sex, pMove->data.tel);
pMove = pMove->next;
}
printf("\n");
}
源文件:StudentSystem.cpp
#include"MyList.h"
void menu()
{
//所有操作都同步到文件
printf("----------【学生信息管理系统】----------\n");
printf("\t\t0.退出系统\n");
printf("\t\t1.录入信息\n");
printf("\t\t2.浏览信息\n");
printf("\t\t3.修改信息\n");
printf("\t\t4.删除信息\n");
printf("\t\t5.查询信息\n");
printf("----------------------------------------\n");
}
struct Node* list = createList();//创建链表
//根据所选的菜单项,做相应的事情
void keyDown()
{
int choice = 0;
struct student data;
struct Node* pMove = NULL;
scanf_s("%d", &choice);
switch (choice)
{
case 0:
printf("正常退出!\n");
system("pause");
exit(0);
break;
case 1:
printf("---------------[录入信息]---------------\n");//插入链表
printf("请输入学生姓名、年龄、性别、电话:");
fflush(stdin);//清空输入缓冲区
scanf_s("%s%u%s%s", data.name, sizeof(data.name), &data.age, data.sex, sizeof(data.sex), data.tel, sizeof(data.tel));
insertNodeByHead(list, data);
break;
case 2:
printf("---------------[浏览信息]---------------\n");//打印链表
printList(list);
break;
case 3:
printf("---------------[修改信息]---------------\n");
//课后作业
printf("请输入需要修改的学生姓名:");
scanf_s("%s", data.name, sizeof(data.name));
alterMessage(list, data.name);
break;
case 4:
printf("---------------[删除信息]---------------\n");
printf("请输入删除的学生姓名:");
scanf_s("%s", data.name, sizeof(data.name));
deleteAppoinNode(list, data.name);
break;
case 5:
printf("---------------[查询信息]---------------\n");
printf("请输入要查找的学生姓名:");
scanf_s("%s", data.name, sizeof(data.name));
pMove = searchInfoByData(list, data.name);//pmove就是searchInfoByData(list,data.name)
if (pMove == NULL)
{
printf("未找到相关信息");
}
else
{
printf("姓名\t年龄\t性别\t电话\n");
printf("%s\t%d\t%s\t%s\n", pMove->data.name, pMove->data.age, pMove->data.sex, pMove->data.tel);
}
break;
default:
printf("选择错误,重新输入!\n");
system("pause");
break;
}
writeInfoToFile(list, "1.txt");
}
int main()
{
readInfoFromFile(list, "1.txt");
while (1)
{
menu();
keyDown();
system("pause");//是暂停的意思,等待用户信号;不然控制台程序会一闪即过,你来不及看到执行结果。
system("cls");//清屏
}
//测试链表代码
//struct Node* list = createList();
//insertNodeByHead(list, 1);
//insertNodeByHead(list, 3);
//insertNodeByHead(list, 2);
//insertNodeByHead(list, 5);
//printList(list);
//printf("删除指定位置:\n");
//deleteAppoinNode(list, 3);
//printList(list);
//printf("链表的查找:\n");
///*printf("%d", searchInfoByData(list, 222)->data);*/
//if (searchInfoByData(list, 222) != NULL)
//{
// printf("%d", searchInfoByData(list, 222)->data);
//}
//else
//{
// printf("没查找到!\n");
//}
//system("pause");
return 0;
}
运行结果: