循环双链表的操作

归纳编程学习的感悟,
记录奋斗路上的点滴,
希望能帮到一样刻苦的你!
如有不足欢迎指正!
共同学习交流!
🌎欢迎各位→点赞 👍+ 收藏⭐ + 留言​📝

每一个裂缝都是为透出光而努力!

CLinkList.h

复制代码
#pragma once
#include<stdio.h>
#include<malloc.h>

#define DataType int

typedef struct CLNode
{
	DataType data;
	struct CLNode* next;
}CLNode,*CLinkList;

void InitCLinkList(CLinkList* head);

int InsertElem(CLinkList head, int i, DataType e);

int DeleteElem(CLinkList head, int i, DataType* e);

void InsertElemR(CLinkList head, DataType e);

void InsertElemF(CLinkList head, DataType e);

CLinkList.cpp

cpp 复制代码
#include "CLinkList.h"

#define _CRT_SECURE_NO_WARNINGS 1

void InitCLinkList(CLinkList* head)
{
	(*head) = (CLinkList)malloc(sizeof(CLNode));
	(*head)->next = *head;
}

int InsertElem(CLinkList head, int i, DataType e)
{
	CLNode* pre, * p;
	if (i <= 0)
	{
		printf("插入位置错误!\n");
		return -1;
	}
	int j;
	pre = head;
	j = 0;
	while (pre->next != head && j < i - 1)
	{
		pre = pre->next;
		j++;
	}
	if (j != i - 1)
	{
		printf("插入位置错误!\n");
		return 0;
	}
	p = (CLNode*)malloc(sizeof(CLNode));
	p->data = e;
	p->next = pre->next;
	pre->next = p;
	return 1;
}

int DeleteElem(CLinkList head, int i, DataType* e)
{
	CLNode* pre, *p;
	int j;
	if (i <= 0)
	{
		printf("删除位置错误!\n");
		return -1;
	}
	pre = head;
	j = 0;
	while (pre->next!= head &&j < i - 1)
	{
		pre = pre->next;
		j++;
	}
	if (j != i - 1)
	{
		printf("删除位置错误!\n");
		return 0;
	}
	p = pre->next;
	*e = p->data;
	pre->next = p->next;
	free(p);
	return 1;
}

void InsertElemR(CLinkList head, DataType e)
{
	CLNode* pre, * p;
	pre = head;
	while (pre->next != head)
	{
		pre = pre->next;
	}
	p = (CLNode*)malloc(sizeof(CLNode));
	p->data = e;
	p->next = pre->next;
	pre->next = p;
}

void InsertElemF(CLinkList head, DataType e)
{
	CLNode* p;
	p = (CLNode*)malloc(sizeof(CLNode));
	p->data = e;
	p->next = head->next;
	head->next = p;
}
相关推荐
hn小菜鸡33 分钟前
LeetCode 153.寻找旋转排序数组中的最小值
数据结构·算法·leetcode
liu****1 小时前
12.线程同步与互斥
linux·数据结构·c++·算法·1024程序员节
屁股割了还要学2 小时前
【C++进阶】STL-string的简单实现
c语言·开发语言·数据结构·c++·学习·考研
wefg12 小时前
【数据结构】红黑树
数据结构·算法
咪咪渝粮2 小时前
112.路径总和
java·数据结构·算法
电子_咸鱼3 小时前
高阶数据结构——并查集
数据结构·c++·vscode·b树·python·算法·线性回归
xiaoye-duck3 小时前
数据结构之二叉树-堆
数据结构
CAU界编程小白3 小时前
数据结构系列之快速排序
数据结构·c++·算法
卡提西亚4 小时前
一本通网站1130:找第一个只出现一次的字符
数据结构·c++·笔记·算法·一本通
lkbhua莱克瓦244 小时前
Java基础——集合进阶用到的数据结构知识点3
java·数据结构·github·平衡二叉树·avl