数据结构第3天

1,双链表


#include "./01double_list.h"
//创建头节点
dou* create_doublelist()
{
	dou* head=(dou*)malloc(sizeof(dou));
	memset(head,0,sizeof(dou));
	return head;
}

//头插
void insret_double(dou* head,datadType data)
{
	dou* temp=(dou*)malloc(sizeof(dou));
	if(NULL==temp)
	{
		printf("新节点创建失败\n");
	}
	temp->txt.data=data;
	temp->next=NULL;
	temp->prev=NULL;
	if(head->next==NULL)
	{
		temp->next=head->next;
		head->next=temp;
		temp->prev=head;
	}
	else
	{
	head->next->prev=temp;
	temp->prev=head;
	temp->next=head->next;
	head->next=temp;
	}
	head->txt.len++;
	return;
}
//遍历
void BL(dou* head)
{

	dou* p=head;
	while(p->next != NULL)
	{
		p=p->next;
		printf("%d ",p->txt.data);
	}
	putchar(10);
}
//尾插
void weicha_doublelist(dou* head, datadType data)
{
	dou* temp=(dou*)malloc(sizeof(dou));
	if(temp==NULL)
	{
		printf("创建节点失败");
		return;
	}
	temp->txt.data=data;
	temp->next=NULL;
	temp->prev=NULL;
	dou* p = head;
	while(p->next != NULL)
	{
		p=p->next;
	}
	temp->next=p->next;
	p->next=temp;
	temp->prev=p;
	head->txt.len++;
	return;
}
//按位置插入
void insret_doublebypos(dou* head, datadType data,int pos)
{
	dou* temp=(dou*)malloc(sizeof(dou));
	if(temp==NULL)
	{
		printf("创建节点失败\n");
		return;
	}
	temp->txt.data=data;
	temp->next=NULL;
	temp->prev=NULL;

	dou* p=head;
	for(int i=0; i<pos-1; i++)
	{
		p=p->next;
	}
	if(p->next == NULL)
	{
		temp->next=p->next;
		p->next=temp;
		temp->prev=head;
	}
	else
	{
	temp->next=p->next;
	p->next=temp;
	temp->next->prev=temp;
	temp->prev=p;
	}
	head->txt.len++;
	return;
}
//头删
datadType Tshan_list(dou* head)
{
	if(NULL==head->next)
	{
		printf("无数据删除失败\n");
		return (datadType)-1;
	}
	dou* p=head;
	dou* temp=p->next;
	if(temp->next==NULL)
	{
		temp->next=NULL;
	}
	else
	{
		p->next=temp->next;
		temp->next->prev=head;
	}
	datadType data = temp->txt.data;
	free(temp);
	head->txt.len--;
	return data;
}
//尾删
datadType Wshan_list(dou* head)
{
	if(head==NULL || head->next==NULL)
	{
		printf("链表为空或者能够非法\n");
		return (datadType)-1;
	}
	dou* temp= head;
	while (temp->next != NULL)
	{
		temp=temp->next;
	}
	datadType data=temp->txt.data;
	temp->prev->next=NULL;
	head->txt.len--;
	return data;
}
//按位置删除
datadType shan_doublelist(dou* head,int pos)
{
	if(pos<1 || pos>head->txt.len)
	{
		printf("pos超出数据范围");
		return (datadType)-1;
	}
	if(head==NULL || head->next==NULL)
	{
		printf("链表为空或者能够非法\n");
		return (datadType)-1;
	}
	dou* p=head;
	for(int i=0;i<pos;i++)
	{
		p=p->next;
	}
	datadType data=p->txt.data;
	if(p->next == NULL)
	{
		p->prev->next=NULL;
	}
	else
	{
	p->next->prev=p->prev;
	p->prev->next=p->next;
	head->txt.len--;
	}
	return data;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值