加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

学生信息管理系统之单链表实现

发布时间:2020-12-13 21:14:24 所属栏目:PHP教程 来源:网络整理
导读:用C实现学生信息管理系统,用到线性表的数据结构,用单链表实现。 首先看看甚么是单链表,有甚么特点: 1.单链表每一个节点存储其数据和指向下1个节点的指针,即数据域和指针域,两个逻辑上相邻的元素存储位置不1定相邻。节点定义以下: typedef struct LNod

用C++实现学生信息管理系统,用到线性表的数据结构,用单链表实现。

首先看看甚么是单链表,有甚么特点:

   1.单链表每一个节点存储其数据和指向下1个节点的指针,即数据域和指针域,两个逻辑上相邻的元素存储位置不1定相邻。节点定义以下:

typedef struct LNode{ ElemType data; LNode *next; }LNode;

    其中ElemType是学生基本信息的结构体,其定义以下:

typedef struct StuInfo{ char stuID[10]; char name[15]; int age; char city[15]; char tel[15]; }ElemType;
    2.单链表可以由头指针唯1肯定,遍历单链表只能从头开始。

    3.每一个元素都有唯1先驱和唯1后继,除头节点和尾节点。

    4.内存动态分配,当需要插入数据时申请1个相应的空间便可,元素个数不受限制较为灵活。

    5.基本操作:插入、删除、输出、修改、查找、排序等等。

由因而C++实现,基本操作全部封装在类的成员函数里面。
源码以下:

/*---------------------------------/ /Date:2016-09⑴4-------------------/ /Function:Using single-list to achieve the management of students/ /From:<<Data Struct Examples>>-----/ /Author:---------------------------/ /---------------------------------*/ #include <iostream> #include <iomanip> #include <string.h> #include <stdlib.h> using namespace std; typedef struct StuInfo{ char stuID[10]; char name[15]; int age; char city[15]; char tel[15]; }ElemType; typedef struct LNode{ ElemType data; LNode *next; }LNode; class CLinkList{ private: LNode *head; public: CLinkList(); virtual ~CLinkList(); bool IsListEmpty(); void ClearList(); void CreatList(); int GetListLength(); LNode* LocateElem(ElemType e); LNode* LocateElem(int pos); void InputStuInfo(ElemType &e); bool ListInsert(ElemType &e,int i); bool UpdateList(const ElemType &e,ElemType e1); LNode *GetElem(int pos); void OutputList(); bool ListDelete(ElemType e,int pos); }; //构造1个只有头节点的空链表 CLinkList::CLinkList(){ head=new LNode; head->next=NULL; } //析构函数清空链表 CLinkList::~CLinkList(){ LNode *p=head->next,*q; while(p){ q=p->next; delete p; p=q; } delete head; } int CLinkList::GetListLength(){ LNode *p; p=head->next; //指针p指向头节点的后继节点 int i=0; while(p){ //p不为空则计数器累加 i++; p=p->next; } return i; }
<span style="font-family: Arial,Helvetica,sans-serif;">void CLinkList::OutputList(){</span>
LNode *p=head->next; if(p==NULL) cout<<"没有信息!"<<endl; else cout<<setw(15)<<"学号"<<setw(15)<<"姓名"<<setw(15)<<"年龄"<<setw(15)<<"生源地"<<setw(15)<<"联系电话"<<endl; while(p){ cout<<setw(15)<<p->data.stuID<<setw(15)<<p->data.name<<setw(15)<<p->data.age<<setw(15)<<p->data.city<<setw(15)<<p->data.tel<<endl; p=p->next; } } bool CLinkList::IsListEmpty(){ if(GetListLength()==0) return true; else return false; } void CLinkList::ClearList(){ LNode *p=head->next,*q; while(p){ q=p->next; delete p; p=q; } head->next=NULL; //得到带头节点的空链表 } void CLinkList::CreatList(){ LNode *s; ElemType e; bool judge; judge=IsListEmpty(); if(judge==false) ClearList(); cout<<"输入学号为!时结束!"<<endl; cout<<"输入学生学号:"; cin>>e.stuID; while(strcmp(e.stuID,"!")){ cout<<"输入学生姓名:"; cin>>e.name; cout<<"输入学生年龄:"; cin>>e.age; cout<<"输入学生源地:"; cin>>e.city; cout<<"输入联系电话:"; cin>>e.tel; cout<<endl; s=new LNode; s->data=e; s->next=head->next; head->next=s; cout<<"输入学生学号:"; cin>>e.stuID; } cout<<"链表建成"<<endl; } bool CLinkList::ListInsert(ElemType &e,int i){ int j=1; LNode *s,*p; s=new LNode; //建立1个待插入的节点s s->data=e; p=head; while(j<i && p->next!=NULL){ p=p->next; j++; } if(j==i){ s->next=p->next; p->next=s; return true; } else return false; } bool CLinkList::ListDelete(ElemType e,int i){ int j=1; LNode *p,*q; q=head; p=q->next; if(p==NULL) cout<<"n此链表为空链表"<<endl; while(j<i && p->next!=NULL){ q=p; p=p->next; j++; } if(p!=NULL){ e=p->data; q->next=p->next; delete p; return true; } return false; } //按内容定位 LNode *CLinkList::LocateElem(ElemType e){ LNode *p; p=head->next; while(p!=NULL && strcmp(p->data.stuID,e.stuID)!=0){ p=p->next; } if(p==NULL){ cout<<"n该链表中不存在该元素"<<endl; return NULL; } return p; } //按序号定位 LNode *CLinkList::LocateElem(int i){ int j=1; LNode *p; p=head; //判断输入是不是合法 if(i<1||i>GetListLength()){ cout<<"单链表中不存在该元素"<<endl; return NULL; } while(j<i && p->next!=NULL){ p=p->next; j++; } if(j==i) return p->next; } void CLinkList::InputStuInfo(ElemType &e){ cout<<"输入学生学号:"; cin>>e.stuID; cout<<"输入学生姓名:"; cin>>e.name; cout<<"输入学生年龄:"; cin>>e.age; cout<<"输入学生源地:"; cin>>e.city; cout<<"输入联系电话:"; cin>>e.tel; cout<<endl; } bool CLinkList::UpdateList(const ElemType &e,ElemType e1){ LNode *p; p=head->next; while(p){ if(strcmp(p->data.stuID,e.stuID)==0){ p->data=e1; return true; } p=p->next; } return false; } LNode *CLinkList::GetElem(int pos){ LNode *p; p=LocateElem(pos); if(p==NULL){ return NULL; } else return p; } int Menu_Select(); void Menu_show(); void Menu_show(){ cout<<"---------------------------------"<<endl; cout<<"---------学生信息管理系统---------"<<endl<<endl; cout<<"-------⑴.生成学生信息表格--------"<<endl; cout<<"-------⑵.插入学生基本信息--------"<<endl; cout<<"-------⑶.修改学生基本信息--------"<<endl; cout<<"-------⑷.删除学生基本信息--------"<<endl; cout<<"-------⑸.查询学生基本信息--------"<<endl; cout<<"-------⑹.输出学生基本信息--------"<<endl; cout<<"-------⑺.退出学生信息系统--------"<<endl; cout<<"---------------------------------"<<endl; cout<<endl; cout<<"*提示:输入编号并回车进行相应操作*"<<endl; } int Menu_Select(){ int selectNum; for(;;){ cout<<"输入操作编号:"; cin>>selectNum; if(selectNum<1||selectNum>7) cout<<"输入有误,请重新输入!"<<endl; else break; } return selectNum; } int main(void){ CLinkList list; ElemType e,e1; LNode *p; Menu_show(); while(1){ switch(Menu_Select()){ case 1: list.CreatList(); break; case 2: int i; bool judge; cout<<"插入的位置:"; cin>>i; cout<<endl; if(i<1||i>list.GetListLength()+1) cout<<"插入的位置不合法!"<<endl; else{ cout<<"请输入学生信息和插入位置!"<<endl; list.InputStuInfo(e); judge=list.ListInsert(e,i); if(false==judge) cout<<"插入位置出错!"<<endl; else cout<<"插入信息成功!"<<endl; } break; case 3: cout<<"请输入要修改的学生学号:"; cin>>e.stuID; cout<<endl; p=list.LocateElem(e); if(p){ cout<<"输入该学生的新信息:"<<endl; list.InputStuInfo(e1); list.UpdateList(e,e1); cout<<"修改信息成功!"<<endl; } break; case 4: int pos; cout<<"删除的位置:"; cin>>pos; cout<<endl; if(pos<1||pos>list.GetListLength()) cout<<"删除位置不合法!"<<endl; else{ list.ListDelete(e,pos); cout<<"删除学生信息成功!"<<endl; } break; case 5: cout<<"输入要查询学生的学号:"<<endl; cin>>e.stuID; p=list.LocateElem(e); if(p!=NULL) cout<<p->data.stuID<<setw(15)<<p->data.name<<setw(15)<<p->data.age<<setw(15)<<p->data.city<<setw(15)<<p->data.tel<<endl; case 6: cout<<"输出结果为:"<<endl; list.OutputList(); break; case 7: cout<<"再见!"<<endl; exit(0); } } }



(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读