#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>
enum Stu {
add=1,
del,
modify,
search,
count1,
show
};
typedef struct stu {
char name[20];
char id[20];
}st;
typedef struct node {
struct stu Data;
struct stu* next;
}*Link,Node;
void InsertNode(Link node) {
printf("请输入学生的姓名:>");
scanf("%s", node->Data.name);
printf("请输入学生的学号:>");
scanf("%s",node->Data.id);
node->next = NULL;
}
bool addNode(Link head) {
Link p, q;
Link node ;
node=(Link)malloc(sizeof(Node));
InsertNode(node);
q = head;
p = head->next;
if (head->next==NULL) {
head->next = node;
}
else {
while (p != NULL) {
if ((strcmp(p->Data.id , node->Data.id))>0){
node->next = p;
q->next = node;
return true;
}
else {
q = p;
p = p->next;
}
}
q->next = node;
}
return true;
}
void showNode(Link head) {
Link p = head->next;
if (head->next == NULL) {
printf("此表为空\n");
}
while(p!=NULL) {
printf("姓名:%s\n", p->Data.name);
printf("学号:%s\n", p->Data.id);
p = p->next;
}
}
void InputId(Link node) {
printf("请输入你要删除的学号;>");
scanf("%s", node->Data.id);
node->next = NULL;
}
bool deleteNode(Link head) {
Link p = head->next;
Link q = head;
Link node = (Link)malloc(sizeof(Node));
InputId(node);
if (head->next==NULL||head==NULL) {
return false;
}
while (p != NULL) {
if (0==(strcmp(p->Data.id , node->Data.id))) {
q->next = p->next;
free(p);
return true;
}
else {
q = p;
p = p->next;
}
}
return false;
}
bool modifyNode(Link head) {
Link p = head->next;
Link q = head;
Link node = (Link)malloc(sizeof(Node));
printf("开始修改>\n");
if (head->next == NULL){
printf("此表为空,无法修改\n");
}
else {
printf("请输入你要修改的名字:>");
scanf("%s", node->Data.name);
while (p != NULL) {
if (0 == strcmp(p->Data.name, node->Data.name)) {
printf("请输入你要修改的学号;>");
scanf("%s", p->Data.id);
return true;
}
p = p->next;
}
}
return false;
}
bool searchNode(Link head) {
Link p = head;
Link node = (Link)malloc(sizeof(Node));
if (head->next == NULL) {
printf("此表无信息\n");
}
else {
printf("请输入你要查找的学号:>");
scanf("%s", node->Data.id);
while (p) {
if (0 == strcmp(p->Data.id, node->Data.id)) {
printf("查找的学生姓名是:>%s\n",p->Data.name);
return true;
}
p = p->next;
}
}
return false;
}
int countNode(Link head) {
Link p = head->next;
int count = 0;
if (head->next == NULL){
return false;
}
else {
while (p != NULL) {
count++;
p = p->next;
}
return count;
}
}
void ClearNode(Link head) {
Link p = head->next;
if (head->next == NULL) {
printf("此表为空");
}
else {
while (head) {
head = p;
p = p->next;
free(p);
}
}
}
void menu() {
printf("***************************************\n");
printf("*** 1.增加学生信息 2.删除学生信息 ***\n");
printf("*** 3.修改学生信息 4.查找学生信息 ***\n");
printf("*** 5.统计学生人数 6.显示学生信息 ***\n");
printf("*** 0.退出系统 ***\n");
printf("***************************************\n");
}
int main() {
int input = 0;
int count = 0;
Link head;
head = (Link)malloc(sizeof(Node));
head->next = NULL;
do {
menu();
printf("请选择:->");
scanf("%d", &input);
switch (input) {
case 0:
ClearNode(head);
printf("退出游戏\n");
break;
case add:
addNode(head);
printf("插入数据成功\n");
break;
case del:
if (deleteNode(head) == 1)
printf("删除成功\n");
else
printf("删除失败\n");
break;
case modify:
if (modifyNode(head))
printf("修改成功\n");
else
printf("修改失败\n");
break;
case search:
if (searchNode(head))
printf("查找成功");
else
printf("查找失败\n");
break;
case count1:
count=countNode(head);
printf("此表的人数为:%d\n", count);
break;
case show:
showNode(head);
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}