parent
53db099d11
commit
7304fbee9a
@ -0,0 +1,40 @@
|
||||
#ifndef NODE_HPP
|
||||
#define NODE_HPP
|
||||
|
||||
template <typename T>
|
||||
class List; // 友元类的前置声明
|
||||
|
||||
template <typename T>
|
||||
class Node
|
||||
{
|
||||
T info; // 数据域
|
||||
Node<T> *link; // 指针域,注意结点类格式,类模板实例化为类
|
||||
public:
|
||||
Node() : link{nullptr} {} // 默认构造函数,用于构建头节点
|
||||
Node(const T &data)
|
||||
: info{data}, link{nullptr} {} // 生成一般结点的构造函数
|
||||
Node(const Node &) = delete;
|
||||
Node &operator=(const Node &) = delete;
|
||||
void InsertAfter(Node<T> *p) // 在当前结点后插入一个结点
|
||||
{
|
||||
p->link = link;
|
||||
link = p;
|
||||
}
|
||||
Node<T> *RemoveAfter() // 删除当前结点的后继结点并返回其地址
|
||||
{
|
||||
if (link) // 判断节点后是否有节点
|
||||
{
|
||||
Node<T> *tempP{link}; // 获取后继节点
|
||||
link = tempP->link;
|
||||
return tempP;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
void print()
|
||||
{
|
||||
std::cout << info << '\t'; // 需要info支持插入运算符重载
|
||||
}
|
||||
friend class List<T>; // 以List为友元类
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in new issue