链表的将基本结构及用法

2023-04-12


/*
 * node_2.cpp
 *
 *  Created on: 2013年8月1日
 *      Author: Administrator
 */

#include 

using namespace std;

typedef int T;//给int起别名为T
struct Node {
	T data;
	Node* next;

	operator T() {
		return data;
	}
	Node(T d) :data(d), next(NULL) {

	}

};

void showList(Node* head) {

	Node* p = head;
	while (p != NULL) {
		cout << *p << ' ';
		/**
		 * (*p).next == p->next
		 * p:指向当前结构体的指针
		 * (*p):当前结构体
		 * p->next:结构体特有的访问成员变量的方法.其值为某个结构体的地址或者是NULL
		 * p=p->next :将p指向当前结构体的下一个结构体
		 *
		 */
		p = p->next;
	}

	cout << endl;
}


int main() {

	Node a(10), b(20), c(30), d(40),e(50),f(60);

	cout<next :k是结构体指针(指向结构体的指针)
	 * (*k).next :k是结构体指针(指向结构体的指针)
	 */
	k->next = c.next;
	c.next = k;

	showList(&a);


}




以下再贴一个简洁版的:



/*
 * node_3.cpp
 *
 *  Created on: 2013年8月1日
 *      Author: Administrator
 */

#include 


using namespace std;

typedef int T;
struct Node{
	T data;
	Node* next;

	operator T(){
		return data;
	}

	Node(T data):data(data),next(NULL){

	}
};

void showList(Node* head){
	Node* p = head;

	while(p!= NULL){

		cout<<(*p)<<' ';

		p=p->next;
	}
	cout<next = c.next;
	c.next = k;
	showList(&a);
}

































































本文仅代表作者观点,版权归原创者所有,如需转载请在文中注明来源及作者名字。

免责声明:本文系转载编辑文章,仅作分享之用。如分享内容、图片侵犯到您的版权或非授权发布,请及时与我们联系进行审核处理或删除,您可以发送材料至邮箱:service@tojoy.com