双向链表C语言实现

2024-04-30 21:22:34 浏览数 (2)

List.h文件

代码语言:javascript复制
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//结构
typedef int LTDatatype;
typedef struct ListNode
{
	LTDatatype data;
	struct ListNode* next;
	struct ListNode* prev;
}LTNode;
//声明接口和方法
void LTInit(LTNode** pphead);
void LTPrint(LTNode* phead);
void LTPushBack(LTNode* phead,LTDatatype x);
void LTPushFront(LTNode* phead,LTDatatype x);
void LTPopBack(LTNode* phead);
void LTPopFront(LTNode* phead);
void LTInsert(LTNode* pos, LTDatatype x);
void LTErase(LTNode* pos);
LTNode* LTFind(LTNode* phead, LTDatatype x);
void LTDesTroy(LTNode* phead);

List.c文件

代码语言:javascript复制
#define _CRT_SECURE_NO_WARNINGS
#include"list.h"
LTNode* LTBuyNode(LTDatatype x)
{
	LTNode* node = (LTNode*)malloc(sizeof(LTNode));
	if(node==NULL)
	{
		perror("malloc fail");
		exit(1);
	}
	node->data = x;
	node->next = node->prev = node;
	return node;
}
void LTInit(LTNode** pphead)
{
	//创建哨兵位
	*pphead = LTBuyNode(-1);
}
void LTPrint(LTNode* phead)
{
	LTNode* pcur = phead->next;
	while (pcur != phead)
	{
		printf("%d->", pcur->data);
		pcur = pcur->next;
	}
	printf("n");
}
void LTPushBack(LTNode* phead, LTDatatype x)
{
	assert(phead);
	LTNode* newnode = LTBuyNode(x);
	newnode->prev = phead->prev;
	newnode->next = phead;

	phead->prev->next = newnode;
	phead->prev = newnode;
}
void LTPushFront(LTNode* phead, LTDatatype x)
{
	assert(phead);
	LTNode* newnode = LTBuyNode(x);
	newnode->next = phead->next;
	newnode->prev = phead;

	phead->next->prev = newnode;
	phead->next = newnode;
}
void LTPopBack(LTNode* phead)
{
	assert(phead && phead->next != phead);

	LTNode* del = phead->prev;
	del->prev->next = phead;
	phead->prev = del->prev;

	free(del);
	del = NULL;

}
void LTPopFront(LTNode* phead)
{
	assert(phead && phead->next != phead);
	LTNode* del = phead->next;

	phead->next = del->next;
	del->next->prev = phead;
	free(del);
	del = NULL;
}
LTNode* LTFind(LTNode* phead, LTDatatype x)
{
	LTNode* pcur = phead->next;
	while (pcur != phead)
	{
		if (pcur->data == x)
		{
			return pcur;
		}
		pcur = pcur->next;
	}
	return NULL;
}
void LTInsert(LTNode* pos, LTDatatype x)
{
	assert(pos);
	LTNode* newnode = LTBuyNode(x);
	newnode->next = pos->next;
	newnode->prev = pos;

	pos->next->prev = newnode;
	pos->next = newnode;
}
void LTErase(LTNode* pos)
{
	assert(pos);
	pos->next->prev = pos->prev;
	pos->prev->next = pos->next;
	free(pos);
	pos = NULL;
}
void LTDesTroy(LTNode* phead)
{
	assert(phead);
	LTNode* pcur = phead->next;
	while (pcur != phead)
	{
		LTNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	free(phead);
	phead = NULL;
}

测试文件

代码语言:javascript复制
#define _CRT_SECURE_NO_WARNINGS
#include"list.h"

void ListTest01()
{
	LTNode* plist = NULL;
	LTInit(&plist);
	LTPushBack(plist, 1);
	LTPrint(plist);
	LTPushBack(plist, 2);
	LTPrint(plist);
	LTPushBack(plist, 3);
	LTPrint(plist);
	LTPushFront(plist, -1);
	LTPushFront(plist, -2);
	LTPrint(plist);
	LTNode* find = LTFind(plist, 2);
	LTErase(find);
	find = NULL;
	LTPrint(plist);
	LTDesTroy(plist);
	plist = NULL;
}

int main()
{
	ListTest01();
}

0 人点赞