在文章最后,是整个代码。我们先从每个操作讲起
先看我们将会构建的函数
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
struct stdunt
{
char name[20];
char stNum[20];
};
struct Node
{
struct stdunt data;
struct Node *next;
};
struct Node *list; //链表主体
void keyDwon(); //按键功能
void menu(); //显示菜单
struct Node *createHead(); //创建链表
void printList(struct Node *headNode); //打印链表数据
struct Node *createNode(struct stdunt data); //创建数据节点
void deleteData(struct Node *headNode,const char *stu_num); //根据学号删除数据
void insertData(struct Node *headNode,struct stdunt data); //插入数据
struct Node *searchDate(struct Node *headNode,const char *stu_num); //根据学号查询学生
struct Node *searchName(struct Node *headNode,const char *name); //根据姓名查询学生
void saveToFile(struct Node *headNode,const char *fileName ); //保存文件
void readFromFile(struct Node *headNode,const char *fileName); //读取文件
int main()
{
list = createHead();
readFromFile(list,"stdunt.txt");
while(1)
{
menu();
keyDwon();
system("pause");
system("cls");
}
return 0;
}
void saveToFile(struct Node *headNode,const char *fileName)
{
struct Node *pmove = headNode->next;
FILE *fp = fopen(fileName,"w");
while(pmove!=NULL)
{
fprintf(fp,"%s %s\n",pmove->data.name,pmove->data.stNum);
pmove = pmove->next;
}
fclose(fp);
}
void readFromFile(struct Node *headNode,const char *fileName)
{
struct stdunt temp;
FILE *fp = fopen(fileName,"r");
if(fp==NULL)
{
fp = fopen(fileName,"w+");
}
while(fscanf(fp,"%s %s",temp.name,temp.stNum)!=EOF)
{
insertData(list,temp);
}
fclose(fp);
}
/*****************************************
* 打印链表数据
* 1.定义一个链表节点并指向头结点下一个
* 2.遍历
*****************************************/
void printList(struct Node *headNode)
{
struct Node *pmove = headNode->next;
while(pmove!=NULL)
{
printf("%s\t %s\n",pmove->data.name,pmove->data.stNum);
pmove = pmove->next;
}
}
/***************************************
* 创建数据节点
*
***************************************/
struct Node *createHead()
{
struct Node *headNode = (struct Node*)malloc(sizeof(struct Node));
assert(headNode);
headNode->next = NULL;
return headNode;
}
struct Node *createNode(struct stdunt data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
assert(newNode);
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertData(struct Node *headNode,struct stdunt data)
{
struct Node *pmove = createNode(data);
pmove->next = headNode->next;
headNode->next = pmove;
}
void deleteData(struct Node *headNode,const char *stu_num)
{
struct Node *prior = headNode;
struct Node *current = headNode->next;
while(current!=NULL && strcmp(current->data.stNum,stu_num))
{
prior = current;
current = prior->next;
}
if(current!=NULL)
{
printf("没有该学生信息");
}
else
{
prior->next = current->next;
free(current);
}
}
struct Node *searchDate(struct Node *headNode,const char *stu_num)
{
struct Node *pmove = headNode->next;
while( pmove!=NULL && strcmp(pmove->data.stNum,stu_num) )
{
pmove = pmove->next;
}
return pmove;
}
struct Node *searchName(struct Node *headNode,const char *name)
{
struct Node *pmove = headNode->next;
while( pmove!=NULL && strcmp(pmove->data.name,name) )
{
pmove = pmove->next;
}
return pmove;
}
void menu()
{
printf("********************************************************\n");
printf("**************** 学生学籍信息 ****************\n");
printf("********************************************************\n");
printf("******** 1.添加 2.删除 ********\n");
printf("******** 3.查找 4.修改 ********\n");
printf("******** 5.显示 0.退出 ********\n");
printf("********************************************************\n");
}
void keyDwon()
{
int key_value=0;
struct stdunt temp;
struct Node *result;
scanf("%d",&key_value);
switch(key_value)
{
case 0:
printf("---【退出模块】---\n");
system("pause");
exit(0);
case 1:
printf("---【插入模块】---\n");
printf("请输入学生信息,各个信息用空格隔开(stdunt_name stdunt_num):\n");
scanf("%s %s",temp.name,temp.stNum);
insertData(list,temp);
saveToFile(list,"stdunt.txt");
break;
case 2:
printf("---【删除模块】---\n");
printf("请输入要删除的学生学号:\n");
scanf("%s",temp.stNum);
deleteData(list,temp.stNum);
saveToFile(list,"stdunt.txt");
break;
case 3:
printf("---【查找模块】---\n");
printf("请输入要查找学生的学号:\n");
scanf("%s",temp.stNum);
result = searchDate(list,temp.stNum);
if(result==NULL)
printf("没有找到该学生信息,请重新输入");
else
printf("%s\t%s\n",result->data.name,result->data.stNum);
break;
case 4:
printf("---【修改模块】---\n");
printf("请输入要修改学生的姓名:\n");
scanf("%s",temp.name);
result = searchName(list,temp.name);
if(result==NULL)
printf("没有找到该学生信息,请重新输入\n");
else
{
printf("请输入学生信息,各个信息用空格隔开(stdunt_name stdunt_num):\n");
scanf("%s %s",result->data.name,result->data.stNum);
saveToFile(list,"stdunt.txt");
printf("修改成功\n");
}
break;
case 5:
printf("---【插入模块】---\n");
printList(list);
break;
default :
printf("输入有误,请重新输入\n");
break;
}
}
struct stdunt
{
char name[20]; //姓名
char stNum[20]; //学号
};
struct Node
{
struct stdunt data; //结构体数据域
struct Node *next; //指针域
};
/*
typedef struct Node
{
struct stdunt data; //结构体数据域
struct Node *next; //指针域
}linknode,*linklist;
额外说明一点,起别名可以少敲几个键盘挺方便,可我就喜欢敲键盘哈哈
struct Node 等价于 linknode
linknode *L 等价于 linklist *L;
*/