关于基于模板实现的链表为什么编译通不过啊

biaoyiddd biaoyiddd 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <iostream>
#include <string>
using namespace std;
template<class T>
class List{
private:
struct node{
T data;
node* next;
}*head,*tail;
public:
List(){
head=tail=NULL;
}
~List(){
node* curr=head,*p;
while (curr!=NULL)
{
p=curr;
curr=curr->next;
delete p;
}
head=tail=NULL;
}
void inserthead(T *item){
node *p;
p=new node;
p->next=head;
p->data=*item;
head=p;
if(tail==NULL)
tail=p;
}
void inserttail(T * item){
node *p;
p=new node;
p->next=NULL;
p->data=*item;
if(head==NULL)
head=tail=p;
else
tail=p;
}
T get(){
T temp;
node *p=head;
temp=head->data;
if(head==tail)
head=tail=NULL;
else
head=head->next;
delete p;
return temp;
}
};

class Person{
private:
string name;
int score;
public:
Person(string n,int s):name(n),score(s){}
void show(){
cout<<"name is "<<name<<"/n"<<"score is "<<score<<endl;
}
};

void main()
{
Person p1("ldh",90),p2("lm",80),p3("lj",60);
List<int> l;
List<Person> lis=List<Person>();
lis.inserthead(&p1);
lis.inserthead(&p2);
lis.inserttail(&p3);
Person p=lis.get();
p.show();
}
...全文

相关推荐

实现一个模板类的链表(c++)

面向对象程序设计课程作业 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

武玲奈

C++单向链表类模板实现与应用

在数据结构的世界里,链表是一种常见的基础数据结构,被广泛应用于各种算法与程序设计中。单向链表,作为链表的一种,它的每个节点只包含一个指针,指向下一个节点。这使得单向链表在增加或删除元素时,相对数组等其他线性结构更加高效,因为它不需要移动数据,只需调整相关节点的指针即可完成操作。本章节主要目的是向读者介绍单向链表的基本概念,包括节点的定义、链表的组成原理以及基础操作等。我们将从单向链表的数据结构开始讲解,逐步深入到它的应用和实现细节。

大奇鸭

用c++类模板,实现的单链表基本操作

#include "stdafx.h" #include #include using namespace std; template struct LNode { T data; LNode *next; }; template class LinkList { public: LinkList(){L = 0;}//构造函数 ~LinkList();//析构函数,销毁单链表 void CreateList_L(int n);//构造单链表 bool IsEmpty() const{return L->next == 0;}//判断单链表是否为空 int GetElem_L(int i,T &e) const;//当第i个元素存在时,其值赋给e int ListInsert_L(int i,T &e);//在第i个位置插入元素e ……………………

zhongyuan2810

C++利用链表模板类实现简易队列

主要为大家详细介绍了C++利用链表模板类实现一个简易队列,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

weixin_38677936

C++实现链表模板(链表项的数据元素可以为任意类型):链表项的插入、删除、链表的打印、两个链表的连接VS2010

C++实现链表模板(链表项的数据元素可以为任意类型):链表项的插入、删除、链表的打印、两个链表的连接 开发环境为VS2010

JessicaWind
评论 4
收藏 已收藏
分享
返回顶部
biaoyiddd
C++ 语言 65,214 成员 250,514 内容