数据结构---双向链表的操作

目录

1:双向链表的介绍

2:双向链表的定义以及建立

(2)头插法建立

3:双向链表的操作

(1)删除某个值为x的元素

(2)删除pos位置的元素

(3)在位置pos插入值为x的元素

4:完整代码演示


1:双向链表的介绍

双向链表与单链表是一样的,只不过在定义时多了一个可以向前的指针,单链表只有next指向后面的指针,双向既有指向前方也有指向后方。

typedef struct node{
	int data;
	struct node *next;//后指针
	struct node *prior;//前指针
}node;

2:双向链表的定义以及建立

(1)尾插法建立

node* weicha(node *L)
{
	node *s,*t;
	L=(node*)malloc(sizeof(node));
	L->next=NULL;
	s=L;
	int x;
	char ch;
	printf("请输入数据:");
	scanf("%d",&x);
	ch=getchar();
	while(ch!=10){
		t=(node*)malloc(sizeof(node));
		t->data=x;
		t->prior=s;
		t->next=NULL;
		s->next=t;
		s=t;//
		scanf("%d",&x);
		ch=getchar();
	}
	t=(node*)malloc(sizeof(node));
	t->data=x;
	t->next=NULL;
	t->prior=s;
	s->next=t;
	s=t;
	s->next=NULL;
	return L;
}

(2)头插法建立

node* toucha(node *L)
{
	node *s,*r;
	L=(node*)malloc(sizeof(node));
	L->next=NULL;
	int x;
	char ch;
	printf("请输入数据:");
	scanf("%d",&x);
	ch=getchar();
	while(ch!=10){
		s=(node*)malloc(sizeof(node));
		s->data=x;
		s->next=L->next;
		s->prior=NULL;
		L->prior=s;
		L->next=s;
		scanf("%d",&x);
		ch=getchar();
	}
		s=(node*)malloc(sizeof(node));
		s->data=x;
		s->next=L->next;
		s->prior=NULL;
		L->prior=s;
		L->next=s;
		return L;
}

3:双向链表的操作

(1)删除某个值为x的元素

单链表无法删除指定的元素,是因为要删除当前元素,则需要用到该元素的前面一个指针,修改它的next值,单链表如果这样做,通过一个计数器可以获得该位置,可是当删除的元素很多时,这样子的复杂度就很大,所以双向链表用这个很方便。

//删除值为x的元素
void del(int x,node *L)
{
	node *s=L->next;
	while(s){
		if(s->data==x){
			s->prior->next=s->next;
		}
	
		s=s->next;
	}
}

(2)删除pos位置的元素


//删除位置pos的元素
void shanchu(node *L,int pos)
{
	int sum=0;
	node *s=L;
	while(sum<pos){
		sum++;
		s=s->next;
	}
	s->prior->next=s->next;
	s->next->prior=s->prior;
}

(3)在位置pos插入值为x的元素

 

//在pos位置插入值为x的元素
void inser(int pos,int x,node *L)
{
	node *s=L->next;
	int sum=1;
	while(sum<pos-1){
		sum++;
		s=s->next;
	}
	node *t;//插入则要新创建一个空间
	t=(node*)malloc(sizeof(node));
	t->data=x;
	t->next=s->next;
	t->prior=s;
	s->next=t;
	t->next->prior=t;
}

4:完整代码演示

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

typedef struct node{
	int data;
	struct node *next;
	struct node *prior;
}node;

node* toucha(node *L)
{
	node *s,*r;
	L=(node*)malloc(sizeof(node));
	L->next=NULL;
	int x;
	char ch;
	printf("请输入数据:");
	scanf("%d",&x);
	ch=getchar();
	while(ch!=10){
		s=(node*)malloc(sizeof(node));
		s->data=x;
		s->next=L->next;
		s->prior=NULL;
		L->prior=s;
		L->next=s;
		scanf("%d",&x);
		ch=getchar();
	}
		s=(node*)malloc(sizeof(node));
		s->data=x;
		s->next=L->next;
		s->prior=NULL;
		L->prior=s;
		L->next=s;
		return L;
}
//尾插建立
node* weicha(node *L)
{
	node *s,*t;
	L=(node*)malloc(sizeof(node));
	L->next=NULL;
	s=L;
	int x;
	char ch;
	printf("请输入数据:");
	scanf("%d",&x);
	ch=getchar();
	while(ch!=10){
		t=(node*)malloc(sizeof(node));
		t->data=x;
		t->prior=s;
		t->next=NULL;
		s->next=t;
		s=t;//
		scanf("%d",&x);
		ch=getchar();
	}
	t=(node*)malloc(sizeof(node));
	t->data=x;
	t->next=NULL;
	t->prior=s;
	s->next=t;
	s=t;
	s->next=NULL;
	return L;
}

//遍历

//删除值为x的元素
void del(int x,node *L)
{
	node *s=L->next;
	while(s){
		if(s->data==x){
			s->prior->next=s->next;
		}
	
		s=s->next;
	}
}

//删除位置pos的元素
void shanchu(node *L,int pos)
{
	int sum=0;
	node *s=L;
	while(sum<pos){
		sum++;
		s=s->next;
	}
	s->prior->next=s->next;
	s->next->prior=s->prior;
}
void shuchu(node *L)
{
	node *s=L->next;
	while(s){
		printf("%d ",s->data);
		s=s->next;
	}
	printf("\n");
}

//在pos位置插入值为x的元素
void inser(int pos,int x,node *L)
{
	node *s=L->next;
	int sum=1;
	while(sum<pos-1){
		sum++;
		s=s->next;
	}
	node *t;//插入则要新创建一个空间
	t=(node*)malloc(sizeof(node));
	t->data=x;
	t->next=s->next;
	t->prior=s;
	s->next=t;
	t->next->prior=t;
}
int main()
{
	node *L;
	L=weicha(L);
	shuchu(L);
	shanchu(L,3);
	shuchu(L);
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜到极致就是渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值