静态链表的基本操作实现
来源:互联网 发布:js正则表达式 冒号 编辑:程序博客网 时间:2024/06/05 05:00
一.实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二.实验内容
1.建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
#include <iostream>using namespace std;const int MaxSize=100;template<class DataType>struct SNode{DataType data;int next;//指针域(也称游标),注意不是指针类型 }; //以下是类StaticLinkList的声明 template<class DataType> class StaticLinkList { public: StaticLinkList(); StaticLinkList(DataType a[],int n); ~StaticLinkList(){ }; int Locate(DataType x); void Insert(int i,DataType x); DataType Delete(int i); void PrintStaticList(); private: SNode<DataType> List[MaxSize]; int first; int avail; }; //以下为类StaticLinkList的成员函数定义 template<class DataType> StaticLinkList<DataType>::StaticLinkList() { first=0;//初始化头指针 avail=0;//初始化空闲链指针 List[0].next=-1;//头结点后无后续结点 for(int i;i<MaxSize-1;i++) { List[i].next=i+1;//初始化空闲链 } List[MaxSize-1].next=-1;//置空闲链结束标志 } template<class DataType> StaticLinkList<DataType>::StaticLinkList(DataType a[],int n) { int s; if(n>MaxSize||n<=0)throw"输入有误"; first=0; List[0].next=avail=1; for(int i=0;i<MaxSize-1;i++) {List[i].next=i+1;//先将数组置为闲链,这部分也可以在后面,但只讲存放数据后的 空闲链相连}List[MaxSize-1].next=-1;//置空闲链结束标志for(int i=0;i<n;i++) {s=avail;List[s].data=a[i];avail=List[avail].next;}List[s].next=-1; } template<class DataType>void StaticLinkList<DataType>::Insert(int i,DataType x){ int s; s=avail;//不用申请新结点,利用空闲链的第一个结点 avail=List[avail].next;//空闲链的头指针后移 List[s].data=x;//将x填入下标为s的结点 for(int p=0;p<MaxSize-1;p++){ if(p==i) {List[s].next=List[p].next;//将下标为s的结点插入到下标为p的结点后面 List[p].next=s;} } } template<class DataType>DataType StaticLinkList<DataType>::Delete(int i){if(i>0&&i<MaxSize) { int q;i=i-1;q=List[i].next;//暂存被删结点的下标List[i].next=List[q].next;//摘链List[q].next=avail;//将结点q插在空闲链avail的最前端avail=q;return List[q].data;}else{throw"位置";return 0; }} template<class DataType> int StaticLinkList<DataType>::Locate(DataType x) { int count; count=List[first].next; for(count;count!=-1;count++) { if(List[count].data==x){ return count; } } return 0; } template<class DataType> void StaticLinkList<DataType>::PrintStaticList() { int p; p=List[first].next; while(p!=-1) { cout<<List[p].data<<endl; p=List[p].next; } }int main() { int r[5]={87,68,74,90,80}; StaticLinkList<int>L(r,5); cout<<"执行插入成绩操作前数据为:"<<endl; L.PrintStaticList(); try { L.Insert(2,70); } catch(char*s) { cout<<s<<endl; } cout<<"执行插入成绩操作后数据为:"<<endl; L.PrintStaticList(); cout<<"值为74的元素位置为:"; cout<<L.Locate(74)<<endl; cout<<"执行删除第一个学生成绩操作前数据为:"<<endl; L.PrintStaticList(); try { L.Delete(1); } catch(char*s) { cout<<s<<endl; } cout<<"执行删除成绩操作后数据为:"<<endl; L.PrintStaticList(); }
四.实验结果
五、心得
通过这次实验,我基本掌握了静态链表的操作。由于静态链表是用数组来表示单链表,用数组元素的下标来模拟单链表的指针,所以静态链表的结点的定义,与插入、删除的相关操作都会有所不同。我通过仔细研究书上的相关知识点,并向相关同学询问一些问题,最后调试并运行成功出了程序。希望自己能够认真学习,继续努力。
阅读全文
0 0
- 静态链表的基本操作实现
- 静态链表的基本操作实现
- c语言实现静态链表的基本操作
- 静态链表的所有基本操作
- 静态链表的基本操作
- 实现基于静态数组的顺序表的基本操作
- 顺序表的基本操作——静态实现
- 实验2.4 运用静态链表实现对数据的基本操作
- 数据结构笔记(三)静态链表的实现和基本操作
- 静态链表的操作实现
- 静态链表的创建以及基本操作
- 静态链表的基本操作及其应用(实验2.3)
- 静态链表的基本操作及其应用(实验2.3)
- 静态顺序表的基本操作
- 静态顺序表的基本操作
- 静态顺序表的基本操作
- 通过Freemarker实现页面静态化的基本操作
- 实现基于静态数组的顺序表的以下基本操作:
- python第三方库之numpy
- 【SHOI2012】魔法树
- 网页xx秒将返回测JS代码的实现
- Effective Java 读书笔记(六):方法
- 5-预定义变量的使用
- 静态链表的基本操作实现
- 使用Aspose.Cells.dll导出数据到Excel
- orm与数据持久化
- model1(jsp+javaBean)和model2(jsp+servlet+javaBean+MVC)开发模式优缺点
- Unable to resolve target 'android-17'解决方法
- <Web>jQuery黑客帝国,数字下落特效!
- 【LeetCode】Identical Binary Tree等价二叉树
- 通过调用函数来实现乘法表的打印
- tablelayout_demo2