物联网1131-23
实验目的
【巩固线性表的数据结构,学会线性表的应用。】
1.回顾线性表的逻辑结构,线性表的物理存储结构和常见操作。
2.学习运用线性表的知识来解决实际问题。
3.进一步巩固程序调试方法。
4.进一步巩固模板程序设计。
顺序表#include<iostream>
using namespace std;
const int MaxSize = 20;
template<class Score>
class SeqList
{
public:
SeqList();
SeqList(Score a[],int n);
Score Get(int i);
void Insert(int i,Score x);
Score Delete(int i);
void PrintList();
private:
Score data[MaxSize];
int length;
};
template<class Score>
SeqList<Score>::SeqList(Score a[],int n)
{
if(n>MaxSize)throw"wrong";
for (int i=0;i<n;i++)
data[i]=a[i];
length=n;
}
template<class Score>
Score SeqList<Score>::Get(int i)
{
if (i<1&&i>length)throw"wrong";
else return data[i-1];
}
template<class Score>
void SeqList<Score>::Insert(int i,Score x)
{
if (length>=MaxSize)throw"wrong";
if (i<1||i>length+1)throw"wrong";
for (int j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++;
}
template<class Score>
Score SeqList<Score>::Delete(int i)
{
int x,j;
if(length==0)throw"wrong";
if(i<1||i>length)throw "wrong";
x=data[i-1];
for(j=i;j<length;j++)
data[j-1]=data[j];
length--;
return x;
}
template<class Score>
void SeqList<Score>::PrintList()
{
for(int i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}
int main()
{
int a[10]={70,88,90,100,55,75,98,85,79,91};
int i,x;
SeqList<int>Seqlist(a,10);
cout<<"student's scores are as follows."<<endl;
Seqlist.PrintList();
cout<<"please input student's number to get score"<<endl;
cin>>i;
cout<<Seqlist.Get(i)<<endl;
cout<<"please input location and score."<<endl;
cin>>i>>x;
Seqlist.Insert(i,x);
cout<<"student's scores are as follows."<<endl;
Seqlist.PrintList();
cout<<Seqlist.Delete(2)<<endl;
cout<<"student's scores are as follows."<<endl;
Seqlist.PrintList();
return 0;
}
单链表
<span style="font-size:14px;">#include<iostream>
using namespace std;
template<class Score>
struct SS
{
Score data;
SS<Score> * next;
};
template<class Score>
class LinkList
{
public:
LinkList();
LinkList(Score a[],int n);
void Insert(int i,Score x);
Score Get(int i);
Score Delete(int i);
void PrintList();
private:
SS<Score> * first;
};
template<class Score>
LinkList<Score>::LinkList()
{
first=new SS;
first->next=NULL;
}
template<class Score>
LinkList<Score>::LinkList(Score a[],int n)
{
first=new SS;
first->next=NULL;
for(i=0;i<n,i++)
{
s=new SS;
s->data=a[i];
s->next=first->next;
first->next=s;
}}
template<class Score>
void LinkList<Score>::Insert(int i,Score x)
{
p=first; count=0;
while(P!=NULL&&count<i-1)
{
p=p->next;
count++
}
if(p==NULL)throw"WRONG"
else {
s=new SS;s->data=x;
s->next=p->next;
p->next=s;
}}
template<class Score>
Score LinkList<Score>::Delete(int i)
{
p=first;
count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL||P->NEXT==NULL)
throw"WRONG"
else{
q=p->next;
x=q->next;
p->next=q->next;
delete q;
return x;}}
template<class Score>
Score LinkList<Score>::Get(int i)
{
p=first->next;count=1;
while(P!=NULL&&count<i)
{
p=p->next;
count++;
}
if(p=NULL)throw"wrong"
else returnp->data;
}
template<class Score>
void LinkList<Score>::PrintList()
{
p=first->next;
while(p!=NULL)
{
cout<<p->data;
p=p->next;
}}
int main()
{
int i,x;
int a[10]={78,85,100,55,95,91,88,75,64,85};
LinkList<int>LinkList(a, 10);
cout<<"student's scores are as follows."<<endl;
LinkList.PrintList();
cout<<"please input student's number to get score"<<endl;
cin>>i;
cout<<LinkList.Get(i)<<endl;
cout<<"please input location and score."<<endl;
cin>>i>>x;
LinkList.Insert(i,x);
cout<<"student's scores are as follows."<<endl;
LinkList.PrintList();
cout<<LinkList.Delete(2)<<endl;
cout<<"student's scores are as follows."<<endl;
LinkList.PrintList();
return 0;
}
</span>