数据结构 day3

思维导图

1.双向链表的基本操作

头文件:

#ifndef __DOUBLE__
#define __DOUBLE__
#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(node_p H);
void delete_head(node_p H);
void insert_tail(node_p H,int data);
void delete_tail(node_p H);
void insert_location(node_p H,int data,int location);
void delete_location(node_p H,int location);
#endif

主函数:

#include "doublelink.h"
int main(int argc, const char *argv[])
{
	node_p H=create_double();
	insert_head(H,1);
	insert_head(H,3);
	insert_head(H,5);
	insert_head(H,7);
	insert_head(H,18);
	insert_head(H,66);
	insert_head(H,777);
	show(H);
	printf("add 9 at the head\n");
	insert_head(H,9);
	show(H);
	printf("delete the head\n");
	delete_head(H);
	show(H);
	printf("add 15 in the end\n");
	insert_tail(H,15);
	show(H);
	printf("delete the end\n");
	delete_tail(H);
	show(H);
	printf("add 88 at the location 3\n");
	insert_location(H,88,3);
	show(H);
	printf("delete the location 4\n");
	delete_location(H,4);
	show(H);
	return 0;
}

自定义函数:

#include "doublelink.h"
node_p create_double()
{
	node_p H=(node_p)malloc(sizeof(node));
	if(H==NULL)
	{
		printf("apply fail\n");
		return NULL;
	}
	H->data=0;
	H->next=NULL;
	H->next=NULL;
	return H;
}
node_p create_node(int data)
{
	node_p new=(node_p)malloc(sizeof(node));
	if(new==NULL)
	{
		printf("apply fail\n");
		return NULL;
	}
	new->data=data;
	return new;
}
int empty_double(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return -1;
	}
	return H->next==NULL?1:0;
}
void insert_head(node_p H,int data)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	node_p new=create_node(data);
	if(H->next!=NULL)
	{
		new->next=H->next;
		H->next->pri=new;
	}
		H->next=new;
		new->pri=H;
		H->data++;
}
void show(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_double(H))
	{
		printf("the double_link list is void\n");
		return;
	}
	node_p p=H->next;
	while(p!=NULL)
	{
		printf("%d->",p->data);
		p=p->next;
	}

	putchar(10);
	putchar(10);
}
void delete_head(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_double(H))
	{
		printf("the double_link list is void\n");
		return;
	}
	if(H->next->next==NULL)
	{
		node_p q=H->next;
		H->next=NULL;
		free(q);
		H->data--;
		return;
	}
	node_p q=H->next;
	H->next->next->pri=H;
	H->next=H->next->next;
	free(q);
	H->data--;
}
void insert_tail(node_p H,int data)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	node_p p=H->next;
	node_p new=create_node(data);
	while(p->next!=NULL)
	{
		p=p->next;
	}
	new->next=NULL;
	p->next=new;
	new->pri=p;
	H->data++;
}
void delete_tail(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_double(H))
	{
		printf("the double_link list is void\n");
		return;
	}
	node_p p=H->next;
	while(p->next->next!=NULL)
	{
		p=p->next;
	}
	node_p q=p->next;
	p->next=NULL;
	free(q);
	H->data--;
}
void insert_location(node_p H,int data,int location)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_double(H))
	{
		printf("the double_link list is void\n");
		return;
	}
	if(location<=0||location>data+1)
	{
		printf("the location is wrong\n");
		return;
	}
	node_p new=create_node(data);
	node_p p=H;
	for(int i=0;i<location-1;i++)
	{
		p=p->next;
	}
	new->next=p->next;
	p->next->pri=new;
	p->next=new;
	new->pri=p;
	H->data++;
}
void delete_location(node_p H,int location)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_double(H))
	{
		printf("the double_link list is void\n");
		return;
	}
	
	node_p p=H;
	for(int i=0;i<location-1;i++)
	{
		p=p->next;
	}
	if(location==H->data)
	{
		node_p q=p->next;
		p->next=NULL;
		free(q);
		H->data--;
	}
	node_p q=p->next;
	p->next=q->next;
	q->next->pri=p;
	free(q);
	H->data--;
	return;
}

功能实现

2.顺序表和链表的区别

见思维导图

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值