循环双链表的操作

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

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

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;
}
相关推荐
C++忠实粉丝5 小时前
前缀和(8)_矩阵区域和
数据结构·c++·线性代数·算法·矩阵
ZZZ_O^O5 小时前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
代码雕刻家6 小时前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
Kalika0-08 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
代码雕刻家8 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
小字节,大梦想9 小时前
【C++】二叉搜索树
数据结构·c++
我是哈哈hh10 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
丶Darling.10 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo52011 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
Indigo_code11 小时前
【数据结构】【链表代码】合并有序链表
数据结构·windows·链表