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

Student Management System

闲来无事,简单写个项目。最近在看HTML+CSS+Javascript,怕手生。睡醒起来就想写点东西,花了半小时简单敲了下。总听别人说这个东西,外行人我也不知道是个啥。没事,自己敲敲来玩。

主要功能:

  • 添加;
  • 删除;
  • 查找;
  • 显示。

主函数功能简单,就这么点,当然主函数功能简单免不了函数内部实现需要添加提示语句等,考虑输入输出等,就会使得函数体变得冗长一些。

int main(void){
	List ls;
	Init(&ls);
	char ch;
	
	while((ch = menu()) && ch != 'q')
		switch(ch){
			case 'a': add(&ls);break;
			case 'd': del(&ls);break;
			case 's': show(&ls);break;
			case 'f': find(&ls);break;
		}
	printf("Bye!\n");
}

说明

为了方便我就直接功能提示都嵌在函数里了,这样主函数功能清晰简单明了。毕竟我不是在写结构,而是在解决问题。

全文

// 以链表的形式实现学生个人信息管理系统

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#define MAXSIZE 1000

//定义结点类型
typedef struct _node
{
	char college[MAXSIZE];	//学院
	char major[MAXSIZE];	//专业
	int class;				//班级
	long long number;		//学号
	char name[MAXSIZE];		//姓名
	
	struct _node * next;
}Node;

//定义链表类型
typedef struct _list
{
	Node * head;
	Node * tail;
	int size;	
}List;

//清理输入行残留
static void eatline(void)
{
	while(getchar() != '\n')
		continue;
}

//自定义读入字符串函数
static char * s_gets(char * s, int n)
{
	char *  ret;
	char * find;

	ret = fgets(s,n,stdin);
	if(ret)
	{
		find = strchr(s,'\n');
		if(find)
			*find = '\0';
		else
			eatline();
	}
	return ret;
}

//初始化管理系统
void Init(List * ls)
{
	ls->head = ls->tail = NULL;
	ls->size = 0;
}

//添加学生
bool add(List * ls)
{
	//申请结点
	Node * pnew = (Node *) malloc (sizeof(Node));
	pnew->next = NULL;

	//读取信息
	printf("Enter the college: ");
	s_gets(pnew->college,MAXSIZE);

	printf("Enter the major:");
	s_gets(pnew->major,MAXSIZE);

	printf("Enter the name:");
	s_gets(pnew->name,MAXSIZE);

	printf("Enter the class(such as 171,182):");
	scanf("%d",&(pnew->class));
	eatline();
	printf("Enter the number(such as 5601110000)\n");
	scanf("%lld",&(pnew->number));
	eatline();

	//添加
	if(ls->size == 0)
		ls->head = ls->tail = pnew;
	else
		ls->tail = ls->tail->next = pnew;

	//收尾
	ls->size++;
	printf("Add Sucessfully!\n");

	return true;
}

//删除学生
bool del(List * ls)
{
	//根据学号删除
	long long num;
	printf("Please enter the number of the student: ");
	scanf("%lld",&num);
	eatline();

	bool ret = false;
	Node * q, * p, * temp;
	for(q = NULL, p = ls->head;p;q = p, p = p->next)
		if(p->number == num)
		{
			//如果删除的头
			if(q == NULL)
			{
				temp = ls->head;
				ls->head = ls->head->next;
				free(temp);
			}else{
				q->next = p->next;
				free(p);
			}
			ret = true;
			break;
		}
	
	//收尾
	if(!ret)
		printf("No this student!\n");
	else
	{
		printf("Del Sucessfully!\n");
		ls->size--;
		if(ls->size == 0)
		{
			ls->head = ls->tail = NULL;
			printf("Oh no, now there are no students after you del one!\n");
		}
	}

	return ret;
}

//查找学生
bool find(const List * ls)
{
	//根据学号查找
	long long num;
	printf("Please enter the number of the student: ");
	scanf("%lld",&num);
	eatline();

	bool ret = false;
	Node * p;
	for(p = ls->head;p;p = p->next)
		if(p->number == num)
		{
			printf("Find!\nInformation about student as follow:\n");
			printf("%s\t%s\t%s\t%lld\t%d\n",
				p->college,p->major,p->name,p->number,p->class);
			ret = true;
			break;
		}
	if(!ret)
		printf("Not Find!\n");

	return ret;
}

//显示信息
void show(const List * ls)
{
	printf("Here are our students of our university:\n");
	printf("College\tMajor\tName\tNumber\tClass\n");

	if(ls->size == 0)
		printf("Now, there are no students, let's add some!\n");

	Node * p;
	for(p = ls->head;p;p = p->next)
	{
		printf("%s\t%s\t%s\t%lld\t%d\n",
				p->college,p->major,p->name,p->number,p->class);
	}
}

char menu(void)
{
	printf("Welcome to Students Management System Of NCU!\n");
	printf("********************************************\n");
	printf("a)Add   d)Del  f)Find   s)show   q)quit\n");
	printf("********************************************\n");
	char ch;
	ch = tolower(getchar());
	eatline();
	while(!strchr("adfsq",ch))
	{
		printf("Please enter a right choice: ");
		ch = tolower(getchar());
		eatline();
	}

	return ch;
}

int main(void){
	List ls;
	Init(&ls);
	char ch;
	while((ch = menu()) && ch != 'q')
	{
		switch(ch){
			case 'a': add(&ls);break;
			case 'd': del(&ls);break;
			case 's': show(&ls);break;
			case 'f': find(&ls);break;
		}
	}
	printf("Bye!\n");
}

注意

因为删除查找都是通过学号,可能由于用户的错误输入导致不同的学生具有相同的学号,这样相应的查找删除操作就可能出问题。不过简单就简单练习来说,点到为止。本身也不算难实现,优化的话也可以继续优化修改,不过就没什么营养了。

亲测有效!

发布了18 篇原创文章 · 获赞 4 · 访问量 836

猜你喜欢

转载自blog.csdn.net/GuoningningPro/article/details/103841967