数据结构——判断单链表是否有环

http://blog.csdn.net/mci2004/article/details/7458866

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include <stdlib.h>  
  4.   
  5. #define INSERT_NUM 100  
  6. #define FAST_POINT_STEP 2  
  7.   
  8. typedef int ElemType;  
  9.   
  10. typedef struct LNode{  
  11.     ElemType data;  
  12.     struct LNode *next;  
  13. }LinkList, *pNode;  
  14.   
  15. //init the linklist  
  16. void initList(pNode *head){  
  17.     (*head) = (LinkList*)malloc(sizeof(LinkList));  
  18.     (*head)->next = NULL;  
  19.     (*head)->data = 9;  
  20. }  
  21.   
  22. //insert Element  
  23. int insertElem(pNode head, ElemType data){  
  24.   
  25.     pNode pInsertNode;  
  26.     pNode pWork;  
  27.    
  28.     pWork = head;  
  29.   
  30.     pInsertNode = (LinkList*)malloc(sizeof(LinkList));  
  31.     //memset(pInsertNode, 0, sizeof(LinkList));  
  32.     if(pInsertNode == NULL){  
  33.         return 0;  
  34.     }  
  35.     pInsertNode->next = NULL;  
  36.     pInsertNode->data = data;  
  37.   
  38.     while(pWork->next != NULL){  
  39.         pWork = pWork->next;  
  40.     }  
  41.     pWork->next = pInsertNode;  
  42.     return 1;  
  43. }  
  44.   
  45. //print node data one by one  
  46. void printList(pNode head){  
  47.     printf("in the function printList\n");  
  48.     if(head == NULL){  
  49.         printf("List is empty----\n");  
  50.     }  
  51.     pNode p = head->next;  
  52.     do{  
  53.         printf("%d", p->data);  
  54.         printf("--->");  
  55.         p = p->next;  
  56.     }while(p != NULL);  
  57.       
  58. }  
  59.   
  60. //make the LinkList loop  
  61. void makeLoop(pNode head){  
  62.     pNode p = head;  
  63.     while(p->next != NULL ){  
  64.         p = p->next;  
  65.     }  
  66.     p->next = head->next;  
  67. }  
  68.   
  69. //tell if is a loop list?  
  70. int isListLoop(pNode head){  
  71.     int i;  
  72.     pNode slow = head;  
  73.     pNode fast = head;  
  74.     while(fast){  
  75.         for(i = 0 ; i < FAST_POINT_STEP ; i++){  
  76.             fast = fast->next;  
  77.             if(fast == slow)  
  78.                 return 1;  
  79.             else if(fast == NULL)  
  80.                 return 0;  
  81.         }  
  82.         slow = slow->next;  
  83.     }  
  84.     return 0;  
  85. }  
  86.   
  87. void main(){  
  88.     int i;  
  89.     pNode head;  
  90.     initList(&head);  
  91.       
  92.     for(i = 0 ; i < INSERT_NUM ; i++){  
  93.         insertElem(head, i);  
  94.     }  
  95.     makeLoop(head);  
  96.     //printList(head);  
  97.     if(isListLoop(head))  
  98.         printf("is a loop list \n");  
  99.     else  
  100.         printf("is not a loop list\n");  
  101.   
  102. }  

        之前在网上看到有人说,判断一个链表是否有环,还可以使用两个for循环嵌套的方法,当外层循环步进一个节点时,内层循环就遍历外层循环节点之后的所有节点,然后比较内外循环的两个节点。若有节点地址相等,则表明该单链表有循环,反之则不存在循环。这种方法无疑效率比较低。而且如果出现下面这种情况的话,会更加麻烦。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值