结构之外的节点 * head 有什么作用?

What does Node * head outside of the struct do?

本文关键字:什么 作用 head 节点 结构      更新时间:2023-10-16

所以我有类LinkedList。有人可以解释为什么节点 * head 在结构体之外,节点 * 下一个,上一个在结构体内部吗?这样做的目的是什么,它与其他目的有何不同?谢谢

class LinkedList
{
public:
LinkedList();
~LinkedList();
void add(char ch);
bool find(char ch);
bool del(char ch);
void display(
private:
struct node
{
char data;
node * next;
node * prev;          
};
node *head;
};

struct node
{
char data;
node * next;
node * prev;          
};

是类中结构类型的内部声明。

node *head;

是类LinkedList的类型struct node *的数据成员的声明。为了使其更清晰,请按以下方式重写列表定义

struct node
{
char data;
node * next;
node * prev;          
};
class LinkedList
{
public:
LinkedList();
~LinkedList();
void add(char ch);
bool find(char ch);
bool del(char ch);
void display(
private:
node *head;
};

将类定义中的结构声明为私有声明会使结构类型对链表的用户不可见。