Doubly linked list (data structure) (C language)

Table of contents

concept

Implementation of the lead doubly linked list

Reminder

Structure definition of doubly linked list

Initialization of doubly linked list

Thoughts on headless one-way non-circular linked list without initialization function, sequence table and headed two-way circular linked list

Doubly linked list insert x before pos position

Printing of doubly linked list

Double linked list deletes the node at position pos

tail insertion of doubly linked list

Regarding the tail insertion of the singly linked list, the second-level pointer is needed, and the double-linked list does not need the second-level pointer.

Empty judgment of doubly linked list

tail deletion of doubly linked list

header of doubly linked list 

Delete the head of the doubly linked list

Doubly linked list finds the node with value x

Destruction of doubly linked list 

Modification of doubly linked list

Doubly linked list delete node with value x

 Doubly linked list calculates the total number of nodes (excluding phead)

Doubly linked list gets the node at position i

Clearing the doubly linked list

The total code (if you want to see the result directly, you can see here)


concept

A doubly linked list is also called a doubly linked list, which is a kind of linked list. Each data node in it has two pointers, which point to the direct successor and direct predecessor respectively . Therefore, starting from any node in the doubly linked list, you can easily access its predecessor node and successor node. We generally construct a two-way circular linked list . A circular linked list is a chained storage structure in which the last node points to the head node, forming a ring . Therefore, starting from any node in the circular linked list, any other node can be found.


Implementation of the lead doubly linked list

Reminder

List.h   is used for the referenced header file, the definition of the doubly linked list, and the declaration of the function.

List.c  is used for function definition.

Test.c  is used to test the function of the doubly linked list.

Structure definition of doubly linked list

under List.h

#pragma once//使同一个文件不会被包含(include)多次,不必担心宏名冲突

//先将可能使用到的头文件写上
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int LTDataType;//假设结点的数据域类型为 int
//给变量定义一个易于记忆且意义明确的新名字,并且便于以后存储其它类型时方便改动
//(比如我晚点想存double类型的数据,我就直接将 int 改为 double )

// 带哨兵位双向循环链表的结构体定义
typedef struct ListNode
{
	struct ListNode* prev;//前驱指针域:存放上一个结点的地址
	struct ListNode* next;//后继指针域:存放下一个结点的地址
	LTDataType data;//数据域
}LTNode;
//struct 关键字和 ListNode 一起构成了这个结构类型
//typedef 为这个结构起了一个别名,叫 LTNode,即:typedef struct ListNode LTNode 
//现在就可以像 int 和 double 那样直接使用 LTNode 来定义变量

Initialization of doubly linked list

under List.h

// 双向链表的初始化

// 如果是单链表直接给个空指针就行,不需要单独写一个函数进行初始化
// 即:LTNode* plist = NULL;
// 那为什么顺序表、带头双向循环链表有呢?
// 因为顺序表、带头双向循环链表的结构并不简单,
// 如:    顺序表顺序表为空size要为0,还要看capacity是否要开空间,
// 若不开空间capacity=0,指针要给空,若开空间,还要检查malloc是否成功
//	      带头双向循环链表要开个结点,检查malloc是否成功,然后让结点自己指向自己
// 顺序表和双向循环链表的初始化有点复杂,最好构建一个函数
LTNode* LTInit();

under List.c

#include"List.h"//别忘了

//动态申请一个结点
LTNode* BuyListNode(LTDataType x)
{
	LTNode* node = (LTNode*)malloc(sizeof(LTNode));
	if (node == NULL)//如果malloc失败
	{
		perror("malloc fail");
		return NULL;
	}
	//如果malloc成功
	//初始化一下,防止野指针,如果看到返回的是空指针,那逻辑可能有些错误
	node->next = NULL;
	node->prev = NULL;
	node->data = x;

	return node;
}

// 双向链表的初始化
LTNode* LTInit()
{
	LTNode* phead = BuyListNode(-1);//创建哨兵位
	//自己指向自己
	phead->next = phead;
	phead->prev = phead;

	return phead;
}

Thoughts on headless one-way non-circular linked list without initialization function, sequence table and headed two-way circular linked list

The headless one-way non-circular linked list structure is too simple, and the initialization only needs to assign a null pointer directly, without writing a separate function for initialization.

即:LTNode* plist = NULL;

Then why does the sequential table and the leading bidirectional circular linked list write a separate function for initialization?
Because the structure of the sequential list and the leading bidirectional circular linked list is not simple.

like:

If the sequence table is empty, the size must be 0, and it also depends on whether the capacity needs to be opened. If the space is not opened, the pointer must be empty. If the space is opened, it is necessary to check whether the malloc is successful.

The leading two-way circular linked list needs to open a node, check whether malloc is successful, and then let the node point to itself.

The initialization of the sequential list and the doubly circular linked list is a bit complicated, it is best to build a function.

Doubly linked list insert x before pos position

under List.h

// 双向链表在pos位置之前进行插入
void LTInsert(LTNode* pos, LTDataType x);

under List.c

// 双向链表在pos位置之前进行插入
void LTInsert(LTNode* pos, LTDataType x)
{
	assert(pos);//pos肯定不为空

	LTNode* prev = pos->prev;
	LTNode* newnode = BuyListNode(x);//创建一个需要插入的结点

	prev->next = newnode;
	newnode->prev = prev;

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

Printing of doubly linked list

under List.h

// 双向链表打印
void LTPrint(LTNode* phead);

under List.c

// 双向链表打印
void LTPrint(LTNode* phead)
{
	assert(phead);//有哨兵位
	printf("<=>phead<=>");
	LTNode* cur = phead->next;//cur指向第一个要打印的结点
	while (cur != phead)//cur等于头结点时打印就结束了
	{
		printf("%d<=>", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

under Test.c

#include"List.h"//别忘了

//测试函数
void TestList1()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 1);
	LTInsert(plist, 2);
	LTInsert(plist, 3);
	LTInsert(plist, 4);
	LTPrint(plist);
}

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

Double linked list deletes the node at position pos

under List.h

// 双向链表删除pos位置的结点
void LTErase(LTNode* pos);

under List.c

// 双向链表删除pos位置的结点
void LTErase(LTNode* pos)
{
	assert(pos);//pos肯定不为空

	LTNode* posprev = pos->prev;
	LTNode* posnext = pos->next;

	posprev->next = posnext;
	posnext->prev = posprev;

	free(pos);
	pos = NULL;
    //这个置空其实已经没有意义了,形参的改变不会改变实参
}

under Test.c

//测试函数
void TestList1()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 1);
	LTInsert(plist, 2);
	LTInsert(plist, 3);
	LTInsert(plist, 4);
	LTPrint(plist);

	LTErase(plist->next);
	LTPrint(plist);

}

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

tail insertion of doubly linked list

under List.h

//双向链表优于单链表的点——不需要找尾、二级指针
//(我们改的不是结构体的指针,改的是结构体的变量)
// 双向链表的尾插
void LTPushBack(LTNode* phead, LTDataType x);

under List.c

Method 1: (It is convenient for novices to better understand the tail insertion of the doubly linked list) 

// 双向链表的尾插
void LTPushBack(LTNode* phead, LTDataType x)
{
	assert(phead);//有哨兵位
    
    //法一:(便于新手更好地理解双向链表的尾插)
	//一步就可完成链表为空/不为空的尾插——因为有哨兵位
	LTNode* newnode = BuyListNode(x);//创建一个要插入的结点
	LTNode* tail = phead->prev;

	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;
}

Method 2: Function reuse (simple and convenient)

// 双向链表的尾插
void LTPushBack(LTNode* phead, LTDataType x)
{
	assert(phead);//有哨兵位

	//法二:函数复用(简单方便)
	LTInsert(phead, x);
}

Regarding the tail insertion of the singly linked list, the second-level pointer is needed, and the double-linked list does not need the second-level pointer.

The single-linked list changes the pointer of the structure, and the double-linked list changes the variable of the structure

The difference between the second-level pointer and the first-level pointer is that the level of the variable pointed to by the pointer is different. The first-level pointer points to the variable of the structure, while the second-level pointer points to the address of the structure pointer .
In a singly linked list, when deleting or inserting nodes in the linked list, it is necessary to update the pointers between the nodes. If a first-level pointer is used, the operation will directly change the pointer to the node, and it is difficult to achieve the goal. Therefore, it is necessary to pass a secondary pointer so that the function can modify the address pointing to the node pointer, that is, modify the address stored in the previous node pointer variable .
In the doubly linked list, each node not only saves a pointer to the next node, but also saves a pointer to the previous node. The bidirectional pointer relationship between nodes makes the insertion and deletion of nodes more convenient. The doubly linked list does not need to pass the secondary pointer, because in the deletion and insertion operation of the node, only the pointers of the nodes before and after the current node need to be modified first , and there is no need to directly change the addresses stored in the pointer variables of the nodes before and after .
To sum up, the singly linked list only has pointers to the next node, and the pointer relationship between nodes is modified by passing the secondary pointer, making the operation more flexible; while the nodes of the doubly linked list have a two-way pointer relationship, no need Directly change the addresses stored in the front and rear node pointer variables, so only one level of pointers needs to be passed .

Singly linked list (comparison):

under Test.c

//测试函数
void TestList2()
{
	LTNode* plist = LTInit();
	LTPushBack(plist, 5);
	LTPushBack(plist, 6);
	LTPushBack(plist, 7);
	LTPushBack(plist, 8);
	LTPrint(plist);
}

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

Empty judgment of doubly linked list

Before tail deletion/head deletion, we must first determine whether the linked list is empty.

under List.h

// 双向链表的判空
bool LTEmpty(LTNode* phead);

under List.c

// 双向链表的判空
bool LTEmpty(LTNode* phead)
{
	assert(phead);

	return phead->next == phead;
	//两者相等就是空链表(返回真),两者不相等就不是空链表(返回假)
}

tail deletion of doubly linked list

under List.h

// 双向链表的尾删
void LTPopBack(LTNode* phead);

under List.c

Method 1: (It is convenient for novices to better understand the tail deletion of the doubly linked list)

// 双向链表的尾删
void LTPopBack(LTNode* phead)
{
	assert(phead);//有哨兵位
	assert(!LTEmpty(phead));//判空

    //法一:(便于新手更好地理解双向链表的尾删)
	LTNode* tail = phead->prev;
	LTNode* tailPrev = tail->prev;

	tailPrev->next = phead;
	phead->prev = tailPrev;
	free(tail);
	tail = NULL;
}

Method 2: Function reuse (simple and convenient)

// 双向链表的尾删
void LTPopBack(LTNode* phead)
{
	assert(phead);//有哨兵位
    assert(!LTEmpty(phead));//判空
	
    //法二:函数复用
	LTErase(phead->prev);
}

under Test.c

//测试函数
void TestList2()
{
	LTNode* plist = LTInit();
	LTPushBack(plist, 5);
	LTPushBack(plist, 6);
	LTPushBack(plist, 7);
	LTPushBack(plist, 8);
	LTPrint(plist);

	LTPopBack(plist);
	LTPopBack(plist);
	LTPrint(plist);
}

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

header of doubly linked list 

under List.h

// 双向链表头插
void LTPushFront(LTNode* phead, LTDataType x);

under List.c

Method 1: Only use two pointers, phead and newnode (for novices to better understand the head insertion of the doubly linked list)

// 双向链表头插
void LTPushFront(LTNode* phead, LTDataType x)
{
	assert(phead);//有哨兵位

	LTNode* newnode = BuyListNode(x);//创建一个要插入的结点
    
    //法一:只用phead和newnode两个指针(便于新手更好地理解双向链表的头插)
    //顺序很重要!!!
	newnode->next = phead->next;
	phead->next->prev = newnode;

	phead->next = newnode;
	newnode->prev = phead;
}

Method 2: Use first to record the position of the first node (it is convenient for novices to better understand the head insertion of the doubly linked list)

// 双向链表头插
void LTPushFront(LTNode* phead, LTDataType x)
{
	assert(phead);//哨兵位

	LTNode* newnode = BuyListNode(x);//创建一个要插入的结点

	//法二:多用了first先记住第一个结点(便于新手更好地理解双向链表的头插)
	LTNode* first = phead->next;
	phead->next = newnode;
	newnode->prev = phead;

	newnode->next = first;
	first->prev = newnode;
}

Method 3: Function reuse (simple and convenient)

// 双向链表头插
void LTPushFront(LTNode* phead, LTDataType x)
{
	assert(phead);//有哨兵位
	
	//法三:函数复用(简单方便)
	LTInsert(phead->next, x);
}

under Test.c

//测试函数
void TestList3()
{
	LTNode* plist = LTInit();
	LTPushFront(plist, 1);
	LTPushFront(plist, 2);
	LTPushFront(plist, 3);
	LTPushFront(plist, 4);
	LTPrint(plist);
}

int main()
{
	TestList3();
	return 0;
}

Delete the head of the doubly linked list

under List.h

// 双向链表头删
void LTPopFront(LTNode* phead);

under List.c

Method 1: (It is convenient for novices to better understand the head deletion of the doubly linked list)

// 双向链表头删
void LTPopFront(LTNode* phead)
{
	assert(phead);//有哨兵位
	assert(!LTEmpty(phead));//判空
	
    //法一:(便于新手更好地理解双向链表的头删)
	LTNode* head = phead->next;
	LTNode* headnext = head->next;

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

	free(head);
	head = NULL;
}

Method 2: Function reuse (simple and convenient) 

// 双向链表头删
void LTPopFront(LTNode* phead)
{
	assert(phead);//有哨兵位
    assert(!LTEmpty(phead));//判空
	
    //法二:函数复用(简单方便)
	LTErase(phead->next);
}

under Test.c

//测试函数
void TestList3()
{
	LTNode* plist = LTInit();
	LTPushFront(plist, 1);
	LTPushFront(plist, 2);
	LTPushFront(plist, 3);
	LTPushFront(plist, 4);
	LTPrint(plist);

	LTPopFront(plist);
	LTPopFront(plist);
	LTPrint(plist);
}

int main()
{
	TestList3();
	return 0;
}

Doubly linked list finds the node with value x

under List.h

// 双向链表查找值为x的结点
LTNode* LTFind(LTNode* phead, LTDataType x);

under List.c

// 双向链表查找值为x的结点
LTNode* LTFind(LTNode* phead, LTDataType x)
{
	assert(phead);//有哨兵位

	LTNode* cur = phead->next;
	while (cur != phead)//让cur去遍历
	{
		if (cur->data == x)//如果找到
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;//如果没找到
}

under Test.c

//测试函数
TestList4()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 1);
	LTInsert(plist, 2);
	LTInsert(plist, 3);
	LTInsert(plist, 4);
	LTPrint(plist);

	LTNode* pos = LTFind(plist, 3);
	if (pos)
	{
		LTErase(pos);
		pos = NULL;
	}

	LTPrint(plist);
}

int main()
{
	TestList4();
	return 0;
}

Destruction of doubly linked list 

under List.h

// 双向链表的销毁
void LTDestory(LTNode* phead);

under List.c

   

  

 

// 双向链表的销毁
void LTDestory(LTNode* phead)
{
	assert(phead);

	LTNode* cur = phead->next;//让cur遍历
	while (cur != phead)
	{
		LTNode* curnext = cur->next;
		free(cur);
		cur = curnext;
	}
	free(phead);
	phead = NULL;
	//这个置空其实已经没有意义了,形参的改变不会改变实参
	//我们为了保持接口的一致性,不传二级指针,选择在测试的时候置空
}

under Test.c

//测试函数
TestList4()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 1);
	LTInsert(plist, 2);
	LTInsert(plist, 3);
	LTInsert(plist, 4);
	LTPrint(plist);

	LTNode* pos = LTFind(plist, 3);
	if (pos)
	{
		LTErase(pos);
		pos = NULL;
	}
	LTPrint(plist);

	LTDestory(plist);
	plist = NULL;//在这里置空
}

Modification of doubly linked list

under List.h

// 双向链表的修改,修改pos位置的值为x
void LTModify(LTNode* pos, LTDataType x);

under List.c

// 双向链表的修改,修改pos位置的值为x
void LTModify(LTNode* pos, LTDataType x)
{
	assert(pos);
	pos->data = x;
}

under Test.c

//测试函数
TestList5()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 1);
	LTInsert(plist, 2);
	LTInsert(plist, 3);
	LTInsert(plist, 4);
	LTPrint(plist);

	LTModify(plist->next,5);
	LTPrint(plist);
}

int main()
{
	TestList5();
	return 0;
}

Doubly linked list delete node with value x

under List.h

// 双向链表删除值为x的结点
void LTRemove(LTNode* phead,LTDataType x);

under List.c

// 双向链表删除值为x的结点
void LTRemove(LTNode* phead, LTDataType x)
{
	assert(phead);//有哨兵位
	LTNode* pos = phead->next;
	while (pos != phead)
	{
		pos = LTFind(phead, x);
		if (pos == NULL)//如果遍历完
		{
			return NULL;
		}
		LTErase(pos);
		pos = pos->next;
	}
}

under Test.c

TestList6()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 1);
	LTInsert(plist, 3);
	LTInsert(plist, 3);
	LTInsert(plist, 4);
	LTInsert(plist, 3);
	LTPrint(plist);

	LTRemove(plist, 3);
	LTPrint(plist);
}

int main()
{
	TestList6();
	return 0;
}

 Doubly linked list calculates the total number of nodes (excluding phead)

under List.h

// 双向链表计算结点总数(不计phead)
int LTTotal(LTNode* phead);

under List.c

// 双向链表计算结点总数(不计phead)
int LTTotal(LTNode* phead)
{
	assert(phead);//有哨兵位

	int count = 0;//count来计数
	LTNode* cur = phead->next;//让cur去遍历
	while (cur != phead)
	{
		count++;
		cur = cur->next;
	}
	return count;
}

under Test.c

TestList6()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 1);
	LTInsert(plist, 3);
	LTInsert(plist, 3);
	LTInsert(plist, 4);
	LTInsert(plist, 3);
	LTPrint(plist);

	LTRemove(plist, 3);
	LTPrint(plist);

	printf("%d\n", LTTotal(plist));
}

int main()
{
	TestList6();
	return 0;
}

Doubly linked list gets the node at position i

under List.h

// 双向链表获取第i位置的结点
LTNode* LTGet(LTNode* phead, int i);

under List.c

// 双向链表获取第i位置的结点
LTNode* LTGet(LTNode* phead, int i)
{
	assert(phead);//有哨兵位

	int length = LTTotal(phead);
	LTNode* cur = phead->next;
	if (i == 0)
	{
		return phead;
	}
	else if (i<0 || i>length)//位置不合法
	{
		return NULL;
	}
	else if (i <= (length / 2))//从表头开始遍历
	{
		cur = phead->next;
		for (int count = 1; count < i; count++)
		{
			cur = cur->next;
		}
	}
	else//从表尾开始遍历
	{
		cur = phead->prev;
		for (int count = 1; count <= length - i; count++)
		{
			cur = cur->prev;
		}
	}
	return cur;
}

under Test.c

//测试函数
TestList7()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 5);
	LTInsert(plist, 6);
	LTInsert(plist, 7);
	LTInsert(plist, 8);
	LTInsert(plist, 9);
	LTPrint(plist);

	LTNode* pos = LTGet(plist,3);
	if (pos)
	{
		LTErase(pos);
		pos = NULL;
	}
	LTPrint(plist);
}

int main()
{
	TestList7();
	return 0;
}

Clearing the doubly linked list

under List.h

// 双向链表的清空
void LTClean(LTNode* phead);

under List.c

// 双向链表的清空
void LTClear(LTNode* phead)
{
	assert(phead);//有哨兵位

	while (!LTEmpty(phead))//如果不为空就一直头删
	{
		LTPopFront(phead);
	}
}

under Test.c

//测试函数
TestList8()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 5);
	LTInsert(plist, 6);
	LTInsert(plist, 7);
	LTInsert(plist, 8);
	LTInsert(plist, 9);
	LTPrint(plist);

	LTClear(plist);
	LTPrint(plist);
}

int main()
{
	TestList8();
	return 0;
}

The total code (if you want to see the result directly, you can see here)

under List.h

#pragma once//使同一个文件不会被包含(include)多次,不必担心宏名冲突


//先将可能使用到的头文件写上
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int LTDataType;//假设结点的数据域类型为 int
//给变量定义一个易于记忆且意义明确的新名字,并且便于以后存储其它类型时方便改动
//(比如我晚点想存double类型的数据,我就直接将 int 改为 double )

// 带哨兵位双向循环链表的结构体定义
typedef struct ListNode
{
	struct ListNode* prev;//前驱指针域:存放上一个结点的地址
	struct ListNode* next;//后继指针域:存放下一个结点的地址
	LTDataType data;//数据域
}LTNode;
//struct 关键字和 ListNode 一起构成了这个结构类型
//typedef 为这个结构起了一个别名,叫 LTNode,即:typedef struct ListNode LTNode 
//现在就可以像 int 和 double 那样直接使用 LTNode 来定义变量



// 双向链表的初始化

// 如果是单链表直接给个空指针就行,不需要单独写一个函数进行初始化
// 即:LTNode* plist = NULL;
// 那为什么顺序表、带头双向循环链表有呢?
// 因为顺序表、带头双向循环链表的结构并不简单,
// 如:顺序表顺序表为空size要为0,还要看capacity是否要开空间,
//若不开空间capacity=0,指针要给空,若开空间,还要检查malloc是否成功
// 带头双向循环链表要开个结点,检查malloc是否成功,然后让结点自己指向自己
// 顺序表和双向循环链表的初始化有点复杂,最好构建一个函数
LTNode* LTInit();

// 双向链表在pos位置之前进行插入x
void LTInsert(LTNode* pos, LTDataType x);

// 双向链表的打印
void LTPrint(LTNode* phead);

// 双向链表删除pos位置的结点
void LTErase(LTNode* pos);

//双向链表优于单链表的点——不需要找尾、二级指针
// (我们改的不是结构体的指针,改的是结构体的变量)
// 双向链表的尾插
void LTPushBack(LTNode* phead, LTDataType x);

// 双向链表的判空
bool LTEmpty(LTNode* phead);

// 双向链表的尾删
void LTPopBack(LTNode* phead);

// 双向链表头插
void LTPushFront(LTNode* phead, LTDataType x);

// 双向链表头删
void LTPopFront(LTNode* phead);

// 双向链表查找值为x的结点
LTNode* LTFind(LTNode* phead, LTDataType x);

// 双向链表的销毁
void LTDestory(LTNode* phead);

// 双向链表的修改,修改pos位置的值为x
void LTModify(LTNode* pos, LTDataType x);

// 双向链表删除值为x的结点
void LTRemove(LTNode* phead, LTDataType x);

// 双向链表计算结点总数(不计phead)
int LTTotal(LTNode* phead);

// 双向链表获取第i位置的结点
LTNode* LTGet(LTNode* phead, int i);

// 双向链表的清空
void LTClear(LTNode* phead);

under List.c

#include"List.h"

//动态申请一个结点
LTNode* BuyListNode(LTDataType x)
{
	LTNode* node = (LTNode*)malloc(sizeof(LTNode));
	if (node == NULL)//如果malloc失败
	{
		perror("malloc fail");
		return NULL;
	}
	//如果malloc成功
	//初始化一下,防止野指针,如果看到返回的是空指针,那逻辑可能有些错误
	node->next = NULL;
	node->prev = NULL;
	node->data = x;

	return node;
}

// 双向链表的初始化
LTNode* LTInit()
{
	LTNode* phead = BuyListNode(-1);
	//自己指向自己
	phead->next = phead;
	phead->prev = phead;

	return phead;
}

// 双向链表在pos位置之前进行插入x
void LTInsert(LTNode* pos, LTDataType x)
{
	assert(pos);//pos肯定不为空

	LTNode* prev = pos->prev;
	LTNode* newnode = BuyListNode(x);//创建一个需要插入的结点

	prev->next = newnode;
	newnode->prev = prev;

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

// 双向链表的打印
void LTPrint(LTNode* phead)
{
	assert(phead);//有哨兵位
	printf("<=>phead<=>");
	LTNode* cur = phead->next;//cur指向第一个要打印的结点
	while (cur != phead)//cur等于头结点时打印就结束了
	{
		printf("%d<=>", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

// 双向链表删除pos位置的结点
void LTErase(LTNode* pos)
{
	assert(pos);//pos肯定不为空

	LTNode* posprev = pos->prev;
	LTNode* posnext = pos->next;

	posprev->next = posnext;
	posnext->prev = posprev;

	free(pos);
	pos = NULL;
	//这个置空其实已经没有意义了,形参的改变不会改变实参
}

// 双向链表的尾插
void LTPushBack(LTNode* phead, LTDataType x)
{
	assert(phead);//有哨兵位

	//法一:(便于新手更好地理解双向链表的尾插)
	//一步就可完成链表为空/不为空的尾插
	//LTNode* newnode = BuyListNode(x);
	//LTNode* tail = phead->prev;

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

	//法二:函数复用(简单方便)
	LTInsert(phead, x);
}

// 双向链表的判空
bool LTEmpty(LTNode* phead)
{
	assert(phead);

	return phead->next == phead;
	//两者相等就是空链表(返回真),两者不相等就不是空链表(返回假)
}

// 双向链表的尾删
void LTPopBack(LTNode* phead)
{
	assert(phead);//有哨兵位

	//法一:(便于新手更好地理解双向链表的尾删)
	//assert(!LTEmpty(phead));//判空

	//LTNode* tail = phead->prev;
	//LTNode* tailPrev = tail->prev;

	//tailPrev->next = phead;
	//phead->prev = tailPrev;
	//free(tail);
	//tail = NULL;

	//法二:函数复用
	LTErase(phead->prev);
}

// 双向链表头插
void LTPushFront(LTNode* phead, LTDataType x)
{
	assert(phead);//有哨兵位

	//LTNode* newnode = BuyListNode(x);//创建一个要插入的结点

	//法一:只用phead和newnode两个指针(便于新手更好地理解双向链表的头插)
	//newnode->next = phead->next;
	//phead->next->prev = newnode;

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

	//法二:多用了first先记住第一个结点(便于新手更好地理解双向链表的头插)
	//LTNode* first = phead->next;
	//phead->next = newnode;
	//newnode->prev = phead;

	//newnode->next = first;
	//first->prev = newnode;

	//法三:函数复用(简单方便)
	LTInsert(phead->next, x);
}

// 双向链表头删
void LTPopFront(LTNode* phead)
{
	assert(phead);//有哨兵位
	assert(!LTEmpty(phead));//判空

	//法一:(便于新手更好地理解双向链表的头删)
	//LTNode* head = phead->next;
	//LTNode* headnext = head->next;

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

	//free(head);
	//head = NULL;

	//法二:函数复用(简单方便)
	LTErase(phead->next);
}

// 双向链表查找值为x的结点
LTNode* LTFind(LTNode* phead, LTDataType x)
{
	assert(phead);//有哨兵位

	LTNode* cur = phead->next;
	while (cur != phead)//让cur去遍历
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

// 双向链表的销毁
void LTDestory(LTNode* phead)
{
	assert(phead);

	LTNode* cur = phead->next;
	while (cur != phead)
	{
		LTNode* curnext = cur->next;
		free(cur);
		cur = curnext;
	}
	free(phead);
	phead = NULL;
	//这个置空其实已经没有意义了,形参的改变不会改变实参
	//我们为了保持接口的一致性,不传二级指针,选择在测试的时候置空
}

// 双向链表的修改,修改pos位置的值为x
void LTModify(LTNode* pos, LTDataType x)
{
	assert(pos);//pos肯定不为空
	pos->data = x;
}

// 双向链表删除值为x的结点
void LTRemove(LTNode* phead, LTDataType x)
{
	assert(phead);//有哨兵位
	LTNode* pos = phead->next;
	while (pos != phead)
	{
		pos = LTFind(phead, x);
		if (pos == NULL)//如果遍历完
		{
			return NULL;
		}
		LTErase(pos);
		pos = pos->next;
	}
}

// 双向链表计算结点总数(不计phead)
int LTTotal(LTNode* phead)
{
	assert(phead);//有哨兵位

	int count = 0;//count来计数
	LTNode* cur = phead->next;//让cur去遍历
	while (cur != phead)
	{
		count++;
		cur = cur->next;
	}
	return count;
}

// 双向链表获取第i位置的结点
LTNode* LTGet(LTNode* phead, int i)
{
	assert(phead);//有哨兵位

	int length = LTTotal(phead);
	LTNode* cur = phead->next;
	if (i == 0)
	{
		return phead;
	}
	else if (i<0 || i>length)//位置不合法
	{
		return NULL;
	}
	else if (i <= (length / 2))//从表头开始遍历
	{
		cur = phead->next;
		for (int count = 1; count < i; count++)
		{
			cur = cur->next;
		}
	}
	else//从表尾开始遍历
	{
		cur = phead->prev;
		for (int count = 1; count <= length - i; count++)
		{
			cur = cur->prev;
		}
	}
	return cur;
}

// 双向链表的清空
void LTClear(LTNode* phead)
{
	assert(phead);//有哨兵位

	while (!LTEmpty(phead))//如果不为空就一直头删
	{
		LTPopFront(phead);
	}
}

under Test.c

#include"List.h"

//测试函数
void TestList1()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 1);
	LTInsert(plist, 2);
	LTInsert(plist, 3);
	LTInsert(plist, 4);
	LTPrint(plist);

	LTErase(plist->next);
	LTPrint(plist);

}

//测试函数
void TestList2()
{
	LTNode* plist = LTInit();
	LTPushBack(plist, 5);
	LTPushBack(plist, 6);
	LTPushBack(plist, 7);
	LTPushBack(plist, 8);
	LTPrint(plist);

	LTPopBack(plist);
	LTPopBack(plist);
	LTPrint(plist);

}

//测试函数
void TestList3()
{
	LTNode* plist = LTInit();
	LTPushFront(plist, 1);
	LTPushFront(plist, 2);
	LTPushFront(plist, 3);
	LTPushFront(plist, 4);
	LTPrint(plist);

	LTPopFront(plist);
	LTPopFront(plist);
	LTPrint(plist);
}

//测试函数
TestList4()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 1);
	LTInsert(plist, 2);
	LTInsert(plist, 3);
	LTInsert(plist, 4);
	LTPrint(plist);

	LTNode* pos = LTFind(plist, 3);
	if (pos)
	{
		LTErase(pos);
		pos = NULL;
	}
	LTPrint(plist);

	LTDestory(plist);
	plist = NULL;//在这里置空
}

//测试函数
TestList5()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 1);
	LTInsert(plist, 2);
	LTInsert(plist, 3);
	LTInsert(plist, 4);
	LTPrint(plist);

	LTModify(plist->next, 5);
	LTPrint(plist);

}

TestList6()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 1);
	LTInsert(plist, 3);
	LTInsert(plist, 3);
	LTInsert(plist, 4);
	LTInsert(plist, 3);
	LTPrint(plist);

	LTRemove(plist, 3);
	LTPrint(plist);

	printf("%d\n", LTTotal(plist));
}

//测试函数
TestList7()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 5);
	LTInsert(plist, 6);
	LTInsert(plist, 7);
	LTInsert(plist, 8);
	LTInsert(plist, 9);
	LTPrint(plist);

	LTNode* pos = LTGet(plist, 3);
	if (pos)
	{
		LTErase(pos);
		pos = NULL;
	}
	LTPrint(plist);

	LTClear(plist);
	LTPrint(plist);
}

//测试函数
TestList8()
{
	LTNode* plist = LTInit();
	LTInsert(plist, 5);
	LTInsert(plist, 6);
	LTInsert(plist, 7);
	LTInsert(plist, 8);
	LTInsert(plist, 9);
	LTPrint(plist);

	LTClear(plist);
	LTPrint(plist);
}

int main()
{
	//TestList1();
	//TestList2();
	//TestList3();
	//TestList4();
	//TestList5();
	//TestList6();
	//TestList7();
	TestList8();
	return 0;
}

Corrections are welcome❀

 

Guess you like

Origin blog.csdn.net/outdated_socks/article/details/130413379