链表的相关操作
#pragma once
#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);
#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* curr = list->head;
while (curr != NULL) {
Node* next = curr->next;
free(curr);
curr = next;
}
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->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;
}
if (list->size == 0) {
list->head = newNode;
}
list->tail = newNode;
list->size++;
}
void add_node(List* list, int idx, int val) {
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;
}
Node* newNode = malloc(sizeof(Node));
if (newNode == NULL) {
printf("malloc failed in add\n");
exit(1);
}
newNode->val = val;
Node* curr = list->head;
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) {
if (idx < 0 || idx >= list->size) {
printf("Illegal argument: idx = %d\n", idx);
return -1;
}
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;
}
bool delete_node(List* list, int val) {
Node* prev = NULL;
Node* curr = list->head;
while (curr != NULL) {
if (curr->val == val) {
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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了