您好,登錄后才能下訂單哦!
鏈表介紹:
鏈表概念--鏈表是一種線性表,但是并不是順序存儲,而是每個節點里面存儲著下一個節點的指針,把存儲數據元素的數據串鏈起來。
單鏈表演示圖:
創建結構體
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;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。