链表-成员私有的写法
#include <iostream>
using namespace std;
class List;
class Node
{
friend class List;
private:
int data;
Node * next;
};
class List
{
public:
void test();
void show();
private:
Node * head;
};
void List::test(){
Node * node = new Node();
node->data = 44;
node->next = nullptr;
head = node;
node = new Node();
node->data = 66;
node->next = nullptr;
head->next = node;
}
void List::show() {
Node * node = head;
while (node!=nullptr)
{
cout << node->data<<endl;
node = node->next;
}
}
int main(int argc, char ** argv)
{
List * list = new List;
list->test();
list->show();
system("pause");
return 0;
}