2024.4.17 作业 xyt

1.双向链表的头插、头删、尾插、尾删、按位置插入、按位置删除

#include "double_list.h"
int main(int argc, const char *argv[])
{
	node_p H=create_double();
	insert_head(H,11);
	insert_head(H,22);
	insert_tail(H,55);
	show_double(H);
	insert_pos(H,333,4);
	show_double(H);
	dele_tail(H);
	show_double(H);
	dele_pos(H,3);
	show_double(H);
	return 0;
}
#ifndef __DOUBLE_LIST_H__
#define __DOUBLE_LIST_H__
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
	struct node *pri;
}node,*node_p;

//创建双向链表
node_p create_double();
//创建结点
node_p create_node(int data);
//判空
int empty_double(node_p H);
//头插
void insert_head(node_p H,int data);
//输出
void show_double(node_p H);
//头删
void dele_head(node_p H);
//尾插
void insert_tail(node_p H,int data);
//尾删
void dele_tail(node_p H);
//按位置插入
void insert_pos(node_p H,int data,int pos);
//按位置删除
void dele_pos(node_p H,int pos);
#endif
#include "double_list.h"
//创建双向链表
node_p create_double()
{
	node_p H=(node_p)malloc(sizeof(node));
	if(H==NULL)
	{
		printf("空间申请失败\n");
		return NULL;
	}
	H->data=0;
	H->next=NULL;
	H->pri=NULL;
	return H;
}
//创建结点
node_p create_node(int data)
{
	node_p new=(node_p)malloc(sizeof(node));
	if(new==NULL)
	{
		printf("空间申请失败\n");
		return NULL;
	}
	new->data=data;
	return new;
}
//判空
int empty_double(node_p H)
{
	if(H==NULL)
	{
		printf("入参为空\n");
		return -1;
	}
	return H->next==NULL?1:0;
}
//头插
void insert_head(node_p H,int data)
{
	if(H==NULL)
	{
		printf("入参为空\n");
		return;
	}
	node_p new=create_node(data);
	new->next=H->next;
	if(H->next!=NULL)
	{
		H->next->pri=new;
	}
	new->pri=H;
	H->next=new;
	H->data++;
}
//输出
void show_double(node_p H)
{
	if(H==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(empty_double(H))
	{
		printf("链表为空\n");
	}
	node_p p=H->next;
	while(p!=NULL)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	putchar(10);
}
//头删
void dele_head(node_p H)
{
	if(H==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(empty_double(H))
	{
		printf("链表为空\n");
	}
	node_p p=H->next;
	if(H->next->next!=NULL)
	{
		H->next->next->pri=H;
	}
	H->next=H->next->next;
	free(p);
}
//尾插
void insert_tail(node_p H,int data)
{
	if(H==NULL)
	{
		printf("入参为空\n");
		return;
	}
	node_p p=H;
	while(p->next!=NULL)
	{
		p=p->next;
	}
	node_p new=create_node(data);
	new->next=p->next;
	p->next=new;
	new->pri=p;
	H->data++;
}
//尾删
void dele_tail(node_p H)
{
	if(H==NULL)
	 {
		 printf("入参为空\n");
		 return;
	 }
	if(empty_double(H))
	{
		printf("链表为空\n");

	}
	 node_p p=H;
	while(p->next->next!=NULL)
	{
		p=p->next;
	}
	node_p temp=p->next;
	p->next=NULL;
	free(temp);
}
//按位置插入
void insert_pos(node_p H,int data,int pos)
{
	if(H==NULL)
	 {
		 printf("入参为空\n");
		 return;
	 }
	 if(pos<=0||pos>H->data+1)
	 {
		printf("位置不合理\n");
		return;
	 }
	node_p p=H;
	for(int i=0;i<pos-1;i++)
	{
		p=p->next;
	}
	node_p new=create_node(data);
	new->next=p->next;
	if(p->next!=NULL)
	{
		p->next->pri=new;
	}
	new->pri=p;
	p->next=new;
	H->data++;
}
//按位置删除
void dele_pos(node_p H,int pos)
{
	if(H==NULL)
	 {
		 printf("入参为空\n");
		 return;
	 }
	 if(empty_double(H))
	 {
		 printf("链表为空\n");
		 return;
	 }
	 if(pos<=0||pos>H->data)
	 {
		printf("位置不合理\n");
		return;
	 }
	 node_p p=H;
	 for(int i=0;i<pos-1;i++)
	 {
		 p=p->next;
	 }
	 node_p temp=p->next;
	 if(p->next->next!=NULL)
	 {
		p->next->next->pri=p;
	 }
	 p->next=p->next->next;
	 free(temp);
	 H->data--;
}

2.总结链表和顺序表的区别

顺序表适用于数据元素较少、读取操作频繁的场景;

链表适用于数据元素较多、插入、删除操作频繁的场景。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值