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

}
相关推荐
h39744 小时前
DirectShow过滤器开发-视频源过滤器
c++·windows·音视频·directshow
夏幻灵7 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
码完就睡9 小时前
数据结构——红黑树
数据结构
码爸9 小时前
数据结构详细介绍
数据结构
java资料站10 小时前
二、Spring AI Alibaba · ChatModel
java·windows·spring
北冥you鱼11 小时前
Go 语言空接口(interface{})使用场景与最佳实践
开发语言·windows·golang
夏幻灵12 小时前
每日一题:LeetCode 206:反转链表——三指针与原地反转详解
算法·leetcode·链表
CQU_JIAKE12 小时前
10.9【A】
数据结构
第25小时记录12 小时前
数据结构与算法 -第 3 章 常用算法-动态规划
数据结构·python