创建一个简单的双链表
创始人
2024-11-14 12:34:40
0

1.ListNode.h头文件

#pragma once #include #include #include #include typedef int LTDataType; typedef struct ListNode { 	struct ListNode* next; 	struct ListNode* prev; 	LTDataType data; }LN; //初始化 LN* ListInit(); //尾插 void ListPushBank(LN* plist, LTDataType x); //头插 void ListPushFront(LN* plist, LTDataType x); //打印 void List_print(LN* plist); //尾删 void ListPopBank(LN* plist); //头删 void ListPopFront(LN* plist); //查找 LN* ListFind(LN* plist,LTDataType x); //pos之前插入x void ListInsert(LN* pos, LTDataType x); //删除pos位置 void ListErase(LN* pos); //销毁 void ListDestroyed(LN* plist); 

2.ListNode.c源文件函数的实现代码

#include"ListNode.h" //创建节点 LN* BuyListNode(LTDataType x) { 	LN* newnode = (LN*)malloc(sizeof(LN)); 	newnode->data = x; 	newnode->next = newnode->prev = newnode; 	return newnode; } //初始化 LN* ListInit() { 	LN* newnode = BuyListNode(0);//哨兵位 } //尾插 void ListPushBank(LN* plist,LTDataType x) { 	assert(plist); 	LN* ret = plist->prev; 	LN* newnode = BuyListNode(x);  	ret->next = newnode; 	newnode->next = plist; 	newnode->prev = ret; 	plist->prev = newnode; 	//快捷 	//ListInsert(plist->prev->next, x); } //打印 void List_print(LN* plist) { 	LN* ret = plist->next;//哨兵不需要打印 	while (ret != plist) 	{ 		printf("%d->", ret->data); 		ret = ret->next; 	} 	printf("NULL\n"); } //头插 void ListPushFront(LN* plist, LTDataType x) { 	assert(plist); 	LN* newnode = BuyListNode(x); 	LN* first = plist->next;  	plist->next = newnode; 	newnode->next = first; 	first->prev = newnode; 	newnode->prev = plist; 	//ListInsert(plist->next, x); } //尾删 void ListPopBank(LN* plist) { 	assert(plist); 	assert(plist->next != plist);  	LN* pcur = plist->prev; 	pcur->prev->next = plist; 	plist->prev = pcur->prev; 	free(pcur); 	pcur = NULL; 	//ListErase(plist->prev); } //头删 void ListPopFront(LN* plist) { 	assert(plist); 	assert(plist->next != plist); 	LN* first = plist->next; 	plist->next = first->next; 	first->next->prev = plist; 	free(first); 	 	//快捷方式 	//ListErase(plist->next); } //查找 LN* ListFind(LN* plist, LTDataType x) { 	assert(plist); 	LN* ret = plist->next; 	while (ret != plist) 	{ 		if (x == ret->data)  		{ 			return ret; 		} 		ret = ret->next; 	} 	return NULL; } //pos之前插入内容 void ListInsert(LN* pos, LTDataType x) { 	assert(pos); 	LN* newnode = BuyListNode(x); 	LN* pcur = pos->prev; 	pcur->next = newnode; 	newnode->next = pos; 	newnode->prev = pcur; 	pos->prev = newnode; } //删除pos位置 void ListErase(LN* pos) { 	assert(pos); 	LN* prev = pos->prev; 	LN* next = pos->next; 	prev->next = next; 	next->prev = prev; 	free(pos); } //销毁 void ListDestroyed(LN* plist) { 	assert(plist); 	LN* cur = plist->next; 	while (cur != plist) 	{ 		LN* ret = cur->next; 		free(cur); 		cur = ret; 	} 	free(plist); 	plist = NULL; 	printf("销毁成功\n"); }

3.test.c测试代码
 

#define  _CRT_SECURE_NO_WARNINGS 1 #include"ListNode.h" void menu() { 	printf("*******************\n"); 	printf("1.尾插       2.头插\n"); 	printf("3.尾删       4.头删\n"); 	printf("5.打印       6.查找\n"); 	printf("7.pos插入 8.删除pos\n"); 	printf("9.销毁      10.退出\n"); 	printf("*******************\n"); } int main() { 	LN* plist = ListInit(); 	int input = 0, x = 0, y = 0; 	do 	{ 		menu(); 		printf("请输入你需要操作的内容\n"); 		scanf("%d", &input); 		switch (input) 		{ 		case 1: 			printf("请输入你要尾插的内容,输入-1结束\n"); 			do 			{ 				scanf("%d", &x); 				if (x != -1) 				{ 					ListPushBank(plist, x); 				} 			} while (x != -1); 			break; 		case 2: 			printf("请输入你要头插的内容,输入-1结束\n"); 			do 			{ 				scanf("%d", &x); 				if (x != -1) 				{ 					ListPushFront(plist, x); 				} 			} while (x != -1); 			break; 		case 3: 			ListPopBank(plist); 			printf("尾删成功\n"); 			break; 		case 4: 			ListPopFront(plist); 			printf("头删成功\n"); 			break; 		case 5: 			List_print(plist); 			break; 		case 6: 			printf("请输入你要查找的数值\n"); 			scanf("%d", &x); 			LN* ret = ListFind(plist, x); 			if (ret != NULL) 			{ 				printf("找到了\n"); 			} 			else { 				printf("找不到你要的数值\n"); 			} 			break; 		case 7: 			printf("请输入pos的值\n"); 			scanf("%d", &x); 			LN* ret2 = ListFind(plist, x); 			if (ret2 == NULL) 			{ 				printf("你要查找的pos节点不存在\n"); 				exit(1); 			} 			else  			{ 				printf("请输入你要插入的数值\n"); 				scanf("%d", &y); 				ListInsert(ret2, y); 				printf("插入成功\n"); 			} 			break; 		case 8: 			printf("请输入你要删除的pos点\n"); 			scanf("%d", &x); 			LN* ret3 = ListFind(plist, x); 			if (ret3 == NULL) 			{ 				printf("要删除的pos点不存在\n"); 				exit(1); 			} 			else  			{ 				ListErase(ret3); 				printf("删除成功\n"); 			} 			break; 		case 9: 			ListDestroyed(plist); 			break; 		case 10: 			input = -1; 			printf("退出中..."); 			break; 		default: 			printf("请选择1-10的方法\n"); 			break; 		} 	} while (input != -1); 	return 0; }

相关内容

热门资讯

透视秘籍!红龙poker作弊指... 透视秘籍!红龙poker作弊指令,WePoKer功能竟然有透视,十分钟教程(有挂详情)1、起透看视 ...
透视模块!德州机器人代打脚本,... 透视模块!德州机器人代打脚本,新九方科技(辅助)竟然存在有脚本(哔哩哔哩)1、新九方科技公共底牌简单...
透视经验!wpk俱乐部是做什么... 透视经验!wpk俱乐部是做什么的,WPK购买果然是真的有神器,3分钟教程(竟然有挂)1、这是跨平台的...
透视指南!pokemomo辅助... 透视指南!pokemomo辅助工具,仟众部落辅助(辅助)都是真的有插件(哔哩哔哩)1、超多福利:超高...
透视诀窍!哈糖大菠萝挂,HHp... 透视诀窍!哈糖大菠萝挂,HHpoker有用一贯真的是有下载,第一分钟教程(有挂详细)透视诀窍!哈糖大...
透视攻略!werplan外卦神... 透视攻略!werplan外卦神器,唐山撸麻雀德技巧(辅助)切实是有方法(哔哩哔哩)1、不需要AI权限...
透视演示!aapoker怎么拿... 透视演示!aapoker怎么拿好牌,AApoker靠谱本来是有神器,4分钟教程(有挂秘籍)运aapo...
透视练习!pokemmo手机辅... 透视练习!pokemmo手机辅助软件,网易游戏辅助软件(辅助)真是一直总是有攻略(哔哩哔哩)1、进入...
透视指南!aapoker脚本,... 透视指南!aapoker脚本,AApoker工具一直存在有脚本,第二分钟教程(有挂功能)1、玩家可以...
透视练习!哈糖大菠萝有挂吗5个... 透视练习!哈糖大菠萝有挂吗5个常用方法,心悦踢透视辅助器免费(辅助)竟然一直总是有插件(哔哩哔哩)1...