实验13-函数模版与类模版-
题目描述
结点数据类型为int的单链表类CIntList可定义如下:
class CNode
{
public:
int data;
CNode *next;
};
class CIntList
{
private:
CNode *head;
public:
CIntList();
void append(int a); //加到链表最后
void insert(int a, int n); //在第n个结点后加
void remove(int n); //移除第n个结点
int get(int n); //返回第n个结点的数据
void set(int a, int n); //将第n个节点的数据改成a
void print();
~CIntList();
};
试将其改成结点数据类型用参数表示的类模板CList。
输入
第一行输入测试次数
每次测试输入5行,格式为:
数据类型(I:int, D:double, S:string) 数据个数n 数据1 数据2 … 数据n
插入节点号(0表示插在第1个结点前面) 数据
返回结点号
删除结点号
修改结点号 数据
输出
每次测试输出二行.第1行输出返回操作获得的数据(如出错则输出error),第2行输出所有操作后链表全部结点的数据.
输入样例
3
I 5 2 3 5 7 3
1 40
7
7
6 -10
D 6 1.1 2.3 10.05 0.0 -1.8 5.9
4 60.4
5
1
3 -3.7
S 4 this is a test.
0 good
1
8
4 work
输出样例
error
2 40 3 5 7 -10
60.4
2.3 10.05 -3.7 60.4 -1.8 5.9
good
good this is work test.
#include<iostream>
using namespace std;
int i;
template<class Type>
class node
{
public:
Type data;
node *next;
};
template<class Type>//在新class前面也要加template
class list
{
node<Type> *head;
int n;
public:
list(int len,Type *a)
{
head=NULL;//初始化
head=new node<Type>;
head->next=NULL;
n=len;
node<Type> *tail=head;
for(i=0;i<n;i++)
{
node<Type> *p=new node<Type>;
p->data=a[i];
p->next=NULL;
tail->next=p;
tail=p;
}
}
void append(Type e)//加到链表最后
{
n++;
node<Type> *tail=head;
while(tail)
tail=tail->next;
tail->data=e;
}
void insert(int p,Type e)//在第n个结点后加
{
p++;
if(p<1||p>n)
return;
else
{
n++;
node<Type> *q=new node<Type>;
q->data=e;
node<Type> *tail=head;
for(i=0;i<p-1;i++)//在p后面加
tail=tail->next;
q->next=tail->next;//tail后面接p
tail->next=q;
}
}
void remove(int p)//移除第n个结点
{
node<Type> *tail=head;
if(p<0||p>n)
return;
else
{
n--;
for(i=0;i<p-1;i++)
tail=tail->next;
node<Type> *p=tail->next;
tail->next=p->next;
}
}
Type get(int p)//返回第n个结点的数据
{
node<Type> *tail=head;
if(p<n&&p>0)
{
for(i=0;i<p;i++)
tail=tail->next;
}
return tail->data;
}
void set(Type e,int p)//将第n个节点的数据改成a
{
node<Type> *tail=head;
for(i=0;i<p;i++)
tail=tail->next;
tail->data=e;
}
void print()
{
node<Type> *tail=head;
//cout<<n<<endl;
i=0;
while(tail->next)
{
i++;
tail=tail->next;
if(i==n)
cout<<tail->data<<endl;
else
cout<<tail->data<<" ";
}
}
~list()
{
node<Type> *tail;
while(head->next)
{
tail=head->next;
head->next=tail->next;
delete tail;
}
delete head;
}
};
int main()
{
int t,n;
string type;
cin>>t;
while(t--)
{
int insertp,returnp,deletep,changep;//这些一直都是int
cin>>type>>n;
if(type=="I")
{
int *a=new int[n];
for(i=0;i<n;i++)
cin>>a[i];
list<int> c(n,a);
int insertv,changev;
cin>>insertp>>insertv>>returnp>>deletep>>changep>>changev;
c.insert(insertp,insertv);
if(returnp<=n)
cout<<c.get(returnp)<<endl;
else
cout<<"error"<<endl;
c.remove(deletep);
c.set(changev,changep);
c.print();
delete []a;
}
else if(type=="D")
{
double *a=new double[n];
for(i=0;i<n;i++)
cin>>a[i];
list<double> c(n,a);
double insertv,changev;
cin>>insertp>>insertv>>returnp>>deletep>>changep>>changev;
c.insert(insertp,insertv);
if(returnp<=n)
cout<<c.get(returnp)<<endl;
else
cout<<"error"<<endl;
c.remove(deletep);
c.set(changev,changep);
c.print();
delete []a;
}
else if(type=="S")
{
string *a=new string[n];
for(i=0;i<n;i++)
cin>>a[i];
list<string> c(n,a);
string insertv,changev;
cin>>insertp>>insertv>>returnp>>deletep>>changep>>changev;
c.insert(insertp,insertv);
if(returnp<=n)
cout<<c.get(returnp)<<endl;
else
cout<<"error"<<endl;
c.remove(deletep);
c.set(changev,changep);
c.print();
delete []a;
}
}
return 0;
}