數據結構-雙向鏈表(C語言)

c語言實現雙向鏈表

創建鏈表

銷燬鏈表

頭添加

尾添加

插入

通過查找位置刪除

通過查找值刪除

遍歷鏈表

#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* 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; i<index; i++)
		{
			next = next->next;
		}
	}
	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; i<index; i++)
		{
			node = node->next;
		}
	}
	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");
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章