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

C语言 带头双向循环链表 构建、增删

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

C语言 带头双向循环链表 构建、增删

目录

1. List.h头文件

2. List.c源文件

3. main.c源文件

4. 运行截图


1. List.h头文件
#pragma once
#include 
#include 

typedef int NodeDate;

typedef struct Node
{
	NodeDate Data;
	struct Node* prev;    //指向前一个节点
	struct Node* next;    //指向后一个节点
}Node;

Node* InitList();

void PrintList(Node* phead);    //打印链表

void FrontPush(Node* phead, NodeDate x);

void BackPush(Node* phead, NodeDate x);

void FrontPop(Node* phead);

void BackPop(Node* phead);

void InsertNode(Node* pos, NodeDate x);    //在pos节点之前插入一个节点

void EraseNode(Node* pos);    //删除pos节点

void DestoryList(Node* phead);    //销毁链表

Node* FindNode(Node* phead, NodeDate x);

2. List.c源文件
#include "List.h"

Node* BuyNode()
{
	Node* new = (Node*)malloc(sizeof(Node));
	new->next = NULL;
	new->prev = NULL;
	return new;
}

Node* InitList()
{
	Node* node = BuyNode();
	node->next = node;
	node->prev = node;
}

void PrintList(Node* phead)
{
	assert(phead);
	Node* cur = phead->next;
	if (cur == phead)
	{
		printf("list is empty");
	}
	while (cur != phead)
	{
		printf("%d ", cur->Data);
		cur = cur->next;
	}
	printf("n");
}

void FrontPush(Node* phead, NodeDate x)
{
	
	assert(phead);
	InsertNode(phead->next, x);
}

void BackPush(Node* phead, NodeDate x)
{
	
	assert(phead);
	InsertNode(phead, x);
}

void FrontPop(Node* phead)
{
	
	assert(phead);
	printf("FrontPop once: ");
	EraseNode(phead->next);
}

void BackPop(Node* phead)
{
	
	assert(phead);
	printf("BackPop once: ");
	EraseNode(phead->prev);
}

void InsertNode(Node* pos, NodeDate x)
{
	assert(pos);
	Node* new = BuyNode();
	new->Data = x;
	Node* prev = pos->prev;
	prev->next = new;
	new->prev = prev;
	new->next = pos;
	pos->prev = new;
}

void EraseNode(Node* pos)
{
	assert(pos);
	if (pos->next == pos)
	{
		printf("Delete failed, list is emptyn");
		return;
	}
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
	pos = NULL;
	printf("EraseNode once: ");
}

void DestoryList(Node* phead)
{
	assert(phead);
	Node* cur = phead->next;
	while (cur != phead)
	{
		Node* next = cur->next;
		EraseNode(cur);
		cur = next;
	}
	printf("list is destoryn");
}

Node* FindNode(Node* phead, NodeDate x)
{
	Node* cur = phead->next;
	while (cur != phead)
	{
		if (cur->Data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

3. main.c源文件
#include "List.h"

void Test1()
{
	Node* head = InitList();
	FrontPush(head, 1);
	FrontPush(head, 2);
	FrontPush(head, 3);
	FrontPush(head, 4);
	FrontPush(head, 5);
	printf("FrontPush 1~5:");
	PrintList(head);

	EraseNode(FindNode(head, 3));
	PrintList(head);
	FrontPop(head);
	PrintList(head);
	BackPop(head);
	PrintList(head);
	BackPop(head);
	PrintList(head);
	BackPop(head);
	PrintList(head);
	BackPop(head);
	PrintList(head);
	printf("n");
}

void Test2()
{
	Node* head = InitList();
	BackPush(head, 1);
	BackPush(head, 2);
	BackPush(head, 3);
	BackPush(head, 4);
	BackPush(head, 5);
	printf("BackPush 1~5:");
	PrintList(head);

	DestoryList(head);
	PrintList(head);
	printf("n");
}

int main()
{
	Test1();
	Test2();

	return 0;
}

4. 运行截图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937896.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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