数据结构基础(带头节点的双向循环链表)

DLinkList.h

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

typedef int ElemType;

// SList - 单链表
// DList - 双链表
// 带头节点的双向循环链表 - 最优链表结构,任意位置插入、删除数据,时间复杂度O(1)
typedef struct ListNode{
   
	struct ListNode* prev;
	struct ListNode* next;
	ElemType data;
}ListNode;

// 初始化
ListNode* ListInit();
// 打印
void ListPrint(ListNode* phead);
// 销毁
void ListDestory(ListNode* phead);
// 尾插
void ListPushBack(ListNode* phead, ElemType x);
// 头产
void ListPushFront(ListNode* phead, ElemType x);
// 尾删
void ListPopBack(ListNode* phead);
// 头删
void ListPopFront(ListNode* phead);


// 查找节点的pos位置
ListNode* ListFind(ListNode* phead, ElemType x);
// 在pos位置前插入
void ListInsert(ListNode* pos, ElemType x);
// 删除pos位置的值
void ListErase(ListNode* pos);

DLinkList.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "DList.h"

// 返回节点(头节点、要插入的节点)
ListNode* BuyListNode(ElemType x)
{
   
	ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
	newNode->data = x;
	newNode->prev = NULL;
	newNode->next = NULL;

	return newNode;
}
// 初始化
ListNode* ListInit()
{
   
	ListNode* phead = BuyListNode(0);// 申请头节点,数据域传0
	phead->next = phead;
	phead->prev = phead;

	return phead;
}
// 打印
void ListPrint(ListNode* phead)
{
   
	assert(phead);
	ListNode* cur = phead->next;
	while (cur != phead)
	{
   
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
// 销毁
void ListDestory(ListNode* phead)
{
   
	assert(phead);

	ListNode* cur = phead->next;
	while (cur != phead)
	{
   
		ListNode* next = cur->next;// 先找到要删节点的下一个节点
		free(cur);
		cur = next;
	}
	free(phead);
	phead = NULL;
}
// 尾插(直接调用Insert(在pos位置前插入))
void ListPushBack(ListNode* phead, ElemType x)
{
   
	assert(phead);// 头节点不为空

	//ListNode* newNode = BuyListNode(x);
	 带头的双向循环链表不考虑没有节点,有节点的情况
	//ListNode* tail = phead->prev;// 定义一个尾指针,指向头节点的前驱

	 画图分析
	//tail->next = newNode;
	//newNode->prev = tail;
	//newNode->next = phead;
	//phead->prev = newNode;

	ListInsert(phead, x);

}
// 头插(直接调用Insert(在pos位置前插入))
void ListPushFront(ListNode* phead, ElemType x)
{
   
	assert(phead);

	 没有节点也不会有问题
	//ListNode* first = phead->next;
	//ListNode* newNode = BuyListNode(x);

	//phead->next = newNode;
	//newNode->prev = phead;
	//newNode->next = first;
	//first->prev = newNode;

	ListInsert(phead->next, x);

}

// 头删(可以调用Erase)
void ListPopFront(ListNode* phead)
{
   
	//assert(phead);
	//assert(phead->next != phead);// 链表没有一个节点

	//ListNode* first = phead->next;
	//ListNode* second = first->next;

	//phead->next = second;
	//second->prev = phead;

	//free(first);
	//first = NULL;
	ListErase(phead->next);
}
// 尾删(可以调用Erase)
void ListPopBack(ListNode* phead)
{
   
	//assert(phead);
	//assert(phead->next != phead);// 链表没有一个节点

	//ListNode* tail = phead->prev;
	//ListNode* prev = tail->prev;

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

	//free(tail);
	//tail = NULL;
	ListErase(phead->prev);
}

// 查找节点的pos位置
ListNode* ListFind(ListNode* phead, ElemType 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* pos, ElemType x)
{
   
	assert(pos);
	ListNode* prev = pos->prev;
	ListNode* newNode = BuyListNode(x);

	prev->next = newNode;
	newNode->prev = prev;
	newNode->next = pos;
	pos->prev = newNode;
}
// 删除pos位置的值
void ListErase(ListNode* pos)
{
   
	ListNode* prev = pos->prev;
	ListNode* next = pos->next;

	prev->next = next;
	next->prev = prev;
	free(pos);
	pos = NULL;
	
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "DList.h"

void testList1()
{
   
	ListNode* plist = ListInit();

	ListPushBack(plist, 1);
	ListPushBack(plist, 2);
	ListPushBack(plist, 3);
	ListPushBack(plist, 4);
	ListPrint(plist);

	ListPushFront(plist, 0);
	ListPushFront(plist, -1);
	ListPrint(plist);

	ListPopFront(plist);
	ListPrint(plist);

	ListPopBack(plist);
	ListPrint(plist);


	ListDestory(plist);
}
void testList2()
{
   
	ListNode* plist = ListInit();

	ListPushBack(plist, 1);
	ListPushBack(plist, 2);
	ListPushBack(plist, 3);
	ListPushBack(plist, 4);
	ListPrint(plist);

	ListNode* pos = ListFind(plist, 2);
	if (pos)
	{
   
		pos->data *= 10;
		printf("找到了,并且乘10\n");
	}
	else
		printf("没有找到\n");
	ListPrint(plist);

	ListInsert(pos, 300);
	ListPrint(plist);

	ListErase(pos);
	ListPrint(plist);
}
int main()
{
   
	testList1();
	return 0;
}

相关推荐

  1. 数据结构基础带头节点双向循环

    2023-12-05 19:04:04       42 阅读
  2. 数据结构_带头双向循环

    2023-12-05 19:04:04       16 阅读
  3. 数据结构基础(不带头节点单向非循环

    2023-12-05 19:04:04       36 阅读
  4. 带头双向循环基础

    2023-12-05 19:04:04       26 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-05 19:04:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-05 19:04:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-05 19:04:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-05 19:04:04       18 阅读

热门阅读

  1. Python函数(一)

    2023-12-05 19:04:04       42 阅读
  2. JWT 认证机制

    2023-12-05 19:04:04       28 阅读
  3. 【Node.js】笔记整理 1 - 基础知识

    2023-12-05 19:04:04       38 阅读
  4. 学习c#的第二十四天

    2023-12-05 19:04:04       35 阅读
  5. SSH:安全的远程登录和数据传输工具

    2023-12-05 19:04:04       38 阅读
  6. 06_单元测试与反射

    2023-12-05 19:04:04       30 阅读
  7. vue3对象reactive()数据改变页面不刷新

    2023-12-05 19:04:04       34 阅读