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

}
相关推荐
小玮看世界1 小时前
[Python]线段树与二分法
数据结构·算法
Richard.Wong5 小时前
Windows IIS 服务器部署 Vue3 前端项目详细流程
服务器·前端·windows
青山木5 小时前
Hot 100 --- 在排序数组中查找元素的第一个和最后一个位置
java·数据结构·算法·leetcode
依然鸣6 小时前
PTA团体程序设计天梯赛L2真题讲解L2-045-048
数据结构·c++·经验分享·学习·算法·pat考试·pat
CQU_JIAKE7 小时前
8.5【A】
数据结构·算法
Jasmine_llq8 小时前
《P13016 [GESP202506 六级] 最大因数》
数据结构·算法
白狐_7988 小时前
考研408算法设计题保命策略:链表专题暴力解法精讲(2015 & 2019真题实战)
考研·算法·链表
wyg_03111310 小时前
Intel Arc 140T + Triton(Windows)环境搭建全过程记录
windows
程序猿小玉兒10 小时前
Quartz定时任务偶尔不执行
服务器·windows·microsoft
凉茶钱11 小时前
【数据结构】C语言实现队列
c语言·数据结构