双向链表的实现

不一一介绍了 直接上代码

目录

初始化

创建结点

打印链表

头插

尾插

头删

尾删

定向插入(分前后)

查找

删除

修改

初始化

struct ListNode* InitDlist()//双向链表的初识化
{
	struct ListNode* pHead=BuyDList(0);
	pHead->next = pHead;
	pHead->prev = pHead;
	return pHead;
}

创建结点

struct ListNode* BuyDList(DlistType x)//创建新结点 
{
	struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
	newnode->prev = NULL;
	newnode->next = NULL;
	newnode->val = x;
	return newnode;
}

打印链表

void printDlist(struct ListNode* head)//打印
{
	struct ListNode* next = head->next;
	while (next != head)
	{
		printf("%d->", next->val);
		next = next->next;
	}
	printf("over\n");
}

头插


void PushFront(struct ListNode* head, DlistType x)//头插
{
	struct ListNode* newnode = BuyDList(x);//创建新结点 
	struct ListNode* first = head->next;//保存除哨兵位的第一个存有数据的结点
	newnode->next =first;//四步操作让新节点插入原链表 两个方向都是双向 方向要变换四次
	newnode->prev = head;
	head->next = newnode;
	first->prev = newnode;
	
}

头删

void PopBack(struct ListNode* head)//尾删
{
	assert(head);
	assert(head->next!=head);
	struct ListNode* tail = head->prev;
	struct ListNode* tailprev = tail->prev;
	free(tail);
	tailprev->next = head;
	head->prev = tailprev;
}

尾插

void PushFront(struct ListNode* head, DlistType x)//头插
{
	struct ListNode* newnode = BuyDList(x);//创建新结点 
	struct ListNode* first = head->next;//保存除哨兵位的第一个存有数据的结点
	newnode->next =first;//四步操作让新节点插入原链表 两个方向都是双向 方向要变换四次
	newnode->prev = head;
	head->next = newnode;
	first->prev = newnode;
	
}

尾删

void PopBack(struct ListNode* head)//尾删
{
	assert(head);
	assert(head->next!=head);
	struct ListNode* tail = head->prev;
	struct ListNode* tailprev = tail->prev;
	free(tail);
	tailprev->next = head;
	head->prev = tailprev;
}

定向插入(前)

void DListInsertFront(struct ListNode* head, DlistType x, DlistType k)//指定位置前插
{
	assert(head);
	struct ListNode* insert = FindNode(head, x);
	if (insert)
	{
		struct ListNode* newnode = BuyDList(k);
		struct ListNode* prev = insert->prev;
	
		prev->next = newnode;
		newnode->prev = prev;
		newnode->next = insert;
		insert->prev = newnode;
	}

}

定向插入(后)

void DListInsertBehind(struct ListNode* head, DlistType x, DlistType k)//指定位置后插
{
	assert(head);
	struct ListNode* insert = FindNode(head, x);
	if (insert)
	{
		struct ListNode* newnode = BuyDList(k);
		struct ListNode* next = insert->next; 
			newnode->val = k;
		newnode->next = next;
		next->prev = newnode;
		newnode->prev = insert;
		insert->next = newnode;

	}
}

删除

void DListErase(struct ListNode* head, DlistType x)//删除指定结点
{
	assert(head);
	assert(head->next != head);
	struct ListNode* erase = FindNode(head, x);
	if (erase)
	{
		struct ListNode* next = erase->next;
		struct ListNode* prev = erase->prev;
		free(erase);
		next->prev = prev;
		prev->next = next;

	}
}

修改

void DListModify(struct ListNode* head, DlistType x, DlistType k)
{
	assert(head);
	struct ListNode* modify = FindNode(head, x);
	modify->val = k;
}


 

  • 25
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 29
    评论
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值