【C语言】数据结构之带头双向循环链表的增删查改

目录

一.声明及定义

二.函数功能的实现

三.测试函数功能 


一.声明及定义

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#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* 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"DList.h"
ListNode* Buynode(LTDataType x)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	else
	{
		newnode->data = x;
		newnode->next = NULL;
		newnode->prev = NULL;
	}
	return newnode;
}
ListNode* ListCreate()
{
	ListNode* head = Buynode(0);//创建头结点
	head->next = head;
	head->prev = head;
	return head;
}


void ListDestory(ListNode* pHead)
{
	assert(pHead);
	ListNode* cur = pHead;
	while (cur)
	{
		ListNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(pHead);
}


void ListPrint(ListNode* pHead)
{
	assert(pHead);
	ListNode* cur = pHead->next;
	while (cur!=pHead)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
}


void ListPushBack(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* newnode = Buynode(x);
	ListNode* tail = pHead->prev;
	tail->next = newnode;
	newnode->prev = tail;
	pHead->prev = newnode;
	newnode->next = pHead;
}


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


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


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


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


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


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

三.测试函数功能 

#include"DList.h"
void test()
{
	ListNode* head = ListCreate();//头结点
	ListPushBack(head, 1);
	ListPushBack(head, 2);
	ListPushBack(head, 3);
	//ListPopBack(head);
	ListPushFront(head, 0);
	ListPopFront(head);
	ListInsert(ListFind(head, 3), 0);
	ListErase(ListFind(head, 0));
	ListPrint(head);
	printf("%d", ListFind(head, 1)->data);
}
int main()
{
	test();
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值