单链表实现的学生管理系统

本文介绍了一个使用单链表实现的学生管理系统,包括创建、插入、删除、查找和打印学生信息的功能,并提供完整的C语言源代码。

这个学生管理系统是通过单链表实现的,这个管理系统是为了让我们更好得对链表进行操作。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

struct Node
{
	char name[10];
	int num;
	int age;
	struct Node* next;
};

//创建一个学生链表
struct Node* createlist()
{
	struct Node* list = (struct Node*)malloc(sizeof(struct Node));//给list分配一个内存空间
	list->next = NULL;
	return list;
}


//创建一个学生的结点
struct Node* createnode(char *newname, int newnum, int newage)
{
	struct Node* node = (struct Node*)malloc(sizeof(struct Node)); //给结点分配一个内存空间
	strcpy(node->name, newname); //把信息放到结点中
	node->age = newage;
	node->num = newnum;
	node->next = NULL;
	return node;
}

//在尾部插入一个学生的信息
void insertback(struct Node* list, char *newname, int newnum, int newage)
{
	struct Node* newnode = createnode(newname, newnum, newage);
	//找到尾结点
	struct Node* temp = list;//移动的结点用来找到尾结点
	while (temp->next != NULL) //一直移动到最后一个结点
	{
		temp = temp->next;
	}
	newnode->next = NULL;    
	temp->next = newnode;
	
}


//删除学生信息
//删除信息的时候我们需要定义两个移动结点,用来找到需要删除的结点和它前面一个结点
void deleteinfo(struct Node* list, int num)
{
	struct Node* temp = list;  //用来找到要删除的前面一个结点
	struct Node* p = list->next;//指向需要删除的结点
	while (p->num != num)
	{
		temp = temp->next;
		p = p->next;
	}

	temp->next = p->next;
	free(p);
}

//查找学生的信息
//和删除学生差不多
void searchinfo(struct Node* list, char *name)
{
	struct Node* temp = list->next;
	while (strcmp(temp->name, name) != 0)
	{
		if (temp->next == NULL)
		{
			printf("没有此学生的信息\n");
			return;
		}
		temp = temp->next;
		
	}
	printf("姓名:%s\n编号:%d\n年龄 :%d\n\n", temp->name, temp->num, temp->age);
}

void print(struct Node* list)//打印所有的信息
{
	struct Node* temp = list->next;
	if (temp == NULL)
	{
		printf("没有学生信息\n");
	}
	while (temp)
	{
		printf("姓名:%s\n编号:%d\n年龄 :%d\n\n", temp->name, temp->num, temp->age);
		temp = temp->next;
	}

}

//struct Node* student = createlist();
void menu()//菜单界面
{
	printf("****************************************\n");
	printf("*        1、插入学生信息               *\n");
	printf("*        2、查找学生信息               *\n");
	printf("*        3、打印所有信息               *\n");
	printf("*        4、删除学生信息               *\n");
	printf("*        5、退出管理系统               *\n");
	printf("****************************************\n");
}

int choice()//选择需要的操作
{
	int choice;
	printf("请选择您要执行的操作\n");
	scanf("%d", &choice);
	while (choice < 1 || choice > 5)
	{
		printf("您的输入有误,请重新输入\n");
		scanf("%d", &choice);
	}
	return choice;
}


void work(struct Node* student)
{
	
	menu();
	int a = choice();

	switch (a)
	{
		case 1: 
		{
			char name[10] = "0";
			int num = 0;
			int age = 0;
			printf("输入信息姓名,编号,年龄\n");
			scanf("%s",name);
			scanf("%d", &num);
			scanf("%d", &age);
			insertback(student, name, num, age);
			
		}break;
		case 2:
		{
			char searchname[10] = "0";
			printf("请输入你要查找的学生的名字\n");
			scanf("%s", searchname);
			searchinfo(student, searchname);
		}break;
		case 3:
		{
			print(student);
		}break;
		case 4:
		{
			int deletenum = 0;
			printf("请输入你要删除的学生的编号\n");
			scanf("%d", &deletenum);
			deleteinfo(student, deletenum);
		}break;
		case 5:
		{
			exit(0);
		}break;	
		default: printf("输入错误\n"); break;
	}
}

int main()
{
	struct Node* student = createlist();
	
	while (1)
	{
		work(student);
	}

	system("pause");
	return 0;
}
如果有什么不懂的,或者不对的地方可以在评论里指出来哦,欢迎大家浏览。
在C++中,可以使用单链表数据结构来实现一个简单的学生管理系统。以下是基本步骤: 1. 定义节点类(Node):首先,创建一个名为`StudentNode`的类,表示链表中的每个元素,包含学生的姓名、学号等信息以及指向下一个节点的指针。 ```cpp class StudentNode { public: string name; int studentID; StudentNode* next; // 构造函数和析构函数 StudentNode(string n, int id) : name(n), studentID(id), next(nullptr) {} ~StudentNode() {} }; ``` 2. 创建链表类(StudentList):然后创建一个`StudentList`类,管理整个学生链表,包括插入新学生、删除学生、查找学生等功能。 ```cpp class StudentList { private: StudentNode* head; // 链表头节点 public: // 插入学生 void insertStudent(string name, int ID); // 删除学生 void deleteStudent(int ID); // 查找学生 bool findStudent(int ID); // 其他辅助方法,如显示链表等 // ... }; ``` 3. 实现功能方法: - `insertStudent`: 新建一个`StudentNode`并添加到链表尾部。 - `deleteStudent`: 遍历链表查找指定学号的节点并删除。 - `findStudent`: 遍历链表查找是否存在指定学号的学生。 ```cpp // 插入学生实现 void StudentList::insertStudent(string name, int ID) { StudentNode* newNode = new StudentNode(name, ID); if (head == nullptr) { head = newNode; } else { StudentNode* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } // 删除学生实现 void StudentList::deleteStudent(int ID) { if (head == nullptr) return; if (head->studentID == ID) { StudentNode* temp = head; head = head->next; delete temp; return; } StudentNode* current = head; while (current->next != nullptr && current->next->studentID != ID) { current = current->next; } if (current->next != nullptr) { current->next = current->next->next; delete current->next; } } // 查找学生实现 bool StudentList::findStudent(int ID) { StudentNode* current = head; while (current != nullptr) { if (current->studentID == ID) { return true; } current = current->next; } return false; } ```
评论 10
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值