C语言链表的实现

List.h

#pragma once

// 编程的接口(API)
// *.h 文件中一般会放:类型的定义,函数的声明,全局变量

#include <stdbool.h>

typedef struct node {
	int val;
	struct node* next;
} Node;

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


List* create_list();
void destroy_list(List* list);

void add_before_head(List* list, int val);
void add_behind_tail(List* list, int val);
void add_node(List* list, int idx, int val);

bool delete_node(List* list, int val);

int find_by_index(List* list, int idx);
Node* search(List* list, int val);

List.c

#include "List.h"
#include <stdlib.h>
#include <stdio.h>

// 创建空的链表
List* create_list() {
	return calloc(1, sizeof(List));
}

// 释放资源
void destroy_list(List* list) {
	if (list == NULL) return;
	// 先释放所有Node
	Node* curr = list->head;
	while (curr != NULL) {
		Node* next = curr->next;
		free(curr);
		curr = next;
	}
	// 释放List结构体
	free(list);
}

// 头插法
void add_before_head(List* list, int val) {
	// 创建结点
	Node* newNode = malloc(sizeof(Node));
	if (newNode == NULL) {
		printf("malloc failed in add_before_head\n");
		exit(1);
	}
	// 初始化结点
	newNode->val = val;
	newNode->next = list->head;
	// 更新List的信息
	list->head = newNode;
	if (list->size == 0) {
		list->tail = newNode;
	}
	list->size++;
}

// 尾插法
void add_behind_tail(List* list, int val) {
	// 创建结点
	Node* newNode = malloc(sizeof(Node));
	if (newNode == NULL) {
		printf("malloc failed in add_behind_tail\n");
		exit(1);
	}
	// 初始化结点
	newNode->val = val;
	newNode->next = NULL;
	// 链接
	if (list->size != 0) {
		list->tail->next = newNode;
	}
	// 更新List信息
	if (list->size == 0) {
		list->head = newNode;
	}
	list->tail = newNode;
	list->size++;
}

// 在任意位置插入
//void add_node(List* list, int idx, int val) {
//	// 1. 参数校验
//	if (idx < 0 || idx > list->size) {
//		printf("Illegal argument: idx = %d\n", idx);
//		return;
//	}
//	// 2. 创建结点
//	Node* newNode = malloc(sizeof(Node));
//	if (newNode == NULL) {
//		printf("malloc failed in add\n");
//		exit(1);
//	}
//	// 3. 初始化结点
//	newNode->val = val;
//	
//	// 链接, 并且更新 List 信息
//	if (list->size == 0) {
//		newNode->next = NULL;
//
//		list->head = newNode;
//		list->tail = newNode;
//	} 
//	else if (idx == 0) {
//		// 头插法
//		newNode->next = list->head;
//		list->head = newNode;
//	}
//	else {
//		// 找索引为 idx-1 的结点
//		Node* curr = list->head;
//		//循环不变式:curr指向结点的索引为i
//		for (int i = 0; i < idx - 1; i++) {
//			curr = curr->next;
//		}
//		newNode->next = curr->next;
//		curr->next = newNode;
//		if (idx == list->size) {
//			list->tail = newNode;
//		}
//	}
//	
//	list->size++;
//}

void add_node(List* list, int idx, int val) {
	// 1. 参数校验
	if (idx < 0 || idx > list->size) {
		printf("Illegal argument: idx = %d\n", idx);
		return;
	}
	// 头插
	if (idx == 0) {
		add_before_head(list, val);
		return;
	}
	// 尾插
	if (idx == list->size) {
		add_behind_tail(list, val);
		return;
	}

	// 在中间插入
	// 1. 创建结点
	Node* newNode = malloc(sizeof(Node));
	if (newNode == NULL) {
		printf("malloc failed in add\n");
		exit(1);
	}
	// 2. 初始化结点
	newNode->val = val;
	// 3. 找索引为 idx-1 的结点
	Node* curr = list->head;
	//循环不变式:curr指向结点的索引为i
	for (int i = 0; i < idx - 1; i++) {
		curr = curr->next;
	}

	newNode->next = curr->next;
	curr->next = newNode;
	
	list->size++;
}

// 根据索引查找值
int find_by_index(List* list, int idx) {
	// 1. 参数校验
	if (idx < 0 || idx >= list->size) {
		printf("Illegal argument: idx = %d\n", idx);
		return -1;
	}
	// 2. 查找索引为 idx 的元素
	Node* curr = list->head;
	for (int i = 0; i < idx; i++) {
		curr = curr->next;
	}
	return curr->val;
}

// 查找与特定值相等的结点
Node* search(List* list, int val) {
	Node* curr = list->head;
	while (curr != NULL) {
		if (curr->val == val) {
			return curr;
		}
		// 移动到下一个结点
		curr = curr->next;
	}
	return NULL;
}

// 删除第一个值等于val的结点
bool delete_node(List* list, int val) {
	Node* prev = NULL;
	Node* curr = list->head;
	while (curr != NULL) {
		if (curr->val == val) {
			// 删除curr结点

			if (prev == NULL) {
				// 修改链表信息
				
				list->head = curr->next;
				if (list->size == 1) {
					list->tail = NULL;
				}
				list->size--;
				free(curr);
			}
			else {
				// 修改链表信息

				prev->next = curr->next;
				if (curr->next == NULL) {
					list->tail = prev;
				}
				list->size--;
				free(curr);
			}
			return true;
		}
		// 往后移
		prev = curr;
		curr = curr->next;
	}
	return false;
}

main.c

#include "List.h"

int main(void) {
	List* list = create_list();

	// add_before_head(list, 1);
	// add_before_head(list, 2);
	// add_before_head(list, 3);

	// add_behind_tail(list, 1);
	// add_behind_tail(list, 2);
	// add_behind_tail(list, 3);

	add_node(list, 0, 100); // 100
	add_node(list, 0, 200); // 200 -> 100
	add_node(list, 2, 300); // 200 -> 100 -> 300
	add_node(list, 1, 400); // 200 -> 400 -> 100 -> 300

	// printf("%d\n", find_by_index(list, 1));
	// printf("%d\n", find_by_index(list, 3));
	// printf("%d\n", find_by_index(list, 10));

	// Node* node = search(list, 100);
	// node = search(list, 600);

	bool flag = delete_node(list, 200);
	flag = delete_node(list, 200);
	flag = delete_node(list, 100);

	destroy_list(list);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值