实验二 线性表综合实验之《静态链表》

来源:互联网 发布:qq管家软件搬家 编辑:程序博客网 时间:2024/05/21 22:39

实验二 线性表综合实验之《双链表》

一.实验目的

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

二.实验内容

      建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。

要求如下:

1)用顺序表来实现。

2)用单链表来实现。

3)用双链表实现。

4)用静态链表实现。

5)用间接寻址实现。

三.实验报告 

1、代码

#includeusing namespace std;const int MaxSize=100;template struct SNode{T data;int next;   //假指针};template class SList{public:SList();SList(T a[],int n);int Length();T Get(int i);int Locate(T x);void Insert(int i,T x);T Delete(int i);void PrintList();private:SNode S[MaxSize];int first,avail;};//遍历操作templatevoid SList::PrintList(){int p;p=S[first].next;  //使其指向第一个元素while(p!=-1){cout<int SList::Length(){int p,count=0;p=S[first].next;  //使其指向第一个元素while(p!=-1){p=S[p].next;   //指针后移count++;}return count;}//按位查找templateT SList::Get(int i){int p,count=1;p=S[first].next;while(p!=-1&&countint SList::Locate(T x){int p,count=1;p=S[first].next;while(p!=-1){if(S[p].data==x) return count;p=S[p].next;count++;}return 0;}//插入数据,在p后面插入template void SList::Insert(int i,T x){int p,q,count=0;p=first;while(S[p].next!=-1&&countT SList::Delete(int i){T x;int p,q,count=0;p=first;while(S[p].next!=-1&&countSList::SList(){first=0;avail=1;S[0].next=-1;   //头结点无后续结点for(int i=0;iSList::SList(T a[],int n){int p;if(n>MaxSize||a<=0)  throw"错误";first=0;avail=1;S[0].next=avail;for(int i=0;i SL(arr,5);   //install an objectcout<<"\n            静态链表"<>tab;  switch(tab)  {  case 1:   {cout<<"1.输出表:";    SL.PrintList();cout<>loc;    x=SL.Get(loc);cout <<"The number is:"<>x;    loc=SL.Locate(x);     cout<<"The number's location is:"<>loc;    cout<<"Please input the insert num:";    cin>>x;    SL.Insert(loc,x);cout<>loc;    x=SL.Delete(loc);cout<

2、调试结果

①、遍历操作及输出总学生人数


②、按位查找操作


③、按值查找操作


④、插入操作


⑤、删除操作