数据结构-双向链表(C语言)

c语言实现双向链表

创建链表

销毁链表

头添加

尾添加

插入

通过查找位置删除

通过查找值删除

遍历链表

#include 
#include 
#include 

#define TYPE int

typedef struct Node
{
	struct Node* prev;	// 前一个元素
	TYPE data;			// 数据
	struct Node* next;	// 后一个元素
}Node;

// 创建节点
Node* creat_node(TYPE data)
{
	Node* node = malloc(sizeof(Node));
	node->prev = NULL;
	node->data = data;
	node->next = NULL;
	return node;
}

typedef struct List
{
	Node* head;
	Node* tail;
	size_t size;
}List;

// 创建
List* creat_list(void);
// 销毁
void destory_list(List* list);
// 头加
void head_add_list(List* list,TYPE data);
// 尾加
void tail_add_list(List* list,TYPE data);
// 插入
bool insert_list(List* list,int index,TYPE data);
// 位置删除
bool delete_index_list(List* list,int index);
// 值删除
bool delete_value_list(List* list,TYPE data);
// 遍历
void show_list(List* list);
bool del_head_list(List* list);
bool del_tail_list(List* list);

int main()
{
	List* list = creat_list();
	for(int i=0; i<10; i++)
	{
		tail_add_list(list,i);
	}
	insert_list(list,1,100);
	show_list(list);
	delete_value_list(list,0);
	delete_value_list(list,100);
	delete_value_list(list,9);
	show_list(list);
	destory_list(list);
}

// 创建
List* creat_list(void)
{
	List* list = malloc(sizeof(List));
	list->head = NULL;
	list->tail = NULL;
	list->size = 0;
	return list;
}

// 销毁
void destory_list(List* list)
{
	while(list->size>0)
	{
		del_tail_list(list);
	}
	free(list);
}

// 头加
void head_add_list(List* list,TYPE data)
{
	Node* node = creat_node(data);

	if(0 == list->size)
	{
		list->head = node;
		list->tail = node;
	}
	else
	{
		node->next = list->head;
		list->head->prev = node;
		list->head = node;
	}

	list->size++;
}

// 尾加
void tail_add_list(List* list,TYPE data)
{
	Node* node = creat_node(data);
	if(0 == list->size)
	{
		list->head = node;
		list->tail = node;
	}
	else
	{
		list->tail->next = node;
		node->prev = list->tail;
		list->tail = node;
	}
	list->size++;
}

// 插入
bool insert_list(List* list,int index,TYPE data)
{
	if(index < 0 || index >= list->size) return false;
	if(0 == index)
	{
		head_add_list(list,data);
		return true;
	}

	Node* next = NULL;
	if(index < list->size/2)
	{
		next = list->head;
		for(int i=0; inext;
		}
	}
	else
	{
		next = list->tail;
		for(int i=list->size-1; i>index; i--)
		{
			next = next->prev;
		}
	}
	Node* prev = next->prev;
	Node* node = creat_node(data);

	node->next = next;
	node->prev = prev;
	prev->next = node;
	next->prev = node;
	list->size++;

	return true;
}

// 删除头
bool del_head_list(List* list)
{
	if(list->size <= 0) return false;
	Node* node = list->head;
	if(1 == list->size)
	{
		list->head = NULL;
		list->tail = NULL;
	}
	else
	{
		list->head = node->next;
		node->next->prev = NULL;
	}
	free(node);
	list->size--;
	return true;
}

// 删除尾
bool del_tail_list(List* list)
{
	if(list->size <= 0) return false;
	Node* node = list->tail;
	if(1 == list->size)
	{
		list->head = NULL;
		list->tail = NULL;
	}
	else
	{
		list->tail = node->prev;
		node->prev->next = NULL;
	}
	free(node);
	list->size--;
	return true;
}

// 位置删除
bool delete_index_list(List* list,int index)
{
	if(index < 0 || index >= list->size) return false;
	if(0 == index) return del_head_list(list);
	if(list->size-1 == index) return del_tail_list(list);

	Node* node = NULL;
	if(index < list->size/2)
	{
		node = list->head;
		for(int i=0; inext;
		}
	}
	else
	{
		node = list->tail;
		for(int i=list->size-1; i>index; i--)
		{
			node = node->prev;
		}
	}

	Node* prev = node->prev;
	Node* next = node->next;
	
	prev->next = next;
	next->prev = prev;
	free(node);
	list->size--;
	return true;
}

// 值删除
bool delete_value_list(List* list,TYPE data)
{
	if(data == list->head->data) 
		return del_head_list(list);
	if(data == list->tail->data)
		return del_tail_list(list);
	
	Node* node = list->head;
	while(NULL != node)
	{
		if(data == node->data) break;
		node = node->next;
	}

	Node* prev = node->prev;
	Node* next = node->next;
	
	prev->next = next;
	next->prev = prev;
	free(node);
	list->size--;
	return true;	
}

// 遍历
void show_list(List* list)
{
	for(Node* i=list->head; i!=NULL; i=i->next)
	{
		printf("%d ",i->data);
	}
	printf("\n");
	for(Node* i=list->tail; i!=NULL; i=i->prev)
	{
		printf("%d ",i->data);
	}
	printf("\n");
}

 

你可能感兴趣的:(数据结构-双向链表(C语言))