单向循环链表C语言实现实现(全)

01.结构体定义

c 复制代码
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FASLE 0//定义宏标识判断是否成功
typedef struct Node {
       int data;
       struct Node* next;
}Node;

02.初始化

c 复制代码
 Node* InitList() {
        Node* list = (Node*)malloc(sizeof(Node));
        list->data = 0;//创建节点保存data
        list->next = list;
        return list;
}

04.增加节点

c 复制代码
void headInsert(Node*list,int data) {//头插
        Node* node = (Node*)malloc(sizeof(Node));
        node->data = data;
        node->next = list->next;
        list->next = node;
        list->data++;//记录节点数
 }
 
void tailInsert(Node* list,int data) {//带入头指针,尾插
       Node* n = list;//保存list头节点,用n这个指针变量移动进行判断方便判断
       Node* node = (Node*)malloc(sizeof(Node));
       node->data = data;
       while (n->next != list) {
               n = n->next;
       }
       node->next = list;
       n->next = node;
       list->data++;
 }

05.删除节点

c 复制代码
 int DeleteList(Node* list,int data) {
        Node* prenode = list;
        Node* current = list->next;//设置一个指向头街点的node节点
        while (current!=list) {
                if (current->data == data) {
                       prenode->next = current->next;
                       free(current);
                       list->data--;
                       return TRUE;
                }
                else {
                       prenode = current;
                       current = current ->next;
                }
        }
        return FASLE;
 }

}
相关推荐
埃博拉酱4 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
唐宋元明清21885 天前
.NET 本地Db数据库-技术方案选型
windows·c#
加号35 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
琢磨先生David5 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
tryCbest5 天前
Windows环境下配置pip镜像源
windows·pip
呉師傅5 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
百事牛科技5 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
一个人旅程~5 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑
qq_454245035 天前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝5 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode