//链表数据类型
typedef struct node
{
int val;
struct node* pNext;
}NODE,*PNODE;
//主函数
void Circle_Verification()
{
int len_link = 0;
PNODE pHead;
PNODE pMeet = NULL;
cout<<"Length of Linklist:";
cin>>len_link;
//创建链表
Initialize(pHead,len_link);
Print(pHead);
int iJoint;
PNODE pJoint = NULL;
char iAns = 'N';
cout<<"Will you insert Circle?Y/N:";
cin>>iAns;
if(iAns == 'N')
{
}
else
{
cout<<"Insert Circle At: ";
cin>>iJoint;
//在链表中插入环
Insert_Circle(iJoint, pHead);
}
//判断链表中是否存在环
bool is_circle = Is_Circle(pHead,pMeet);
if(is_circle == true) cout<<"Circle exits in linklist!"<<endl;
else cout<<"Circle not exits in linklist!"<<endl;
if(is_circle == true)
{
//寻找链表环开始的始点
pJoint = Find_Joint(pHead, pMeet);
cout<<"Joint At:"<<pJoint->val;
}
}
//链表初始化
void Initialize(PNODE &pHead, int length)
{
assert(length>=0);
pHead = new NODE();
pHead->val = 0;
pHead->pNext = NULL;
PNODE pTail = pHead;
for(int i=1;i<=length;i++)
{
PNODE pInsert = new NODE();
pInsert->val = i;
pInsert->pNext = NULL;
//在链表中插入节点
Insert_Into_Linklist(pInsert,pTail);
pTail = pInsert;
}
}
//在链表中插入节点
void Insert_Into_Linklist(PNODE pInsert, PNODE pBefore)
{
assert( pBefore != NULL || pInsert != NULL);
pInsert->pNext = pBefore->pNext;
pBefore->pNext = pInsert;
}
//在链表中插入环
void Insert_Circle(int iJoint, PNODE pHead)
{
int link_len = Get_Length(pHead);
assert(iJoint<=link_len);
assert(iJoint>0);
PNODE pJoint = Get_Node(pHead, iJoint);
PNODE pTail = Get_Node(pHead, link_len);
pTail->pNext = pJoint;
}
//判断链表中是否存在环
bool Is_Circle(PNODE pHead, PNODE &pMeet)
{
assert(pMeet == NULL);
PNODE pStep1;
PNODE pStep2;
pStep1 = pStep2 = pHead;
int iStep1 = 1;
int iStep2 = 1;
do
{
pStep1 = pStep1->pNext;
if(pStep1 == NULL) return false;
if(pStep2 -> pNext == NULL ) return false;
pStep2 = pStep2->pNext->pNext;
if(pStep2 == NULL) return false;
}while(pStep1 != pStep2);
if(pStep1 == pStep2)
{
pMeet = pStep1;
return true;
}
else return false;
}
//寻找链表环开始的始点
PNODE Find_Joint(PNODE pHead, PNODE pMeet)
{
assert(pMeet!= NULL);
assert(pHead!=NULL);
//获取2个节点之间的距离。
int step1_len = Get_Distance(pHead, pMeet);
int step2_len = Get_Distance(pMeet->pNext,pMeet);
assert(step1_len >=0 || step2_len >= 0);
//int step_max = int max(step1_len, step2_len);
int step_diff = step1_len - step2_len;
PNODE pStep1 = pHead;
PNODE pStep2 = pMeet->pNext;
if(step_diff>0)
{
for(int i=1; i<=step_diff; i++)
{
pStep1 = pStep1->pNext;
}
}
else
{
for(int i=1; i<=(-step_diff); i++)
{
pStep2 = pStep2->pNext;
}
}
while(pStep1 != pStep2)
{
pStep1 = pStep1->pNext;
pStep2 = pStep2->pNext;
}
if(pStep1 == pStep2) return pStep1;
else return NULL;
}
//获取2个节点之间的距离
int Get_Distance(PNODE pHead, PNODE pTail)
{
assert(pHead!=NULL);
PNODE pNext;
pNext = pHead;
int i=0;
while(pNext!= NULL && pNext != pTail)
{
pNext = pNext->pNext;
i++;
}
if(pNext == pTail) return i;
else return -1;
}
其他链表的算法
void Reverse_Linklist(PNODE pHead)
{
assert( pHead != NULL );
if(Is_Linklist_Empty(pHead) == true)
{
return;
}
PNODE pDeleted = NULL;
PNODE pHead_New = new NODE();
pHead_New->pNext = NULL;
while(Is_Linklist_Empty(pHead)== false)
{
Delete_From_Linklist(pHead, pDeleted);
Insert_Into_Linklist(pDeleted, pHead_New);
}
pHead->pNext = pHead_New->pNext;
delete pHead_New;
}
void Insert_Into_Linklist(PNODE pInsert, PNODE pBefore)
{
assert( pBefore != NULL || pInsert != NULL);
pInsert->pNext = pBefore->pNext;
pBefore->pNext = pInsert;
}
bool Is_Linklist_Empty(PNODE pHead)
{
assert( pHead != NULL);
if(pHead->pNext== NULL)
{
return true;
}
else
{
return false;
}
}
void Print(PNODE pHead)
{
int i=1;
while(pHead != NULL)
{
cout<<pHead -> val<<" , ";
pHead = pHead -> pNext;
}
cout<<endl;
}
int Get_Length(PNODE pHead)
{
assert(pHead!=NULL);
PNODE pNext = pHead->pNext;
int i = 0;
while(pNext!=NULL)
{
pNext = pNext->pNext;
i++;
}
return i;
}
void Delete_From_Linklist(PNODE pBefore, PNODE &pDeleted)
{
assert( pBefore != NULL || pDeleted != NULL);
pDeleted = pBefore->pNext;
pBefore->pNext = (pBefore->pNext)->pNext;
}