编辑 - 下面回答,错过了斜角的大括号.谢谢大家.
我一直试图写一个简单的单链表,我可以在其他程序中使用.我希望它能够使用内置和用户定义的类型,这意味着它必须是模板化的.
由于这个原因,我的节点也必须模板化,因为我不知道它将要存储的信息.我写了一个节点类如下 -
template <class T> class Node
{
T data; //the object information
Node* next; //pointer to the next node element
public:
//Methods omitted for brevity
};
Run Code Online (Sandbox Code Playgroud)
我的链表类是在一个单独的类中实现的,并且在将新节点添加到列表末尾时需要实例化一个节点.我已经实现了如下 -
#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 …
Run Code Online (Sandbox Code Playgroud)