数据结构-5

头删

 尾删

 按位置插入

 按位置删除

#ifndef __DOUBLE_LIST_H__
#define __DOUBLE_LIST_H__
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct node
{
	struct node *pri;
	union 
	{
		datatype data;
		int len;
	};
	struct node *next;
}node,*double_p;

//1、创建双向链表
double_p create_double();
//2、创建双向结点
double_p create_node(datatype data);
//3、头插
void insert_head(double_p H,datatype data);
//4、输出
void show_double(double_p H);
//5、头删
void dele_head(double_p H);
//6、尾删
void dele_tail(double_p H);
//7、按位置插入
void insert_pos(double_p H,datatype data,int pos);
//8、按位置删除
void dele_pos(double_p H,int pos);

#endif
#include "double_list.h"
int main(int argc, const char *argv[])
{
	double_p H=create_double();
	insert_head(H,10);	
	insert_head(H,20);	
	insert_head(H,30);	
	insert_head(H,40);	
	insert_head(H,60);	
	printf("头插后\n");
	show_double(H);
	dele_head(H);
	printf("头删后\n");
	show_double(H);
	insert_pos(H,11,5);
	printf("位置插入后\n");
	show_double(H);
	dele_tail(H);
	printf("尾删后\n");
	show_double(H);
	dele_pos(H,1);
	printf("按位置删除后\n");
	show_double(H);
	return 0;
}
#include "double_list.h"
//1、创建双向链表
double_p create_double()
{
	double_p H=(double_p)malloc(sizeof(node));
	if(H==NULL)
	{
		printf("空间申请失败\n");
		return NULL;
	}
	H->len=0;
	H->next=NULL;
	H->pri=NULL;
	return H;
}

//2、创建双向结点
double_p create_node(datatype data)
{
	double_p new=(double_p)malloc(sizeof(node));
	if(new==NULL)
	{
		printf("创建失败\n");
		return NULL;
	}
	new->data =data;
	return new;
}
//3、头插
void insert_head(double_p H,datatype data)
{
	if(H==NULL)
	{
		printf("入参为空\n");
		return;
	}
	double_p new=create_node(data);
	new->next=H->next;
	if(H->next!=NULL)
	{
		H->next->pri=new;
	}
	new->pri=H;
	H->next=new;
	H->len++;
}
//4、输出
void show_double(double_p H)
{
	if(H==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(H->next==NULL)
	{
		printf("链表为空\n");
		return;
	}
	double_p p=H->next;
	while(p!=NULL)
	{
		printf("%d->",p->data);
		p=p->next;
	}
	printf("NULL\n");
}

//5、头删
void dele_head(double_p H)
{
	if(H==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(H->next==NULL)
	{
		printf("链表为空\n");
		return;
	}
	double_p del=H->next;
	if(del->next!=NULL)
		del->next->pri=H;
	H->next=del->next;
	free(del);
	H->len--;
}
//6、尾删
void dele_tail(double_p H)
{
	if(H==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(H->next==NULL)
	{
		printf("链表为空\n");
		return;
	}
	double_p p=H;
	while(p->next->next!=NULL)
	{
		p=	p->next;
	}
	double_p del=p->next;
	p->next=NULL;
	free(del);
	H->len--;
}
//7、按位置插入
void insert_pos(double_p H,datatype data,int pos)
{
	if(H==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(pos<1||pos>H->len+1)
	{
		printf("位置不合理\n");
	}
	double_p p=H;
	for(int i= 0;i<pos-1;i++,p=p->next);
	double_p new=create_node(data);
	new->next=p->next;
	if(p->next!=NULL)
	{
		p->next->pri=new;
	}
	new->pri=p;
	p->next=new;
	H->len++;
}
//8、按位置删除
void dele_pos(double_p H,int pos)
{
	if(H==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(pos<1||pos>H->len)
	{
		printf("位置不合理\n");
	}

	if(H->next==NULL)
	{
		printf("链表为空\n");
		return;
	}
	double_p p=H;
	for(int i= 0;i<pos-1;i++,p=p->next);
	double_p del=p->next;
	p->next=del->next;
	del->next->pri=p;
	free(del);
	H->len--;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值