数据结构实验二(单链表)
来源:互联网 发布:活体脑细胞结局知乎 编辑:程序博客网 时间:2024/06/05 22:55
#include <iostream>using namespace std;template <class DataType>struct Node{ DataType data; Node <DataType> *next;};template <class DataType>class Linklist{ public: Linklist(); Linklist(DataType a[],int n); ~Linklist(){}; int Length(); DataType Get(int i); int Locate(DataType x); void Insert(int i,DataType x); DataType Delete (int i); void PrintList(); private: Node<DataType>*first;};template <class DataType>void Linklist<DataType>::PrintList(){ Node<DataType>*p; p=first->next; while(p!=NULL) { cout<<p->data; p=p->next; }}template<class DataType>int Linklist<DataType>::Length(){ Node<DataType>*p=NULL; p=first->next; int count=0; while(p!=NULL) { p=p->next; count++; } return count;}template <class DataType>DataType Linklist<DataType>::Get(int i){ Node<DataType>*p; p=first->next; int count=1; while(p!=NULL && count<i) { p=p->next; count++; } if(p==NULL) throw"位置"; else return p->data;}template <class DataType>int Linklist<DataType>::Locate(DataType x){ Node<DataType>*p; p=first->next; int count=1; while(p!=NULL ) { if(p->data==x) return count; p=p->next; count++; } return 0;}template <class DataType>void Linklist<DataType>::Insert(int i,DataType x){ Node<DataType>*p; p=first; int count=0; while(p!= NULL && count<i-1) { p=p->next; count++; } if (p==NULL) throw "位置"; else { Node<DataType>*s; s= new Node<DataType>; s->data=x; s->next=p->next; p->next=s; }}template <class DataType>Linklist <DataType>::Linklist(){ first= new Node<DataType>; first->next=NULL;}template <class DataType>Linklist<DataType>::Linklist(DataType a[],int n){ Node<DataType>*r,*s; first=new Node<DataType>; r=first; for(int i=0;i<n;i++) { s=new Node<DataType>; s->data=a[i]; r->next=s; r=s; } r->next=NULL;}template <class DataType>DataType Linklist<DataType>::Delete(int i){ Node<DataType> *p; int x; int count=0; p=first; while(p!=NULL && count<i-1) { p=p->next; count++; } if(p==NULL || p->next==NULL) throw "位置"; else { Node<DataType> *q; q= new Node<DataType>; q->next=p->next; x=q->data; delete q; return x; }}int main(){ int score[10]={100,95,90,88,86,85,80,76,75,70}; Linklist<int>a(score,10); a.PrintList(); //遍历学生分数 cout << "------------" << endl; cout<<a.Get(2)<<endl; //取第二个位置的学生分数 cout << "------------" << endl; a.Insert(3, 100); //在第三个位置插入学生分数 cout << "------------" << endl; a.PrintList(); //再次遍历 cout << "------------" << endl; cout << a.Length() << endl; //获取分数表长度 cout << "------------" << endl; cout << a.Locate(96) << endl; //获取分数为96的位置 cout << "------------" << endl; cout<<a.Delete(1)<<endl; //删除第一个分数 cout << "------------" << endl; a.PrintList(); //再次遍历 cout << "------------" << endl; return 0;}
阅读全文
0 0
- 数据结构实验二(单链表)
- 数据结构实验二之单链表
- 数据结构上机实验(二)
- 《数据结构》实验二线性表实验(实验报告)
- 《数据结构》实验二:线性表的实验(实验报告)
- 数据结构实验二--单链表的基本实现
- 数据结构和算法分析实验(二)
- 数据结构课程上机实验题(二)
- 数据结构:实验二(修改后)
- 《数据结构》实验二(顺序表)
- 数据结构实验(二) 静态链表
- 数据结构实验(二):间接寻址
- 数据结构实验二
- 数据结构:实验二
- 《数据结构 实验二》
- 数据结构实验二
- 《数据结构》实验二:线性表综合实验——(2)单链表
- 数据结构:实验二线性表综合实验②单链表
- CodeForces 55D Beautiful numbers (数位dp)
- Container With Most Water-水桶装水问题
- java
- 每天一个linux命令(11):nl命令
- bzoj1774 [Usaco2009 Dec]Toll 过路费
- 数据结构实验二(单链表)
- 机器学习——支持向量机SVM之软间隔与正则化
- Android clipChildren与clipToPadding使用以及简单ViewPager自定义切换效果
- linux 编译android openssl库
- CentOS修改环境变量
- 数据挖掘小白入门-整体思路
- 打印杨辉三角(非直角三角形打印)
- springmvc 之HandlerExceptionResolver
- 安卓开发之Button