#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
using namespace std;
int t=1;
typedef struct Node {
char id[10];
char name[10];
int age;
struct Node* next;
}Node;
Node* headNode() {
Node* head = (Node*)malloc(sizeof(Node));
if (!head) {
return NULL;
}
head->next = NULL;
return head;
}
Node* setNode(char id[10], char name[10], int age) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
return NULL;
}
strcpy(newNode->id, id);
strcpy(newNode->name, name);
newNode->age = age;
return newNode;
}
void menu() {
printf("Welcome to Student Information Library!!\n");
printf("Please:\n");
printf("1.Student Information input\n");
printf("2.Show Student Information\n");
printf("3.Insert student information from the front of the specified position\n");
printf("4.Insert student information after the specified location\n");
printf("5.Modify current student information\n");
printf("6.Search student information\n");
printf("7.Delete student information\n");
printf("8.Exit\n");
printf("请输入菜单编号:\n");
}
void insertNode(Node* head) {
char id[10];
char name[10];
int age;
printf("请输入你的学号:\n");
scanf("%s", id);
printf("请输入你的姓名:\n");
scanf("%s", name);
printf("请输入你的年龄:\n");
scanf("%d", &age);
Node* newNode = setNode(id, name, age);
newNode->next = head->next;
head->next = newNode;
}
void frontinsertNode(Node* head) {
char id[10];
char name[10];
int age;
Node* pre = head;
Node* pMove = head->next;
printf("请输入要插入的位置学号:\n");
char ID[10] = {" "};
scanf("%s", ID);
printf("请输入你的学号:\n");
scanf("%s", id);
printf("请输入你的姓名\n");
scanf("%s", name);
printf("请输入你的年龄:\n");
scanf("%d", &age);
Node* newNode = setNode(id, name, age);
while (pMove != NULL) {
if (strcmp(pMove->id, ID) == 0) {
newNode->next = pMove;
pre->next = newNode;
}
pre = pMove;
pMove = pMove->next;
}
}
void backinsertNode(Node* head) {
char id[10];
char name[10];
int age;
Node* pre = head;
Node* pMove = head->next;
printf("请输入要插入的位置学号:\n");
char ID[10] = { " " };
scanf("%s", ID);
printf("请输入你的学号:\n");
scanf("%s", id);
printf("请输入你的姓名\n");
scanf("%s", name);
printf("请输入你的年龄:\n");
scanf("%d", &age);
Node* newNode = setNode(id, name, age);
while (pMove != NULL) {
if (strcmp(pre->id, ID) == 0) {
newNode->next = pMove;
pre->next = newNode;
}
pre = pMove;
pMove = pMove->next;
}
}
void deleteList(Node* head) {
printf("请输入要删除学生的学号:\n");
char ID[10] = { " " };
scanf("%s", ID);
Node* pre = head;
Node* pMove = head->next;
if (pMove == NULL) {
printf("你还没有添加学生\n");
}
else {
while (strcmp(pMove->id, ID) != 0) {
pre = pMove;
pMove = pMove->next;
}
if (pMove == NULL) {
printf("没有该学生的学号,无法删除");
}
else {
pre->next = pMove->next;
free(pMove);
printf("\n删除成功\n\n");
}
}
}
void reviseList(Node* list) {
Node* pre = list;
Node* pMove = list->next;
printf("请输入要修改的学生的学号:\n");
char ID[10] = { " " };
scanf("%s", ID);
while (pMove != NULL) {
if (strcmp(pMove->id, ID) == 0)
{
printf("请输入要修改的学生信息:\n");
printf("1.学号\n");
printf("2.姓名\n");
printf("3.年龄\n");
int op;
scanf("%d", &op);
switch (op) {
case 1:
printf("请输入新的学号:\n");
char id[10];
scanf("%s", id);
strcpy(pMove->id, id);
break;
case 2:
printf("请输入新的姓名:\n");
char name[10];
scanf("%s", name);
strcpy(pMove->name, name);
break;
case 3:
printf("请输入新的年龄:\n");
int age=0;
scanf("%d", age);
pMove->age = age;
break;
}
break;
}
pre = pMove;
pMove = pMove->next;
}
if (pre->next == NULL) {
printf("\n没有找到该学生的信息\n");
}
else {
printf("\n\n修改成功\n\n");
}
}
void findList(Node* list) {
Node* pre = list;
Node* pMove = list->next;
printf("请输入要查找学生的学号:\n");
char id[10] = { " " };
scanf("%s",id);
while (pMove != NULL) {
if (strcmp(pMove->id, id) == 0) {
printf("\n查找成功\n\n");
printf("学号\t\t姓名\t\t年龄\t\t\n");
printf("%s\t\t%s\t\t%d\t\t", pMove->id, pMove->name, pMove->age);
break;
}
pre = pMove;
pMove = pMove->next;
}
if (pre->next == NULL) {
printf("\n没有找到该学生的信息\n");
}
}
void printList(Node* list) {
Node* pMove = list->next;
if (pMove == NULL) {
printf("链表为空\n");
return;
}
else {
printf("学号\t\t姓名\t\t年龄\t\t\n");
}
while (pMove != NULL) {
printf("%s\t\t%s\t\t%d\t\t", pMove->id, pMove->name, pMove->age);
printf("\n");
pMove = pMove->next;
}
printf("\n");
}
void tuichu() {
int h;
//int t;
printf("即将退出,确认请按1,取消请按0\n");
scanf("%d", &h);
if (h == 1) {
t = 0;
printf("成功退出!\n");
}
if (h == 0) {
printf("退出已取消!\n");
system("pause");
}
}
void Keydown(Node* list) {
int op;
scanf("%d", &op);
switch (op) {
case 1:
insertNode(list);
break;
case 2:
printList(list);
break;
case 3:
frontinsertNode(list);
break;
case 4:
backinsertNode(list);
break;
case 5:
reviseList(list);
break;
case 6:
findList(list);
break;
case 7:
deleteList(list);
break;
case 8:
tuichu();
break;
default:printf("输入有误,请重新输入!");
}
}
int main() {
Node* list = headNode();
while (t) {
menu();
Keydown(list);
system("pause");
system("cls");
}
return 0;
}
C语言链表实现学生管理系统 20240716
于 2024-07-16 20:27:20 首次发布