带头双向循环链表的实现

main.c文件

#define _CRT_SECURE_NO_WARNINGS

#include"List.h"

int main()
{
	ListTest();
	system("pause");
	return 0;
}

List.h文件

#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

//带头结点的双向循环链表
typedef int LTDataType;
typedef struct ListNode
{
	LTDataType _data;
	struct ListNode *_prev;
	struct ListNode *_next;
}ListNode;

typedef struct List
{
	ListNode *head;
}List;

void ListInit(List *plist);
void ListDestory(List *plist);

void ListPushBack(List *plist, LTDataType x);
void ListPopBack(List *plist);
void ListPushFront(List *plist, LTDataType x);
void ListPopFront(List *plist);

//pos位置之前插入
void ListInsert(ListNode *pos, LTDataType x);
//删除pos位置
void ListErase(ListNode *pos);

ListNode * ListFind(List *plist, LTDataType x);

void ListPrint(List *plist);
void ListTest();

List.c文件

#include"List.h"

ListNode *BuyListNode(LTDataType x)
{
	ListNode *newNode = (ListNode *)malloc(sizeof(ListNode));
	assert(newNode);
	newNode->_data = x;
	newNode->_next = NULL;
	newNode->_prev = NULL;
	return newNode;
}

void ListInit(List *plist)
{
	assert(plist);
	plist->head = BuyListNode(0);
	plist->head->_next = plist->head;
	plist->head->_prev = plist->head;
}

void ListDestory(List *plist)
{
	assert(plist);
	ListNode *cur = plist->head->_next;
	ListNode *next;
	while (cur != plist->head)
	{
		next = cur->_next;
		free(cur);
		cur = next;
	}
	free(plist->head);
	plist->head = NULL;
}

void ListPushBack(List *plist, LTDataType x)
{
	assert(plist);

	/*ListNode *newNode = BuyListNode(x);
	ListNode *head = plist->head;
	ListNode *tail = head->_prev;

	tail->_next = newNode;
	newNode->_prev = tail;
	newNode->_next = head;
	head->_prev = newNode;*/
	ListInsert(plist->head, x);
}

void ListPopBack(List *plist)
{
	assert(plist);

	/*ListNode *head = plist->head;
	ListNode *tail = head->_prev;
	ListNode*prev = tail->_prev;

	prev->_next = head;
	head->_prev = prev;
	free(tail);
	tail = NULL;*/
	
	ListErase(plist->head->_prev);
}

void ListPushFront(List *plist, LTDataType x)
{
	assert(plist);

	/*ListNode *head = plist->head;
	ListNode *next = head->_next;
	ListNode *newNode = BuyListNode(x);

	head->_next = newNode;
	newNode->_prev = head;
	newNode->_next = next;
	next->_prev = newNode;*/

	ListInsert(plist->head->_next, x);
}

void ListPopFront(List *plist)
{
	assert(plist);

	/*ListNode *head = plist->head;
	ListNode *next = head->_next;
	ListNode *nextNext = next->_next;

	head->_next = nextNext;
	nextNext->_prev = head;
	free(next);
	next = NULL;*/

	ListErase(plist->head->_next);
}

void ListInsert(ListNode *pos, LTDataType x)
{
	assert(pos);

	ListNode *prev = pos->_prev;
	ListNode *newNode = BuyListNode(x);

	prev->_next = newNode;
	newNode->_prev = prev;
	newNode->_next = pos;
	pos->_prev = newNode;
}

void ListErase(ListNode *pos)
{
	assert(pos);

	ListNode *prev = pos->_prev;
	ListNode *next = pos->_next;

	prev->_next = next;
	next->_prev = prev;
	free(pos);
	pos = NULL;
}

ListNode * ListFind(List *plist, LTDataType x)
{
	assert(plist);

	ListNode *head = plist->head;
	ListNode *cur = head->_next;
	while (cur != head)
	{
		if (cur->_data == x)
		{
			return cur;
		}
		cur = cur->_next;
	}
	return NULL;
}

void ListPrint(List *plist)
{
	ListNode *head = plist->head;
	ListNode *cur = head->_next;
	while (cur != head)
	{
		printf("%d->", cur->_data);
		cur = cur->_next;
	}
	printf("<->\n");
}

void ListTest()
{
	List lt;
	ListInit(&lt);
	ListPushBack(&lt, 1);
	ListPushBack(&lt, 3);
	ListPushBack(&lt, 4);
	ListPopBack(&lt);
	ListPopFront(&lt);
	ListPrint(&lt);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值