一、实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二、实验时间
准备时间为第3周到第4周,具体集中实验时间为第4周第2次课。2个学时。
三、实验内容
建立一个由n个学生信息的单链表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
四、代码实现
1、Linklist.h
//Linklist.h
#pragma once
template<class DataType>
struct Node //用结构体定义节点
{
DataType data;
Node<DataType> *next;
};
template<class DataType>
class Slinklist { //定义单链表类模板
public:
Slinklist();
Slinklist(DataType a[], int n);
~Slinklist();
int Length();
void Get(int n);
int Locate(char a[]);
void Insert(int i, DataType x);
DataType Delete(int i);
void Printlist();
private:
Node<DataType> *first;
};
struct Student //定义学生信息结构体数组
{
char name[5];
char sno[14];
char sex[4];
char age[4];
char Class[12];
};
2、各功能函数实现:Linklist.cpp
//Linklist.cpp
#include"Linklist.h"
#include<iostream>
using namespace std;
template<class DataType>
Slinklist<DataType>::Slinklist() //无参构造函数定义
{
first = new Node<DataType>; //生成头节点
first->next = NULL; //头节点的指针域置空
}
template<class DataType>
Slinklist<DataType>::Slinklist(DataType a[], int n) //有参构造函数定义,用头插法建立单链表
{
first = new Node<DataType>; first->next = NULL;
for (int i = 0; i < n; i++)
{
Node<DataType> *s;
s = new Node<DataType>; s->data = a[i];
s->next = first->next; first->next = s;
}
}
template<class DataType>
int Slinklist<DataType>::Length() //计算单链表长度
{
p = new Node<DataType>;
p = first->next; count = 0;
while (p != NULL)
{
p = p->next;
count++;
}
return count;
}
template<class DataType>
void Slinklist<DataType>::Get(int i) //查找在第i位的学生信息
{
Node<DataType> *p;
p = first->next; int count = 0;
while(p->data.name != NULL&&count < i - 1)
{
p = p->next;
count++;
}
if (p->data.name == NULL)throw"位置";
else cout << p->data.name << " " << p->data.sno << " " << p->data.sex << " " << p->data.age << " " << p->data.Class << endl;
}
template<class DataType>
int Slinklist<DataType>::Locate(char a[]) //查找学生信息,返回学生所在位置
{
Node<DataType> *p;
p = new Node<DataType>;
p = first->next; int count = 1;
while (p->data.name != NULL)
{
if ((string)p->data.name == (string)a)return count;
p = p->next;
count++;
}
return 0;
}
template<class DataType>
void Slinklist<DataType>::Insert(int i, DataType x) //从第i位插入学生信息
{
Node<DataType> *p;
p = new Node<DataType>;
p = first; int count = 0;
while (p->data.name != NULL&&count < i - 1)
{
p = p->next;
count++;
}
if (p->data.name == NULL)throw"位置";
else {
Node<DataType> *s;
s = new Node<DataType>; s->data = x;
s->next = p->next; p->next = s;
}
}
template<class DataType>
DataType Slinklist<DataType>::Delete(int i) //删除第i位学生信息
{
Node<DataType> *p;
p = new Node<DataType>;
p = first; int count = 1;
while (p->data.name != NULL&&count < i)
{
p = p->next;
count++;
}
if (p->data.name == NULL||p->data.name == NULL )throw"位置";
else {
Node<DataType> *q;
q = new Node<DataType>;
q = p->next; DataType x = q->data;
p->next = q->next;
delete q;
return x;
}
}
template<class DataType>
void Slinklist<DataType>::Printlist() //单链表遍历信息
{
Node<DataType> *p;
p = new Node<DataType>;
p = first->next;
while (p != NULL)
{
cout << p->data.name << " " << p->data.sno << " " << p->data.sex << " " << p->data.age << " " << p->data.Class << endl;
p = p->next;
}
cout << endl;
}
template<class DataType>
Slinklist<DataType>::~Slinklist() //析构函数
{
while (first != NULL)
{
Node<DataType> *q;
q = new Node<DataType>;
q = first;
first = first->next;
delete q;
}
}
3、主程序对信息进行基本操作:linklistmain.cpp
//linklistmain.cpp
#include"Linklist.cpp"
#include<iostream>
using namespace std;
int main()
{
Student student[5] = { { "小波","201611671119","男","20","信管1161" },
{ "小明","201611671114","男","19","信管1161" },
{ "小红","201611671121","女","18","信管1161" },
{ "小芳","201611671127","女","17","信管1161" },
{ "小李","201611671125","男","18","信管1161" } };
Slinklist<Student>score(student, 5);
cout << ">>>显示所有学生信息:" << endl;
score.Printlist();
cout << ">>>将“小刚”的信息插入第二位后显示所有学生信息:" << endl;
score.Insert(2, { "小刚","201611671101","男","15","信管1161" });
score.Printlist();
cout << ">>>获取第三位学生的信息:"<<endl;
score.Get(3) ;
cout << ">>>查找学生“小明”信息所在的位置:" << endl;
cout << score.Locate("小明") << endl;
cout << ">>>删除数据前所有学生数据:" << endl;
score.Printlist();
score.Delete(2);
cout << ">>>删除第二位学生数据后所有学生数据:" << endl;
score.Printlist();
return 0;
}
五、运行结果如下:
六、总结
在能够完成基本操作的同时对操作数据稍微复杂化了一点。