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

}
相关推荐
胡耀超14 分钟前
3.Python高级数据结构与文本处理
服务器·数据结构·人工智能·windows·python·大模型
云:鸢15 分钟前
C语言链表设计及应用
c语言·开发语言·数据结构·链表
离越词1 小时前
C++day8作业
开发语言·c++·windows
fasewer2 小时前
玄机--windows日志分析
运维·服务器·windows·网络安全
在下雨5992 小时前
项目讲解1
开发语言·数据结构·c++·算法·单例模式
今后1232 小时前
【数据结构】栈详解
数据结构·
水饺编程3 小时前
Windows 命令行:cd 命令3,当前目录,父目录,根目录
c语言·c++·windows·visual studio
songx_994 小时前
leetcode10(跳跃游戏 II)
数据结构·算法·leetcode
先做个垃圾出来………5 小时前
差分数组(Difference Array)
java·数据结构·算法
dragoooon347 小时前
[数据结构——lesson5.1链表的应用]
数据结构·链表