栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

List.h

#pragma once
#include
#include

typedef int LTDataType;

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

ListNode* BuyListNode(LTDataType x);

ListNode* ListInit();

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);

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

void ListDestory(ListNode* pHead);

List.c

#include"List.h"

ListNode* BuyListNode(LTDataType x)
{
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	node->data = x;
	return node;
}

ListNode* ListInit()
{
	ListNode* plist = BuyListNode(-1);
	plist->next = plist;
	plist->prev = plist;
	return plist;
}

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 = BuyListNode(x);
	pHead->prev->next = newnode;
	newnode->prev = pHead->prev;
	newnode->next = pHead;
	pHead->prev = newnode;
}

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

void ListPushFront(ListNode* pHead, LTDataType x)
{
	ListNode* newnode = BuyListNode(x);
	pHead->next->prev = newnode;
	newnode->next = pHead->next;
	newnode->prev = pHead;
	pHead->next = newnode;
}

void ListPopFront(ListNode* pHead)
{
	ListNode* del = pHead->next;
	pHead->next = del->next;
	del->next->prev = pHead;
	free(del);
}

ListNode* ListFind(ListNode* pHead, LTDataType x)
{
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		if (cur->data == x)
			return cur;
		cur = cur->next;
	}
	return NULL;
}

void ListInsert(ListNode* pos, LTDataType x)
{
	ListNode* newnode = BuyListNode(x);
	pos->prev->next = newnode;
	newnode->prev = pos->prev;
	newnode->next = pos;
	pos->prev = newnode;
}

void ListErase(ListNode* pos)
{
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
}

void ListDestory(ListNode* pHead)
{
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		ListNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(pHead);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/929114.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号