介绍:学生信息(姓名,性别,年龄,身份证号,班级)等信息的增删改查
源码下载:点我下载
头文件function.h
#ifndef FUNCTION_H
#define FUNCTION_H
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
typedef struct
{
string id;
string name;
string sex;
string grade;
int age;
} Student;
typedef struct Node
{
Student stu;
struct Node *next;
} Node, *LinkList;
int initList(LinkList &l);
int addtail(LinkList &t);
void queryall(LinkList &l);
void querybyid(LinkList &l);
void deletebyid(LinkList &l);
void deleteall(LinkList &l,LinkList &t);
#endif
function.cpp内容如下:
#include "function.h"
/*初始化一个带头结点的单链表*/
int initList(LinkList &l)
{
l = new Node;
l->next = NULL;
return 0;
}
/*头插法*/
// int addhead(LinkList &l, Student stu)
// {
// Node *p;
// p = new Node;
// cout << "请输入姓名:" << endl;
// cin >> p->stu.name;
// cout << "请输入性别:" << endl;
// cin >> p->stu.sex;
// cout << "请输入年龄:" << endl;
// cin >> p->stu.age;
// p->next = l->next;
// l->next = p;
// cout << "添加成功!" << endl;
// return 0;
// }
/*尾插法*/
int addtail(LinkList &t)
{
Node *p;
p = new Node;
cout << "请输入身份证号:" << endl;
cin >> p->stu.id;
cout << "请输入姓名:" << endl;
cin >> p->stu.name;
cout << "请输入性别:" << endl;
cin >> p->stu.sex;
cout << "请输入班级:" << endl;
cin >> p->stu.grade;
cout << "请输入年龄:" << endl;
cin >> p->stu.age;
p->next = NULL;
t->next = p;
t = p;
cout << "添加成功!" << endl;
return 0;
}
/*查询全部*/
void queryall(LinkList &l)
{
Node *p;
p = new Node;
p = l->next;
cout << endl;
if (p == NULL)
{
cout << "没有学生信息!" << endl;
cout << endl;
return;
}
cout << "*********************************************************" << endl;
while (p)
{
cout << p->stu.id << " ";
cout << p->stu.name << " ";
cout << p->stu.sex << " ";
cout << p->stu.grade << " ";
cout << p->stu.age << endl;
p = p->next;
}
cout << endl;
}
/*按身份证号查找*/
void querybyid(LinkList &l)
{
cout << "请输入身份证号" << endl;
string id;
cin >> id;
Node *p;
p = new Node;
p = l->next;
while (p)
{
if (p->stu.id == id)
{
break;
}
p = p->next;
}
if (p)
{
cout << endl;
cout << p->stu.id << " ";
cout << p->stu.name << " ";
cout << p->stu.sex << " ";
cout << p->stu.grade << " ";
cout << p->stu.age << endl;
cout << endl;
}
else
{
cout << endl;
cout << "未找到该学生!" << endl;
}
}
/*通过id删除*/
void deletebyid(LinkList &l)
{
cout << "请输入身份证号" << endl;
string id;
cin >> id;
Node *p;
p = new Node;
p = l->next;
Node *front;
front = new Node;
front = l;
bool is = false;
while (p)
{
if (p->stu.id == id)
{
front->next = p->next;
delete (p);
p = NULL;
is = true;
cout << "删除成功!" << endl;
cout << endl;
break;
}
front = p;
p = p->next;
}
if (is == false)
{
cout << "不存在改学生!" << endl;
}
}
void deleteall(LinkList &l,LinkList &t)
{
Node *p,*q;
p = l->next;
if (p == NULL)
{
cout << "没有学生记录可删除!" << endl;
return;
}
while (p)
{
q=p->next;
delete (p);
p = q;
}
l->next=NULL;
t=l;
cout << "全部删除完成!" << endl;
}
app.cpp内容如下
#include "function.h"
int main()
{
LinkList l;
initList(l);
Node *t;
t = new Node;
t = l;
Student stu;
while (1)
{
int num;
cout << "========================学生信息管理系统============================" << endl;
cout << "1.添加学生信息" << endl;
cout << "2.查询学生信息" << endl;
cout << "3.按身份证号查找" << endl;
cout << "4.按身份证号删除" << endl;
cout << "5.删除全部" << endl;
cout << "0.退出" << endl;
cout << "请输入数字选择:" << endl;
cin >> num;
switch (num)
{
case 1:
addtail(t);
break;
case 2:
queryall(l);
break;
case 3:
querybyid(l);
break;
case 4:
deletebyid(l);
break;
case 5:
deleteall(l,t);
break;
case 0:
exit(0);
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
}
源码下载:点我下载