#include <iostream>
using namespace std;
template <class elemType>
class linkList{
private:
struct node{
elemType data;
node *next;
node(const elemType x,node *p=NULL){data=x; next=p;}
node():next(NULL){}
~node(){}
};
node *head;
public:
linkList();
~linkList(){Clear(); delete head;}
void Clear();
void Insert(int i,const elemType &x) const;
void print() const;
linkList & operator+(const linkList &ls) ;
};
template <class elemType>
linkList<elemType>::linkList(){
head=new node();
}
template <class elemType>
void linkList<elemType>::Clear(){
node *p,*q;
p=head->next;
while (p){
q=p;
p=p->next;
delete q;
}
head->next=NULL;
}
template <class elemType>
void linkList<elemType>::Insert(int i,const elemType &x) const {
if (i<0) return;
node *p,*q;
p=head;
for (int j=0;j<i;j++)
if (p) p=p->next;
else break;
q=new node(x,p->next);
p->next=q;
}
template <class elemType>
void linkList<elemType>::print() const{
node *p;
p=head->next;
while(p){
cout<<p->data;
cout<<' ';
p=p->next;
}
}
template <class elemType>
linkList<elemType> &linkList<elemType>::operator+(const linkList &ls) {
node *p,*q;
p=head->next;
q=ls.head->next;
int i=0;
while (p) {
i++;
p=p->next;
}
while (q){
this->Insert(i,q->data);
q=q->next;
i++;
}
return *this;
}
int main()
{
char *p;
int n,m;
p=new char[6];
cin>>p;
cin>>n;
cin>>m;
switch(p[0])
{
case 'i':
{
linkList<int> li,ls;
int tmp;
for (int i=0;i<n;i++){
cin>>tmp;
li.Insert(i,tmp);
}
for (int j=0;j<m;j++){
cin>>tmp;
ls.Insert(j,tmp);
}
(li+ls);
li.print();
break;
}
case 'c':
{
linkList<char> li,ls;
char tmp;
for (int i=0;i<n;i++){
cin>>tmp;
li.Insert(i,tmp);
}
for (int j=0;j<m;j++){
cin>>tmp;
ls.Insert(j,tmp);
}
(li+ls).print();
break;
}
case 'd':
{
linkList<double> li,ls;
double tmp;
for (int i=0;i<n;i++){
cin>>tmp;
li.Insert(i,tmp);
}
for (int j=0;j<m;j++){
cin>>tmp;
ls.Insert(j,tmp);
}
(li+ls).print();
break;
}
default:
return 0;
}
return 0;
}
SJTU OJ (1203)
最新推荐文章于 2020-08-27 21:22:51 发布