C++语言数据结构代码

# include <stdio.h>
# include <iostream>
# include <Windows.h>
using namespace std;
typedef int T;
struct Node
{
	T data;
	Node * next;
	//构造函数,其构造函数是公开的
	Node(const T& d):data(d),next(NULL){}
	//将a、b数据转换成T类型的数据,相当于Java方法toString
	
	operator T()
	{
		return data;
	};
};
void showList(Node * head){
	Node *p = head;
	while(p!=NULL){
	    cout<<(*p)<<endl;
		p = (*p).next;
		//
		//p=p->next;
	}
	cout<<""<<endl;
}
int main ()
{
	Node a(10),b(20),c(30),d(40),e(50),f(60);
	a.next=&b;
	b.next=&c;
	c.next=&d;
	d.next=&e;

	Node *p=&a;
	//下面是引用的使用
	//Node* &p = b.next;

	showList(&a);

	/*cout << a << " ";


	while(p!=NULL){
	    cout<<(*p)<<endl;
		p = (*p).next;
	}
	cout<<endl;*/

	e.next=b.next;           //&c
//	b.next=&e;

	showList(&a);
	//while(...)
	//{
	//	cout << (*p)<<" "<<endl;
	//}
	//cout << "a="<<a<<",b="<<b<< endl;
	//Node b={20,NULL}


	Node *&q = a.next;
	f.next=q;
	q=&f;
	showList(&a);

	MessageBoxA(0,"结构体","QQ",0);
	while(1)
	{
	}
	return 0;
}

 

 

你可能感兴趣的:(C++语言数据结构代码)