数据结构实验2.2(循环链表Test1)
来源:互联网 发布:不同表格相同数据查找 编辑:程序博客网 时间:2024/05/18 15:55
一.实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二..实验内容
四.实验截图
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二..实验内容
建立一个由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.0c1.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文件并在一起才可以正确运行,具体原因还不明,希望自己能在老师或者同学的帮助下解开此问题,
也希望以后自己多敲码,更加熟练,做得更好。
阅读全文
0 1
- 数据结构实验2.2(循环链表Test1)
- 数据结构循环链表与双链表实验
- 数据结构实验 约瑟夫环的循环链表实现
- 静态链表Test1
- 双向链表Test1
- 循环链表实验
- 实验二 线性表综合实验(循环链表)
- 数据结构 循环链表
- 数据结构-循环链表
- 数据结构----循环链表
- 数据结构--循环链表
- 【数据结构】循环链表
- 数据结构-循环链表
- 数据结构-循环链表
- 数据结构--循环链表
- 数据结构-循环链表
- 数据结构 循环链表
- 数据结构-循环链表
- icml和nips等会议论文地址
- 2017.9.24 森林 失败总结
- Redis 有序集合(Zset)介绍与基本操作
- js和jquery文件冲突,$冲突
- 我拒绝平庸
- 数据结构实验2.2(循环链表Test1)
- LeetCode674. Longest Continuous Increasing Subsequence
- 捷联导航算法
- hello world!
- HTML DOM学习笔记(二)_DOM常用方法
- 初学js遇到的判断问题?字符穿的清除空字符
- php开发遇到的Access denied for user 'root'@'localhost'(using password:NO)
- 42. Trapping Rain Water
- Oracle RAC技术随笔1