课程名:数据结构
实验目的:
1、掌握线性表的定义;
2、掌握线性表的基本操作,如建立、查找、插入和删除等。
实验要求:定义一个包含学生信息(学号,姓名,成绩)的顺序表和链表,使其具有如下功能:
(1) 根据指定学生个数,逐个输入学生信息;
(2) 逐个显示学生表中所有学生的相关信息;
(3) 根据姓名进行查找,返回此学生的学号和成绩;
(4) 根据指定的位置可返回相应的学生信息(学号,姓名,成绩);
(5) 给定一个学生信息,插入到表中指定的位置;
(6) 删除指定位置的学生记录;
(7) 统计表中学生个数。
实验题目:线性表的基本操作及其作用
实验过程:
按照实验要求编写相应程序代码,并调试运行。
附:顺序表与链表操作的主函数代码。
运行演示过程如下(这部分不需要写到报告上):
1、 创建一个学生表(5个学生);
2、 显示该表中所有的元素;
3、 根据姓名查找到第3个学生的信息并显示;
4、 插入一个新的学生并显示全部学生信息;
5、 删除第3个学生的信息并显示全部学生信息;
6、 统计学生表中元素的个数(即学生人数);
7、 退出
实验结果:
能够顺利完成顺序表和单链表的创建、插入、删除等操作。
实验分析:
1、顺序表和单链表在各种操作实现过程中的差别
2、程序调试运行中出现的错误信息原因分析。
参考信息:
Definition of structure student:
typedef struct {
char no[8]; //8位学号
char name[20]; //姓名
int score; //成绩
}Student;
Definition of sequential list:
typedef struct {
Student *elem; //指向数据元素的基地址
int length; //线性表的当前长度
}SqList;
Definition of linked list:
typedef struct LNode{
Student data; //数据域
struct LNode *next; //指针域
}LNode,*LinkList;
实验要求:
(1) 程序要添加适当的注释,程序的书写要采用缩进格式。
(2) 程序要具在一定的健壮性,即当输入数据非法时,程序也能适当地做出反应,如插入删除时指定的位置不对等等。
(3) 程序要做到界面友好,在程序运行时用户可以根据相应的提示信息进行操作。
(4) 上传源程序到课堂派。顺序表的源程序保存为SqList.cpp,链表的源程序保存为LinkList.cpp。。
顺序表:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define OVERFLOW -1
#define ERROR 2;
#define OK 1
#define MAXSIZE 100
using namespace std;
typedef struct //多项式的顺序储存结构的类型定义
{
char num[20]; //学号
char name[20]; //名字
float score; //成绩
}Student;
typedef struct
{
Student *elem; //指向数据元素的基地址
int length; //线性表的当前长度
}SqList;
int InitList(SqList &L)
{
L.elem=new Student[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间
if(!L.elem) {
cout<<"内存分配失败"<<endl;
exit(-1);
}
L.length = 0; //空表长度为0
return 1;
}
int InputList(SqList &L)
{
if(L.length == 0) return 2;
else {
cout<<"请依次输入学生信息"<<endl;
for(int i=1; i<=L.length; i++) {
cout<<"请输入第"<<i<<"个学生信息"<<endl;
cout<<"学号:"<<endl;
cin>>L.elem[i].num;
cout<<"名字"<<endl;
cin>>L.elem[i].name;
cout<<"成绩"<<endl;
cin>>L.elem[i].score;
}
cout<<"请确认继续"<<endl;
}
}
void ShowList(SqList L)
{
if(L.length == 0)
cout<<"学生信息为空"<<endl;
else {
cout<<"目前学生总人数为:"<<L.length<<endl;
cout<<"学生信息如下"<<endl;
for(int i=1; i<=L.length; i++) {
cout<<"学号:"<<L.elem[i].num<<endl;
cout<<"名字:"<<L.elem[i].name<<endl;
cout<<"成绩:"<<L.elem[i].score<<endl;
if(i!=L.length) cout<<endl;
}
cout<<"请确认继续"<<endl;
}
}
void NameCheckList(SqList L)
{
cout<<"请输入查找的姓名:";
char name[20];
cin>>name;
int flag=0,m;
for(int i=1; i<=L.length; i++) {
if(strcmp(L.elem[i].name , name) == 0) {
flag=1;
m=i;
break;
}
}
if(flag==0)
cout<<"查无此人!"<<endl;
else {
cout<<name<<"的各项信息如下:"<<endl;
cout <<"学号:"<<L.elem[m].num<<endl;
cout<<"名字:"<<L.elem[m].name<<endl;
cout <<"成绩:"<<L.elem[m].score<<endl;
}
}
void LocationCheckList(SqList L)
{
cout<<"请输入查找位置:";
int loc;
cin>>loc;
if(loc<1 || loc>L.length) {
cout<<"查无此人!"<<endl;
}
else {
cout<<"第"<<loc<<"个学生的信息如下:"<<endl;
cout <<"学号:"<<L.elem[loc].num<<endl;
cout<<"名字:"<<L.elem[loc].name<<endl;
cout <<"成绩:"<<L.elem[loc].score<<endl;
}
}
void CheckList(SqList L)
{
if(L.length == 0)
cout<<"无此查找信息!"<<endl;
else {
cout<<"请选择查找方式:"<<endl;
cout<<"1.姓名查找"<<endl;
cout<<"2.地址查找"<<endl;
int way;
cin>>way;
switch(way)
{
case 1:NameCheckList(L); break;
case 2:LocationCheckList(L); break;
default: cout<<"输入有误!请继续"<<endl;
}
cout<<"请确认继续"<<endl;
}
}
int InsertList(SqList &L)
{
cout<<"请输入插入学生信息的位置:";
int location;
int flag;
do
{
flag=0;
cin>>location;
if(location<1 || location>L.length)
{
cout<<"输入有误!(i值不合法)请重新输入:"<<endl;
flag=1;
// return 2;
}
}while(flag==1);
if(L.length==MAXSIZE) {
cout<<"当前存储空间已满"<<endl;
return 2;
}
Student e;
for(int i=L.length; i>=location; i--)
L.elem[i+1]=L.elem[i];
cout<<"请输入插入学生信息:"<<endl;
cout<<"学号:";
cin>>e.num;
cout<<"名字:";
cin>>e.name;
cout<<"成绩:";
cin>>e.score;
L.elem[location]=e;
++L.length;
return 1;
cout<<"请确认继续"<<endl;
}
int DeleteList(SqList &L)
{
if(L.length==0) return 2;
cout<<"请输入删除学生信息的位置:";
int location;
int flag;
do
{
flag=0;
cin>>location;
if(location<1 || location>L.length)
{
cout<<"输入有误!(i值不合法)请重新输入:"<<endl;
flag=1;
// return 2;
}
}while(flag==1);
for(int i=location+1; i<=L.length; i++)
L.elem[i-1]=L.elem[i];
--L.length;
cout<<"指定学生信息已删除,请确认继续"<<endl;
return 1;
}
int main()
{
SqList L;
InitList(L);
int n;
cout<<"请输入初始学生人数"<<endl;
cin>>n;
L.length = n;
InputList(L);
cout<<"创建初始学生信息成功"<<endl;
cout<<"请确认继续"<<endl;
getchar();
getchar();
while(1)
{
for(int j=0;j<6;j++)
cout<<endl;
cout<<"=================操作选项如下================="<<endl;
cout<<"==============1.输出所有学生信息=============="<<endl;
cout<<"==============2.查找学生信息=================="<<endl;
cout<<"==============3.插入学生信息=================="<<endl;
cout<<"==============4.删除学生信息=================="<<endl;
cout<<"==============5.退出=========================="<<endl;
cout<<endl<<endl<<endl;
cout<<"请输入操作选项:";
int way;
cin>>way;
switch(way)
{
case 1:ShowList(L); break;
case 2:CheckList(L); break;
case 3:InsertList(L); break;
case 4:DeleteList(L); break;
case 5:exit(0); break;
default:cout<<"输入信息有误!请确认继续"<<endl;
}
getchar();
getchar();
system("cls");
}
return 0;
}
/*
3
2017
wang
80.1
2018
ya
80.2
2019
biao
80.3
*/