链表实现学生管理系统

#include<iostream>
#include<malloc.h>
#include<string.h>
using namespace std;

//定义节点结构 
struct Node{
	char name[10];
	char no[8];
	int score;
	struct Node *next;
}; 

//创建学生链表(头节点) ,返回指针 
struct Node *createList(){
	struct Node *list = (struct Node *)malloc(sizeof(struct Node));
	list->next = NULL;
	return list;
} 

//创建数据节点,返回节点指针 
struct Node *createNode(char name[10],char no[8],int score){
	struct Node *node = (struct Node *)malloc(sizeof(struct Node));
	strcpy(node->name,name);
	strcpy(node->no,no);
	node->score = score; 
	node->next = NULL;
	return node;
} 

//在链表尾部插入新节点
void InsertNode(struct Node *list,char name[10],char no[8],int score){
	//创建新节点 
	struct Node *newNode = createNode(name,no,score);
	 //使用移动的指针找到尾节点,不改变list的指向
	struct Node *temp = list; 
	while(temp->next!= NULL){
		temp = temp->next;
	}
	newNode->next = NULL;
	temp->next = newNode;
} 

//任意位置插入节点
void InsertNode2(struct Node *list,int i,char name[10],char no[8],int score){
	struct Node *node = createNode(name,no,score);
	struct Node *temp = list;
	int j = 0;
	while(temp->next && j<i-1){
		temp = temp->next;
		++j;
	}
	node->next = temp->next;
	temp->next = node;
} 

//根据学号删除节点
void DeleteNode(struct Node *list,char no[8]){
	//指向删除节点的前节点 
	struct Node *temp = list;
	//指向删除节点的后节点 
	struct Node *p = list->next;
	while(strcmp(p->no,no)!=0){
		temp = temp->next;
		p = p->next;
	} 
	temp->next = p->next;
	free(p);
}

//任意位置删除节点 
void DeleteNode(struct Node *list,int i){
	//指向删除节点的前节点 
	struct Node *temp = list;
	//指向删除节点的后节点 
	struct Node *p = list->next;
	int j = 0;
	while(p && j < i-1){
		temp = temp->next;
		p = p->next;
		++j;
	} 
	temp->next = p->next;
	free(p);
}  

void searchList(struct Node *list,char newname[10]){
	struct Node *temp = list->next;
	while(strcmp(temp->name,newname)!=0){
		if(temp->next == NULL){
			cout<<"没有该学生信息\n";
		}
		temp = temp->next;
	}
	cout<<"该学生的学号和成绩分别为:"<<temp->no<<","<<temp->score<<endl;
}

//打印学生信息
void print(struct Node *list){
	struct Node *temp = list->next;
	if(!temp){
		cout<<"没有学生信息\n"; 
	} 
	while(temp){
		cout<<"学生学号,名字,成绩:"<<temp->no<<","<<temp->name<<","<<temp->score<<endl;
		if(temp->next==NULL){
			cout<<"打印学生信息完毕\n"; 
		}	 
		temp = temp->next;
	}
} 

int studentcount(struct Node *list){
	int count = 0;
	struct Node *temp = list;
	while(temp->next){
		temp = temp->next;
		count++;
	}
	return count;
}



int main(){
	cout<<"****************************************\n"; 
	cout<<"1.输入指定学生个数,逐个输入学生信息\n";
	cout<<"2.姓名查找,返回学生的学号和成绩\n";
	cout<<"3.插入学生信息到指定位置\n";
	cout<<"4.根据学号删除学生信息\n";
	cout<<"5.删除指定位置的学生信息\n";
	cout<<"6.打印学生信息\n";
	cout<<"7.统计学生个数\n";
	cout<<"0.退出学生管理系统\n";
	cout<<"****************************************\n"; 
	int choose = -1;
	//初始化头节点
	struct Node *student = createList();
	while(choose !=0){
		cout<<"\n请选择功能:";
		cin>>choose;
		cout<<"\n";
		switch(choose){
			case 1:
				char no[8],name[10];
				int score,count;
				cout<<"输入录入学生个数:";
				cin>>count;
				cout<<"输入学生的学号,姓名,成绩\n";
				for(int i=0; i<count; i++){
					cin>>no>>name>>score;
					InsertNode(student,name,no,score);
				}
					cout<<"插入数据成功\n";
				break;
			case 2:
				char findname[10];
				cout<<"输入查找学生的名字,查询信息:";
				cin>>findname;
				searchList(student,findname);
				break;
			case 3:
				char newno[8],newname[10];
				int newscore,index5;
				cout<<"输入插入位置:";
				cin>>index5;
				cout<<"输入学生数据:\n";
				cin>>newno>>newname>>newscore;
				InsertNode2(student,index5,newname,newno,newscore);
				cout<<"插入成功\n";
				break;
			case 4:
				char findno[8];
				cout<<"输入你要删除的学号对应的学生信息:";
				cin>>findno;
				DeleteNode(student,findno);
				cout<<"删除完成\n"; 
				break;
			case 5:
				int index3;
				cout<<"输入你要删除的位置所对应的学生信息:";
				cin>>index3;
				DeleteNode(student,index3);
				cout<<"删除完成\n";
				break;
			case 6:
				print(student);
				break;
			case 7:
				cout<<"链表人数为:"<<studentcount(student)<<endl; 
				break;
			case 0:
				choose = 0; 
				break;
		}
	}
	return 0; 
	 
}

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值