双向链表 练习

#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef int DataType;

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

----------------------------------------------------------------

DListNode* DListInit()//链表初始化
{
	DListNode *head = (DListNode*)malloc(sizeof(DListNode));
	assert(head);
	head->_data = 0;
	head->_next = NULL;
	head->_prev = NULL;
	return head;
}

------------------------------------------------------------------

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

--------------------------------------------------------------

void DListDestory(DListNode* head)//销毁链表
{
	assert(head);
	DListNode *flag = head;
	while (flag)
	{
		flag = flag->_next;
		free(head);
		head = flag;
	}
}

--------------------------------------------------------

void DListPrint(DListNode* head)//打印链表
{
	if (head->_next == NULL)
	{
		printf("the dlist is empty.");
		return;
	}
	head = head->_next;
	while (head)
	{
		printf("%d ", head->_data);
		head = head->_next;
	}
}

-------------------------------------------------------------

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

}

----------------------------------------------------------

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

--------------------------------------------------------

void DListPopBack(DListNode* head)//尾删
{
	assert(head);
	if ((head)->_next == NULL)
	{
		return;
	}
	DListNode *p = ((head)->_next);
	DListNode *next = p->_next;
	while (next->_next)
	{
		next = next->_next;
		p = p->_next;
	}
	p->_next = NULL;
	free(next);
}

--------------------------------------------------

void DListPopFront(DListNode* head)//头删
{
	assert(head);
	DListNode *p = head->_next;
	if (p == NULL)
	{
		return;
	}
	DListNode *next = p->_next;
	head->_next = next;
	next->_prev = head;
	free(p);
	
}

-----------------------------------------------------------

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

----------------------------------------------------------

void DListInsert(DListNode* pos, DataType x)//任意位置插
{
	assert(pos);
	DListNode *prev = pos->_prev;
	DListNode *newnode = BuyDListNode(x);
	newnode->_next = pos;
	pos->_prev = newnode;
	prev->_next = newnode;
	newnode->_prev = prev;

}

------------------------------------------------------

void DListErase(DListNode* pos)//任意位置删
{
	assert(pos);
	DListNode *prev = pos->_prev;
	DListNode *next = pos->_next;
	if (next == NULL)
	{
		prev->_next = NULL;
		free(pos);
		return;
	}
	prev->_next = next;
	next->_prev = prev;
	free(pos);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值