Linked List Related Exercises

#include<iostream>
#include<stack>
#include<assert.h>
using namespace std;

typedef struct Node
{
	Node * _pNext;
	int _data;
}*PNode;


//Initialize the linked list
void InitList (PNode * pHead)
{
	assert (pHead);
	* pHead = NULL;
}


/ / Find the middle node of the singly linked list, you can only traverse the linked list once
//Set a fast pointer and a slow pointer, when the fast pointer reaches the last node
//The position of the slow pointer is exactly the middle node
PNode FindMidNode (PNode pHead)
{
	PNode pSlow = pHead;
	PNode pFast = pHead;
	while (pFast && pFast->_pNext)
	{
		pSlow = pSlow->_pNext;
		pFast = pFast->_pNext->_pNext;
	}
	return pSlow;
}


/ / Find the k-th node from the bottom of the linked list, you can only traverse the linked list once
//Define a fast pointer and a slow pointer, let the fast pointer walk k nodes first than the slow pointer, and then walk together
//When the fast pointer goes to the last node, the position of the slow pointer is the kth node from the bottom
PNode FindLastKNode(PNode pHead, size_t k)
{
	if (NULL == pHead || 0 == k)
		return NULL;

	pNode pFront = NULL;
	PNode pBack = NULL;

	while (--k)
	{
		if (NULL == pFront)
			return NULL;

		pFront = pFront->_pNext;
	}
	while (pFront)
	{
		pFront = pFront->_pNext;
		pBack = pBack->_pNext;
	}
	return pBack;
}

//Delete the k-th node from the bottom of the linked list, you can only traverse the linked list once
void DeleteLastKNode(PNode* pHead, size_t k)
{
	assert (pHead);
	if (NULL == *pHead || 0 == k)
		return ;
	//find the kth node
	if (NULL == pHead || 0 == k)
		return ;

	pNode pFront = NULL;
	PNode pBack = NULL;

	while (k--)
	{
		if (NULL == pFront)
			return ;

		pFront = pFront->_pNext;
	}
	PNode pPreBack = NULL;
	while (pFront)
	{
		pFront = pFront->_pNext;
		pPreBack = pBack;
		pBack = pBack->_pNext;
	}

	//delete
	if (pBack == * pHead)
	{
		* pHead = (* pHead) -> _ pNext;
		delete pBack;
	}
	else
	{
		pPreBack->_pNext = pBack->_pNext;
		delete pBack;
	}

}

/ / Determine whether the singly linked list has a ring
//Define a fast pointer and a slow pointer. If the linked watch has a ring, the fast pointer can catch up with the slow pointer in one circle
HasCircle bool (PNode pHead)
{
	PNode pFast = pHead;
	PNode pSlow = pHead;

	while (pFast&&pSlow->_pNext)
	{
		if (pFast == pSlow)
			return true;

		pFast = pFast->_pNext->_pNext;
		pSlow = pSlow->_pNext;
	}
}


// find the length of the ring
//Define an encounter node, then let the linked list go backwards, define a count++
//When the linked list pointer reaches the encounter node, the number of count is the number of nodes
int GetCircleLen(PNode pMeetNode)
{
	if (NULL == pMeetNode)
		return 0;

	int count = 1;
	PNode pCur = pMeetNode;
	while (pCur->_pNext != pMeetNode)
	{
		count++;
		pCur = pCur->_pNext;
	}
	return count;
}

//find the entry node of the ring
//Idea reference formula
	PNode GetEnterNode(PNode pHead, PNode pMeetNode)
	{
		if (NULL == pHead || NULL == pMeetNode)
			return NULL;

		PNode pH = pHead;
		PNode pM = pMeetNode;

		while (pH != pM)
		{
			pH = pH->_pNext;
			pM = pM->_pNext;
		}
		return pM;
	}


// Determine if the linked list intersects
	bool IsListCross(PNode pHead1, PNode pHead2)
	{
		if (NULL == pHead1 || NULL == pHead2)
			return false;

		PNode pLastNode1 = pHead1;
		while (pLastNode1->_pNext)
			pLastNode1 = pLastNode1->_pNext;

		PNode pLastNode2 = pHead2;
		while (pLastNode2->_pNext)
			pLastNode2 = pLastNode2->_pNext;

		if (pLastNode2 == pLastNode2)
			return true;

		return false;
	}


// find the length of the ring
	int Size(PNode pHead)
	{
		int count = 0;
		PNode pCur = pHead;
		while (pCur)
		{
			count++;
			pCur = pCur->_pNext;
		}
		return count;
	}

//find the intersection of two rings
	PNode GetCrossNode(PNode pHead1, PNode pHead2)
	{
		if (!IsListCross(pHead1, pHead2))
			return NULL;

		int len1 = Size(pHead1);
		int len2 = Size(pHead2);
		int gap = len1 - len2;

		PNode pCur1 = pHead1;
		PNode pCur2 = pHead2;
		//head1 is long
		if (gap >= 0)
		{
			while (gap--)
				pCur1 = pCur1->_pNext;
		}
		//head2 is long
		else
		{
			while (gap++)
				pCur2 = pCur2->_pNext;
		}
		while (pCur1 != pCur2)
		{
			pCur1 = pCur1->_pNext;
			pCur2 = pCur2->_pNext;
		}
		return pCur1;
	}


//Print the linked list in reverse order 1 -> 2 -> 3 -> 4 -> NULL
//Method 1: recursion, sub-cases: 1. The linked list exists 2. The linked list does not exist
/ * void PrintListFromTail2Head (PNode pHead)
{
	if (pHead)
	{
		PrintListFromTail2Head (pHead -> _ pNext);
		cout << pHead->_data << "" << endl;
	}
	else
}*/

//Method 2: Loop. stack: last in first out
void PrintListFromTail2Head (PNode pHead)
{
	if (NULL == pHead)
		return;

	stack<Node*> s;
	PNode pCur = pHead;
	while (pCur)
	{
		s.push(pCur);
		pCur = pCur->_pNext;
	}
	while (!s.empty())
	{
		pCur = s.top();
		cout << pCur << "";
		s.pop();
	}
}



//Delete the non-tail node of a headless singly linked list (cannot traverse the linked list)
//1->2->3->NULL replacement
void Delete(PNode pos,PNode pdel)
{
	assert(pos);
	pos->_data = pdel->_data;
	pos->_pNext = pdel->_pNext;
	delete pdel;
}




//Insert a node before a non-head node in a headless singly linked list (cannot traverse the linked list)
void InsertNode(PNode pos)
{
	assert(pos);
	 PNode pNewNode = new Node;
	 pNewNode->_pNext = pos->_pNext;
	 pos->_pNext = pNewNode;
}


//header
void PushFront (PNode pHead)
{
	PNode pNewNode = new Node;
	pNewNode -> _ pNext = pHead;
	pHead = pNewNode;
}


//return the last node
PNode Back (PNode pHead)
{
	if (NULL == pHead)
		return NULL;

	PNode pCur = pHead;
	while (pCur->_pNext)
		pCur = pCur->_pNext;

	return pCur;
}


//Single linked list implements Joseph ring 1->2->3->4->5->6->NULL
PNode JosephCircle(PNode& pHead, int M)
{
	// form a singly linked list into a ring
	Back (pHead) -> _ pNext = pHead;
	Node * pCur = pHead;
	while (pCur->_pNext == pCur)
	{
		//1. Number of reports
		while (--M)
			pCur = pCur->_pNext;
		//2. Delete (replacement method)
		PNode pDel = pCur->_pNext;
		pCur->_data = pDel->_data;
		pCur->_pNext = pDel->_pNext;
		delete pDel;
	}
	//unwind
	pCur->_pNext = NULL;
	pHead = pCur;
	return pCur;
}




//Invert/reverse the singly linked list 1->2->3->4->NULL, three pointers
//Method 1: Replacement method
/*
PNode ReserverList(PNode& pHead)
{
	if (pHead == NULL || NULL == pHead -> _ pNext)
		return pHead;

	PNode pCur = pHead;
	PNode pCurPre = NULL;
	PNode pCurNext = pCur->_pNext;
	while (pCurNext)
	{
		pCur->_pNext = pCurPre;
		pCurPre = pCur;
		pCur = pCurNext;
		pCurNext = pCur->_pNext;
	}
	pCur->_pNext = NULL;
	pHead = pCur;
}
*/




//Method 2: Build a new linked list and insert the original linked list header
PNode ReserverList(PNode& pHead)
{
	pNode pNewNode = NULL;
	PNode pCur = pHead;
	PNode pCurNext = NULL;
	while (pCur)
	{
		pCurNext = pCur->_pNext;
		pCur->_pNext = pNewNode;
		pNewNode = pCur;
		pCur = pCurNext;
	}
	return pNewNode;
}




//Bubble Sort
void BubbleSort(PNode pHead)
{
	if (pHead == NULL || NULL == pHead -> _ pNext)
		return;
	PNode pCur =NULL ;
	PNode pCurNext = NULL;
	PNode pTail = NULL;
	bool isChange = false;
	while (pHead != pTail)
	{
		while (pCurNext != pTail)
		{
			if (pCur->_data > pCurNext->_data)
			{
				swap(pCur->_data, pCurNext->_data);
				isChange = true;
			}
		}
		pCur = pCurNext;
		pCurNext = pCur->_pNext;
	}
	pTail = pCur;
	if (!isChange)
		return;
}



//Merge the ordered list, and it is still in order after the merger
PNode MergeList (PNode pHead1, PNode pHead2)
{
	//Method 1:
	if (pHead1 == NULL)
		return pHead2;
	if (pHead2 == NULL)
		return pHead1;
   //Method 2:
	/*
	if (NULL == pHead1 || NULL == pHead2)
		return (NULL == pHead1) ? pHead2 : pHead1;
		*/
	PNode pL1 = pHead1;
	PNode pL2 = pHead2;
	pNode pNewHead = NULL;
	PNode pTail = NULL;
	if (pL1->_data <= pL2->_data)
	{
		pNewHead = pL1;
		pTail = pNewHead;
		pL1 = pL1->_pNext;
	}
	while (pL1 && pL2)
	{
		if (pL1->_data <= pL2->_data)
		{
			pTail->_pNext = pL1;
			pL1 = pL1->_pNext;
		}
		else
		{
			pTail->_pNext = pL2;
			pL2 = pL2->_pNext;
		}
		pTail = pTail->_pNext;
	}
	if (pL1)
		pTail->_pNext = pL1;
	if (pL2)
		pTail->_pNext = pL2;
}




//recursively destroy the linked list
void Destroy(PNode& pHead)
{
	if (pHead)
	{
		Destroy(pHead->_pNext);
		delete pHead;
	}
}




//Recursively find whether the element exists
bool Find(PNode pHead, int data)
{
	if (pHead->_pNext)
	{
		while (pHead->_data == data)
		{
			pHead = pHead -> _ pNext;
		}
		return;
	}
}




intmain()
{
	PNode pHead = NULL;
	InitList (& pHead);
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325889026&siteId=291194637