【数据结构4】单向循环链表实现

目录

一、节点结构

二、单向循环链表的头结构

三、接口设计

[3.1 初始化接口(表在栈上)实现](#3.1 初始化接口(表在栈上)实现)

[3.2 头插/尾插实现](#3.2 头插/尾插实现)

[3.3 遍历链表](#3.3 遍历链表)

[3.4 删除链表元素](#3.4 删除链表元素)

[3.5 释放数据域](#3.5 释放数据域)


一、节点结构

复制代码
C

typedef int Element;
// 节点结构
typedef struct _loop_node
{
    Element val;
    struct _loop_node *next;
}LoopNode;

二、单向循环链表的头结构

复制代码
C

typedef struct
{
    LoopNode header;
    LoopNode *rear;
    int num;
}LinkLoopList;

三、接口设计

复制代码
C

//初始化接口
void initLinkLoopList(LinkLoopList *link_loop);
//插入(头插 尾插)
int insertLinkLoopListHeader(LinkLoopList *link_loop,Element value);
int insertLinkLoopListRear(LinkLoopList *link_loop,Element value);
//遍历显示
void showLinkLoopList(const LinkLoopList *link_loop);
//删除
int deleteLinkLoopList(LinkLoopList *lin_loop,Element value);
//释放整个数据域,不释放头(栈上无需手动释放)
void destroyLinkLoopList(LinkLoopList *link_loop);
3.1 初始化接口(表在栈上)实现
复制代码
C

void initLinkLoopList(LinkLoopList* link_loop)
{
    link_loop->header.next = link_loop->rear = &link_loop->header;
    link_loop->num = 0;
}
3.2 头插/尾插实现
复制代码
C

int insertLinkLoopListHeader(LinkLoopList* link_loop, Element value)
{
    // 先有新节点
    LoopNode *node = malloc(sizeof(LoopNode));
    if (node == NULL)
    {
        return -1;
    }
    node->val = value;
    node->next = link_loop->header.next;
    link_loop->header.next = node;
    // 判断尾指针是否需要更新
    // 现在已经插入一个节点,如果尾指针还是初始化的状态 那么需要更新尾指针的状态
    if (link_loop->rear == &link_loop->header)
    {
        link_loop->rear = node;
    }
    ++link_loop->num;
    return 0;
}

int insertLinkLoopListRear(LinkLoopList* link_loop, Element value)
{
    // 先有新节点
    LoopNode *node = malloc(sizeof(LoopNode));
    if (node == NULL)
    {
        return -1;
    }
    node->val = value;

    node->next = link_loop->rear->next;
    link_loop->rear->next = node;
    link_loop->rear = node;

    link_loop->rear = node;
    ++link_loop->num;
    return 0;
}
3.3 遍历链表
复制代码
C

void showLinkLoopList(const LinkLoopList* link_loop)
{
    LoopNode *node = link_loop->header.next;
    while (node != &link_loop->header)
    {
        printf("%d\t",node->val);
        node = node->next;
    }
    printf("\n");
}
3.4 删除链表元素
复制代码
C

int deleteLinkLoopList(LinkLoopList* link_loop, Element value)
{
    LoopNode *node = &link_loop->header;

    while (node->next != &link_loop->header && node->next->val != value)
    {
        node = node->next;
    }
    if (node->next->val == value)
    {
        LoopNode *tmp = node->next;
        node->next = tmp->next;
        free(tmp);
        --link_loop->num;
    }else
    {
        printf("no %d element",value);
    }
    return 0;

}
3.5 释放数据域
复制代码
C

void destroyLinkLoopList(LinkLoopList* link_loop)
{
    LoopNode *node = link_loop->header.next;
    while (node != &link_loop->header)
    {
        LoopNode *tmp = node;
        node = node->next;
        free(tmp);
        --link_loop->num;
    }
    printf("%d element",link_loop->num);
}

愿你遍历链表,归来仍是少年;
遍历二叉树,不丢一个节点;
排序人生,复杂度永远是 O(1)
祝你:岁岁常欢愉,年年皆胜意
身体安康,万事顺遂。

除夕快乐!

如果我的内容对你有帮助,请点赞,评论,收藏。创作不易,大家的支持就是我坚持下去的动力!

相关推荐
哈里谢顿12 小时前
跳表(Skip List):简单高效的有序数据结构
数据结构
任沫2 天前
字符串
数据结构·后端
祈安_2 天前
Java实现循环队列、栈实现队列、队列实现栈
java·数据结构·算法
NineData4 天前
数据库管理工具NineData,一年进化成为数万+开发者的首选数据库工具?
运维·数据结构·数据库
琢磨先生David12 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
qq_4542450312 天前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝12 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
岛雨QA12 天前
常用十种算法「Java数据结构与算法学习笔记13」
数据结构·算法
weiabc12 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
wefg112 天前
【算法】单调栈和单调队列
数据结构·算法