#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DataType;
typedef struct Node
{
struct Node* _pNext;
DataType _data;
}Node, *PNode;
void DeleteNotTailNode(PNode pos);
void InsertPosFront(PNode pos, DataType data);
void JosephCircle(PNode* pHead, const int M);
void BubbleSort(PNode pHead);
void ReverseSList(PNode* pHead);
PNode ReverseSListOP(PNode pHead);
PNode MergeSList(PNode pHead1, PNode pHead2, PNode pHead3);
PNode FindMiddleNode(PNode pHead);
PNode FindLastKNode(PNode pHead, int K);
int IsCrossWithoutCircle(PNode pHead1, PNode pHead2);
PNode GetCrossNode(PNode pHead1, PNode pHead2);
PNode IsCircle(PNode pHead);
int GetCircleLen(PNode pHead);
PNode GetEnterNode(PNode pHead, PNode pMeetNode);
int IsListCrossWithCircle(PNode pHead1, PNode pHead2);
void DeleteNotTailNode(PNode pos)
{
PNode pCur = NULL;
if (pos)
{
pCur= pos->_pNext;
pos->_data = pCur->_data;
pos->_pNext = pCur->_pNext;
free(pCur);
}
}
void InsertPosFront(PNode pos, DataType data)
{
PNode pCur = NULL;
if (!pos)
{
pCur = BuySListNode(pos->_data);
pos->_data = data;
pCur->_pNext = pos->_pNext;
pos->_pNext = pCur;
}
}
void JosephCircle(PNode* pHead, const int M)
{
PNode pCur = NULL;
PNode pDel = NULL;
assert(pHead);
if (NULL == pHead)
return;
pCur = pHead;
while (pCur->_pNext)
{
pCur = pCur->_pNext;
}
pCur->_pNext =pHead ;
pCur = pHead;
while (pCur != pCur->_pNext)
{
int count = M;
while (--count)
{
pCur = pCur->_pNext;
}
pDel = pCur->_pNext;
pCur->_data = pDel->_data;
pCur->_pNext = pDel->_pNext;
free(pDel);
}
pCur->_pNext = NULL;
return pCur;
}
void BubbleSort(PNode pHead)
{
PNode cur = NULL;
PNode tail = NULL;
assert(pHead);
cur = pHead->_pNext;
while (cur != tail)
{
while (cur->_pNext != tail)
{
if (cur->_data > cur->_pNext->_data)
{
DataType tmp = cur->_data;
cur->_data = cur->_pNext->_data;
cur->_pNext->_data = tmp;
}
cur = cur->_pNext;
}
tail = cur;
cur = pHead->_pNext;
}
return;
}
void ReverseSList(PNode* pHead)
{
PNode p1, p2, p3;
assert(pHead);
if (NULL == pHead)
return pHead;
p1 = pHead;
p2 = p1->_pNext;
while (p2)
{
p3 = p2->_pNext;
p2->_pNext = p1;
p1 = p2;
p2 = p3;
}
(*pHead)->_pNext = NULL;
*pHead = p1;
return pHead;
}
PNode ReverseSListOP(PNode pHead)
{
PNode pCur = NULL;
PNode p = NULL;
assert(pHead);
if (NULL == pHead)
return pHead;
pCur = pHead->_pNext;
pHead->_pNext = NULL;
while (!pCur)
{
p = pCur->_pNext;
pCur->_pNext = pHead->_pNext;
pHead->_pNext = pCur;
pCur = p;
}
}
PNode MergeSList(PNode pHead1, PNode pHead2,PNode pHead3)
{
PNode pa=NULL;
PNode pb=NULL;
PNode pc=NULL;
pa = pHead1->_pNext;
pb = pHead2->_pNext;
pHead3 = pc = pHead1;
while (pa && pb)
{
if (pa->_data <= pb->_data)
{
pc->_pNext = pa;
pc = pa;
pa = pa->_pNext;
}
else
{
pc->_pNext = pb;
pc = pb;
pb = pb->_pNext;
}
}
pc->_pNext = pa ? pa : pb;
free(pHead2);
}
PNode FindMiddleNode(PNode pHead)
{
assert(pHead);
PNode pFast = pHead;
PNode pSlow = pHead;
while (NULL != pHead || NULL != pHead->_pNext)
{
pFast = pFast->_pNext->_pNext;
pSlow = pSlow->_pNext;
}
return pSlow;
}
PNode FindLastKNode(PNode pHead, int K)
{
PNode pFast = NULL;
PNode pSlow = NULL;
assert(pHead);
if (NULL == pHead)
return 0;
pFast = pHead;
pSlow = pHead;
int count = 0;
while (NULL != pFast)
{
if (count < K)
{
count++;
pFast = pFast->_pNext;
}
else
{
pSlow = pSlow->_pNext;
pFast = pFast->_pNext;
}
}
if (count < K)
return 0;
else
printf("%d", pSlow->_data);
return 1;
}
int IsCrossWithoutCircle(PNode pHead1, PNode pHead2)
{
assert(pHead1);
assert(pHead2);
PNode pTail1 = pHead1;
PNode pTail2 = pHead2;
if (NULL == pHead1 || NULL == pHead2);
return 0;
while (pTail1 && pTail1->_pNext)
{
pTail1 = pTail1->_pNext++;
}
while (pTail2 && pTail2->_pNext)
{
pTail2 = pTail2->_pNext++;
}
if ((pTail1 == pTail2) && (NULL != pTail1))
return 1;
else
return NULL;
}
PNode GetCrossNode(PNode pHead1, PNode pHead2)
{
assert(pHead1);
assert(pHead2);
PNode pTail1 = pHead1;
PNode pTail2 = pHead2;
DataType len1 = 0;
DataType len2 = 0;
DataType Sub = 0;
if (NULL == pHead1 || NULL == pHead2);
return 0;
while (pTail1 && pTail1->_pNext)
{
pTail1 = pTail1->_pNext;
len1++;
}
while (pTail2 && pTail2->_pNext)
{
pTail2 = pTail2->_pNext;
len2++;
}
if (pTail1 != pTail2)
{
return NULL;
}
Sub = len1 - len2;
if ((0 == Sub) && (pTail1 == pTail2))
{
return 1;
}
else if (Sub > 0)
{
while (Sub != 0)
{
pTail2 = pTail2->_pNext;
Sub--;
}
while (pTail1 != pTail2)
{
pTail1 = pTail1->_pNext;
pTail2 = pTail2->_pNext;
}
return 1;
}
else
{
while (Sub !=0)
{
pTail1 = pTail1->_pNext;
Sub--;
}
while (pTail1 != pTail2)
{
pTail1 = pTail1->_pNext;
pTail2 = pTail2->_pNext;
}
return 1;
}
}
PNode IsCircle(PNode pHead)
{
assert(pHead);
PNode pFast = pHead;
PNode pSlow = pHead;
while (pFast && pFast->_pNext)
{
pFast = pFast->_pNext->_pNext;
pSlow = pSlow->_pNext;
}
if (pFast == pSlow)
{
printf("链表带环\n");
return pSlow;
}
else
return NULL;
}
int GetCircleLen(PNode pHead)
{
DataType i = 0;
PNode pCur = pHead;
while (pCur->_pNext != pHead)
{
++i;
pCur = pCur->_pNext;
}
return i;
}
PNode GetEnterNode(PNode pHead, PNode pMeetNode)
{
PNode pEnterNode = NULL;
PNode pCur1 = pHead;
PNode pCur2 = pMeetNode;
while (pCur1 != pCur2)
{
pCur1 = pCur1->_pNext;
pCur2 = pCur2->_pNext;
}
pEnterNode = pCur1;
return pEnterNode;
}
int IsListCrossWithCircle(PNode pHead1, PNode pHead2)
{
assert(pHead1);
assert(pHead2);
PNode pCur1 = IsCircle(pHead1);
PNode pCur2 = IsCircle(pHead2);
PNode pEnter1 = NULL;
PNode pEnter2 = NULL;
PNode pCrossNode = NULL;
if (NULL == pCur1 && NULL == pCur2)
{
pCrossNode = GetEnterNode(pCur1, pCur2);
return 0;
}
else if ((NULL == pCur1 && NULL != pCur2) || (NULL != pCur1 && NULL == pCur2))
{
return -1;
}
else
{
PNode pMeet = pCur1->_pNext;
while (pMeet !=pCur1)
{
if (pMeet == pCur2)
{
pEnter1 = GetEnterNode(pHead1, pCur1);
pEnter2 = GetEnterNode(pHead2, pCur2);
if (pEnter1 == pEnter2)
{
printf("环外相交\n");
pCrossNode = GetEnterNode(pCur1, pCur2);
return 1;
}
else
{
printf("环内相交\n");
return 2;
}
}
pMeet = pMeet->_pNext;
}
return -1;
}
}