一.实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二..实验内容
建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,
实现数据的对表进行插入、删除、查找等操作。分别输出结果。
(用循环链表来实现)。
三..源代码
#include<iomanip>
template<class T>
struct Node
{
T data;
Node<T> *next;
};
template<class T>
class CircleList
{
private:
int length; //链表长度
Node<T> *first; //定义头指针
public:
CircleList(); //默认构造函数
CircleList(T a[], int n); //定义长度为N的有参构造函数
~CircleList();//析构函数
int GetLength(); //求链表长度
T Get(int i); //按位查找
int Locate(T x); //按值查找
bool Insert(int i, T x); //插入操作
T Delete(int i); //删除操作
void PrintList(); //遍历操作
};
template<class T>
CircleList<T>::CircleList()
{
first = new Node<T>;
first->next = first;
length = 0;
}
template<class T>
CircleList<T>::CircleList(T a[], int n)
{
first = new Node<T>;
first->next = first;
Node<T> *p, *temp;
p = first;
for (int i = 0; i < n; i++)
{
temp = new Node<T>;
temp->data = a[i];
temp->next = p->next;
p->next = temp;
p = p->next;
length++;
}
p->next = first;
}
template<class T>
CircleList<T>::~CircleList()
{
Node<T> *p = first->next;
Node<T> *temp = first;
while (p != first)
{
delete temp;
temp = p;
p = p->next;
}
delete temp;
length = 0;
}
template<class T>
int CircleList<T>::GetLength()
{
return length;
}
template<class T>
T CircleList<T>::Get(int i)
{
if (i > length)
{
throw("输入错误");
}
Node<T> *p;
p = first;
for (int count = 1; count <= i; count++)
{
p = p->next;
}
return p->data;
}
template<class T>
int CircleList<T>::Locate(T n)
{
Node<T> *p;
p = first->next;
int count = 1;
while (p->data != n&&p->next != first)
{
p = p->next;
count++;
}
if (p->next == first)
{
return -1;
}
return count;
}
template<class T>
bool CircleList<T>::Insert(int i, T x)
{
if (i > length)
{
return false;
}
Node<T> *p;
p = first;
for (int count = 1; count < i; count++)
{
p = p->next;
}
Node<T> *s = new Node<T>;
s->data = x;
s->next = p->next;
p->next = s;
length++;
return true;
}
template<class T>
T CircleList<T>::Delete(int i)
{
if (i > length)
{
throw("参数错误");
}
Node<T> *p;
p = first;
for (int k= 1; k < i; k++)
{
p = p->next;
}
Node<T> *s = new Node<T>;
s = p->next;
T k;
k = s->data;
p->next = s->next;
delete s;
length--;
return k;
}
template<class T>
void CircleList<T>::PrintList()
{
Node<T> *p;
p = first;
p=p->next;
for (int i = 0; i < length; i++)
{
cout <<setiosflags(ios::fixed) << setprecision(1)<<p->data << " ";
p = p->next;
}
}
#include<iostream>
#include"CircleList.h"
using namespace std;
int main(void)
{
float a[5] = { 99.5,96.5,97.0,94.5,98.0 };
CircleList<float> c1(a, 5);
c1.PrintList(); //遍历成绩
cout << endl<<"----------------------------------" << endl;
cout << c1.Get(2) << endl; //取第二个成绩
cout << "----------------------------------" << endl;
cout << c1.Locate(91.0) << endl; //取成绩为91.0的位置(实际没有,应返回-1)
cout << "----------------------------------" << endl;
c1.Insert(5, 100.0); //在第五个位置插入分数:100.0
c1.PrintList(); //再次遍历
cout <<endl<< "----------------------------------" << endl;
cout << c1.GetLength() << endl;
cout << "----------------------------------" << endl;
c1.Delete(1); //删除第一个成绩
c1.PrintList(); //再次遍历
cout << endl<<"----------------------------------" << endl;
system("pause");
return 0;
}
四.实验截图
首先遍历,结果正确。
输出第二个成绩为:96.5
输出成绩列不含的91.0,输出-1
在第五个位置插入100.0,再次遍历
获取长度,为6
删除第一个成绩,再次遍历
经验证,输出结果准确。
五.实验心得
通过这次实验,我掌握了循环链表的操作,实现对数据的增删查输出等操作,尽管这敲代码时出现不解的错误,最终
将.h和.cpp文件并在一起才可以正确运行,具体原因还不明,希望自己能在老师或者同学的帮助下解开此问题,
也希望以后自己多敲码,更加熟练,做得更好。
本次实验旨在巩固线性表的循环链表实现,包括创建包含学生成绩的顺序表,并进行插入、删除和查找操作。源代码展示了一个有效的循环链表操作流程,实验结果显示所有操作均正确执行,如在第五个位置插入100.0,删除第一个成绩等。实验加深了对循环链表的理解,但也遇到了.h和.cpp文件整合运行的问题,希望通过进一步学习和实践找到解决方案。
656





