SJTU OJ (1203)

本文介绍了一个使用C++模板实现的链表类,该类支持整数、字符和浮点数类型的数据插入、打印及链表连接操作。通过具体实例展示了如何创建链表并进行合并。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值