就以张三李四两人关系来看下循环链表 ,你可以再加一个人王五并停止无限循环
#include <iostream>
#include <string.h>
typedef struct link_list_s{
//张三记住了李四的信息
char name[25];//张三
link_list_s *address;//张三记住李四的信息
}link_list_t;
void test2() {
//本人张三
link_list_t *zhangsan = new link_list_t;
strcpy(zhangsan->name, "张三");
zhangsan->address = NULL;//没有认识人就不用这一块
//本人李四
link_list_t *lisi = new link_list_t;
strcpy(lisi->name, "李四");
lisi->address = NULL;//没有认识人就不用这一块
//两人开始认识了
//张三记住李四,也属于张三的一部分
zhangsan->address = lisi;
//李四记住张三,也属于李四的一部分
lisi->address = zhangsan;
//就像两人认识一样,你知道他的名字他知道你的名字,所以就是一样的link_list_t结构
std::cout << zhangsan->name << ", " << zhangsan->address->name << std::endl;
std::cout << lisi->name << ", " << lisi->address->name << std::endl;
//其实这就形成了一个环
//有人问张三你认识谁,只认识李四,你又找到李四问他认识谁,只认识张三,然后你又找到张三问
//就这样无限循环反复问下去address->address->address->address->address,所以可以无限指下去
//下面其实就是他们自己问自己认识谁,找到对方的一个反复过程
std::cout << zhangsan->name << ", " << zhangsan->address->address->address->address->address->name << std::endl;
std::cout << lisi->name << ", " << lisi->address->address->address->address->address->name << std::endl;
}
int main() {
test2();
return 0;
}