单链表学生信息管理

  在学习了单链表的增删查改后,可以写一个简单的学生信息管理程序,但与真正的管理系统相比,它只是一个雏形,还差了很多内容,它还没有和文件联系在一起,因此对于里面输入的数据无法保存。

下来直接看代码,详细注释都在代码中。

#define _CRT_SECURE_NO_WARNINGS 1       
#pragma warning(disable:6031)          //避免scanf返回值被忽略的警告
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Student {
	char XH[20];        //学号
	char name[40];       //姓名
	int sumscore;         //总分
	struct Student* next;
};
void getInput(struct Student* student); //输入学生信息
void printStudent(struct Student* student); //打印学生信息
void addStudent(struct Student** contacts); //增
void changeStudent(struct Student* contacts); //改
void delStudent(struct Student** contacts); //删
struct Student* findStudent(struct Student* contacts); //查
void displayContacts(struct Student* contacts); //全部列出
void releaseContacts(struct Student** contacts); //销毁链表

void getInput(struct Student* student) { //输入学生信息
	printf("请输入姓名,学号,总成绩(用空格隔开):");//将学生信息放在一行,节省空间,较为美观
	scanf("%s %s %d", &student->name,&student->XH,&student->sumscore);
}
void addStudent(struct Student** contacts) { //增
	struct Student* student;
	struct Student* temp;
	student = (struct Student*)malloc(sizeof(struct Student));

	if (student == NULL) {
		printf("malloc fair!");
		exit(1);
	}
	getInput(student);

	if (*contacts != NULL) {
		temp = *contacts;
		*contacts = student;
		student->next = temp;

	}
	else {
		*contacts = student;
		student->next = NULL;
	}
}
void printStudent(struct Student* student) { //打印学生信息
	printf("学生姓名:%s  学号:%s  总成绩:%d", student->name,student->XH,student->sumscore);
	printf("\n");
}
struct Student* findStudent(struct Student* contacts) {
	struct Student* current;
	char input[20] = { 0 };
	printf("请输入学生姓名:");
	scanf("%s", &input);
	current = contacts;

	while (current != NULL && strcmp(current->name, input)) {
		current = current->next;
	}
	return current;
}
void changeStudent(struct Student* contacts) { //改
	struct Student* student;
	student = findStudent(contacts);

	if (student == NULL) {
		printf("该学生不存在!\n");

	}
	else {
		printf("请输入新的学生信息:");
		scanf("%d", &student->sumscore);
	}
}
void delStudent(struct Student** contacts) { //删
	struct Student* student;
	struct Student* current;
	struct Student* previous;
	//找到待删除的结点指针
	student = findStudent(*contacts);
	if (student == NULL) {
		printf("找不到该学生!\n");
	}
	else {
		current = *contacts;
		previous = NULL;
		//current定位到待删除的结点

		while (current != NULL && current != student) {
			previous = current;
			current = current->next;
		}if (current == NULL)
		{
			exit(-1);
		}else if (previous == NULL) {
			//待删除的结点是第一个结点
			*contacts = current->next;

		}else {
			//待删除的结点不是第一个结点
			previous->next = current->next;
		}
		free(student);
	}
}
void displayContacts(struct Student* contacts) {   //列出全部学生的信息
	struct Student* current;
	current = contacts;
	while (current != NULL) {
		printStudent(current);
		current = current->next;
	}
}
void releaseContacts(struct Student** contacts) {       //退出登录
	struct Student* temp;
	while (*contacts != NULL) {
		temp = *contacts;
		*contacts =(*contacts)->next;
		free(temp);
	}
}
int main(void) {
	long password;
	printf("                                 请输入登录密码:");
	scanf("%ld", &password);     
	if (password != 521412) {    //这个密码可以自己设定,此处示例
	printf("                                    密码错误!\n");//如果密码错误,则就此退出
	}
	else {
	printf("                                    登录成功!\n");
	system("pause");   //暂停操作,此时按任意键可以继续
	system("cls");      //清屏操作,之前的内容不显示,system()函数存在于stblib.h这个头文件里
		int code;
		struct Student* contacts = NULL;
		struct Student* student;
		printf("                   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");//菜单界面
		printf("                   *                      欢迎使用学生信息管理程序                     *\n");
		printf("                   *                            1.插入学生信息                         *\n");
		printf("                   *                            2.查找学生信息                         *\n");
		printf("                   *                            3.更改学生信息                         *\n");
		printf("                   *                            4.删除学生信息                         *\n");
		printf("                   *                            5.显示当前信息系统                     *\n");
		printf("                   *                            6.退出当前信息系统                     *\n");
		printf("                   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
		while (1) {
			printf("请输入指令代码:");
			scanf("%d", &code);
			switch (code) {                   //通过循环来根据指令执行相应的操作
			case 1:
				addStudent(&contacts);
				break;
			case 2:
				student = findStudent(contacts);

				if (student == NULL) {
					printf("该学生不存在!\n");

				}
				else {
					printStudent(student);
					break;
				}
			case 3:
				changeStudent(contacts);
				break;
			case 4:
				delStudent(&contacts);
				break;
			case 5:
				displayContacts(contacts);
				break;
			case 6:
				goto END;
			}
		}
	END:
		releaseContacts(&contacts);
	}
	return 0;
}



该代码包含了密码登录这个环节,运行结果如下:

1.密码不正确

2.密码正确

按完任意键后清屏继续

这是我的输入示例

Good luck! 

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我是一个语言模型AI,无法提供完整的代码。但是,我可以提供一个链表学生信息管理系统的基本框架和思路供您参考。 首先,我们需要定义一个学生信息的结构体,包含学号、姓名、性别、年龄等基本信息。 typedef struct student { int id; char name[20]; char sex; int age; struct student *next; } Student; 接下来,我们需要定义一些基本的操作函数,比如添加学生信息、查找学生信息、删除学生信息等。其中,添加学生信息需要创建一个新的节点,将学生信息插入到链表中;查找学生信息需要遍历整个链表,查找指定学生信息;删除学生信息需要找到指定学生的节点,并将其从链表中删除。 void AddStudent(Student **head, Student *newStudent) { if (*head == NULL) { *head = newStudent; return; } Student *p = *head; while (p->next != NULL) { p = p->next; } p->next = newStudent; } Student *FindStudent(Student *head, int id) { Student *p = head; while (p != NULL && p->id != id) { p = p->next; } return p; } void DeleteStudent(Student **head, int id) { Student *p = *head; Student *prev = NULL; while (p != NULL && p->id != id) { prev = p; p = p->next; } if (p == NULL) { return; } if (prev == NULL) { *head = p->next; } else { prev->next = p->next; } free(p); } 以上是基本的操作函数,我们还需要实现一个菜函数,用于显示菜和调用相应的操作函数。 void ShowMenu() { printf("1. 添加学生信息\n"); printf("2. 查找学生信息\n"); printf("3. 删除学生信息\n"); printf("4. 退出程序\n"); } int main() { Student *head = NULL; int choice; do { ShowMenu(); scanf("%d", &choice); switch (choice) { case 1: { // 添加学生信息 Student *newStudent = (Student *)malloc(sizeof(Student)); printf("请输入学生学号:"); scanf("%d", &newStudent->id); printf("请输入学生姓名:"); scanf("%s", newStudent->name); printf("请输入学生性别:"); scanf("%s", &newStudent->sex); printf("请输入学生年龄:"); scanf("%d", &newStudent->age); newStudent->next = NULL; AddStudent(&head, newStudent); printf("学生信息已添加成功!\n"); break; } case 2: { // 查找学生信息 int id; printf("请输入要查找的学生学号:"); scanf("%d", &id); Student *p = FindStudent(head, id); if (p == NULL) { printf("没有找到该学生信息!\n"); } else { printf("学号:%d\n", p->id); printf("姓名:%s\n", p->name); printf("性别:%c\n", p->sex); printf("年龄:%d\n", p->age); } break; } case 3: { // 删除学生信息 int id; printf("请输入要删除的学生学号:"); scanf("%d", &id); DeleteStudent(&head, id); printf("学生信息已删除成功!\n"); break; } case 4: { // 退出程序 printf("程序已退出!\n"); break; } default: { printf("无效的选项,请重新输入!\n"); break; } } } while (choice != 4); return 0; } 以上是一个基本的链表学生信息管理系统的框架和思路,您可以根据自己的需求和实际情况进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值