单链表的环操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单链表的环操作相关的知识,希望对你有一定的参考价值。
链表介绍:
链表概念--链表是一种线性表,但是并不是顺序存储,而是每个节点里面存储着下一个节点的指针,把存储数据元素的数据串链起来。
创建结构体
typedef int DataType;
typedef struct ListNode
{
DataType data;
struct ListNode *pNext;
}SListNode, *PSListNode;
求环入口点:
PSListNode FindEnterNode(PSListNode pHead, PSListNode pMeetNode)
{
PSListNode pNode1 = pHead;
PSListNode pNodeM = pMeetNode;
if (pHead == NULL || pMeetNode == NULL)
{
return NULL;
}
while (pNode1 != pNodeM)
{
pNode1 = pNode1->pNext;
pNodeM = pNodeM->pNext;
}
return pNode1;
}
判断两个链表
是否相交:
int IsListCroseWithCycle(PSListNode pL1, PSListNode pL2)
{
PSListNode pL1MeetNode = NULL;
PSListNode pL2MeetNode = NULL;
PSListNode pL1Node = pL1;
PSListNode pL2Node = pL2;
PSListNode pNode = NULL;
if (pL1 == NULL || pL2 == NULL)
{
return 0;
}
pL1MeetNode = HasCycle(pL1Node);
pL2MeetNode = HasCycle(pL2Node);
//都不带环
if (pL1MeetNode == NULL && pL2MeetNode == NULL)
{
while (pL1Node->pNext != NULL)
{
pL1Node = pL1Node->pNext;
}
while (pL2Node->pNext != NULL)
{
pL2Node = pL2Node->pNext;
}
if (pL1Node == pL2Node)
{
return 1;
}
return 0;
}
//两个都带环
if (pL1MeetNode != NULL && pL2MeetNode != NULL)
pNode = pL1MeetNode;
{
while (pNode->pNext != pL1MeetNode)
{
if (pL2MeetNode == pNode)
{
return 1;
}
pNode = pNode->pNext;
}
return 0;
}
}
本文出自 “11275984” 博客,请务必保留此出处http://11285984.blog.51cto.com/11275984/1760551
以上是关于单链表的环操作的主要内容,如果未能解决你的问题,请参考以下文章