判断单链表是否有环

来源:互联网 发布:安全评价行业 知乎 编辑:程序博客网 时间:2024/06/05 08:26
/*判断单链表是否有环,如果有,把指向环的开始指针返回;如果没有环,返回NULL这个程序:设置两个指针,一个pSlow,一个pFast,pSLow每次走一步,pFast每次走两步,如果后面pSlow->_next等于NULL了,但是pFast还没有等于pSlow,那么不存在环否则若pFast == pSlow表明存在环在确认存在环的基础上重新设置,pOri等于链表头指针,而pSlow不变,两者每次走一步,两者再次相遇的时候必然是环开始的地方输入:5(链表长度) 2(环的起始下标)0 1 2 3 4(链表中每个节点的元素)输出:有环环的起始地址环的长度3*//*关键:1 while(pNode1 != pNode2)//根据头指针距离环形链表起始节点的距离 = 碰撞点距离环形链表起始节点的距离(注意是从碰撞点走到环形链表起始节点,距离不能反过来),将指针1设立在{//头指针,指针2设置在碰撞点,下次相遇的位置就是环形链表的开始节点pNode1 = pNode1->_next;pNode2 = pNode2->_next;2 pFast = pFast->_next->_next;//环形链表长度等于快慢指针在碰撞点处开始走,下次再相遇时走的步数while(pSlow != pFast){iLen++;pSlow = pSlow->_next;pFast = pFast->_next->_next;*///首先建立一个带环的链表,我们可以用尾插法建立正常的链表之后,再让尾结点连接到某一指定节点#include <stdio.h>#include <string.h>const int MAXSIZE = 1001;#define INF 1000000000;typedef struct Node{int _iVal;struct Node* _next;}Node;//实在不想删除节点,用静态数组Node nodeArr[MAXSIZE];int _iIndex;Node* createNode(){return &nodeArr[_iIndex++];}void buildCircularList(Node** pHead,int* pArr,int n,Node* (*pFun)(),int iCircleIndex){if(!pHead || !(*pHead) ||  n <= 0 || iCircleIndex < 0){return;}Node* pNode,*pCurNode;Node* pCircularNode;int iCnt = 0;for(int i = 0 ; i < n ; i++){if(i)//尾插法建立单链表{pNode = pFun();//动态策略pNode->_iVal = pArr[i];pNode->_next = NULL;pCurNode->_next = pNode;pCurNode = pNode;iCnt++;}else{pCurNode = *pHead;pCurNode->_iVal = pArr[i];pCurNode->_next = NULL;}if(iCircleIndex == iCnt)//如果找到这个环的起始节点,就记录下来{pCircularNode = pCurNode;}}pCurNode->_next = pCircularNode;//将尾结点与指定节点下标连接起来,构成环,此时的尾结点起始就是pCurNode}Node* isCircularList(Node* pHead)//判断是否是环形链表,如果是:返回第一次相遇的节点指针,否则返回NULL{Node* pSlow,*pFast;pSlow = pFast = pHead;while(pSlow->_next != NULL){pSlow = pSlow->_next;pFast = pFast->_next->_next;if(pSlow == pFast){return pSlow;}}return NULL;}Node* findCircularBeginNode(Node* pHead,Node* pFirstMeetNode)//寻找环形链表的开始节点{if(!pHead || !pFirstMeetNode){return NULL;}Node* pNode1 = pHead;Node* pNode2 = pFirstMeetNode;while(pNode1 != pNode2)//根据头指针距离环形链表起始节点的距离 = 碰撞点距离环形链表起始节点的距离(注意是从碰撞点走到环形链表起始节点,距离不能反过来),将指针1设立在{//头指针,指针2设置在碰撞点,下次相遇的位置就是环形链表的开始节点pNode1 = pNode1->_next;pNode2 = pNode2->_next;}return pNode1;}int circularListLen(Node* pMeetNode)//计算环形链表的长度{Node* pSlow,*pFast;pSlow = pFast = pMeetNode;int iLen = 0;iLen++;pSlow = pSlow->_next;pFast = pFast->_next->_next;//环形链表长度等于快慢指针在碰撞点处开始走,下次再相遇时走的步数while(pSlow != pFast){iLen++;pSlow = pSlow->_next;pFast = pFast->_next->_next;}return iLen;}void process(){int iLen,iCircleIndex;while(EOF != scanf("%d %d",&iLen,&iCircleIndex)){if(iLen < 1 || iLen >= MAXSIZE || iCircleIndex < 0 || iCircleIndex >= MAXSIZE){continue;}int iListArr[MAXSIZE];for(int i = 0; i < iLen; i++){scanf("%d",&iListArr[i]);}memset(nodeArr,NULL,sizeof(nodeArr));_iIndex = 0;Node* head =  createNode();Node** pHead = &head;buildCircularList(pHead,iListArr,iLen,createNode,iCircleIndex);//构建环形链表Node* pFirstMeetNode = isCircularList(*pHead);Node* pCircularBegNode;if(pFirstMeetNode){pCircularBegNode = findCircularBeginNode(*pHead,pFirstMeetNode);printf("%d\n",pCircularBegNode->_iVal);printf("%d\n",circularListLen(pFirstMeetNode));}}}int main(int argc,char* argv[]){process();getchar();return 0;}

0 0