数据结构day5

双向链表的按位置删除

双向链表的按位置修改

双向链表的按位置查找

//按位置删除
int delete_pos(doublelink L,int pos)
{
	if(L==NULL || pos<1 || pos>L->len+1)
		return -1;
	doublelink p=L;
	for(int i=1;i<pos;i++)
	{
		p=p->next;
	}
	doublelink q=p->next;
	p->next=q->next;
	if(p->next)
	{
		q->next->prev=p;
	}
	L->len--;
	free(q);
	q=NULL;
}

//按位置修改
int update_pos(doublelink L,int pos,datatype e)
{
	if(L==NULL || pos<1 || pos>L->len+1)
		return -1;
	doublelink p=L;
	for(int i=0;i<pos;i++)
	{
		p=p->next;
	}
	p->data=e;
	return 0;
}

//按位置查找
int search_pos(doublelink L,int pos)
{
	if(L==NULL || pos<1 || pos>L->len+1)
		return -1;
	doublelink p=L;
	for(int i=0;i<pos;i++)
	{
		p=p->next;
	}
	printf("按位置 %d 查找到的是 %c\n",pos,p->data);
	return 0;
}

双向循环链表头插

双向循环链表头删

双向循环链表尾删

#include "head2.h"

loopdouble create(int flag)
{
	loopdouble L=(loopdouble)malloc(sizeof(struct Node));
	if(L==NULL)
	{
		return NULL;
	}
	if(flag==1)  //头结点
	{
		L->len=0;
		L->next=L;
		L->prev=L;
	}
	else if(flag==0)
	{
		L->data=0;
		L->next=NULL;
		L->prev=NULL;
	}
	return L;
}

//头插
int Insert_head(loopdouble L,datatype e)
{
	if(L==NULL)
		return -1;
	loopdouble p=L->next;
	loopdouble q=create(0);
	if(q==NULL)
		return -1;
	L->next=q;
	q->prev=L;
	if(L->next!=L)
	{
		p->prev=q;
		q->next=p;
	}
	else if(L->next==L)
	{
		L->prev=q;
		q->next=L;
	}
	q->data=e;
	L->len++;
	return 0;
}

//遍历
void output(loopdouble L)
{
    if(L==NULL || L->next==L)
        return ;
    printf("正向遍历\n");
    loopdouble p=L;
    while(p->next!=L)
    {
        p=p->next;
        printf("%c\t",p->data);
    }
    printf("\n逆向遍历\n");
    while(p->prev!=L->prev)
    {
        printf("%c\t",p->data);
        p=p->prev;
    }
    putchar(10);
}

//尾插
int Insert_rear(loopdouble L,datatype e)
{
	if(L==NULL)
		return -1;
	loopdouble p=create(0);
	if(p==NULL)
		return -1;
	L->prev->next=p;
	p->prev=L->prev;
	p->next=L;
	L->prev=p;
	p->data=e;
	L->len++;
	return 0;
}

//头删
int delete_head(loopdouble L)
{
	if(L==NULL || L->next==L)
		return -1;
	loopdouble p=L->next;
	if(p->next!=L)
	{
	L->next=p->next;
	p->next->prev=L;
	}
	else 
	{
		L->next=L;
		L->prev=L;
	}
	L->len--;
	free(p);
	p=NULL;
	return 0;
}

//尾删
int delete_rear(loopdouble L)
{
	if(L==NULL || L->next==L)
		return -1;
	loopdouble p=L->prev;
	p->prev->next=L;
	L->prev=p->prev;
	L->len--;
	free(p);
	p=NULL;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值