ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

数据结构之 带头、双向、循环链表的增删查改

2021-05-19 09:32:02  阅读:170  来源: 互联网

标签:ListNode void pos 链表 pHead 查改 双向 增删


目录

 

目录

双向链表的定义:

Dlist.h

DList.c

20210518.c

运行结果:


正文:

双向链表的定义:

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表

Dlist.h

// 带头+双向+循环链表增删查改实现
#pragma once

typedef int LTDataType;
typedef struct ListNode
{
	LTDataType data;
	struct ListNode* next;
	struct ListNode* prev;
}ListNode;

// 初始化链表
void DListInit(ListNode** pHead);
// 双向链表销毁
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);

//*****************测试******************//
void Test();

DList.c

#include"DList.h"
#include<stdio.h>
#include<malloc.h>
#include<assert.h>


ListNode* BuyDlistNode(LTDataType data)
{
	ListNode*newNode = (ListNode*)malloc(sizeof(ListNode));
	if (NULL == newNode)
	{
		assert(0);
		return NULL;
	}
	newNode->data = data;
	newNode->next = NULL;
	newNode->prev = NULL;

	return newNode;

}
// 初始化链表
void DListInit(ListNode** pHead)
{
	assert(pHead);
	*pHead = BuyDlistNode(0);//这个值随便填
	(*pHead)->next = *pHead;
	(*pHead)->prev = *pHead;
}


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

// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
	assert(pHead);
	ListErase(pHead->prev);
}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
	ListInsert(pHead->next, x);
}
// 双向链表头删
void ListPopFront(ListNode* pHead)
{
	assert(pHead);
	ListErase(pHead->next);
}


// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{

}



// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{
	ListNode *newNode = NULL;
	if (NULL == pos)
		return;
	newNode = BuyDlistNode(x);

	newNode->prev = pos->prev;
	newNode->next = pos;
	pos->prev->next = newNode;
	pos->prev = newNode;
}


// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{
	if (NULL == pos)
		return;

	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;

	free(pos);
}

// 双向链表销毁
void ListDestory(ListNode** pHead)
{
	assert(pHead);
	ListNode* cur = (*pHead)->next;
	while (cur != pHead)
	{
		(*pHead)->next = cur->next;
		free(cur);
		cur = (*pHead)->next;
	}
	free(*pHead);
	*pHead = NULL;
}


// 双向链表打印
void ListPrint(ListNode* pHead)
{
	assert(pHead);
	ListNode*cur = pHead->next;
	while (cur != pHead)
	{
		printf("%d", cur->data);
		cur = cur->next;
	}
	printf("\n");

}

void Test()
{
	ListNode* head = NULL;
	DListInit(&head);

	ListPushBack(head, 1);
	ListPushBack(head, 2);
	ListPushBack(head, 3);
	ListPushBack(head, 4);
	ListPushBack(head, 5);
	ListPushBack(head, 6);
	ListPrint(head);

	ListPushFront(head, 0);
	ListPrint(head);

	ListPopFront(head);
	ListPrint(head);

	ListPopBack(head);
	ListPopBack(head);
	ListPrint(head);

	ListDestory(&head);
}

20210518.c

写一个test.c让程序开始运行

#include"DList.h"

int main()
{

	Test();
	return 0;
}

运行结果:

 

 像链表这种题多画图很容易解决的,感谢小伙伴们的支持与帮助,希望大家都可以越来越好!

 

标签:ListNode,void,pos,链表,pHead,查改,双向,增删
来源: https://blog.csdn.net/weixin_47730988/article/details/117014157

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有