双链表

单链表的局限性:是对数组的扩展,解决了数组的大小比较死板不容易扩展的问题。使用堆内存来存储数据,将数据分散到各个节点之间,其各个节点在内存中可以不相连,节点之间通过指针进行单向链接。链表中各个节点内存不相连,有利于碎片化的内存

单链表各个节点之间只由一个指针单向链接,局限在只能由指针单向移动

双链表:有效数据+两个指针
单链表节点:有效数据+指针(指针指向后一个节点)
双链表节点:有效数据+两个指针(一个指向后一个节点,另一个指向亲一个节点)

在这里插入图片描述

插入节点 尾部插入
在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
struct node
{
	int data;
	struct node *prev;
	struct node *pnext;
};

struct node *creat_node(int data)
{
	struct node *p=(struct node *)malloc(sizeof(sturct node));
	if(NULL ==p)
	{
		printf("error \n");
		return NULL;
	}
	bzero(p,sizeof(struct node));
	p->data=data;
	p->prev=NULL;  //默认创建的前后指针都指向NULL
	p->pnext=NULL;
	return p;
}
//将新节点new插入到链表ph的尾部
void insert_tail(//struct node *ph,struct node *new)
{
	//先走到链表的尾节点
	struct node *p=ph;
	while(NULL != p->pnext)
	{
		p = p->pnext; //第一次循环走过头节点

`	}//循环结束后p指向了最后一个节点
	//将新节点插入到原来的尾节点的后面
	p->pnext = new;
	new->prev = p; 
}


int main(void)
{
	struct node *header = create_node(0);
	insert_tail(header,create_node(2));
	insert_tail(header,create_node(3));
	insert_tail(header,create_node(4));
	
	
	return 0;
}

插入节点 头部插入

#include<stdio.h>
#include<stdlib.h>
struct node
{
	int data;
	struct node *prev;
	struct node *pnext;
};

struct node *creat_node(int data)
{
	struct node *p=(struct node *)malloc(sizeof(sturct node));
	if(NULL ==p)
	{
		printf("error \n");
		return NULL;
	}
	bzero(p,sizeof(struct node));
	p->data=data;
	p->prev=NULL;  //默认创建的前后指针都指向NULL
	p->pnext=NULL;
	return p;
}
//将新节点new插入到链表ph的头部
void insert_head(//struct node *ph,struct node *new)
{
	//头节点的next指针指向新节点地址 第一个有效节点prev指针指向新节点地址
	//头指针的next指针指向新节点地址   新节点prev指针指向头节点地址
	new->pnext = ph->pnext;
	if(NULL != ph->pnext) 
	ph->pnext->prev = new;
	ph->pnext = new;
	new->prev = ph;
	
}


int main(void)
{
	struct node *header = create_node(0);
	insert_head(header,create_node(2));
	insert_head(header,create_node(3));
	insert_head(header,create_node(4));
	
	
	return 0;
}

遍历节点 后向遍历 前向遍历

#include<stdio.h>
#include<stdlib.h>
struct node
{
	int data;
	struct node *prev;
	struct node *pnext;
};

struct node *creat_node(int data)
{
	struct node *p=(struct node *)malloc(sizeof(sturct node));
	if(NULL ==p)
	{
		printf("error \n");
		return NULL;
	}
	bzero(p,sizeof(struct node));
	p->data=data;
	p->prev=NULL;  //默认创建的前后指针都指向NULL
	p->pnext=NULL;
	return p;
}
//将新节点new插入到链表ph的头部
void insert_head(//struct node *ph,struct node *new)
{
	//头节点的next指针指向新节点地址 第一个有效节点prev指针指向新节点地址
	//头指针的next指针指向新节点地址   新节点prev指针指向头节点地址
	new->pnext = ph->pnext;
	if(NULL != ph->pnext) 
	ph->pnext->prev = new;
	ph->pnext = new;
	new->prev = ph;
	
}
void bianli(struct node *ph)
{
	struct node *p = ph;
	
	while(NULL !=p->pnext)
	{
		p = p->pnext;
		printf("%d\n",p->data);
	}

}
void head_bianli(struct node *ph)
{
	struct node *p = ph;
while (NULL != ph->prve)
	{	
		printf("%d\n",p->data);
		p = p->prve;
	}
}


int main(void)
{
	struct node *header = create_node(0);
	insert_head(header,create_node(2));
	insert_head(header,create_node(3));
	insert_head(header,create_node(4));
	bianli(header);
	struct node *p= header->pnext->pnext->pnext;
	head_bianli(p);
	return 0;
}

删除节点

#include<stdio.h>
#include<stdlib.h>
struct node
{
	int data;
	struct node *prev;
	struct node *pnext;
};

struct node *creat_node(int data)
{
	struct node *p=(struct node *)malloc(sizeof(sturct node));
	if(NULL ==p)
	{
		printf("error \n");
		return NULL;
	}
	bzero(p,sizeof(struct node));
	p->data=data;
	p->prev=NULL;  //默认创建的前后指针都指向NULL
	p->pnext=NULL;
	return p;
}
//将新节点new插入到链表ph的头部
void insert_head(//struct node *ph,struct node *new)
{
	//头节点的next指针指向新节点地址 第一个有效节点prev指针指向新节点地址
	//头指针的next指针指向新节点地址   新节点prev指针指向头节点地址
	new->pnext = ph->pnext;
	if(NULL != ph->pnext) 
	ph->pnext->prev = new;
	ph->pnext = new;
	new->prev = ph;
	
}
void bianli(struct node *ph)
{
	struct node *p = ph;
	
	while(NULL !=p->pnext)
	{
		p = p->pnext;
		printf("%d\n",p->data);
	}

}

int delete_node(struct node *ph,int data)
{
	struct node *p = ph;
	while(NULL !=p->pnext)
	{
		p = p->pnext;
		//在这里判断当前节点是不是我们要删除的节点
		if(p->data == data)
		{
			//删除 当前节点为p   p->pnext表示后一个节点的地址,p->prve表示前一个节点的地			址
			if(NULL ==p->pnext)
			{
				//尾节点
				p->prev->pnext = NULL;
				p->prev = NULL;
				free(p);
			}
			else
			{
				//	普通节点
				//前一个节点的next指针,指向后一个节点的首地址
				p->prev->pnext = p->pnext;
				//后一个节点的prev指针指向前一个节点的首地址
				p->prev->pnext = p->prev;
				free(p);
			}
			return 0;
		}
	}
	printf("未找到目标\n");
	return -1;
}

int main(void)
{
	struct node *header = create_node(0);
	insert_head(header,create_node(2));
	insert_head(header,create_node(3));
	insert_head(header,create_node(4));
	bianli(header);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值