创建一个简单的双链表

1.ListNode.h头文件

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
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;
}

在C++中,创建一个双向链表通常涉及到定义两个节点结构(Node),以及包含指向头结点和尾节点指针的链表类。以下是创建一个简单的双向链表的基本步骤: 1. 定义节点(Node)结构: ```cpp class Node { public: int data; Node* prev; Node* next; // 构造函数 Node(int value) : data(value), prev(nullptr), next(nullptr) {} }; ``` 这里,`data`用于存储数据,`prev`和`next`分别是指向前一个节点和下一个节点的指针。 2. 创建链表类(LinkedList): ```cpp class DoublyLinkedList { private: Node* head; Node* tail; public: DoublyLinkedList() : head(nullptr), tail(nullptr) {} // 添加元素到链表尾部 void addNode(int value) { if (!head) { head = new Node(value); tail = head; } else { Node* newNode = new Node(value); tail->next = newNode; newNode->prev = tail; tail = newNode; } } // 删除指定值的节点(仅删除第一个找到的) void removeNode(int value) { Node* current = head; while (current != nullptr) { if (current->data == value) { if (current == head) { head = head->next; if (head) head->prev = nullptr; } else { current->prev->next = current->next; if (current == tail) tail = current->prev; } delete current; break; } current = current->next; } } // 其他操作,如遍历、查找等... }; ``` 以上是一个基本的双向链表实现,包含了添加节点和删除节点的操作。注意,这只是一个简化的版本,实际应用中可能需要处理更多边界条件和异常情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值