带头双链表增删查改实现

分为三个文件实现,头文件,函数实现文件,测试文件

头文件

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

//带头双向循环链表结构体声明
typedef struct DList
{
	int data;
	struct DList* prev;
	struct DList* next;
}DList;

//创建返回链表的头节点
DList* ListCreate();

//创建新节点
DList* Newnode(int x);

//打印
void DLPrint(DList* head);

//尾插
void DListPushback(DList* head, int x);

//尾删
void DListPopback(DList* head);

//头插
void DListPushfront(DList* head, int x);

//头删
void DListPopfront(DList* head);

//查找
DList* DListFind(DList* head, int x);

//在pos的前面插入
void DListInsert(DList* pos, int x);

//删除pos位置的节点
void DListErase(DList* pos);

//销毁
void DListDestory(DList* head);

函数实现文件

#include"DList.h"

//创建返回链表的头节点
DList* ListCreate()
{
	DList* head = (DList*)malloc(sizeof(DList));
	if (head == NULL)
	{
		perror("create malloc fail");
		return NULL;
	}
	head->next = head;
	head->prev = head;

	return head;
}

//创建新节点
DList* Newnode(int x)
{
	DList* newnode = (DList*)malloc(sizeof(DList));
	if (newnode == NULL)
	{
		perror("newnode malloc fail");
		return NULL;
	}
	newnode->data = x;
	newnode->prev = NULL;
	newnode->next = NULL;
	return newnode;
}

//打印
void DLPrint(DList* head)
{
	assert(head);
	DList* cur = head->next;
	printf("头节点<==>");
	while (cur != head)
	{
		printf("%d<==>", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

//尾插新节点
void DListPushback(DList* head, int x)
{
	assert(head);
	DList* newnode = Newnode(x);
	newnode->prev = head->prev;
	head->prev->next = newnode;
	newnode->next = head;
	head->prev = newnode;
}

//尾删
void DListPopback(DList* head)
{
	assert(head);
	assert(head->next!=head);
	DList* del = head->prev;
	DList* tailprev = head->prev->prev;
	tailprev->next = head;
	head->prev = tailprev;
	free(del);
}

//头插
void DListPushfront(DList* head,int x)
{
	assert(head);
	DList* newnode = Newnode(x);
	newnode->next = head->next;
	head->next->prev = newnode;
	newnode->prev = head;
	head->next = newnode;
}

//头删
void DListPopfront(DList* head)
{
	assert(head);
	assert(head->next!=head);
	DList* del = head->next;
	DList* newfirst = head->next->next;
	newfirst->prev = head;
	head->next = newfirst;
	free(del);
}

//查找
DList* DListFind(DList* head, int x)
{
	assert(head);
	assert(head->next != head);
	DList* cur = head->next;
	while (cur != head)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

//在pos的前面插入
void DListInsert(DList* pos, int x)
{
	assert(pos);
	DList* newnode = Newnode(x);
	DList* pospre = pos->prev;
	newnode->next = pos;
	pospre->next = newnode;
	newnode->prev = pospre;
	pos->prev = newnode;
}

//删除pos位置的节点
void DListErase(DList* pos)
{
	assert(pos);
	DList* del = pos;
	DList* posprev = pos->prev;
	DList* posnext = pos->next;
	posprev->next = posnext;
	posnext->prev = posprev;
	free(del);
}

//销毁
void DListDestory(DList* head)
{
	DList* cur = head->prev;
	DList* prev = cur->prev;
	while (cur != head)
	{
		free(cur);
		cur = prev;
		prev = prev->prev;
	}
	free(head);
	printf("销毁成功\n");
}

测试文件

#include"DList.h"

void testDLpushback()
{
	printf("测试尾插:\n");
	DList* head = ListCreate();
	DListPushback(head, 1);
	DListPushback(head, 2);
	DListPushback(head, 3);
	DListPushback(head, 4);
	DLPrint(head);
	DListDestory(head);
}

void testDLPopback()
{
	printf("测试尾删:\n");
	DList* head = ListCreate();
	DListPushback(head, 1);
	DListPushback(head, 2);
	DListPushback(head, 3);
	DListPushback(head, 4);
	DLPrint(head);

	DListPopback(head);
	DListPopback(head);
	DLPrint(head);
	DListDestory(head);
}

void testDLPushfront()
{
	printf("测试头插:\n");
	DList* head = ListCreate();
	DListPushfront(head, 1);
	DListPushfront(head, 2);
	DListPushfront(head, 3);
	DListPushfront(head, 4);
	DLPrint(head);
}

void testDLPopfront()
{
	printf("测试头删:\n");
	DList* head = ListCreate();
	DListPushfront(head, 1);
	DListPushfront(head, 2);
	DListPushfront(head, 3);
	DListPushfront(head, 4);
	DLPrint(head);
	DListPopfront(head);
	DListPopfront(head);
	DLPrint(head);
}

void testDListFind()
{
	printf("测试查找:\n");
	DList* head = ListCreate();
	DListPushfront(head, 1);
	DListPushfront(head, 2);
	DListPushfront(head, 3);
	DListPushfront(head, 4);
	DList* pos = DListFind(head, 4);
	if (pos == NULL)
	{
		printf("在链表中未找到该节点\n");
	}
	else
	{
		printf("找到了\n");
	}
	DListDestory(head);
}

void testInsert()
{
	printf("测试在pos位置插入:\n");
	DList* head = ListCreate();
	DListPushfront(head, 1);
	DListPushfront(head, 2);
	DListPushfront(head, 3);
	DListPushfront(head, 4);
	DLPrint(head);
	DList* pos = DListFind(head, 4);
	DListInsert(pos, 100);
	DLPrint(head);
	DListDestory(head);
}

void testPosErase()
{
	printf("测试删除pos节点:\n");
	DList* head = ListCreate();
	DListPushfront(head, 1);
	DListPushfront(head, 2);
	DListPushfront(head, 3);
	DListPushfront(head, 4);
	DLPrint(head);
	DList* pos = DListFind(head, 2);
	DListErase(pos);
	DLPrint(head);
	DListDestory(head);
}

void testDListDestory()
{
	printf("测试销毁:\n");
	DList* head = ListCreate();
	DListPushfront(head, 1);
	DListPushfront(head, 2);
	DListPushfront(head, 3);
	DListPushfront(head, 4);
	DLPrint(head);
	DListDestory(head);
}

int main()
{
	testDLpushback();
	testDLPopback();
	testDLPushfront();
	testDLPopfront();
	testDListFind();
	testInsert();
	testPosErase();
	testDListDestory();

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值