打印与判断环形链表
打印环形链表
void showitall(node *phead)
{
node *p = phead1;
for(; p->pNext!=phead; p = p->pNext)
{
printf("%d", p->data)
}
printf("%d", p->data);
}
void main()
{
node *phead = NULL;
phead = addback(phead1, 1);
phead = addback(phead1, 2);
phead = addback(phead1, 3);
phead = addback(phead1, 4);
phead = addback(phead1, 5);
phead = addback(phead1, 6);
phead = addback(phead1, 7);
node *p = phead1;
for(; p->pNext!=NULL; p=p->pNext)
{
}
p->pNext = phead;
showitall(phead);
}
判断一个单链表是否有环;
用两个指针,一个前进一步,一个前进两布,如果有重合则有环
int isit(node *phead)
{
int flag = 0;
for(node *p1 = phead1, *p2 = phead1; p1 != NULL&&p2 != NULL; p1 = p1->pNext,p2 = p2->pNext)
{
if(p2->pNext!=NULL)
{
p2 = p2->pNext;
}
if(p1 == p2)
{
flag = 1;
break;
}
}
return flag;
}