C++数据结构 线性表的链式存储结构应用 简单的学生成绩管理系统

C++数据结构 线性表的链式存储结构应用 简单的学生成绩管理系统

#include<iostream>
#include<string.h> 
#include <fstream>
#include <string>
#include"stdlib.h"
using namespace std;

struct Node{
 
		int num;
		char name[20];
	    float Score;
		Node *next;
};
class StudList  
{  
private:  
    Node *head;                                                    
public:  
    StudList();  
    void CreateList();//创建链表  
    void Display(); //显示链表  
    int Getlen() ; //获取链表长度  
    Node *GetHead();
    void pushBack(); //尾部添加一个学生 信息 
    void Insert(); //在指定位置插入一个学生 
    int Delete(); //删除一个个学生 
    int Update(); //修改一个个学生 的信息 
    void Sort();//按成绩排序 
	void FindByNum();	// 按学号查找
	void FindByName();  //按姓名查找							   	
  };  
 
void menu(StudList *list); 
int main(){
	StudList list;
	list.CreateList();
	menu(&list);
	return 0;
}
 
StudList::StudList()  //::是作用域操作符,表示你引用的变量限定在该作用域内。
{  
    head=new Node();  
    head->next=NULL;
} 

//建立一个空表 
void StudList::CreateList()  
{  
	head->num=0;
}
 
Node *StudList::GetHead(){
	return head;
}
 
void StudList::Display(){
	Node *cur=head;
	int len = Getlen();
	cout<<" 人数:"<<len<<endl;
	while(len--){
		cout<<"  学号:"<<cur->num;
		cout<<"  姓名:"<<cur->name;
		cout<<"  成绩:"<<cur->Score;
		cout<<endl;
	 cur=cur->next;
	}
}
 

//在单链表末尾加上一个学生的信息 
void StudList::pushBack(){
	Node *cur,*p,*t;
	if(!head){
		head = new Node();	
	}
	cur=head;
	p=new Node();
	t= head;
 
	while(cur->next){
		cur=cur->next;
	}
	cout<<" 输入学号:"<<endl;
	cin>>p->num;
	while(t){
		if(t->num==p->num){
			cout<<" 已存在该学号!"<<endl; 
			return;
		}
		t=t->next;
	}
	cout<<" 输入姓名:"<<endl;	
	cin>>p->name;
	cout<<" 输入成绩:"<<endl;
	cin>>p->Score;
	
	if(head->num==0){
		head = p;
		return;
	}
	cur->next = p;
	p->next=NULL;
}
//获取单链表长度 (=学生总数) 
int StudList::Getlen(){
	int n=0;
	Node *cur;
	cur=head;
	while( cur ){
		n++;	
		cur=cur->next;
	}
	return n;
}
 
//在指定位置插入一个结点 
void StudList::Insert(){
	int i,len;
	cout<<" 输入要插入学生的位置:";
	cin>>i; 
	len=Getlen();
	
	Node *cur,*t,*pr,*newNode;
	if(head==NULL||head->num==0){
		cout<<" 当前表为空!"<<endl;
		StudList(); 
		return;
	}
	cur=head;
	pr = head;
	i-=1;		 
	while(cur&&i--){
		pr=cur;
		cur=cur->next;	
	}
	newNode = new Node();
	t=head;							
	cout<<" 输入插入学生的学号:"<<endl;
	cin>>newNode->num;
		while(t){		//判断是否是已存在学号 
			if(t->num==newNode->num){
				cout<<" 已存在该学号!"<<endl; 
				return;
			}
			t=t->next;
		}
	cout<<" 输入姓名:"<<endl;	
	cin>>newNode->name;
	cout<<" 输入成绩:"<<endl;
	cin>>newNode->Score;
	
	pr->next=newNode;
	newNode->next=cur; 
	
}
 
//删除 
int StudList::Delete() {//n表示学号 
	int n; 
	Node *cur,*pr;
	cur=head;
	pr=head;
	cout<<" 输入要删除学生的学号:";
	cin>>n; 
	while(cur){
		if(cur->num==n){
			if(cur==head){//如果删除的是头节点,直接令头结点为头结点的下一结点 
				head=head->next;
				return 1; 
			}
			pr->next=cur->next;
			return 1; //若删除的是最后一个结点则不能使用 cur->next->next,前边已经 cur = cur->next;不许操作 
		}
		pr=cur;
		cur=cur->next;
	}
	cout<<" 没有查到要删除的学号"<<endl;
	return -1; 
}
 
 //修改指定学号学生的信息 
 int StudList::Update(){//n为学号 
	int a,n;	
	Node *cur;
	cur=head;
	cout<<" 输入要修改学生的学号:";
	cin>>n; 
	while(cur){
		if(cur->num==n){
			cout<<" 1.修改姓名"<<endl; 
			cout<<" 2.修改成绩"<<endl; 
		
			cout<<" 请输入要修改的项目(输入其它不做修改):";
			cin>>a ;
			switch(a){
				case 1:
					cout<<" 请输入修改姓名:";
					cin>>cur->name;
					return 1;
				case 2:
					cout<<" 请输入修改的成绩:";
					cin>>cur->Score;
					return 1;
		
				default:
					cout<<" 退出修改"<<endl; 
					return 1;
			}
		}
		cur=cur->next;
	}
	return -1; 
}
 
 //交换两结点的信息 
void swapNode(Node *a,Node *b){	
	int temp;
	char c;
	float ft;
	
	temp=a->num;
	a->num=b->num;
	b->num=temp;
	
//姓名交换 
	for(int i=0;i<20;i++){
		c=a->name[i];
		a->name[i]=b->name[i];
		b->name[i]=c;
	}
//成绩交换		
	ft=a->Score;
	a->Score=b->Score;
	b->Score=ft;
 
}
 
 //按成绩排序 
void StudList::Sort(){			
	int len,inlen;
	Node *cur;
	len =Getlen();
	while(--len){
		inlen = len;		
		cur = head;
		while(inlen--){
			if(cur->Score<cur->next->Score){
				swapNode(cur,cur->next);
			}
			cur=cur->next;
		}
	}
}

//按学号查找某一学生的信息
void StudList::FindByNum(){
	int n;	
	int flag=0;
	Node *cur;
	cur=head;
	cout<<" 输入要查询的学生的学号:";
	cin>>n;
    while(cur!=NULL){
    	if(cur->num==n){
    	flag=1;
    	cout<<" 下面是查找结果:"<<endl;
		cout<<" 学号:"<<cur->num<<" ";
		cout<<" 姓名:"<<cur->name<<" ";
		cout<<" 成绩:"<<cur->Score<<" ";
		cout<<endl;
			}
			cur=cur->next;
	   if(flag==0){
		cout<<" 不存在该学号!"<<endl; 
	}
	}
	
}

//按姓名查找某一学生的信息
void StudList::FindByName(){
	string name;	
	int flag=0;
	Node *cur;
	cur=head;
	cout<<" 输入要查询的学生的姓名:";
	cin>>name;
    while(cur!=NULL){
    	if(cur->name==name){
    	flag=1;
    	cout<<" 下面是查找结果:"<<endl;
		cout<<" 学号:"<<cur->num<<" ";
		cout<<" 姓名:"<<cur->name<<" ";
		cout<<" 成绩:"<<cur->Score<<" ";
		cout<<endl;
			}
			cur=cur->next;
	}
	if(flag==0){
		cout<<" 不存在该学生!"<<endl; 
	}

} 

 
//菜单 
void menu(StudList *list){
	int a; 
    while(1){
		system("pause");
		system("cls");
		cout<<"**********************************"<<endl;
	    cout<<"**  欢迎使用学生成绩表管理系统  **"<<endl; 
	    cout<<"**********************************"<<endl;
		cout<<"********************************"<<endl;
		cout<<"*   1.添加一个学生成绩。        *"<<endl;
		cout<<"*   2.删除一个学生成绩。        *"<<endl;
		cout<<"*   3.修改一个学生成绩。        *"<<endl;
		cout<<"*   4.插入一个学生成绩。        *"<<endl;
		cout<<"*   5.显示学生成绩表。          *"<<endl;
		cout<<"*   6.按成绩排序。              *"<<endl; 
		cout<<"*   7.计算学生的总数。          *"<<endl; 
		cout<<"*   8.按学号查找学生。          *"<<endl; 
		cout<<"*   9.按姓名查找学生。          *"<<endl; 
		cout<<"********************************"<<endl;
		cin>>a;
		switch(a){
			case 1:
				list->pushBack();
				break;
				
			case 2:
				list->Display();
				list->Delete();
				list->Display();
				break;
				
			case 3:
				list->Display();
				list->Update();
				list->Display();
				break;
				
			case 4:
				list->Display();
				list->Insert();
				list->Display();
				break;	
			case 5:
				list->Display();
				break;
	
			case 6:
				list->Sort();
				list->Display();
				break; 
				
			case 7: 
			    list->Getlen();
			    list->Display();
            	break;
            	
			case 8:
				list->FindByNum();
				break;
			
			case 9:
				list->FindByName();
				break;
	}
}
}

运行结果:

下面是每一步的验证
1.插入数据

在这里插入图片描述

2.显示学生成绩列表

在这里插入图片描述

3.删除一个学生的成绩

在这里插入图片描述

4.插入一个学生的成绩

在这里插入图片描述

5.按成绩排序

在这里插入图片描述

6.计算学生的总数。

在这里插入图片描述

7.按学号查找学生。

在这里插入图片描述

8.按姓名查找学生。

在这里插入图片描述

  • 24
    点赞
  • 158
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
C++中的线性表可以使用数组或链表来实现。数组实现的线性表具有随机访问的特点,而链表实现的线性表则具有插入和删除元素的高效性。 以下是C++中数组实现线性表的基本操作: 1.初始化线性表 ```c++ const int MAXSIZE = 100; // 线性表的最大长度 typedef struct { int data[MAXSIZE]; // 存储线性表元素的数组 int length; // 线性表的当前长度 } SqList; void InitList(SqList &L) { for (int i = 0; i < MAXSIZE; i++) { L.data[i] = 0; } L.length = 0; } ``` 2.插入元素 ```c++ bool ListInsert(SqList &L, int i, int e) { if (i < 1 || i > L.length + 1) { return false; } if (L.length >= MAXSIZE) { return false; } for (int j = L.length; j >= i; j--) { L.data[j] = L.data[j - 1]; } L.data[i - 1] = e; L.length++; return true; } ``` 3.删除元素 ```c++ bool ListDelete(SqList &L, int i, int &e) { if (i < 1 || i > L.length) { return false; } e = L.data[i - 1]; for (int j = i; j < L.length; j++) { L.data[j - 1] = L.data[j]; } L.length--; return true; } ``` 4.查找元素 ```c++ int LocateElem(SqList L, int e) { for (int i = 0; i < L.length; i++) { if (L.data[i] == e) { return i + 1; } } return 0; } ``` 以下是C++中链表实现线性表的基本操作: 1.定义链表节点 ```c++ typedef struct LNode { int data; struct LNode *next; } LNode, *LinkList; ``` 2.初始化链表 ```c++ void InitList(LinkList &L) { L = (LNode*)malloc(sizeof(LNode)); L->next = NULL; } ``` 3.插入元素 ```c++ bool ListInsert(LinkList &L, int i, int e) { if (i < 1) { return false; } LNode *p = L; int j = 0; while (p && j < i - 1) { p = p->next; j++; } if (!p) { return false; } LNode *s = (LNode*)malloc(sizeof(LNode)); s->data = e; s->next = p->next; p->next = s; return true; } ``` 4.删除元素 ```c++ bool ListDelete(LinkList &L, int i, int &e) { if (i < 1) { return false; } LNode *p = L; int j = 0; while (p->next && j < i - 1) { p = p->next; j++; } if (!(p->next) || j > i - 1) { return false; } LNode *q = p->next; e = q->data; p->next = q->next; free(q); return true; } ``` 5.查找元素 ```c++ LNode* GetElem(LinkList L, int i) { if (i < 0) { return NULL; } LNode *p = L; int j = 0; while (p && j < i) { p = p->next; j++; } return p; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值