13-G. 链表类模板

本文介绍了一个使用C++模板实现的通用链表类,该类能够处理不同数据类型(如int、double和string)。文章详细展示了如何从一个特定类型的链表(CIntList)扩展到一个通用模板类(CList),并提供了完整的代码示例和测试用例。

实验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;
}

面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List型的模板对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List型的模板对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List型的模板对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List型的模板对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
修改代码中的错误:#include<iostream> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MVNum 100 //最大顶点数 using namespace std; typedef struct ArcNode {//边结点 int adjvex; //邻接点域:该边所指向的顶点的位置 int data; //数据域:存储和边相关的信息 struct ArcNode* nextarc; //链域:指向下一条边的指针 }ArcNode; typedef struct VNode {//顶点信息 int data; //顶点结点的数据域 ArcNode *firstarc; //链域:指向第一条依附该顶点的边的指针 }VNode,AdjList[MVNum]; //AdjList表示邻接表型 typedef struct {//邻接表 AdjList vertices; int vexnum,arcnum; //图的当前顶点数和边数 }ALGragh; int CreateUDG(ALGragh &G,int vexnum,int arcnum) {//采用邻接表表示法,创建无向图G G.vexnum=vexnum; //输入总顶点数 G.arcnum=arcnum; //输入总边数 if(G.vexnum>MVNum) return ERROR; //超出最大顶点数则结束函数 for(int i=0;i<G.vexnum;++i) //构造表头结点表 { G.vertices[i].data=i+1; //输入顶点值 G.vertices[i].firstarc=NULL; //初始化表头结点的指针域为NULL } for(int j=0;j<G.arcnum;j++) //输入各边,构造邻接表 { int h,k; cin>>h>>k; //输入一条边依附的两个顶点h和k ArcNode* p1=new ArcNode; //生成一个新的边结点*p1 p1->adjvex=h-1; //邻接点序号为h-1,即顶点在G.vertices中的序号 p1->data=k; p1->nextarc=G.vertices[h-1].firstarc; //将新结点*p1插入顶点vh-1的边表头部 G.vertices[h-1].firstarc=p1; ArcNode* p2=new ArcNode; //生成一个新的边结点*p2 p2->adjvex=k-1; //邻接点序号为k-1,即顶点在G.vertices中的序号 p2->data=h; p2->nextarc=G.vertices[k-1].firstarc; //将新结点*p2插入顶点vk-1的边表头部 G.vertices[k-1].firstarc=p2; } return OK; } int InsertVex(ALGragh &G) {//在以邻接表形式存储的无向图G上插入顶点v /**************begin************/ int vet; void adddVertex(G.vertices *vet){ if(AdjList.count(vet)) return; AdjList[vet]=vector<G.vertices *>(); } /**************end************/ } int PrintGraph(ALGragh G) {//输出图G for(int i=0;i<G.vexnum;i++) { cout<<G.vertices[i].data; //先输出顶点信息 ArcNode* p=G.vertices[i].firstarc; //初始化指针p指向与顶点邻接的第一个边结点 if(p==NULL) //不存在与顶点邻接的第一个边结点 { cout<<endl; continue; } else //存在时遍历边表 { cout<<" "; while(p->nextarc) { cout<<p->data<<" "; p=p->nextarc; } cout<<p->data<<endl; } } return OK; } int main() { int n,m; while(cin>>n>>m) { if(n==0&&m==0) break; //输入结束标志 ALGragh G; CreateUDG(G,n,m); //采用邻接表表示法,创建无向图G InsertVex(G); //在图G中增添新顶点 PrintGraph(G); //输出图G } return 0; }
最新发布
07-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值