码迷,mamicode.com
首页 > 编程语言 > 详细

用链表写的学生管理系统 成绩的录入与查询都已经是实现了

时间:2014-04-27 18:38:33      阅读:499      评论:0      收藏:0      [点我收藏+]

标签:链表   c 语言 算法   

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct teacher
{
	char name[32];
	int math;
	int english;
	int data;
	struct Node *next;
}SLIST;
int Creat_SList();
int SList_Print();
int SList_SelectPrint(SLIST *pHead);
int Creat_SList(SLIST **handle)
{
	char name[32] = {0};
	int flag = 0;
	int math = 0;
	int english = 0;
	int ret = 0;
	SLIST *pHead = NULL, *pCur = NULL, *pM = NULL;
	//先分配内存
	pHead = (SLIST *)malloc(sizeof(SLIST));
	if (pHead == NULL)
	{
		ret = -1;
		printf("func Creat_SList err ret=%d", ret);
		return ret;
	}
	pHead->data = 0;
	pHead->next = NULL;
//这里让当前节点等于头节点
	pCur = pHead;
	while (flag != -1)
	{
		pM = (SLIST *)malloc(sizeof(SLIST));
		if (pM == NULL)
		{
			//SList_Destory(pHead);
			ret = -2;
			printf("func Creat_SList() err:%d malloc err", ret);
			return ret;
		}
		//char name[32] = { 0 };
		printf("请输入学生的姓名   ");
		scanf("%s",name);
		strcpy(pM->name, name);
		//pM->name[] = name;

		printf("请输入学生数学成绩");
		scanf("%d", &math);
		pM->math = math;

		printf("请输入学生英语成绩");
		scanf("%d", &english);
		pM->english = english;

		printf("如果完成当前学生的编辑请输入 -1 若要继续输入学生信息 则输入 1  \n");
		scanf("%d",&flag);
		
		pM->next = NULL;
		//让pM接在pCur的后面也就是说明pHead中是没有存储数据的而是一个  空的头结点
		pCur->next = pM;
		pCur = pM;
	}
	*handle = pHead;
	return ret;
	//END:
}
int SList_Print(SLIST *pHead)
{
	int ret = 0;
	SLIST *p = NULL;
	p = pHead->next;//bug
	if (pHead == NULL)
	{
		return -1;
	}
	printf("\nBegin ");
	//p = p->next;
	while (p)
	{
		//printf("%d\n", p->data);
		printf("学生的姓名   ");
		printf("%s\n", p->name);

		printf("学生数学成绩");
		printf("%d\n", p->math);

		printf("学生英语成绩");
		printf("%d\n", p->english);
		p = p->next;
		system("pause");
	}
	printf("End ");
	return ret;
}
//对查询进行输出
int SList_SelectPrint(SLIST *pHead)
{
	int ret = 0;
	SLIST *p = NULL;
	p = pHead;
	if (pHead == NULL)
	{
		return -1;
	}
	printf("\nBegin ");
	{

		printf("您要查找的同学信息如下");
		printf("学生的姓名   ");
		printf("%s\n", p->name);

		printf("学生数学成绩");
		printf("%d\n", p->math);

		printf("学生英语成绩");
		printf("%d\n", p->english);
	}
	printf("End ");
	return ret;
}
//对学生信息进行查找 需要打印查找结果
int SList_Select(SLIST *pHead,char *name)
{
	int ret = 0;
	int flag = 0;
	char myname[32] = { 0 };
	SLIST  *pCur;
	strcpy(myname, name);
	if (pHead == NULL)
	{
		int ret = -1;
		printf("SList_Insert err");
		return ret;
	}
	//pCur = pHead->next;
	pCur = pHead->next;
	while (pCur)
	{
		//name 进行比较  第一个人可以进行正常查询但是不能 正常输出
		if (strcmp(pCur->name, myname) == 0)
		{
			//这里判断是正确的
		//	SList_Print(pCur);
			SList_SelectPrint(pCur);
			flag = 1;
			break;
		}
		pCur = pCur->next;
	}
	if (flag == 0)
	{
		printf("查无此人查无此人查无此人查无此人");
	}
	return ret;
}
//在x的出现位置插入y
int SList_NodeInsert(SLIST *pHead, int x, int y);
int SList_NodeInsert(SLIST *pHead, int x, int y)
{
	int ret = 0;
	SLIST *pPre, *pCur, *pM;
	if (pHead == NULL)
	{
		int ret = -1;
		printf("SList_Insert err");
		return ret;
	}
	pPre = pHead;
	pCur = pHead->next;
	//不断的malloc新节点数据域赋值
	pM = (SLIST *)malloc(sizeof(SLIST));
	pM->data = y;
	pM->next = NULL;

	while (pCur)
	{
		if (pCur->data == x)
		{
			break;
		}
		pPre = pCur;
		pCur = pCur->next;
	}

	if (pM == NULL)
	{
		ret = -2;
		printf("SList_Insert err");
		return ret;
	}
	pM->next = pCur;//pPre->next;
	pPre->next = pM;//pPre = pM;  
	return ret;
}
//删除 找到y并将它删除
int SList_NodeDel(SLIST *pHead, int y)
{
	int ret = 0;
	SLIST *pPre, *pCur;
	if (pHead == NULL)
	{
		int ret = -1;
		printf("SList_Insert err");
		return ret;
	}
	pPre = pHead;
	pCur = pHead->next;
	while (pCur)
	{
		if (pCur->data == y)
		{
			break;
		}
		pPre = pCur;
		pCur = pCur->next;
	}
	if (pCur == NULL)
	{
		printf("没有找到节点 y:%d", y);
		return -2;
	}
	pPre->next = pCur->next;//pTemp = pPre->next;
	//pPre = pTemp;
	return ret;
}

void main()
{
	SLIST * pHead = NULL;
	int flag = 0;
	printf("------------------------学生管理系统-------------------------\n");
	for (;;)
	{
		printf("如果要输入学生信息请输入: 1  \n");
		printf("如果要查询学生信息请输入: 2  \n");
		printf("如果要查询所有学生请输入: 3  \n");
		scanf("%d", &flag);
		if (flag == 1)
		{
			Creat_SList(&pHead);
		}
		if (flag == 2)
		{
			printf("请输入要查询的人的名字");
			char name[32] = { 0 };
			scanf("%s", name);
			printf("s%",name);
			//进行查询并且打印结果
			SList_Select(pHead,name);
		}
		if (flag == 3)
		{
			SList_Print(pHead);
		}
		printf("----------------------------------------------------------");
		system("pause");
	}
	//printf("\n");
	//SList_Select(pHead, "han");
	//system("pause");
}

用链表写的学生管理系统 成绩的录入与查询都已经是实现了,码迷,mamicode.com

用链表写的学生管理系统 成绩的录入与查询都已经是实现了

标签:链表   c 语言 算法   

原文地址:http://blog.csdn.net/han1558249222/article/details/24580247

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!