这是什么类型的C++语法,我应该采取什么步骤来理解这一点

What Type Of C++ Syntax Is This And What Steps Should I Take To Understand This

本文关键字:什么 这一点 类型 是什么 C++ 语法 我应该      更新时间:2023-10-16

首先,这是我在数据结构和算法课程中的第一个项目。我在第一年用过C++,但代码呈指数级"漂亮",从那以后我与 Swift 和 Javascript 进行了大量交互。这段代码对我来说是合法的,我很沮丧,因为我无法弄清楚如何做最简单的事情。

该项目是关于双向链表的,链表类和嵌套的 Node 类是

template <typename Type>
class Double_sentinel_list {
public:
class Double_node {
public:
//imutable reference to Type
Double_node( Type const & = Type(), Double_node * = nullptr, Double_node * = nullptr );
//Functions to return variables
Type value() const;
Double_node *previous() const;
Double_node *next() const;
//Variables
Type         node_value;
Double_node *previous_node;
Double_node *next_node;
};
Double_sentinel_list();
Double_sentinel_list( Double_sentinel_list const & );
Double_sentinel_list( Double_sentinel_list && );
~Double_sentinel_list();
// Accessors
int size() const;
bool empty() const;
Type front() const;
Type back() const;
Double_node *begin() const;
Double_node *end() const;
Double_node *rbegin() const;
Double_node *rend() const;
Double_node *find( Type const & ) const;
int count( Type const & ) const;
// Mutators
void swap( Double_sentinel_list & );
Double_sentinel_list &operator=( Double_sentinel_list );
Double_sentinel_list &operator=( Double_sentinel_list && );
void push_front( Type const & );
void push_back( Type const & );
void pop_front();
void pop_back();
int erase( Type const & );
private:
Double_node *list_head; //looks like head/tail pointers are nodes as well..
Double_node *list_tail;
int list_size;
// List any additional private member functions you author here
// Friends
template <typename T>
friend std::ostream &operator<<( std::ostream &, Double_sentinel_list<T> const & );
};

链表构造函数编写如下:

//Constructor
template <typename Type>
Double_sentinel_list<Type>::Double_sentinel_list():
// Updated the initialization list here
list_head( nullptr ),
list_tail( nullptr ),
list_size( 0 )
{
list_size = 0;
//Creating Sentinal Nodes
//Node* used so double node type points to the address that holds the new double_node()
Double_node* headSentinal = new Double_node(); //new calls constructor for double node
Double_node* tailSentinal = new Double_node();
//Interconnecting Sentinals
headSentinal->next_node = tailSentinal;
tailSentinal->previous_node = headSentinal;
//Assigning head/tail pointers to sentinal nodes
//arrow operator used to access the object that the pointer of the object is pointing to 
list_head->next_node = headSentinal;
list_tail->next_node = tailSentinal;
}

我相信我对正在发生的事情有一个大致的了解,但对于一些特定的函数调用,我不知道如何返回值。

例如,此类应该只返回列表中的下一个节点

template <typename Type>
typename Double_sentinel_list<Type>::Double_node   *Double_sentinel_list<Type>::Double_node::next() const {
// Enter your implementation here
return Double_node->next_node; ????
}

我一辈子都无法在这个函数声明中弄清楚要为返回值调用 ->next_node 的内容。如果我看得太久,整个事情看起来就像一团垃圾。见鬼,我什至如何在节点上调用此函数?

C++这里的初学者(仍在学习自己(,但我可以给你一些建议,希望有更多经验的人也能帮助你。

Double_node* tailSentinal = new Double_node();正在创建一个指向Double_node的指针。

模板

现在进入模板,所以template <typename Type> Double_sentinel_list<Type>::Double_sentinel_list():是一个模板,允许您创建一个类型的Double_sentinel_list的新实例,例如intfloat,因此您可以像Double_sentinel_list<int> myList;左右一样使用它。

至于为什么头部和反面Double_node的,是因为这个模板是双向链表的模板。我在下面包含了对双向链表的一个小解释。

在维基百科中,双向链表是一种链接的数据结构,由一组称为节点的顺序链接记录组成。每个节点包含两个称为链接的字段,它们是对节点序列中上一个节点和下一个节点的引用。

维基百科上的双链表: https://en.wikipedia.org/wiki/Doubly_linked_list

相关文章: