链表介绍:
链表概念--链表是一种线性表,但是并不是顺序存储,而是每个节点里面存储着下一个节点的指针,把存储数据元素的数据串链起来。
单链表演示图:
创建结构体
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;
}
}