带头双向链表的实现

链表结构示意图 

无论插入还是删除,建议将指定位置前面一个节点和后面一个节点都标记出来,然后就不用考虑次序问题,四句代码任意倒,就可以得出正确的代码

//头文件,包括节点结构体的定义和函数的声明
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int LTDataType;
typedef struct ListNode
{
	LTDataType _data;
	struct ListNode* _next;
	struct ListNode* _prev;
}ListNode;

// 创建返回链表的头结点.
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* pos, LTDataType x);
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos);
//函数实现的.c文件

#include"list.h"
ListNode* buynewnode(LTDataType x)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	if (newnode == NULL)
	{
		perror("malloc fail\n");
	}
	newnode->_data = x;
	newnode->_next = NULL;
	newnode->_prev = NULL;
	return newnode;
}
// 创建返回链表的头结点.
ListNode* ListCreate()
{
	ListNode* headnode = (ListNode*)malloc(sizeof(ListNode));
	if (headnode == NULL)
	{
		perror("malloc fail\n");
	}
	headnode->_data = 0;
	headnode->_next = headnode;
	headnode->_prev = headnode;
	return headnode;
}
// 双向链表销毁
void ListDestory(ListNode* pHead)
{
	ListNode* cur = pHead->_next;
	while (cur!=pHead)
	{
		ListNode* next = cur->_next;
		free(cur);
		//释放当前节点,然后向下继续走
		cur=next;
		
	}
	free(pHead);
}
// 双向链表打印
void ListPrint(ListNode* pHead)
{
	assert(pHead);
	ListNode* cur = pHead->_next;
	while (cur != pHead)
	{
		printf("%d ", cur->_data);
		cur = cur->_next;
	}


}
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListInsert(pHead, x);
}
// 双向链表尾删
void ListPopBack(ListNode* pHead)
{

}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListInsert(pHead->_next, x);
}
// 双向链表头删
void ListPopFront(ListNode* pHead)
{
	ListErase(pHead->_next);
}
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* cur = pHead->_next;
	while (cur != pHead)
	{
		if (cur->_data == x)
		{
			printf("found success\n");
			return cur;
		}
		cur = cur->_next;
	}
	printf("found fail\n");
	return pHead;
}
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);
	ListNode* newnode = buynewnode(x);
	ListNode* prev = pos->_prev;
	ListNode* next = pos->_next;

	prev->_next = newnode;
	newnode->_prev = prev;
	newnode->_next = pos;
	pos->_prev = newnode;

}
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{
	assert(pos);
	ListNode* prev = pos->_prev;
	ListNode* next = pos->_next;
	prev->_next = next;
	next->_prev = prev;
	free(pos);
	pos = NULL;
}
//测试结果是否正确的.c文件
#include"list.h"
int main()
{
	ListNode* s; 
	int a = 0;
	s = ListCreate();
	ListPushBack(s, 5);
	ListPushBack(s, 4);
	ListPushBack(s, 3);
	ListPushBack(s, 2);

	ListPrint(s);
	printf("\n");
	ListPushFront(s, 1);
	ListPushFront(s, 8);
	ListPushFront(s, 9);

	ListPrint(s);
	printf("\n");

	ListPopFront(s);
	ListPopFront(s);
	ListPopFront(s);
	ListPopFront(s);
	ListPrint(s);
	printf("\n");

	a=ListFind(s, 4)->_data;
	printf("%d\n", a);
	a=ListFind(s, 9)->_data;
	printf("%d\n", a);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值