双链表

一、DList.h

#ifndef __DList_h__
#define __DList_h__

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

typedef int DataType;

typedef struct DListNode
{
	DataType _data;
	struct DListNode* _prev;
	struct DListNode* _next;
}DListNode;

DListNode* BuyDListNode(DataType x);
DListNode* DListInit();
void DListDestroy(DListNode* head);
void DListPrint(DListNode* head);

void DListPushBack(DListNode* head, DataType x);
void DListPushFront(DListNode* head, DataType x);
void DListPopBack(DListNode* head);
void DListPopFront(DListNode* head);

DListNode* DListFind(DListNode* head, DataType x);
void DListInsert(DListNode* pos, DataType x);
void DListErase(DListNode* pos);

#endif __DList_h__

二、DList.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "DList.h" 

//创建结点
DListNode* BuyDListNode(DataType x)
{
	DListNode* newnode = (DListNode*)malloc(sizeof(DListNode));
	assert(newnode);
	newnode->_data = x;
	newnode->_next = NULL;
	newnode->_prev = NULL;
	return newnode;
}

//初始化
DListNode* DListInit()
{
	DListNode* list = BuyDListNode(0);
	list->_next = list;
	list->_prev = list;
	return list;
}

//销毁
void DListDestroy(DListNode* head)
{
	DListNode* cur = head->_next;
	while (cur!=head)
	{
		DListNode* next = cur->_next;
		free(cur);
		cur = next;
	}
	free(head);
}

//输出
void DListPrint(DListNode* head)
{
	DListNode* cur = head->_next;
	while (cur != head)
	{
		printf("%d->", cur->_data);
		cur = cur->_next;
	}
	printf("head\n");
}

//尾插
void DListPushBack(DListNode* head, DataType x)
{
	assert(head);
	DListNode* tail = head->_prev;
	DListNode* newnode = BuyDListNode(x);
	tail->_next = newnode;
	newnode->_prev = tail;
	newnode->_next = head;
	head->_prev = newnode;

	//DListInsert(head, x);
}

//头插
void DListPushFront(DListNode* head, DataType x)
{
	assert(head);
	DListNode* first = head->_next;
	DListNode* newnode = BuyDListNode(x);	
	head->_next = newnode;
	newnode->_prev = head; 
	newnode->_next = first;
	first->_prev = newnode;

	//DListInsert(head->_next, x);
}

//尾删
void DListPopBack(DListNode* head)
{
	assert(head);
	if (head->_next == NULL)
	{
		printf("该链表为空\n");
		return;
	}
	DListNode* tail = head->_prev;
	DListNode* prev = tail->_prev;
	prev->_next = head;
	head->_prev = prev;
	free(tail);

	//DListErase(head->_prev);
}

//头删
void DListPopFront(DListNode* head)
{
	assert(head);
	if (head->_next == NULL)
	{
		printf("该链表为空\n");
		return;
	}
	DListNode* first = head->_next;
	DListNode* next = first->_next;
	head->_next = next;
	next->_prev = head;
	free(first);

	//DListErase(head->_next);
}

//查找
DListNode* DListFind(DListNode* head, DataType x)
{
	DListNode* cur = head->_next;
	while (cur != head)
	{
		if (cur->_data == x)
		{
			return cur;
		}
		cur = cur->_next;
	}
	return NULL;
}

//插入
void DListInsert(DListNode* pos, DataType x)
{
	assert(pos);
	DListNode* prev = pos->_prev;
	DListNode* newnode = BuyDListNode(x);
	prev->_next = newnode;
	newnode->_prev = prev;
	newnode->_next = pos;
	pos->_prev = newnode;
}

//删除
void DListErase(DListNode* pos)
{
	assert(pos);
	DListNode* prev = pos->_prev;
	DListNode* next = pos->_next;
	prev->_next = next;
	next->_prev = prev;
	free(pos);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值