栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c语言实现数据结构学生管理系统作业

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c语言实现数据结构学生管理系统作业

用 c语言实现数据结构学生管理系统作业,可在VS2019上运行。
但是名字只能打单个字符,因为不支持char字符串。

#include
#include
#include
#include 
#include//解决bool类型报错(c++才有bool类型)
//#include //由NULL标红添加

//#define NAME_LENGTH 11


typedef struct Student
{
	int studentNo;
	char studentName;

}st;

typedef struct node
{
	struct Student data;
	struct node* next;

}Node, * Link;

//定义提示菜单
void myMenu() {
	printf("* * * * * * * * * * * * *    菜   单    * * * * * * * * * * * * *n");
	printf("            1 增加学生记录                  2 删除学生记录n");
	printf("            3 查找学生记录                  4 修改学生记录n");
	printf("            5 统计学生记录                  6 显示学生记录n");
	printf("            7 退出系统n");
	printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *n ");
}

void inputStudent(Link l) {
	printf("请输入学生学号:");
	scanf_s("%d", &l->data.studentNo);
	printf("请输入学生的姓名:");
	getchar();
	scanf_s("%c", &l->data.studentName);
	//每个新创建结点的next域都初始化为NULL
	l->next = NULL;
}
int inputStudentNo(char s, int no)
{
	printf("请输入要%c的学生学号:", s);
	scanf_s("%d", &no);
	return no;
}//从用户那里获得要查询/修改/删除的学号
void displayNode(Link head)
{
	Link p;
	p = head;
	p = p->next;
	while (p)
	{
		printf("学生学号:%d 学生姓名:%cn", p->data.studentNo, p->data.studentName);
		p = p->next;
	}

}//扫描链表显示所有节点的信息

bool addNode(Link head)
{
	Link p, q;//一前一后两个指针
	Link node;//node 指针指向新创建的结点
	node = (Link)malloc(sizeof(Node));//创建大容量指针
	inputStudent(node);
	q = head;
	p = head->next;
	if (head->next == NULL)
	{
		head->next = node;
	}
	else
	{
		while (p)
		{
			if (node->data.studentNo < p->data.studentNo)
			{
				node->next = p;
				q->next = node;
				return true;//如果找到了就会跳出循环
			}
			else
			{
				q = p;
				p = p->next;
			}
		}//如果遍历完了都没有,就会自动跳出循环
		q->next = node;
	}
	return true;
}
bool deleteNode(Link head)//按照给定的学号删除学生记录,如果删除成功返回true,没找到学号返回false
{
	int no = 0;
	no=inputStudentNo('d', no);
	Link p, q;
	q = head;
	p = head->next;
	while (p)
	{
		if (p->data.studentNo == no)
		{
			q->next = p->next;
			free(p);
			return true;
		}
		else
		{
			q = p;
			p = p->next;
		}
	}return false;

}
bool queryNode(Link head)//按给定的学号查询学生记录,如果查询成功就返回true,查不到就返回false
{
	int no = 0;
	no=inputStudentNo('q', no);
	Link p;
	p = head->next;
	while (p)
	{
		if (p->data.studentNo == no)
		{
			return true;
		}
		else
		{
			p = p->next;
		}
	}
	return false;
}
bool modifyNode(Link head) //按照给定的学号找到学生记录节点,如果修改成功返回true,没找到学号返回false
{
	int no = 0;
	
	no=inputStudentNo('m', no);
	Link p;
	p = head->next;
	while (p)
	{
		if (p->data.studentNo == no)
		{
			printf("请修改学生姓名:");
			scanf_s("%c", &p->data.studentName);
			return true;
		}
		else
		{
			p = p->next;
		}
	}
	return false;
}
int countNode(Link head)//统计学生人数,扫描链表统计节点个数,返回节点数
{
	Link  p;
	int count = 0;
	p = head->next;
	if (p)
	{
		while (p)
		{
			count++;
			p = p->next;

		}
		return count;
	}
	else return false;


}
void clearLink(Link head)//遍历链表,用free语句删除链表中用malloc建立起的所有结点
{
	Link p, q;
	q = head;
	p = head->next;
	while (p)
	{
		q = p;
		p = p->next;
		free(q);
	}
}
int main()
{
	int select;
	int count;
	Link head;
	//建立head头结点,在这个程序中head的data域没有内容
	head = (Link)malloc(sizeof(Node));
	head->next = NULL;
	while (1)
	{
		myMenu();
		printf("n请输入你的选择(输入0-7中的数字):");
		scanf_s("%d", &select);
		switch (select)
		{
		case 1://增加
			if (addNode(head))printf("Add successfully.n");
			break;

		case 2:
			if (deleteNode(head))printf("Delete successfully.n");
			else printf("Fail to delete.n");
			break;

		case 3:
			if (queryNode(head))printf("Query sucessfully.n");
			else printf("Fail to query.n");
			break;
		case 4:
			if (modifyNode(head))printf("Modify data successfully.n");
			else printf("没有找到要修改的学生结点n");
			break;
		case 5:
			count = countNode(head);
			printf("学生人数为:%dn", count);
			break;
		case 6:
			displayNode(head);
			break;

		case 7:
			clearLink(head);
			return 0;
		default:
			printf("输入不正确,请重新输入0-7中的数。nn");
			break;
		}



	}
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/925779.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号