双链表的基本操作


**前言:**双链表可以分为带头的双链表和不带头的双链表
而本次是针对带头的循环双链表的基本操作:

定义链表中元素的数据类型:

typedef int LTDataType;

定义链表的基本结构:

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

初始化双链表:

LTNode* ListInit() {

	//申请头结点
	LTNode* phead = (LTNode *)malloc(sizeof(LTNode));
	phead->next = phead;
	phead->prev = phead;

	return phead;
}

打印双链表中的元素:

void ListPrint(LTNode *phead) {
	assert(phead);
	LTNode* cur = phead;
	while (cur->next!=phead) {
		cur = cur->next;
		printf("%d ", cur->data);
	}
	printf("\n");
}

申请一个新结点:

LTNode* BuyListNode(LTDataType x) {
	LTNode* newnode = (LTNode *)malloc(sizeof(LTNode));

	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	

	return  newnode;
}

双链表中进行后插操作:

void ListPushBack(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;
}

双链表中进行后删操作:

void ListPopBack(LTNode *phead) {
	assert(phead);
	assert(phead->next);
	LTNode *tail = phead->prev;

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

双链表中进行前插操作:

void ListPushFront(LTNode *phead, LTDataType x) {
	assert(phead);
	LTNode* newnode = BuyListNode(x);
	LTNode *cur = phead->next;

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

双链表中进行前删操作:

void ListPopFront(LTNode* phead) {
	assert(phead);
	assert(phead->next != phead);
	LTNode* cur = phead->next;
	phead->next = cur->next;
	cur->next->prev = phead;
	free(cur);
	cur = NULL;
}

双链表中找特定值为x的结点:

LTNode* ListFind(LTNode* phead, LTDataType x) { //找结点
	assert(phead);

	LTNode* cur = phead->next;
	while (cur != phead) {
		if (cur->data == x) {
			return cur;
		}
		else {
			cur = cur->next;
		}
	}
	return NULL;
}

双链表中在特定结点前插入一个结点:

void ListInsert(LTNode* pos, LTDataType x) {
	assert(pos);
	LTNode* posPrev = pos->prev;
	LTNode* newnode = BuyListNode(x);

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

双链表中删除结点:

void ListErase(LTNode* pos) {
	assert(pos);

	LTNode* posPrev = pos->prev;
	LTNode* posNext = pos->next;

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

销毁链表:

void ListDestroy(LTNode* phead) {
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead) {
		LTNode* curNext = cur->next;
		free(cur);
		cur = curNext;
	}
	free(phead);
	phead = NULL;
}

双链表中增删的优化:

在基本的功能实现后我们可以看到代码其实是具有耦合性的,我们可以对双链表的增删进行优化:

后插操作:

void ListPushBack(LTNode* phead, LTDateType x)
{
	assert(phead);
	ListInsert(phead, x);
}

前插操作:

void ListPushFront(LTNode* phead, LTDateType x)
{
	assert(phead);
	ListInsert(phead->next, x);
}

后删操作:

void ListPopBack(LTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	ListErase(phead->prev);
}

前删操作:

void ListPopFront(LTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	ListErase(phead->next);
}

VS中代码:

DList.h头文件:

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

typedef int LTDataType;

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

LTNode* ListInit();
void ListDestroy(LTNode *phead);
void ListPrint(LTNode *phead);

LTNode *BuyListNode(LTDataType);
void ListPushBack(LTNode* phead, LTDataType x);
void ListPopBack(LTNode* phead);
void ListPushFront(LTNode* phead, LTDataType x);
void ListPopFront(LTNode* phead);

LTNode* ListFind(LTNode* phead, LTDataType x);

void ListInsert(LTNode* pos, LTDataType x);
void ListErase(LTNode* pos);

DList.c源文件:

#include"DList.h"

LTNode* ListInit() {

	//申请头结点
	LTNode* phead = (LTNode *)malloc(sizeof(LTNode));
	phead->next = phead;
	phead->prev = phead;

	return phead;
}
LTNode* BuyListNode(LTDataType x) {
	LTNode* newnode = (LTNode *)malloc(sizeof(LTNode));

	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	

	return  newnode;
}
void ListPrint(LTNode *phead) {
	assert(phead);
	LTNode* cur = phead;
	while (cur->next!=phead) {
		cur = cur->next;
		printf("%d ", cur->data);
	}
	printf("\n");
}
void ListPushBack(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;
}

void ListPopBack(LTNode *phead) {
	assert(phead);
	assert(phead->next);
	LTNode *tail = phead->prev;

	phead->prev = tail->prev;
	tail->prev->next = phead;
	free(tail);
	tail = NULL;
}
void ListPushFront(LTNode *phead, LTDataType x) {
	assert(phead);
	LTNode* newnode = BuyListNode(x);
	LTNode *cur = phead->next;

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

void ListPopFront(LTNode* phead) {
	assert(phead);
	assert(phead->next != phead);
	LTNode* cur = phead->next;
	phead->next = cur->next;
	cur->next->prev = phead;
	free(cur);
	cur = NULL;
}

LTNode* ListFind(LTNode* phead, LTDataType x) { //找结点
	assert(phead);

	LTNode* cur = phead->next;
	while (cur != phead) {
		if (cur->data == x) {
			return cur;
		}
		else {
			cur = cur->next;
		}
	}
	return NULL;
}
void ListInsert(LTNode* pos, LTDataType x) {
	assert(pos);
	LTNode* posPrev = pos->prev;
	LTNode* newnode = BuyListNode(x);

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

void ListErase(LTNode* pos) {
	assert(pos);

	LTNode* posPrev = pos->prev;
	LTNode* posNext = pos->next;

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

void ListDestroy(LTNode* phead) {
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead) {
		LTNode* curNext = cur->next;
		free(cur);
		cur = curNext;
	}
	free(phead);
	phead = NULL;
}

main.c源文件:

#include"DList.h"
int main() {
	LTNode* plist = ListInit();
	ListPushBack(plist, 1);
	ListPushBack(plist, 2);
	ListPushBack(plist, 3);
	ListPushBack(plist, 4);
	ListPushFront(plist, 0);
	ListPrint(plist);
	ListPopBack(plist);
	ListPopFront(plist);
	ListPrint(plist);
	LTNode* pos = ListFind(plist, 1);//如果找到就删除这个数
	if (pos) {
		ListInsert(pos, 5);
		ListInsert(pos, 6);
		ListErase(pos);

	}
	ListPrint(plist);
	ListDestroy(plist);
	plist = NULL;
	return 0;
}

顺序表和链表的优缺点的对比:

顺序表:

优点:
  • 支持随机访问,需要随机访问结构支持算法可以很好的运用。
  • CPU高速缓存命中率高
缺点:
  • 头部中部插入删除的时间复杂度高:O(n)
  • 连续的物理地址存储空间,空间不够时,学要开辟连续的空间,代价比较大。
  • 可能造成一定的空间浪费

链表:

优点:
  • 任意位置插入或者删除的时间效率高:O(1)
  • 可以根据自己的需求进行申请和释放空间。
缺点:
  • 不支持随机访问(用下标访问),意味着一些排序,二分查找在此结构中不适用。
  • 链表中存储一个值的同时也要存储连接指针,同时也有一定的消耗。
  • CPU高速缓存的命中率更低。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值