链表的基本操作可以参考上一篇博文
链表的基本操作
// 逆序打印单链表
void ReverseListPrint(PNode PHead)
{
if (NULL == PHead)
return;
if (PHead)
{
ReverseListPrint(PHead->_pNext);
printf("%d ", PHead->_data);
}
}
// 查找链表的中间结点,要求只能遍历一次链表
PNode FindMiddleNode(PNode PHead)
{
PNode slow = NULL;
if (NULL == PHead)
return NULL;
slow = PHead;
while (PHead && PHead->_pNext)
{
PHead = PHead->_pNext;
PHead = PHead->_pNext;
if (!PHead) //如果为偶数个结点,返回中间的前一个结点
{
return slow;
}
slow = slow->_pNext;
}
return slow;
}
// 查找链表的倒数第K个结点,要求只能遍历一次链表
PNode FindLastKNode(PNode PHead, int K)
{
PNode slow = NULL;
if (NULL == PHead)
return NULL;
slow = PHead;
while (PHead && --K)
{
PHead = PHead->_pNext;
}
if (!PHead && K!=0) //如果K!=0的时候PHead为空,则返回
{
return NULL;
}
while (PHead->_pNext)
{
slow = slow->_pNext;
PHead = PHead->_pNext;
}
return slow;
}
// 用单链表实现约瑟夫环
void JosephCircle(PNode* PHead, const int M)
{
PNode PTail = NULL;
int tmp = 0;
assert(PHead);
if (NULL == *PHead)
{
return;
}
PTail = (*PHead);
while (PTail->_pNext)
{
PTail = PTail->_pNext;
}
PTail->_pNext = *PHead; //成环
while ((PTail)->_pNext != PTail)
{
tmp = M;
while (tmp)
{
PTail = PTail->_pNext;
tmp--;
}
printf("%d ", PTail->_data);
ListErase(&PTail, PTail);
}
(*PHead) = PTail;
(*PHead)->_pNext = NULL;
}
void ReverseList(PNode* PHead) // 链表的逆置--三个指针
{
PNode PPre = NULL;
PNode PCur = NULL;
PNode PNext = NULL;
assert(PHead);
if (NULL == *PHead || (*PHead)->_pNext == NULL)
return;
PCur = *PHead;
while (PCur)
{
PNext = PCur->_pNext;
PCur->_pNext = PPre;
PPre = PCur;
PCur = PNext;
}
*PHead = PPre;
}
// 合并两个有序单链表,合并后依然有序
PNode MergeSList(PNode PHead1, PNode PHead2)
{
PNode NewHead;
NewHead = NULL;
if (NULL == PHead1 || NULL == PHead2)
return NULL;
while (PHead1 && PHead2)
{
if ((PHead1->_data) >= (PHead2->_data))
{
ListPushBack(&NewHead, PHead2->_data);
PHead2 = PHead2->_pNext;
}
else if ((PHead1->_data) < (PHead2->_data))
{
ListPushBack(&NewHead, PHead1->_data);
PHead1 = PHead1->_pNext;
}
}
while (PHead1)
{
ListPushBack(&NewHead, PHead1->_data);
PHead1 = PHead1->_pNext;
}
while (PHead2)
{
ListPushBack(&NewHead, PHead2->_data);
PHead2 = PHead2->_pNext;
}
return NewHead;
}
PNode FindBack(PNode PHead)
{
PNode PCur = NULL;
PNode PPre = NULL;
if (!PHead)
{
return NULL;
}
PCur = PHead;
while (PCur->_pNext)
{
PPre = PCur;
PCur = PCur->_pNext;
}
return PCur;
}
// 判断两个单链表是否相交---链表不带环
int IsSListCross(PNode PHead1, PNode PHead2)
{
PNode PCur = NULL;
PNode PCur2 = NULL;
if (NULL == PHead1 || NULL == PHead2)
assert(0);
PCur = FindBack(PHead1);
PCur2 = FindBack(PHead2);
if (PCur->_pNext == PCur2->_pNext)
{
return 1;
}
else
return 0;
}
// 求两个单链表相交的交点---链表不带环
PNode GetCorssNode(PNode PHead1, PNode PHead2)
{
PNode PCur = NULL;
PNode PCur2 = NULL;
if (NULL == PHead1 || NULL == PHead2)
assert(0);
if (IsSListCross(PHead1, PHead1))
{
int count = 0;
int count2 = 0;
int tmp = 0;
PCur = PHead1;
while (PCur)
{
PCur = PCur->_pNext;
++count;
}
PCur2 = PHead2;
while (PCur2)
{
PCur2 = PCur2->_pNext;
++count2;
}
tmp = count - count2;
PCur = PHead1;
PCur2 = PHead2;
if (tmp>0)
{
while (tmp--)
{
PCur = PCur->_pNext;
}
}
else if (tmp<0)
{
while (tmp++)
{
PCur2 = PCur2->_pNext;
}
PCur = PHead1;
}
while (PCur != PCur2)
{
PCur = PCur->_pNext;
PCur2 = PCur2->_pNext;
}
return PCur;
}
return NULL;
}