手撕双向链表

无头单向非循环链表:

结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。

带头双向循环链表:

结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环鲜表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了,后商我们代码实现了就知道了。

双向带头循环链表实现

DList.h
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<windows.system.h>

typedef int DLTdatetype;

typedef struct DList
{
	DLTdatetype data;
	struct DList* next;
	struct Dlist* prev;
}DLnode;



//show一下
void Showlist(DLnode* pphead);
//初始化
DLnode* Initial();
//尾插
void PushBack(DLnode* head,DLTdatetype data);
//头插
void PushFront(DLnode* head, DLTdatetype data);
//尾删
void PopBack(DLnode* head);
//头删
void PopFront(DLnode* head);
//查找
DLnode* Find(DLnode* head, DLTdatetype data);
//插入
DLnode* Insert(DLnode* pos, DLTdatetype data);
//删除
DLnode* Erase(DLnode* pos);
//销毁
void Distory(DLnode* head);
DList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"DList.h"






//show一下
void Showlist(DLnode* phead)
{
	assert(phead);

	DLnode* cur = phead->next;
	while (cur!=phead)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}

	printf("\n");
}


//初始化
DLnode* Initial()
{
	DLnode* newnode = (DLnode*)malloc(sizeof(DLnode));

	newnode->next = newnode;
	newnode->prev = newnode;
	
	return newnode;
}

//尾插
void PushBack(DLnode* phead, DLTdatetype data)
{
	assert(phead);

	DLnode* tail = phead->prev;
	DLnode* newnode = (DLnode*)malloc(sizeof(DLnode));
	newnode->data = data;


	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;
}
//尾删
void PopBack(DLnode* head)
{
	assert(head);
	assert(head->next != head);

	DLnode* tail = head->prev;
	head->prev = tail->prev;
	DLnode* newtail = tail->prev;
	newtail->next = head;

	tail->next = NULL;
	tail->prev = NULL;
	free(tail);
	tail = NULL;
}
//头插
void PushFront(DLnode* phead, DLTdatetype data)
{
	assert(phead);

	DLnode* newnode = (DLnode*)malloc(sizeof(DLnode));
	newnode->next = phead->next;
	phead->next->prev = newnode;
	newnode->data = data;
	newnode->prev = phead;
	phead->next = newnode;
}
//头删
void PopFront(DLnode* head)
{
	assert(head);
	assert(head->next != head);
	
	DLnode* nextnode = head->next;
	head->next = head->next->next;
	head->next->prev = head;

	nextnode->next = NULL;
	nextnode->prev = NULL;
	free(nextnode);
	nextnode = NULL;
}


//查找
DLnode* Find(DLnode* head, DLTdatetype data)
{
	assert(head);

	DLnode* cur = head;
	while (cur->next != head)
	{
		if (cur->data==data)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
//插入
DLnode* Insert(DLnode* pos, DLTdatetype data)
{
	assert(pos);

	DLnode* newnode = (DLnode*)malloc(sizeof(DLnode));
	DLnode* nextnode = pos->next;
	newnode->data = data;

	pos->next = newnode;
	newnode->prev = pos;

	nextnode->prev = newnode;
	newnode->next = nextnode;
}
//删除
DLnode* Erase(DLnode* pos)
{
	assert(pos);
	DLnode* next = pos->next;
	DLnode* prev = pos->prev;

	next->prev = prev;
	prev->next = next;

	pos->next = NULL;
	pos->prev = NULL;
	pos->data = NULL;

	free(pos);
	pos = NULL;
}
//销毁
void Distory(DLnode* head)
{
	assert(head);

	DLnode* cur = (head)->next;
	while (cur!=(head))
	{
		DLnode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(head);
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"DList.h"

void test1()
{
	DLnode* data = Initial();
	PushBack(data, 0);
	PushBack(data, 1);
	PushBack(data, 2);

	Showlist(data);
	PopBack(data);
	Showlist(data);
}

void test2()
{
	DLnode* data = Initial();
	PushFront(data, 0);
	PushFront(data, 1);
	PushFront(data, 2);

	Showlist(data);
	PushFront(data, -1);
	Showlist(data);
	PopFront(data);
	Showlist(data);
}

void test3()
{
	DLnode* data = Initial();
	PushFront(data, 0);
	PushFront(data, 1);
	PushFront(data, 2);
	Showlist(data);
	Erase(Find(data, 1));
	Distory(&data);
	free(data);
	Sleep(1000);
	Showlist(data);

}

int main()
{

	//test1();
	//test2();
	test3();


	return 0;
}

此处双向链表相对于上次写的单链表除了双向的区别,最大的不同在于此处传参是传的一级指针,而上次的单链表是传的二级指针。其根本原因是此处双向链表存在头指针哨兵位,而上次的单链表没有。
所以说还是有哨兵位的链表更好用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值