带头双向循环链表(c语言)

 带头双向循环链表相比单链表,在进行增删改时会方便许多,其独特的结构,可以省去遍历找尾操作。下面让我们来看看代码的具体实现。

首先我们要定义节点的结构体。

typedef int LTDataType;
typedef struct ListNode
{
	LTDataType data;
	struct ListNode* next;//后指针,指向下一个结点
	struct ListNode* prev;//前指针,指向前一个结点
}ListNode;

接着我们定义一个函数用于创建节点并初始化:

//创建结点
ListNode* BuySListNode(LTDataType x) {
	ListNode* newnode = (ListNode *)malloc(sizeof(ListNode));
	if (newnode == NULL) {
		perror("malloc fail");
		return NULL;
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

下一步是创建并初始化链表表头,这一步是带头双向链表的精髓,表头head的两个指针均指向head自己。

// 创建返回链表的头结点.
ListNode* ListCreate() {
	ListNode* head = BuySListNode(-1);
	head->data = -1;
	head->next = head;
	head->prev = head;
	return head;
}

链表的尾部插入:需要改变四个指针的指向

void ListPushBack(ListNode* pHead, LTDataType x) {
	ListNode* newnode = BuySListNode(x);
	pHead->prev->next = newnode;//头结点前指针指向的前一位,即是尾部结点的位置,将尾部结点的后 
                                //--指针指向新结点
	newnode->prev = pHead->prev;//新结点的前指针指向链表尾部结点
	pHead->prev = newnode;//头节点前指针指向新结点
	newnode->next = pHead;//新结点的后指针指向头结点
//另一种写法:
	//ListInsert(pHead,x);

}

链表的尾部删除:

void ListPopBack(ListNode* pHead) {
	ListNode* cur1 = pHead->prev;
	ListNode* cur2 = pHead->prev->prev;
	pHead->prev = cur2;
	cur2->next = pHead;
	free(cur1);
}

链表的头插:

// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x) {
	assert(pHead);
	ListNode* newnode = BuySListNode(x);
	ListNode* tail = pHead->next;
	pHead->next = newnode;
	newnode->prev = pHead;
	newnode->next = tail;
	tail->prev = newnode;
	//另一种写法:
	//ListInsert(pHead->next,x);
}

 链表的头删:

// 双向链表头删
void ListPopFront(ListNode* pHead) {
	assert(pHead);
	if (pHead->next!= pHead) {
		ListNode* first = pHead->next;
		ListNode* second = pHead->next->next;
		pHead->next = second;
		second->prev = pHead;
		free(first);
	}
	else {
		return;
	}
}

链表结点的查找:

// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x) {
	assert(pHead);
	ListNode *cur = pHead->next;
	while (cur != pHead) {
		if (cur->data == x) {
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

在链表的pos的结点前面进行插入:

// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x) {
	assert(pos);
	ListNode* newnode = BuySListNode(x);
	ListNode* cur = pos->prev;
	cur->next = newnode;
	newnode->prev = cur;
	newnode->next = pos;
	pos->prev = newnode;
}

链表删除pos位置的节点:

// 双向链表删除pos位置的节点
void ListErase(ListNode* pos) {
	assert(pos);
	ListNode* first = pos->prev;
	ListNode*second = pos->next;
	first->next = second;
	second->prev = first;
	free(pos);
}

链表的打印以及销毁:

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

完整代码:

#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;
}ListNode;
//创建结点
ListNode* BuySListNode(LTDataType x);
// 创建返回链表的头结点.
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);
#include"list.h"
//创建结点
ListNode* BuySListNode(LTDataType x) {
	ListNode* newnode = (ListNode *)malloc(sizeof(ListNode));
	if (newnode == NULL) {
		perror("malloc fail");
		return NULL;
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
// 创建返回链表的头结点.
ListNode* ListCreate() {
	ListNode* head = BuySListNode(-1);
	head->data = -1;
	head->next = head;
	head->prev = head;
	return head;
}
// 双向链表销毁
void ListDestory(ListNode* pHead) {
	assert(pHead);
	while (pHead->next != pHead) {
		ListNode* first = pHead->next;
		ListNode* second =pHead->next->next;
		pHead->next = second;
		second->prev = pHead;
		pHead = second;
		free(first);
	}
	free(pHead);
}
// 双向链表打印
void ListPrint(ListNode* pHead) {
	ListNode* cur = pHead->next;
	while (cur != pHead) {
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x) {
	ListNode* newnode = BuySListNode(x);
	pHead->prev->next = newnode;
	newnode->prev = pHead->prev;
	pHead->prev = newnode;
	newnode->next = pHead;
	//另一种写法:
	//ListInsert(pHead,x);
}
// 双向链表尾删
void ListPopBack(ListNode* pHead) {
	ListNode* cur1 = pHead->prev;
	ListNode* cur2 = pHead->prev->prev;
	pHead->prev = cur2;
	cur2->next = pHead;
	free(cur1);
}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x) {
	assert(pHead);
	ListNode* newnode = BuySListNode(x);
	ListNode* tail = pHead->next;
	pHead->next = newnode;
	newnode->prev = pHead;
	newnode->next = tail;
	tail->prev = newnode;
	//另一种写法:
	//ListInsert(pHead->next,x);
}
// 双向链表头删
void ListPopFront(ListNode* pHead) {
	assert(pHead);
	if (pHead->next!= pHead) {
		ListNode* first = pHead->next;
		ListNode* second = pHead->next->next;
		pHead->next = second;
		second->prev = pHead;
		free(first);
	}
	else {
		return;
	}
}
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType 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, LTDataType x) {
	assert(pos);
	ListNode* newnode = BuySListNode(x);
	ListNode* cur = pos->prev;
	cur->next = newnode;
	newnode->prev = cur;
	newnode->next = pos;
	pos->prev = newnode;
}

// 双向链表删除pos位置的节点
void ListErase(ListNode* pos) {
	assert(pos);
	ListNode* first = pos->prev;
	ListNode*second = pos->next;
	first->next = second;
	second->prev = first;
	free(pos);
}
#include"list.h"
int main() {
	ListNode *plist = ListCreate();
	ListPushBack(plist, 1);
	ListPushBack(plist, 2);
	ListPushBack(plist, 3);
	ListPushBack(plist, 4);
	ListPrint(plist);

	ListPushFront(plist, 5);
	ListPushFront(plist, 6);
	ListPrint(plist);

	ListPopBack(plist);
	ListPrint(plist);

	ListPopFront(plist);
	ListPrint(plist);

	ListNode* pos= ListFind(plist, 2);
	ListInsert(pos, 11);
	ListPrint(plist);

	ListErase(pos);
	ListPrint(plist);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值