Node(StructName x) :Struct(x), next(NULL) {} 是什么意思?
我怀疑你看的教材有错,结构体应该是没有构造器的。
这个本来应该是c++里面的 class的构造函数,struct结构体里面 应该没有构造函数才对。
冒号是赋值语句,冒号:后面是给class 里面的成员变量赋值Struct(x), next(NULL), x赋值给StructName Struct; NULL 赋值给Node *next;
我发给你我自己写的链表节点类吧。
#pragma once
#include "stdafx.h"
//结点类
template
class Node {
private:
T data;
Node* next;
//6个方法
public:
//构造器 2参
Node(T &elem, Node* ptr = NULL) :data(elem), next(ptr) {}
//构造器 1参
Node(Node* p = nullptr) :next(p) {}
//析构器
virtual ~Node() {}
//在当前结点之后插入指针p所指结点
void InsertAfter(Node*p) {
//c++对象调用成员函数自动加 this.指针
//this指的当前对象是Node类
//this.next省略了this
p->next = this.next;
this.next = p;
}
//删除当前结点的后继结点
Node* DeleteAfter(void) {
if (next == NULL)
return NULL;
Node* tempptr = this.next;
this.next = tempptr->next;
return tempptr;
}
//GetNext(),返回指向当前结点的后继结点的指针
Node* NextNode(void) const {
return this.next;
}
};//!_class Node
这是用结构体模仿C++类的写法。Node(StructName x)是这个结构体的构造函数,:后的称为成员初始化列表,实际上就是其他类(结构体的对象)。你不妨放一放,看了“对象成员”的内容后就自然清楚了……