刪除單鏈表給定節點

//刪除單鏈表給定節點

#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);
	void Print();

private:
	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;
			}
		}
	}
}
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 list;
	int nArr[] = {12,13,43,1,2,4,55,23};
	for (int i = 0; i < 8; i++)
	{
		list.InsertValue(nArr[i]);
	}
	list.Print();
//	bool IsOdd;
//	LinkNode *pFind = list.FindMidNode(IsOdd);
//	if (pFind != NULL)
//	{
//		if (IsOdd)
//		{
//			cout<<pFind->nValue<<endl;
//		}
//		else
//		{
//			cout<<pFind->nValue<<" "<<pFind->pNext->nValue<<endl;
//		}
//		
//	}

	LinkNode *pFind = list.FindLastNum(3);
	if (pFind != NULL)
	{
		list.DeleteNode(pFind);
	}
	list.Print();
	return 0;
}

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