C++ C++;模板-链接列表
编辑--回答如下,遗漏了角括号。谢谢大家 我一直在尝试编写一个基本的单链表,我可以在其他程序中使用它。我希望它能够与内置和用户定义的类型一起工作,这意味着它必须被模板化 由于这个原因,我的节点也必须被模板化,因为我不知道它将要存储的信息。我编写了一个节点类,如下所示-C++ C++;模板-链接列表,c++,templates,linked-list,C++,Templates,Linked List,编辑--回答如下,遗漏了角括号。谢谢大家 我一直在尝试编写一个基本的单链表,我可以在其他程序中使用它。我希望它能够与内置和用户定义的类型一起工作,这意味着它必须被模板化 由于这个原因,我的节点也必须被模板化,因为我不知道它将要存储的信息。我编写了一个节点类,如下所示- template <class T> class Node { T data; //the object information Node* next; //pointer to the next no
template <class T> class Node
{
T data; //the object information
Node* next; //pointer to the next node element
public:
//Methods omitted for brevity
};
#include <iostream>
#include "Node.h"
using namespace std;
template <class T> class CustomLinkedList
{
Node<T> *head, *tail;
public:
CustomLinkedList()
{
head = NULL;
tail = NULL;
}
~CustomLinkedList()
{
}
//Method adds info to the end of the list
void add(T info)
{
if(head == NULL) //if our list is currently empty
{
head = new Node<T>; //Create new node of type T
head->setData(info);
tail = head;
}
else //if not empty add to the end and move the tail
{
Node* temp = new Node<T>;
temp->setData(info);
temp->setNextNull();
tail->setNext(temp);
tail = tail->getNext();
}
}
//print method omitted
};
#include "CustomLinkedList.h"
using namespace std;
int main()
{
CustomLinkedList<int> firstList;
firstList.add(32);
firstList.printlist();
//Pause the program until input is received
int i;
cin >> i;
return 0;
}
模板类节点
{
T data;//对象信息
Node*next;//指向下一个节点元素的指针
公众:
//为简洁起见省略了方法
};
我的链表类是在一个单独的类中实现的,在向列表末尾添加新节点时需要实例化一个节点。我已按如下方式实现了这一点-
template <class T> class Node
{
T data; //the object information
Node* next; //pointer to the next node element
public:
//Methods omitted for brevity
};
#include <iostream>
#include "Node.h"
using namespace std;
template <class T> class CustomLinkedList
{
Node<T> *head, *tail;
public:
CustomLinkedList()
{
head = NULL;
tail = NULL;
}
~CustomLinkedList()
{
}
//Method adds info to the end of the list
void add(T info)
{
if(head == NULL) //if our list is currently empty
{
head = new Node<T>; //Create new node of type T
head->setData(info);
tail = head;
}
else //if not empty add to the end and move the tail
{
Node* temp = new Node<T>;
temp->setData(info);
temp->setNextNull();
tail->setNext(temp);
tail = tail->getNext();
}
}
//print method omitted
};
#include "CustomLinkedList.h"
using namespace std;
int main()
{
CustomLinkedList<int> firstList;
firstList.add(32);
firstList.printlist();
//Pause the program until input is received
int i;
cin >> i;
return 0;
}
#包括
#包括“Node.h”
使用名称空间std;
模板类CustomLinkedList
{
节点*头,*尾;
公众:
CustomLinkedList()
{
head=NULL;
tail=NULL;
}
~CustomLinkedList()
{
}
//方法将信息添加到列表的末尾
无效添加(T信息)
{
if(head==NULL)//如果我们的列表当前为空
{
head=新节点;//创建T类型的新节点
头部->设置数据(信息);
尾=头;
}
else//如果不是空的,则添加到末尾并移动尾部
{
Node*temp=新节点;
临时->设置数据(信息);
temp->setNextNull();
尾部->设置下一步(温度);
tail=tail->getNext();
}
}
//省略打印方法
};
我已经设置了一个驱动程序/测试类,如下所示-
template <class T> class Node
{
T data; //the object information
Node* next; //pointer to the next node element
public:
//Methods omitted for brevity
};
#include <iostream>
#include "Node.h"
using namespace std;
template <class T> class CustomLinkedList
{
Node<T> *head, *tail;
public:
CustomLinkedList()
{
head = NULL;
tail = NULL;
}
~CustomLinkedList()
{
}
//Method adds info to the end of the list
void add(T info)
{
if(head == NULL) //if our list is currently empty
{
head = new Node<T>; //Create new node of type T
head->setData(info);
tail = head;
}
else //if not empty add to the end and move the tail
{
Node* temp = new Node<T>;
temp->setData(info);
temp->setNextNull();
tail->setNext(temp);
tail = tail->getNext();
}
}
//print method omitted
};
#include "CustomLinkedList.h"
using namespace std;
int main()
{
CustomLinkedList<int> firstList;
firstList.add(32);
firstList.printlist();
//Pause the program until input is received
int i;
cin >> i;
return 0;
}
#包括“CustomLinkedList.h”
使用名称空间std;
int main()
{
CustomLinkedList firstList;
第一个清单。添加(32);
printlist();
//暂停程序,直到收到输入
int i;
cin>>i;
返回0;
}
编译时出现错误-错误C2955:“节点”:使用类模板需要模板参数列表--这将指向add方法中的以下代码行-
Node* temp = new Node<T>;
Node*temp=新节点;
我不明白为什么它没有关于类型的信息,因为它是在我的驱动程序类中创建时传递给链表的我应该如何将类型信息传递给节点?
我是否应该创建一个私有节点结构而不是一个单独的类,并将两个类的方法合并到一个文件中?我不确定这是否能克服这个问题,但我认为可能。如果可能的话,我宁愿有单独的课程
谢谢,安德鲁。可能想试试
Node<T>* temp = new Node<T>;
Node*temp=新节点;
另外,为了获得关于如何设计列表的提示,您当然可以查看std::list,尽管它有时会有点令人生畏
Node<T>* temp = new Node<T>;
Node*temp=新节点;
节点类中的
下一个指针也是如此。如前所述,解决方案是
Node<T>* temp = new Node<T>;
Node*temp=新节点;
。。。因为节点
本身不是一种类型,节点
是一种类型。您需要:
Node<T> *temp = new Node<T>;
Node*temp=新节点;
可能值得在CustomLinkedList
类中使用typedef NodeType=Node
,以防止再次出现此问题。并且您还需要在printlist中为Node*temp指定模板参数。虽然已经提供了答案,但我想我还是要补充一点
在设计templates类时,最好不要在任何地方重复模板参数,以防您希望(某一天)更改特定的细节。通常,这是通过使用typedefs完成的
template <class T>
class Node
{
public:
// bunch of types
typedef T value_type;
typedef T& reference_type;
typedef T const& const_reference_type;
typedef T* pointer_type;
typedef T const* const_pointer_type;
// From now on, T should never appear
private:
value_type m_value;
Node* m_next;
};
template <class T>
class List
{
// private, no need to expose implementation
typedef Node<T> node_type;
// From now on, T should never appear
typedef node_type* node_pointer;
public:
typedef typename node_type::value_type value_type;
typedef typename node_type::reference_type reference_type;
typedef typename node_type::const_reference_type const_reference_type;
// ...
void add(value_type info);
private:
node_pointer m_head, m_tail;
};
请注意,使用适当的构造函数这一简单事实是如何提高我们的异常安全性的:如果在构造函数期间抛出任何东西,则需要new
不分配任何内存,因此不会泄漏任何内容,我们还没有执行任何操作。我们的List::insert
方法现在具有弹性
最后问题:
通常的insert
单链表方法在开头插入,因为这样更容易:
template <class T>
typename List<T>::iterator insert(value_type info)
{
m_head = new node_type(info, m_head); // if this throws, m_head is left unmodified
return iterator(m_head);
}
模板
类型名称列表::迭代器插入(值\类型信息)
{
m_head=new node_type(info,m_head);//如果抛出,m_head保持不变
返回迭代器(m_头);
}
是否确实要在结尾插入?还是因为在传统的向量和列表上使用了
push_-back
方法,才这样做?//文件:main.cc
// file: main.cc
#include "linkedlist.h"
int main(int argc, char *argv[]) {
LinkedList<int> list;
for(int i = 1; i < 10; i++) list.add(i);
list.print();
}
// file: node.h
#ifndef _NODE_H
#define _NODE_H
template<typename T> class LinkedList;
template<typename T>class Node {
friend class LinkedList<T>;
public:
Node(T data = 0, Node<T> *next = 0)
: data(data), next(next)
{ /* vacio */ }
private:
T data;
Node<T> *next;
};
#endif//_NODE_H
// file: linkedlist.h
#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H
#include <iostream>
using namespace std;
#include "node.h"
template<typename T> class LinkedList {
public:
LinkedList();
~LinkedList();
void add(T);
void print();
private:
Node<T> *head;
Node<T> *tail;
};
#endif//_LINKEDLIST_H
template<typename T>LinkedList<T>::LinkedList()
: head(0), tail(0)
{ /* empty */ }
template<typename T>LinkedList<T>::~LinkedList() {
if(head) {
Node<T> *p = head;
Node<T> *q = 0;
while(p) {
q = p;
p = p->next;
delete q;
}
cout << endl;
}
}
template<typename T>LinkedList<T>::void add(T info) {
if(head) {
tail->next = new Node<T>(info);
tail = tail->next;
} else {
head = tail = new Node<T>(info);
}
}
template<typename T>LinkedList<T>::void print() {
if(head) {
Node<T> *p = head;
while(p) {
cout << p->data << "-> ";
p = p->next;
}
cout << endl;
}
}
#包括“linkedlist.h”
int main(int argc,char*argv[]){
链接列表;
对于(inti=1;i<10;i++)列表,添加(i);
list.print();
}
//文件:node.h
#ifndef\u节点\u H
#定义节点
模板类链接列表;
templateclass节点{
朋友类链接列表;
公众:
节点(T数据=0,节点*next=0)
:数据(数据),下一个(下一个)
{/*vacio*/}
私人:
T数据;
节点*下一步;
};
#endif/\u节点\u H
//文件:linkedlist.h
#ifndef链接列表
#定义链接列表
#包括
使用名称空间std;
#包括“node.h”
模板类链接列表{
公众:
LinkedList();
~LinkedList();
无效添加(T);
作废打印();
私人:
节点*头;
节点*尾部;
};
#endif/\u链接列表\u H
templateLinkedList::LinkedList()
:头部(0),尾部(0)
{/*空*/}
templateLinkedList::~LinkedList(){
若有(总目){
节点*p=头部;
节点*q=0;
while(p){
q=p;
p=p->next;
删除q;
}
cout next=新节点(信息);
tail=tail->next;
}否则{
头部=尾部=新节点(信息);
}
}
templateLinkedList::void print(){
若有(总目){
节点*p=头部;
while(p){
下一步是收集数据;
}
cout您应该以这种方式添加新节点
Node*temp=新节点;
希望你能解决:)#包括
使用na