带头双向循环链表的实现

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
#pragma once
#include <stdlib.h>
typedef int LTDataType;
typedef struct ListNode
{
	LTDataType val;
	struct ListNode* next; 
	struct ListNode* prev;
}ListNode;
ListNode* ListInit();                                //初始化双向链表
void ListPushBack(ListNode* pplist, LTDataType x);   //双向链表尾插
void ListPrint(ListNode* pplist);                    //双向链表打印
void ListPopBcak(ListNode* pplist);                  //双向链表尾删
void ListPushFront(ListNode* pplist, LTDataType x);  //双向链表头插
void ListPopFront(ListNode* pplist);                 //双向链表头删
ListNode* ListFind(ListNode* pplist, LTDataType x);  //双向链表查找
void ListInsert(ListNode* pos,LTDataType x);         //双向链表在pos的前面进行插入
void ListErase(ListNode* pos);                       //双向链表删除pos位置的节点
void ListDestory(ListNode* pplist);                  //双向链表销毁

ListNode* BuyListNode(LTDataType x)                              //新建一个节点
{
	ListNode* newnode = malloc(sizeof(ListNode));
	if (newnode == NULL)
	{
		perror("malloc::ListNode");
		exit(-1);
	}
	newnode->val = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	return newnode;
}
ListNode* ListInit()                       //初始化双向链表
{
	ListNode* phead = BuyListNode(0);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}
void ListPushBack(ListNode* pplist, LTDataType x)             //双向链表尾插
{
	//assert(pplist);
	//ListNode* newnode = BuyListNode(x);
	//ListNode* next = pplist->prev;
	//pplist->prev = newnode;
	//newnode->next = pplist;
	//next->next = newnode;
	//newnode->prev = next;
	ListInsert(pplist,x);
}
void ListPrint(ListNode* pplist)                          //双向链表打印
{
	assert(pplist);
	ListNode* cur = pplist->next;
	while (cur != pplist)
	{
		printf("%d ", cur->val);
		cur = cur->next;
	}
	printf("\n");
}
void ListPopBcak(ListNode* pplist)                          //双向链表尾删
{
	//assert(pplist);
	//assert(pplist->next != pplist)
	//ListNode* next = pplist->prev->prev;                
	//free(pplist->prev);
	//pplist->prev = NULL;
	//pplist->prev = next;
	//next->next = pplist;
	ListErase(pplist->prev);
}
void ListPushFront(ListNode* pplist, LTDataType x)                     //双向链表头插
{
	//assert(pplist);
	//ListNode* next = pplist->next;
	//ListNode* newnode = BuyListNode(x);
	//pplist->next = newnode;
	//newnode->prev = pplist;
	//newnode->next = next;
	//next->prev = newnode;
	ListInsert(pplist->next, x);
}
void ListPopFront(ListNode* pplist)                                //双向链表头删
{
	//assert(pplist);
	//assert(pplist->next != pplist);
	//ListNode* next = pplist->next->next;
	//free(pplist->next);
	//pplist->next = next;
	//next->prev = pplist;
	ListErase(pplist->next);
}
ListNode* ListFind(ListNode* pplist, LTDataType x)                 //双向链表查找
{
	assert(pplist);
	ListNode* cur = pplist->next;
	while (cur != pplist)
	{
		if (cur->val != x)
		{
			cur = cur->next;
		}
		else
		{
			return cur;
		}
	}
	return NULL;
}
void ListInsert(ListNode* pos, LTDataType x)       //双向链表在pos的前面进行插入
{
	ListNode* newnode = BuyListNode(x);
	ListNode* cur = pos->prev;
	cur->next = newnode;
	newnode->prev = cur;
	newnode->next = pos;
	pos->prev = newnode;
}
void ListErase(ListNode* pos)                     //双向链表删除pos位置的节点
{
	assert(pos);
	ListNode* tail = pos->next;
	ListNode* cur = pos->prev;
	free(pos);
	pos = NULL;
	cur->next = tail;
	tail->prev = cur;
}
void ListDestory(ListNode* pplist)               //双向链表销毁
{
	assert(pplist);
	ListNode* cur = pplist->next;
	while (cur != pplist)
	{
		ListNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(pplist);
}

void Test1()
{
	ListNode* plist = ListInit();
	ListPushBack(plist, 1);
	ListPushBack(plist, 2);
	ListPushBack(plist, 6);
	ListPushBack(plist, 5);
	ListPushBack(plist, 3);
	ListPrint(plist);
	ListPopBcak(plist);
	ListPopBcak(plist);
	ListPopBcak(plist);
	ListPopBcak(plist);
	ListPopBcak(plist);
	ListPrint(plist);
	ListPushFront(plist, 10);
	ListPushFront(plist, 11);
	ListPushFront(plist, 12);
	ListPushFront(plist, 13);
	ListPushFront(plist, 14);
	ListPrint(plist);
	ListPopFront(plist);
	ListPopFront(plist);
	ListPopFront(plist);
	ListPopFront(plist);
	ListPrint(plist);
	ListNode* pos = ListFind(plist, 10);
	ListInsert(pos, 100);
	ListPrint(plist);
	ListErase(pos);
	ListPrint(plist);
	ListDestory(plist);
	ListPrint(plist);
}
int main()
{
	Test1();
    return 0;
}

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁华的梦境

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值