#include<stdlib.h>
#include<stdio.h>
int JudCircle(ElemSN *h)
{
ElemSN **s = NULL, *p = h->next, *q = h;
int i = 0, j;
s = (ElemSN
**)malloc(100 * sizeof(ElemSN*));//创建一个超级大的数组,用来存储链表结点地址
while (p&&p->next)//如果链表没有环,指针p一定会走到终点
{
s[i++] = q;//存储结点地址
//用指针p的地址跟数组里面的地址做比较,如果出现重复,则说明链表出现环状结构
for (j = 0; j < i; j++)
{
if (p == s[j])
{
return 1;
}
//if不成立,指针向后移。继续判断
q = p;
p = p->next;
}
}
return 0;
}
int main(void)
{
int a[8] = { 3,2,5,8,4,7,6,9 };
ElemSN *h1 = CreatLink1(a, 8);
ElemSN *h2 = CreatLink3(a, 8);
ElemSN *t = h1;
for (t; t->next; t = t->next);
//t->next = h2;
if (JudCircle(h1))
{
printf("有环结构\n");
}
else
{
printf("没有环结构\n");
}
system("pause");
return 0;
}