4.17作业

发布于:2024-04-18 ⋅ 阅读:(33) ⋅ 点赞:(0)

#include "double_link_list.h"
node_p create_double_link_list()  //创建双向链表
{
	node_p H=(node_p)malloc(sizeof(node));
	if(H==NULL)
	{
		printf("空间申请失败\n");
		return NULL;
	}
	H->data=0;
	H->pri=NULL;
	H->next=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_link_list(node_p H)  //判空
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return -1;
	}
	return H->pri==NULL && H->next==NULL?1:0;
}
void head_insert(node_p H,int data)  //头插
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	node_p new=create_node(data);
	if(H->next!=NULL)
	{
		new->next=H->next;
		H->next->pri=new;
	}
	new->pri=H;
	H->next=new;
	H->data++;
}
void head_delet(node_p H)  //头删
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	if(empty_double_link_list(H))
	{
		printf("链表为空,无需删除\n");
		return;
	}
	node_p del=H->next;
	if(del->next!=NULL)
	{
		del->next->pri=H;
		H->next=del->next;
	}
	else
		H->next=NULL;
	free(del);
	H->data--;
}
void show_double_link_list(node_p H)  //输出
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	if(empty_double_link_list(H))
	{
		printf("链表为空,无需输出\n");
		return;
	}
	node_p p=H;
	for(int i=0;i<H->data;i++)
	{
		p=p->next;
		printf("%-4d",p->data);
	}
	printf("NULL");
	putchar(10);
}
void tail_insert(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 tail_delet(node_p H)  //尾删
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	if(empty_double_link_list(H))
	{
		printf("链表为空,无需删除\n");
		return;
	}
	node_p p=H;
	while(p->next->next!=NULL)
		p=p->next;
	node_p del=p->next;
	p->next=del->next;
	free(del);
	H->data--;
}
void pos_insert(node_p H,int pos,int data)  //按位置插入
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	if(pos<1 || 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);
	if(p->next!=NULL)
	{
		new->next=p->next;
		p->next->pri=new;
	}
	new->pri=p;
	p->next=new;
	H->data++;
}
void pos_delet(node_p H,int pos)  //按位置删除
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	if(empty_double_link_list(H))
	{
		printf("表为空,无需删除\n");
		return;
	}
	node_p p=H;
	for(int i=0;i<pos-1;i++)
		p=p->next;
	node_p del=p->next;
	if(p->next!=NULL)
		del->next->pri=p;
	p->next=del->next;
	free(del);
	H->data--;
}
void overturn_link(node_p H)   //单向链表的逆置
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	if(empty_link_list(H))
	{
		printf("链表为空,无需逆置\n");
		return;
	}
	if(H->next->next==NULL)
	{
		printf("链表只有一个元素,无需逆置\n");
		return;
	}
	node_p p=H->next->next;
	H->next->next=NULL;
	while(p!=NULL)
	{
		node_p q=p->next;
		p->next=H->next;
		H->next=p;
		p=q;
	}
}

链表和顺序表的区别:

顺序表的物理结构是顺序存储,在逻辑上连续,物理地址也连续,但会造成内存空间浪费。

链表的物理结构是链式存储,在逻辑上连续,物理地址不连续,单个结点创建和删除,不会造成内存空间浪费。