9判斷兩個單鏈表是否相交

//單鏈表交換任意兩個元素(不包括表頭)
#include <iostream>
using namespace std;

struct LinkNode
{
	LinkNode *pNext;
	int nValue;
	LinkNode(int value = 0)
	{
		nValue = value;
		pNext = NULL;
	}
};

class Linklist
{
public:
	Linklist(){ pHead = new LinkNode(); };
	~Linklist(){ delete pHead;}; 
	void InsertValue(int nValue);
	void Reverse();
	void Reverse2();
	LinkNode *FindLastNum(int nNum);
	LinkNode *FindMidNode(bool &IsOdd);
	void DeleteNode(LinkNode *pDel);
	Linklist* Mergelist(const Linklist &list) const;
	void SwapNode(LinkNode *p1, LinkNode *p2);
	bool IsCircle();
	int CircleLengh();
	bool IsIntersect(const Linklist &list);
	void Print();

public:
	LinkNode *pHead;
};


void Linklist::InsertValue(int nValue)
{
	if (pHead == NULL)
	{
		return;
	}
	LinkNode *pCur = pHead;
	while (pCur->pNext != NULL)
	{
		pCur = pCur->pNext;
	}

	LinkNode *pTmp = new LinkNode(nValue);
	pCur->pNext = pTmp;
}

void Linklist::Reverse()
{
	if (pHead == NULL)
	{
		return;
	}

	//特殊情況:如果鏈表爲空或者只有一個元素,直接返回
	if (pHead->pNext == NULL || pHead->pNext->pNext == NULL)
	{
		return;
	}

	LinkNode *pPre = pHead;
	LinkNode *pCur = pHead->pNext;
	LinkNode *pNext;
	
	while (pCur != NULL)
	{
		pNext = pCur->pNext;
		pCur->pNext = pPre;
		pPre = pCur;
		pCur = pNext;
	}
	
	pHead->pNext->pNext = NULL;
	pHead->pNext = pPre;
}

void Linklist::Reverse2()
{
	if (pHead == NULL)
	{
		return;
	}

	//特殊情況:如果鏈表爲空或者只有一個元素,直接返回
	if (pHead->pNext == NULL || pHead->pNext->pNext == NULL)
	{
		return;
	}

	LinkNode *pCur = pHead->pNext;
	LinkNode *pNext = NULL;
	LinkNode *pNextNext = NULL;
	
	while (pCur->pNext != NULL)
	{
		pNext = pCur->pNext;
		pNextNext = pNext->pNext;
		
		pNext->pNext = pHead->pNext;
		pHead->pNext = pNext;
		pCur->pNext = pNextNext;
	}
}

LinkNode *Linklist::FindLastNum(int nNum)
{
	if (pHead == NULL)
	{
		return NULL;
	}
	LinkNode *pFirst = pHead;
	int i = 0;
	while (i < nNum)
	{
		if (pFirst == NULL)
		{
			return NULL;
		}
		pFirst = pFirst->pNext;
		i++;
	}
	LinkNode *pSecond = pHead;
	while (pFirst != NULL)
	{
		pFirst = pFirst->pNext;
		pSecond = pSecond->pNext;
	}

	return pSecond;
}

//考慮鏈表個數爲奇數Odd、偶數Even兩種情況
LinkNode* Linklist::FindMidNode(bool &IsOdd)
{
	if (pHead == NULL)
	{
		return NULL;
	}
	LinkNode *pFirst = pHead;
	LinkNode *pSecond = pHead;

	while (pFirst != NULL && pFirst->pNext != NULL)
	{
		pFirst = pFirst->pNext->pNext;
		pSecond = pSecond->pNext;
	}
	if (pFirst == NULL)
	{
		IsOdd = true;
	}
	else
	{
		IsOdd = false;
	}
	return pSecond;
}

void Linklist::DeleteNode(LinkNode *pDel)
{
	if (pHead == NULL || pDel == NULL)
	{
		return;
	}

	//如果要刪除的節點不是最後一個節點
	if (pDel->pNext != NULL)
	{
		pDel->nValue = pDel->pNext->nValue;
		LinkNode *pTmp = pDel->pNext;
		pDel->pNext = pTmp->pNext;
		
		delete pTmp;
	}
	else
	{
		LinkNode *pCur = pHead->pNext;
		while (pCur != NULL)
		{
			if (pCur->pNext = pDel)
			{
				pCur->pNext = NULL;
				delete pDel;
			}
			else
			{
				pCur = pCur->pNext;
			}
		}
	}
}

Linklist*  Linklist::Mergelist(const Linklist &list) const
{
	Linklist *pList = new Linklist;
	if (pList == NULL || pHead == NULL || list.pHead == NULL)
	{
		return NULL;
	}

	LinkNode *pCur1 = pHead->pNext;
	LinkNode *pCur2 = list.pHead->pNext;
	LinkNode *pCur3 = pList->pHead;

	while (pCur1 && pCur2)
	{
		if (pCur1->nValue > pCur2->nValue)
		{
			pList->InsertValue(pCur2->nValue);
			pCur2 = pCur2->pNext;
		}
		else
		{
			pList->InsertValue(pCur1->nValue);
			pCur1 = pCur1->pNext;
		}
	}
	while (pCur1)
	{
		pList->InsertValue(pCur1->nValue);
		pCur1 = pCur1->pNext;
	}
	while (pCur2)
	{
		pList->InsertValue(pCur2->nValue);
		pCur2 = pCur2->pNext;
	}

	return pList;
}

//需要三個額外指針變量
void Linklist::SwapNode(LinkNode *p1, LinkNode *p2)
{
	if (pHead == NULL || p1 == NULL || p2 == NULL)
	{
		return;
	}
	if (p1 == p2)
	{
		return;
	}
	LinkNode *pCur = pHead->pNext;
	LinkNode *pCur1 = p1;
	LinkNode *pCur2 = p2;
	LinkNode *pPre1 = NULL;
	LinkNode *pPre2 = NULL;
	int nCount = 0;
	while (pCur)
	{
		if (pCur->pNext == pCur1)
		{
			pPre1 = pCur;
			nCount++;
			if (nCount == 2)
			{
				break;
			}
		}
		if (pCur->pNext == pCur2)
		{
			pPre2 = pCur;
			nCount++;
			if (nCount == 2)
			{
				break;
			}
		}
		pCur = pCur->pNext;
	}
	pCur = pCur1->pNext;

	pPre1->pNext = pCur2;
	pCur1->pNext = pCur2->pNext;
	pPre2->pNext = pCur1;
	pCur2->pNext = pCur;
}

bool Linklist::IsCircle()
{
	if (pHead == NULL || pHead->pNext == NULL)
	{
		return false;
	}
	LinkNode *pFirst = pHead->pNext;
	LinkNode *pSecond = pHead->pNext;
	while (pFirst != NULL && pFirst->pNext != NULL)
	{
		pFirst = pFirst->pNext->pNext;
		pSecond = pSecond->pNext;
		if (pFirst == pSecond)
		{
			return true;
		}
	}
	return false;
}

int Linklist::CircleLengh()
{
	if (pHead == NULL)
	{
		return 0;
	}
	LinkNode *pCur = pHead->pNext;
	int nLen = 0;
	if (pCur)
	{
		nLen++;
		pCur = pCur->pNext;
	}
	
	while (pCur != pHead->pNext)
	{
		nLen++;
		pCur = pCur->pNext;
	}

	return nLen;
}

/*
//方法1:如果兩鏈表末尾元素相同,必相交

bool Linklist::IsIntersect(const Linklist &list)
{
	if (pHead == NULL || list.pHead == NULL)
	{
		return false;
	}
	LinkNode *pCur1 = pHead;
	LinkNode *pCur2 = list.pHead;

	while (pCur1->pNext)
	{
		pCur1 = pCur1->pNext;
	}
	while (pCur2->pNext)
	{
		pCur2 = pCur2->pNext;
	}

	return pCur1 == pCur2;
}
*/

//方法2:把一個鏈表 A 接在另一個鏈表 B 的末尾,如果有環,則必然相交。如
//		 何判斷有環呢?從 A 開始遍歷,如果能回到 A 的表頭,則肯定有環。
bool Linklist::IsIntersect(const Linklist &list)
{
	if (pHead == NULL || list.pHead == NULL)
	{
		return false;
	}
	LinkNode *pCur1 = pHead;
	LinkNode *pCur2 = list.pHead;

	//到達鏈表1的末尾
	while (pCur1->pNext)
	{
		pCur1 = pCur1->pNext;
	}
	pCur1->pNext = pCur2;
	
	while (pCur2->pNext)
	{
		if (pCur2->pNext == list.pHead)
		{
			pCur1->pNext = NULL;	//斷開環
			return true;
		}
		pCur2 = pCur2->pNext;
	}

	pCur1->pNext = NULL;	//斷開環
	return false;
}

void Linklist::Print()
{
	if (pHead == NULL)
	{
		return;
	}
	LinkNode *pCur = pHead->pNext;
	while (pCur != NULL)
	{
		cout<<pCur->nValue<<" ";
		pCur = pCur->pNext;
	}
	cout<<endl;
}


int main()
{	
	Linklist list1;
	Linklist list2;
	LinkNode p1(1);
	LinkNode p2(2);
	LinkNode p3(3);
	LinkNode p4(4);
	LinkNode p5(5);
	LinkNode p6(6);
	LinkNode p7(7);
	p1.pNext = &p2;
	p2.pNext = &p5;
	p3.pNext = &p4;
	p4.pNext = &p5;
	p5.pNext = &p6;
	p6.pNext = &p7;

	list1.pHead->pNext = &p1;
	list2.pHead->pNext = &p3;
	if (list1.IsIntersect(list2))
	{
		cout<<"相交"<<endl;
	}
	else
	{
		cout<<"不相交"<<endl;
	}
	
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章