【数据结构与算法】链表(下)

记录自己所学,无详细讲解

带头循环双链表实现

1.项目目录文件

2.头文件 List.h

复制代码
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
typedef struct List
{
	int data;
	struct List* prev;
	struct List* next;
}List;
void ListInit(List** phead);//初始化
List* Buynewnode(List* phead,int n);//创建新节点
void ListPushfront(List* phead, int n);//头插
void ListPushback(List* phead, int n);//尾插
void ListPopfront(List* phead);//头删
void ListPopback(List* phead);//尾删
List* ListIsFind(List* phead, int n);//查找节点并返回地址
void ListInsertfront(List* phead, List* pos, int n);//节点前插入
void ListInsertback(List* phead, List* pos, int n);//节点后插入
void ListDel(List* phead, List* pos);//删除节点
void ListModify(List* pos, int n);//修改节点
void ListDestory(List* phead);//销毁节点
void ListPrint(List* phead);//打印输出

3.函数定义源文件 list.c

复制代码
#include "list.h"
void ListInit(List** phead)
{
	*phead = (List*)malloc(sizeof(List));
	(*phead)->next = *phead;
	(*phead)->prev = *phead;
	(*phead)->data = 0;
}
List* Buynewnode(int n)
{
	List* newnode = (List*)malloc(sizeof(List));
	assert(newnode);
	newnode->next = NULL;
	newnode->prev = NULL;
	newnode->data = n;
		return newnode;
	
}
void ListPushfront(List* phead, int n)
{
	List* newnode = Buynewnode(n);
	(phead->next)->prev = newnode;
	newnode->next = phead->next;
	newnode->prev = phead;
	phead->next = newnode;
}
void ListPushback(List* phead, int n)
{
	List* newnode = Buynewnode(n);
	List* cur = phead;
	while (cur->next != phead)
	{
		cur = cur->next;
	}
	cur->next = newnode;
	newnode->prev = cur;
	newnode->next = phead;
	phead->prev = newnode;
}
void ListPopfront(List* phead)
{
	if (phead->next == phead)
	{
		printf("没东西无需删除\n");
	}
	else
	{
		List* node = phead->next;
		(node->next)->prev = phead;
		phead->next = node->next;
		free(node);
		node = NULL;
	}
}
void ListPopback(List* phead)
{
	if (phead->next == phead)
	{
		printf("无需删除\n");
	}
	else
	{
		List* node = phead->prev;
		(node->prev)->next = phead;
		phead->prev = node->prev;
		free(node);
		node = NULL;
	}
}
List* ListIsFind(List* phead, int n)
{
	List* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == n)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void ListInsertfront(List* phead, List* pos, int n)
{
	assert(pos);
	List* newnode = Buynewnode(n);
	newnode->next = pos;
   (pos->prev)->next = newnode;
	newnode->prev = pos->prev;
	pos->prev = newnode;
}
void ListInsertback(List* phead, List* pos, int n)
{
	assert(pos);
	List* newnode = Buynewnode(n);
	newnode->next = (pos->next);
	(pos->next)->prev = newnode;
	pos->next = newnode;
	newnode->prev = pos;
}
void ListDel(List* phead,List* pos)
{
	assert(pos);
	List* node = pos;
	(node->prev)->next = node->next;
	(node->next)->prev = (node->prev)->next;

}
void ListModify(List* pos, int n)
{
	assert(pos);
	pos->data = n;
}
void ListDestory(List* phead)
{
	List* cur = phead->next;
	List* cur1 = cur;
	while (cur != phead)
	{
		cur1 = cur;
		cur = cur->next;
		free(cur1);
		cur1 = NULL;
	}
	phead->next = phead;
	phead->prev = phead;
}
void ListPrint(List* phead)
{
	List* cur = phead->next;
	printf("NULL->");
	while (cur != phead)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
}

4.函数调用测试源文件test.c

复制代码
#include "list.h"
int main()
{
	List * phead = NULL;
	ListInit(&phead);
	ListPushback(phead, 5);
	ListPushback(phead, 4);
	ListPushback(phead, 3);
	ListPushback(phead, 2);
	ListPushback(phead, 1);
	ListPushfront(phead, 6);
	//ListPopfront(phead);
	//ListPopfront(phead);
	//ListPopfront(phead);
	//ListPopfront(phead);
	//ListPopback(phead);
	//ListDestory(phead);
	//ListInsertback(phead, ListIsFind(phead, 5), 9);
	//ListModify(ListIsFind(phead, 9),10);
	//ListDel(phead,ListIsFind(phead, 10));
	ListPrint(phead);
}
相关推荐
小柯博客8 分钟前
从零开始打造 OpenSTLinux 6.6 Yocto 系统(基于STM32CubeMX)(十二)
c语言·stm32·单片机·嵌入式硬件·php·嵌入式
C++ 老炮儿的技术栈1 小时前
UDP 与 TCP 的区别是什么?
开发语言·c++·windows·算法·visual studio
殇者知忧1 小时前
【论文笔记】若干矿井粉尘检测算法概述
深度学习·神经网络·算法·随机森林·机器学习·支持向量机·计算机视觉
wgslucky1 小时前
Dubbo报错:module java.base does not “opens java.lang“ to unnamed module
java·开发语言·dubbo
whyeekkk2 小时前
python打卡第48天
开发语言·python
DougLiang3 小时前
关于easyexcel动态下拉选问题处理
java·开发语言
mochensage3 小时前
C++信息学竞赛中常用函数的一般用法
java·c++·算法
chengooooooo3 小时前
leetcode Top100 238. 除自身以外数组的乘积|数组系列
算法·leetcode
GUIQU.3 小时前
【每日一题 | 2025年6.2 ~ 6.8】第16届蓝桥杯部分偏简单题
算法·蓝桥杯·每日一题
全职计算机毕业设计3 小时前
基于Java Web的校园失物招领平台设计与实现
java·开发语言·前端