实验二(2)用单链表实现信息操作

这篇博客旨在巩固线性表数据结构的存储和操作,通过建立包含多个学生信息的单链表,实现数据的插入、删除和查找功能,并展示实际运行结果。在实验中,博主对操作进行了略微复杂化的处理。

一、实验目的

        巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。

二、实验时间

        准备时间为第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;
}


五、运行结果如下:



六、总结

       在能够完成基本操作的同时对操作数据稍微复杂化了一点。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_linbobo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值