简单实现一个双链表

DList.h的内容:

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int  DLTDataType ;
typedef struct DListNode
{
	struct DListNode* next;
	struct DListNode* prev;
	DLTDataType data;
}DLTNode;
DLTNode* DListInit();
void DListPrint(DLTNode* phead);
void DListPushFront(DLTNode* phead, DLTDataType  x);//头插
void DListPushBack(DLTNode* phead, DLTDataType x);//尾插
void DListPopFront(DLTNode* phead);//头删
void DListPopBack(DLTNode* phead);//尾删
bool DListEmpty(DLTNode* phead);
size_t DListSize(DLTNode*phead);
DLTNode* DListFind(DLTNode* phead, DLTDataType x);
//在pos之前
void DListInsert(DLTNode* pos, DLTDataType x);
//删除pos位置
void DListEra(DLTNode* pos);

Dlist.c的内容

#include"DList.h"
DLTNode* DListInit()
{
	DLTNode* guard = (DLTNode*)malloc(sizeof(DLTNode));
	if (guard == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	guard->next = guard;
	guard->prev = guard;
return guard;
}
DLTNode* BuyListNode(DLTDataType x)
{
	DLTNode* node = (DLTNode*)malloc(sizeof(DLTNode));
	if (node == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	node->next = NULL;
	node->prev = NULL;
	node->data = x;
	return node;
}
void DListPrint(DLTNode* phead)
{
	assert(phead);
	printf("phead<=>");

	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d<=>", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
void DListPushBack(DLTNode* phead, DLTDataType x)//尾插
{
	assert(phead);
	DLTNode* newnode = BuyListNode(x);
	DLTNode* tail = phead->prev;
	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;
	//DListInsert(phead->next, x);
}
void DListPushFront(DLTNode* phead, DLTDataType  x)//头插
{
	assert(phead);
	//先链接newnode和phead->next节点的关系
	/*DLTNode* newnode = BuyListNode(x);
	DLTNode* first = phead->next;
	newnode->next = phead->next;
	phead->next->prev = newnode;
	newnode->prev = phead;*/
	//不关心顺序
	DLTNode* newnode = BuyListNode(x);
	DLTNode* first = phead->next;
	phead->next = newnode;
	newnode->prev = phead;
	newnode->next = first;
	first->prev = newnode;
}
void DListPopFront(DLTNode* phead)//头删
{
	assert(phead);
	assert(!DListEmpty(phead));
	DLTNode* first = phead->next;
	DLTNode* second = first->next;

	phead->next = second;
	second->prev = phead;
	free(first);
	first = NULL;
	//DListEra(phead->next);

}
void DListPopBack(DLTNode* phead)//尾删
{
	assert(phead);
	assert(!DListEmpty(phead));

	DLTNode* tail = phead->next;
	DLTNode* prev = tail->prev;

	prev->next = phead;
	phead->prev = prev;

	free(tail);
	tail = NULL;
	//DListEra(phead->prev);
}
bool DListEmpty(DLTNode* phead)
{
	assert(phead);
	return phead->next == phead;
}
size_t DListSize(DLTNode* phead)
{
	assert(phead);
	size_t n = 0;
	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		++n;
		cur = cur->next;
	}
	return n;
}
DLTNode* DListFind(DLTNode* phead, DLTDataType x)
{
	assert(phead);
	size_t n = 0;
	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return n;
}
//在pos之前
void DListInsert(DLTNode* pos, DLTDataType x)
{
	assert(pos);
	DLTNode* prev = pos->prev;
	DLTNode* newnode = BuyListNode(x);
	prev->next = newnode;
	newnode->prev = prev;
	newnode->next = pos;
	pos->prev = newnode;
}
//删除pos位置
void DListEra(DLTNode* pos)
{
	assert(pos);
	DLTNode* prev = pos->next;
	DLTNode* next = pos->next;
	prev->next = next;
	next->prev = prev;
	free(pos);
}
//可以传二级指针,内部置空头结点
//建议:也可以考虑一级指针,让调用的DListDestory人置空(保持接口的一致性)
void DListDestory(DLTNode* phead)//释放链表
{
	assert(phead);
	DLTNode* cur = phead->next;
	while (cur!=phead)
	{
		DLTNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
}

test.c的内容:

#include"DList.h"
void TestList1()
{
	//DLTNode* plist = NULL;
	DLTNode* plist = DListInit();
	DListPushBack(plist, 1);
	DListPushBack(plist, 2);
	DListPushBack(plist, 3);
	DListPushBack(plist, 4);

	DListPrint(plist);

	DListPushFront(plist, 10);
	DListPushFront(plist, 20);
	DListPushFront(plist, 30);
	DListPushFront(plist, 40);

	DListPrint(plist);

	DListPopBack(plist);
	DListPopBack(plist);
	DListPopBack(plist);
	DListPopBack(plist);
	DListPrint(plist);

	DListDestory(plist);
}
void TestList2()
{
	DLTNode* plist = DListInit();
	DListPushBack(plist, 1);
	DListPushBack(plist, 2);
	DListPushBack(plist, 3);
	DListPushBack(plist, 4);
	DListPrint(plist);

	DListPopFront(plist);
	DListPopFront(plist);
	DListPrint(plist);

	DListPopFront(plist);
	DListPopFront(plist);
	DListPrint(plist);

	DListDestory(plist);
	plist = NULL;
}

int main()
{
	//TestList1();
	TestList2();
	return  0;
}

运行一下TestList2():

在这里插入图片描述

在这里插入图片描述

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值