单向循环链表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;
 }

}
相关推荐
夏鹏今天学习了吗6 小时前
【LeetCode热题100(31/100)】K 个一组翻转链表
算法·leetcode·链表
Pluchon7 小时前
硅基计划4.0 算法 字符串
java·数据结构·学习·算法
麦格芬2307 小时前
LeetCode 416 分割等和子集
数据结构·算法
2401_8414956411 小时前
【数据结构】顺序表的基本操作
数据结构·c++·算法·顺序表·线性表·线性结构·顺序表的基本操作
自信的小螺丝钉11 小时前
Leetcode 138. 随机链表的复制 哈希 / 拼接+拆分
leetcode·链表·哈希算法
未知陨落11 小时前
LeetCode:70.最小栈
数据结构·算法·leetcode
小糖学代码12 小时前
STL的list模拟实现(带移动构造和emplace版本)
c语言·数据结构·c++·windows·list
一人の梅雨12 小时前
淘宝店铺全量商品接口深度开发:从分页优化到数据完整性保障
linux·windows·microsoft
shenghaide_jiahu12 小时前
leetcode430:扁平化多级双向链表
数据结构·链表
失散1312 小时前
软件设计师——03 数据结构(上)
数据结构·软考·软件设计师