双向链表 double_list2(封装)

实现双向链表的各个功能

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define TYPE int

typedef struct Node
{
	struct Node* prev;
	TYPE data;
	struct Node* next;
}Node;

Node* create_node(TYPE data)
{
	Node* node = malloc(sizeof(Node));
	node->prev = node;
	node->data = data;
	node->next = node;
	return node;
}

typedef struct DoubleList
{
	Node* head;
	size_t size;
}DoubleList;

DoubleList* create_list(void)
{
	DoubleList* list = malloc(sizeof(DoubleList));
	list->head = create_node(0);
	return list;
}

void _add_list(Node* p,Node* n,TYPE data)
{
	Node* node = create_node(data);
	node->prev = p;
	node->next = n;
	p->next = node;
	n->prev = node;
}

void _del_list(Node* node)
{
	node->prev->next = node->next;
	node->next->prev = node->prev;
	free(node);
}

Node* _index_list(DoubleList* list,size_t index)
{
	if(index >= list->size)	return NULL;
	if(index < list->size/2)
	{
		Node* n = list->head->next;
		while(index--)	n = n->next;
		return n;
	}
	else
	{
		Node* n = list->head->prev;
		while(++index < list->size)	n = n->prev;
		return n;
	}
}

Node* _value_list(DoubleList* list,TYPE data)
{
	for(Node* n=list->head->next; n!=list->head; n=n->next)
	{
		if(n->data == data)	return n;
	}
	return NULL;
}

void add_head_list(DoubleList* list,TYPE data)
{
	_add_list(list->head,list->head->next,data);
	list->size++;
}

void add_tail_list(DoubleList* list,TYPE data)
{
	_add_list(list->head->prev,list->head,data);
	list->size++;
}

bool insert_list(DoubleList* list,size_t index,TYPE data)
{
	Node* node = _index_list(list,index);
	if(NULL == node)	return false;

	_add_list(node->prev,node,data);
	list->size++;
	return true;
}

bool modify_index_list(DoubleList* list,size_t index,TYPE data)
{
	Node* node = _index_list(list,index);
	if(NULL == node)	return false;

	node->data = data;
	return true;
}


int modify_value_list(DoubleList* list,TYPE old,TYPE data)
{
	int cnt = 0;
	for(Node* n=list->head->next; n!=list->head; n=n->next)
	{
		if(n->data == old)
		{
			n->data = data;
			cnt++;
		}
	}
	return cnt;
}

bool access_list(DoubleList* list,size_t index,TYPE* val)
{
	Node* node = _index_list(list,index);
	if(NULL == node)	return false;

	*val = node->data;
	return true;
}

int query_list(DoubleList* list,TYPE data)
{
	Node* n = list->head->next;
	for(int i=0; i<list->size; i++)
	{
		if(n->data == data)	return i;
		n = n->next;
	}
	return -1;
}

bool del_index_list(DoubleList* list,size_t index)
{
	Node* node = _index_list(list,index);
	if(NULL == node)	return false;
	
	_del_list(node);
	list->size--;
	return true;
}

bool del_value_list(DoubleList* list,TYPE data)
{
	Node* node = _value_list(list,data);
	if(NULL == node)	return false;

	_del_list(node);
	list->size--;
	return true;
}

void sort_list(DoubleList* list)
{
	for(Node* n1=list->head->next; n1->next!=list->head; n1=n1->next)
	{
		for(Node* n2=n1->next; n2!=list->head; n2=n2->next)
		{
			if(n1->data > n2->data)
			{
				TYPE t = n1->data;
				n1->data = n2->data;
				n2->data = t;
			}
		}
	}
}

void show_list(DoubleList* list)
{
	for(Node* n=list->head->next; n!=list->head; n=n->next)
	{
		printf("%d ",n->data);
	}
	printf("\n");
}

void clean_list(DoubleList* list)
{
	Node* n = list->head->next;
	while(n!=list->head)
	{
		Node* temp = n;
		n = n->next;
		free(temp);
	}
	list->head->prev = list->head;
	list->head->next = list->head;
	list->size = 0;
}

void destory_list(DoubleList* list)
{
	clean_list(list);
	free(list->head);
	free(list);
}


int main(int argc,const char* argv[])
{
	DoubleList* list = create_list();

	for(int i=0; i<10; i++)
	{
		add_tail_list(list,i);
	}
	insert_list(list,1,9);
	show_list(list);
	modify_value_list(list,9,-1);
	show_list(list);
	modify_index_list(list,1,11);
	show_list(list);
	TYPE val;
	access_list(list,10,&val);
	printf("%d\n",val);
	sort_list(list);
	show_list(list);
	printf("----------\n");
	clean_list(list);
	for(int i=0; i<10; i++)
	{
		add_tail_list(list,i);
	}
	show_list(list);
	
	destory_list(list);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LJIEIJL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值