单向环形链表的创建与判断链表是否有环

        单向环形链表的创建与单向链表的不同在于,最后一个节点的next需要指向头结点;

        判断链表是否带环,只需要使用两个指针,一个步长为1,一个步长为2,环状链表这两个指针总会相遇。

如下示例代码:

list.h

#ifndef  LIST_H__
#define LIST_H__

typedef struct _listNode {

    int data;
    struct _listNode *next;
}listNode_t;

typedef struct _list {
    int size;
    listNode_t *head;
}list_t;
#endif

list.c

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "list.h"

list_t *listCreate(void)
{
    list_t *list = (list_t *)malloc(sizeof(list_t));
    if(list == NULL) {
        perror("malloc :");
        exit(-1);
    }
    list->size = 0;
    list->head = NULL;
    return list;
}

int listInsertHead(list_t *list, int dat)
{
    listNode_t *pNode = (listNode_t *)malloc(sizeof(listNode_t));
    if(pNode == NULL) {
        perror("malloc :");
        exit(-1);
    }

    pNode->data = dat;
    pNode->next = NULL;
    listNode_t *headNode = list->head;
    if(headNode == NULL) {
        list->head = pNode;
        pNode->next = list->head; //pNode作为头结点,同时pNode的next指向头结点,构成环状
    } else {
        pNode->next = list->head; //在头部插入新的节点
        //找到末尾的节点,将末尾节点的next指向新的节点。
        while(headNode->next != list->head) {
            headNode = headNode->next;
        }
        headNode->next = pNode;
        //头结点指向新的节点
        list->head = pNode;
    }
    list->size ++;
    return 0;
}
bool listIsLoop(list_t *list)
{
    //如果链表是环状的,fast和slow总会相遇
    listNode_t *fast = list->head;
    listNode_t *slow = list->head;
    if(list->head == NULL || list->head->next == NULL) {
        return false;
    }
    while(fast->next->next != NULL || slow->next != NULL) {
        if(fast == slow) {
            return true;
        }
    }
    return false;
}

void listDump(list_t *list)
{

    listNode_t  *temp = list->head;
    //这里可以验证链表是不是环状的,将size改为两倍大小,看是否循环输出
    while(list->size) {
        printf("%d-> ", temp->data);
        temp = temp->next;
        list->size --;
    }
    printf("\n");
    return ;
}
int main()
{
    list_t *list = listCreate();
    for(int i = 0; i < 5; i++) {
        listInsertHead(list, i);
    }
    listDump(list);
    printf("list %s loop\n", listIsLoop(list)? "is" : "is not" );

    return 0;

相关推荐

  1. 单向环形创建判断是否

    2024-06-17 18:14:01       7 阅读
  2. leetcode 141 判断是否存在

    2024-06-17 18:14:01       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-17 18:14:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-17 18:14:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-17 18:14:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-17 18:14:01       18 阅读

热门阅读

  1. 第一节 初识Redis

    2024-06-17 18:14:01       8 阅读
  2. 你好rust

    2024-06-17 18:14:01       7 阅读
  3. torch多机器多卡推理大模型

    2024-06-17 18:14:01       6 阅读
  4. mybatisplus 笔记

    2024-06-17 18:14:01       7 阅读
  5. Eclipse 查找功能解析

    2024-06-17 18:14:01       8 阅读
  6. Eclipse下载安装

    2024-06-17 18:14:01       7 阅读
  7. MySQL 保姆级教程(二):使用 MySQL 检索数据

    2024-06-17 18:14:01       6 阅读
  8. QT图片转PNG项目实战(含源码)

    2024-06-17 18:14:01       7 阅读
  9. Docker配置与使用详解

    2024-06-17 18:14:01       6 阅读
  10. HTML中的<a>标签使用指南

    2024-06-17 18:14:01       6 阅读
  11. python写excel

    2024-06-17 18:14:01       6 阅读
  12. shell循环控制

    2024-06-17 18:14:01       6 阅读
  13. FormData 对象

    2024-06-17 18:14:01       7 阅读
  14. MybatisPlus逻辑删除

    2024-06-17 18:14:01       7 阅读
  15. Azure 基础

    2024-06-17 18:14:01       6 阅读