带头 + 双向 + 循环链表增删查改实现

发布时间:2024年01月22日

目录

源码:

List.c文件:

List.h文件:

简单的测试:



很简单,没什么好说的,直接上源码。

源码:

List.c文件:

#include"DLList.h"

ListNode* creadNode(LTDataType x)
{
	ListNode* temp = (ListNode*)malloc(sizeof(ListNode));
	if (temp == NULL)
	{
		perror("malloc fail !\n");
		return -1;
	}
	temp->data = x;
	temp->next = NULL;
	temp->prev = NULL;
	return  temp;

}

// 创建返回链表的头结点.
ListNode* ListCreate()
{
	ListNode* temp = (ListNode*)malloc(sizeof(ListNode));
	if (temp == NULL)
	{
		perror("malloc fail !\n");
		return -1;
	}
	temp->next = temp;
	temp->prev = temp;
	temp->data = 0;
	return  temp;
}
// 双向链表销毁
void ListDestory(ListNode* pHead)
{
	//头节点一定不会为空
	assert(pHead);
	if (pHead->next == pHead)
	{
		free(pHead);
		pHead = NULL;
	}
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		ListNode* next = cur->next;
		free(cur);
		cur = next;
	}
	printf("销毁成功!");
}
// 双向链表打印
void ListPrint(ListNode* pHead)
{
	assert(pHead);
	if (pHead->next == pHead)
	{
		printf("List is NULL!\n");
		exit(-1);
	}
	printf("哨兵位<=>");
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		printf("%d<=>",cur->data);
		cur = cur->next;
	}
	printf("\n");
}

// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{
	ListNode* newNode = creadNode(x);
	ListNode* tail = pHead->next;
	while (tail->next != pHead)
	{
		tail = tail->next;
	}
	newNode->next = pHead;
	pHead->prev = newNode;
	tail->next = newNode;
	newNode->prev = tail;
}

// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
	assert(pHead);
	if (pHead->next == pHead)
	{
		printf("List is NULL!\n");
		exit(-1);
	}
	ListNode* tail = pHead->prev;
	ListNode* prev = tail->prev;
	pHead->prev = prev;
	prev->next = pHead;
	free(tail);
	
}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
	ListNode* newNode = creadNode(x);
	newNode->next = pHead->next;
	pHead->prev = newNode;
	pHead->next = newNode;
	newNode->prev = pHead;
}
// 双向链表头删
void ListPopFront(ListNode* pHead)
{
	assert(pHead);
	if (pHead->next == pHead)
	{
		printf("List is  NULL!\n");
		exit(-1);
	}
	ListNode* firstNode = pHead->next;
	ListNode* secondNode = firstNode->next;
	pHead->next = secondNode;
	secondNode->prev = pHead;
	free(firstNode);
	firstNode = NULL;
}
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
// 双向链表在pos的前面进行插入
void ListInsert(ListNode *pHead, ListNode* pos, LTDataType x)
{
	assert(pos);
	if (pos == pHead)
	{
		ListPushBack(pHead,x);
	}
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		if (cur->data == pos->data)
		{
			ListNode* prev = pos->prev;
			ListNode *newNode  = creadNode(x);
			prev->next = newNode;
			newNode->prev = prev;
			newNode->next = pos;
			pos->prev = newNode;
			return;
		}
		cur = cur->next;
	}
}
// 双向链表删除pos位置的节点
void ListErase(ListNode* pHead,ListNode* pos)
{
	assert(pHead);
	if (pos == pHead->next)
	{
		ListPopFront(pHead);
	}
	if (pos == pHead->prev)
	{
		ListPopBack(pHead);
	}
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		if (cur->data == pos->data)
		{
			ListNode* prev = cur->prev;
			ListNode* next = cur->next;
			prev->next = next;
			next->prev = prev;
			free(cur);
			return;
		}
		cur = cur->next;
	}
}

List.h文件:

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

// 带头 + 双向 + 循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{
	LTDataType data;
	struct ListNode* next;
	struct ListNode* prev;
}ListNode;

ListNode* creadNode();
// 创建返回链表的头结点.
ListNode* ListCreate();
// 双向链表销毁
void ListDestory(ListNode* pHead);
// 双向链表打印
void ListPrint(ListNode* pHead);
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);
// 双向链表尾删
void ListPopBack(ListNode* pHead);
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x);
// 双向链表头删
void ListPopFront(ListNode* pHead);
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pHead,ListNode* pos, LTDataType x);
// 双向链表删除pos位置的节点
void ListErase(ListNode* pHead,ListNode* pos);

简单的测试:

#include"DLList.h"

void test1(ListNode * head) {
	ListPushBack(head, 1);
	ListPushBack(head, 2);
	ListPushBack(head, 3);
	ListPushBack(head, 4);
	ListPushBack(head, 5);
	ListPopBack(head);
	ListPrint(head);

	ListPopBack(head);
	ListPrint(head);

	ListPopBack(head);
	ListPrint(head);

	ListPopBack(head);
	ListPrint(head);

	ListPopBack(head);
	ListPrint(head);

	ListPopBack(head);
	ListPrint(head);

}

void test2(ListNode* head) {
	ListPushBack(head, 1);
	ListPushBack(head, 2);
	ListPushBack(head, 3);
	ListPushBack(head, 4);
	ListPushBack(head, 5);
	ListPopFront(head);
	ListPrint(head);
}


void test3(ListNode* head) {
	ListPushBack(head, 1);
	ListPushBack(head, 2);
	ListPushBack(head, 3);
	ListPushBack(head, 4);
	ListPushBack(head, 5);
	ListFind(head,2);
	ListFind(head,7);
	ListPrint(head);
}

void test4(ListNode* head) {
	ListPushBack(head, 1);
	ListPushBack(head, 2);
	ListPushBack(head, 3);
	ListPushBack(head, 4);
	ListPushBack(head, 5);
	ListInsert(head, ListFind(head, 2),10);
	ListInsert(head, ListFind(head, 1),10);
	ListInsert(head, ListFind(head, 5),10);
	ListInsert(head, ListFind(head, 6),10);
	ListPrint(head);
}

void test5(ListNode* head) {
	ListPushBack(head, 1);
	ListPushBack(head, 2);
	ListPushBack(head, 3);
	ListPushBack(head, 4);
	ListPushBack(head, 5);
	ListErase(head,ListFind(head,5));
	ListErase(head,ListFind(head,1));
	ListErase(head,ListFind(head,2));
	ListPrint(head);
}


int main()
{
	ListNode* head = ListCreate();
	//test1(head);
	//test2(head);
	//test3(head);
	//test4(head);
	test5(head);

	ListDestory(head);

	return 0;
}

文章来源:https://blog.csdn.net/qq_51216031/article/details/135754134
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。