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

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

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

   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.单链表可以由头指针唯一确定,遍历单链表只能从头开始。

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

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

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

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

/*---------------------------------/
/Date:2016-09-14-------------------/
/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);
};
//构造一个只有头节点的空链表
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;  //建立一个待插入的节点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<<"--------1.生成学生信息表格--------"<<endl;
  cout<<"--------2.插入学生基本信息--------"<<endl;
  cout<<"--------3.修改学生基本信息--------"<<endl;
  cout<<"--------4.删除学生基本信息--------"<<endl;
  cout<<"--------5.查询学生基本信息--------"<<endl;
  cout<<"--------6.输出学生基本信息--------"<<endl;
  cout<<"--------7.退出学生信息系统--------"<<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);
    }
  }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值