C++:单列表结点类基础应用

单链表结点类的基础应用

#include <iostream>
using namespace std;

//单链表结点类:
class Node{
    
    
	public://成员函数
	//this:类型为对象指针,指向当前对象
	void show(){
    
    
		cout << this->data;
		cout<<"," << this->next;
		cout << endl;
	}
	//private://成员变量
	int data;
	Node * next;//指向下一个结点的内存地址
};//;不能省略

int main()
{
    
    
	Node node0, node1;
	node0.data = 2020;
	node0.next = &node1;
	
	node1.data = 2021;
	node1.next = NULL;//最后一个结点的next指针置空 

	node0.show();//当前对象node0.访问成员用
	node1.show();//当前对象为node1
	
	//next类型为对象指针,访问成员->
	node0.next->show();
	node0.next->next->show();

	cout << &node1 << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_51354361/article/details/114779626