一.实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二.实验内容
建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。(单链表)
源代码:#include<iostream>
using namespace std;
template<class DT>
struct Node
{
DT data;
Node<DT>*next;
};
template<class DT>
class Linklist
{
public:
Linklist(DT a[],int n);
int Length();
DT Get(int i);
int Locate(DT x);
void Insert(int i,DT x);
DT Delete(int i);
void Printlist();
private:
Node<DT>*first;
};
template<class DT>
Linklist<DT>::Linklist(DT a[],int n)
{
Node<DT>*r,*s;
first=new Node<DT>;
r=first;
for (int i=0;i<n;i++)
{
s=new Node<DT>;s->data=a[i];
r->next=s;r=s;
}
r->next=NULL;
}
template<class DT>
int Linklist<DT>::Length()
{
Node<DT>*p=first->next;int count=0;
while(p!=NULL)
{
p=p->next;
count++;
}
return count++;
}
template<class DT>
DT Linklist<DT>::Get(int i)
{
Node<DT>*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 DT>
int Linklist<DT>::Locate(DT x)
{
Node<DT>*p=first->next;int count=1;
while(p!=NULL)
{
if(p->data==x)return count;
p=p->next;
count++;
}
return 0;
}
template<class DT>
void Linklist<DT>::Insert(int i,DT x)
{
Node<DT>*p=first;
for(int count=1;count<=i-1;count++)
{
p=p->next;
}
Node<DT>*temp;
temp=new Node<DT>;
temp->data=x;
temp->next=p->next;
p->next=temp;
}
template<class DT>
DT Linklist<DT>::Delete(int i)
{
Node<DT>*p=first;int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if (p==NULL||p->next==NULL)
throw"位置";
else{
Node<DT>*q=p->next;DT x=q->data;
p->next=q->next;
delete q;
return x;
}
}
template<class DT>
void Linklist<DT>::Printlist()
{
Node<DT>*p=first->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
void main()
{
int a[10]={100,98,96,94,91,89,86,81,76,62};
Linklist<int> Link1(a,10);
cout<<"10人成绩:"<<endl;
Link1.Printlist();
cout<<endl;
cout<<"排名第3的成绩为:"<<endl;
cout<<Link1.Get(7)<<endl;
cout<<"81分的同学排名为:"<<endl;
cout<<Link1.Locate(81)<<endl;
cout<<"尾端插入成绩为56且倒数第一:"<<endl;
Link1.Insert(11,56);
cout<<"当前表为:"<<endl;
Link1.Printlist();
cout<<endl;
cout<<"删除不及格成绩"<<endl;
Link1.Delete(11);
cout<<"当前表为:"<<endl;
Link1.Printlist();
cout<<endl;
}
实验结果:
#include<iostream>
using namespace std;
template<class DT>
struct Node
{
DT data;
Node<DT>*next;
};
template<class DT>
class Linklist
{
public:
Linklist(DT a[],int n);
int Length();
DT Get(int i);
int Locate(DT x);
void Insert(int i,DT x);
DT Delete(int i);
void Printlist();
private:
Node<DT>*first;
};
template<class DT>
Linklist<DT>::Linklist(DT a[],int n)
{
Node<DT>*r,*s;
first=new Node<DT>;
r=first;
for (int i=0;i<n;i++)
{
s=new Node<DT>;s->data=a[i];
r->next=s;r=s;
}
r->next=NULL;
}
template<class DT>
int Linklist<DT>::Length()
{
Node<DT>*p=first->next;int count=0;
while(p!=NULL)
{
p=p->next;
count++;
}
return count++;
}
template<class DT>
DT Linklist<DT>::Get(int i)
{
Node<DT>*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 DT>
int Linklist<DT>::Locate(DT x)
{
Node<DT>*p=first->next;int count=1;
while(p!=NULL)
{
if(p->data==x)return count;
p=p->next;
count++;
}
return 0;
}
template<class DT>
void Linklist<DT>::Insert(int i,DT x)
{
Node<DT>*p=first;
for(int count=1;count<=i-1;count++)
{
p=p->next;
}
Node<DT>*temp;
temp=new Node<DT>;
temp->data=x;
temp->next=p->next;
p->next=temp;
}
template<class DT>
DT Linklist<DT>::Delete(int i)
{
Node<DT>*p=first;int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if (p==NULL||p->next==NULL)
throw"位置";
else{
Node<DT>*q=p->next;DT x=q->data;
p->next=q->next;
delete q;
return x;
}
}
template<class DT>
void Linklist<DT>::Printlist()
{
Node<DT>*p=first->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
void main()
{
int a[10]={100,98,96,94,91,89,86,81,76,62};
Linklist<int> Link1(a,10);
cout<<"10人成绩:"<<endl;
Link1.Printlist();
cout<<endl;
cout<<"排名第3的成绩为:"<<endl;
cout<<Link1.Get(7)<<endl;
cout<<"81分的同学排名为:"<<endl;
cout<<Link1.Locate(81)<<endl;
cout<<"尾端插入成绩为56且倒数第一:"<<endl;
Link1.Insert(11,56);
cout<<"当前表为:"<<endl;
Link1.Printlist();
cout<<endl;
cout<<"删除不及格成绩"<<endl;
Link1.Delete(11);
cout<<"当前表为:"<<endl;
Link1.Printlist();
cout<<endl;
}
心得体会
本次实验要求用不同的链表来实现同一个程序,我先写了单链表,单链表与顺序表相比,对于指针的运用要求更高了,也更加灵活。由于单链表没有限制长度,所以如果有较大数据和需要经常插入和删除数据的话,建议使用链表。