鏈表介紹:
創(chuàng)新互聯(lián)建站技術(shù)團(tuán)隊(duì)十載來(lái)致力于為客戶提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作、品牌網(wǎng)站制作、成都全網(wǎng)營(yíng)銷推廣、搜索引擎SEO優(yōu)化等服務(wù)。經(jīng)過(guò)多年發(fā)展,公司擁有經(jīng)驗(yàn)豐富的技術(shù)團(tuán)隊(duì),先后服務(wù)、推廣了成百上千網(wǎng)站,包括各類中小企業(yè)、企事單位、高校等機(jī)構(gòu)單位。
鏈表概念--鏈表是一種線性表,但是并不是順序存儲(chǔ),而是每個(gè)節(jié)點(diǎn)里面存儲(chǔ)著下一個(gè)節(jié)點(diǎn)的指針,把存儲(chǔ)數(shù)據(jù)元素的數(shù)據(jù)串鏈起來(lái)。
單鏈表演示圖:
創(chuàng)建結(jié)構(gòu)體
typedef int DataType;
typedef struct ListNode
{
DataType data;
struct ListNode *pNext;
}SListNode, *PSListNode;
求環(huán)入口點(diǎn):
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;
}
判斷兩個(gè)鏈表
是否相交:
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);
//都不帶環(huán)
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;
}
//兩個(gè)都帶環(huán)
if (pL1MeetNode != NULL && pL2MeetNode != NULL)
pNode = pL1MeetNode;
{
while (pNode->pNext != pL1MeetNode)
{
if (pL2MeetNode == pNode)
{
return 1;
}
pNode = pNode->pNext;
}
return 0;
}
}