拆分:
typedef int ElementType;
typedef struct node {
ElementType data;
struct node* next;
} Node;
typedef struct {
Node* head;
Node* tail;
int size;
} LinkedList;
- 建表
LinkedList* create_linked_list() {
return calloc(1, sizeof(LinkedList)); // calloc()自动初始化写入0
}
- 头插法
bool head_insertion(LinkedList* list, ElementType new_data) {
Node* new_node = (Node*)malloc(sizeof(Node));
// 动态分配失败的处理
if (new_node == NULL) {
printf("Error: malloc failed in head_insertion.\n");
return false;
}
// 新结点要链接在整个单链表的 头部
new_node->data = new_data;
new_node->next = list->head;
list->head = new_node;
list->size++;
if (list->tail == NULL) {
list->tail = new_node;
}
return true;
}
- 尾插法
bool tail_insertion(LinkedList* list, ElementType new_data) {
Node* new_node = (Node*)malloc(sizeof(Node));
// 动态分配失败的处理
if (new_node == NULL) {
printf("Error: malloc failed in head_insertion.\n");
return false;
}
// 新结点要链接在整个单链表的 尾部
new_node->data = new_data;
new_node->next = NULL;
// 链表为空的特殊处理
if (list->head == NULL) {
list->head = list->tail = new_node;
list->size++;
return true;
}
list->tail->next = new_node;
list->tail = new_node;
list->size++;
return true;
}
- 按位操作
// 按位查找
Node* search_by_order(LinkedList* list, int order) {
// 无效位序的处理,有效范围:[1,list->size]
if (order < 1 || order > list->size) {
printf("Error: invalid order in search_by_order.\n");
return NULL;
}
Node* tmp = list->head;
while (order != 1) {
tmp = tmp->next;
order--;
}
return tmp;
}
// 按位插入
// 在位序(1...n)为order的位置插入新结点
bool insert_by_order(LinkedList* list, int order, ElementType new_data) {
// 无效位序的处理,有效范围:[1,list->size+1]
if (order < 1 || order > list->size+1) {
printf("Error: invalid order in insert_by_order.\n");
return false;
}
// 边界位序的处理
if (order == 1) {
return head_insertion(list, new_data);
}
if (order == list->size + 1) {
return tail_insertion(list, new_data);
}
// 常规位序的处理
Node* new_node = (Node*)malloc(sizeof(Node));
// 动态分配失败的处理
if (new_node == NULL) {
printf("Error: malloc failed in insert_by_order.\n");
return false;
}
new_node->data = new_data;
Node* pre = list->head;
while (order != 2) {
pre = pre->next;
order--;
}
new_node->next = pre->next;
pre->next = new_node;
list->size++;
return true;
}
// 按位删除
bool delete_by_order(LinkedList* list, int order) {
// 无效位序的处理,有效范围:[1,list->size]
if (order < 1 || order > list->size) {
printf("Error: invalid order in delete_by_order.\n");
return false;
}
// 边界位序的处理
if (order == 1) {
Node* tmp = list->head;
list->head = tmp->next;
free(tmp);
list->size--;
return true;
}
if (order == list->size) {
Node* pre = list->head;
while (order != 2) {
pre = pre->next;
order--;
}
Node* tmp = pre->next;
pre->next = NULL;
list->tail = pre;
list->size--;
free(tmp);
return true;
}
// 常规位序的处理
Node* pre = list->head;
while (order != 2) {
pre = pre->next;
order--;
}
Node* tmp = pre->next;
pre->next = tmp->next;
free(tmp);
list->size--;
return true;
}
- 按值操作
// 按值查找
Node* search_by_value(LinkedList* list, int value) {
Node* tmp = list->head;
while (tmp) {
if (tmp->data == value) {
return tmp;
}
tmp = tmp->next;
}
printf("Error: node not found in search_by_value.\n");
return NULL;
}
// 按值插入
// 在元素值为value的结点前插入新结点
bool insert_by_value(LinkedList* list, int value, ElementType new_data) {
Node* new_node = (Node*)malloc(sizeof(Node));
// 动态分配失败的处理
if (new_node == NULL) {
printf("Error: malloc failed in insert_by_value.\n");
return false;
}
new_node->data = new_data;
// 边界情况的处理
if (list->head->data == value) {
new_node->next = list->head;
list->head = new_node;
list->size++;
return true;
}
// 常规情况的处理
Node* pre = list->head;
while (pre->next) {
if (pre->next->data == value) {
new_node->next = pre->next;
pre->next = new_node;
list->size++;
return true;
}
pre = pre->next;
}
printf("Error: node not found in insert_by_value.\n");
return false;
}
// 按值删除
bool delete_by_value(LinkedList* list, int value) {
// 边界情况的处理
if (list->head->data == value) {
Node* tmp = list->head;
list->head = tmp->next;
free(tmp);
list->size--;
return true;
}
// 常规位序的处理
Node* pre = list->head;
Node *tmp = pre->next;
while (tmp) {
if (tmp->data == value) {
pre->next = tmp->next;
free(tmp);
list->size--;
return true;
}
pre = tmp;
tmp = tmp->next;
}
printf("Error: node not found in delete_by_value.\n");
return false;
}
- 打印链表
void printf_linked_list(LinkedList* list) {
Node* curr = list->head;
for (int i = 0; i < list->size; i++) {
printf("%d", curr->data);
if (i < list->size-1) {
printf("->");
}
curr = curr->next;
}
printf("\n");
}
- 销毁链表
void destroy_linked_list(LinkedList* list) {
Node* curr = list->head;
while (curr != NULL) {
Node* tmp = curr->next;
free(curr);
curr = tmp;
}
free(list);
}
整合:
linked_list.h
#ifndef LINKED_LIST_H
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int ElementType;
typedef struct node {
ElementType data;
struct node* next;
} Node;
typedef struct {
Node* head;
Node* tail;
int size;
} LinkedList;
// 1 建表
LinkedList* create_linked_list();
// 2 头插法
bool head_insertion(LinkedList* list, ElementType new_data);
// 3 尾插法
bool tail_insertion(LinkedList* list, ElementType new_data);
// 4 按位查找 / 插入 / 删除
Node* search_by_order(LinkedList* list, int order);
bool insert_by_order(LinkedList* list, int order, ElementType new_data);
bool delete_by_order(LinkedList* list, int order);
// 5 按值查找 / 插入 / 删除
Node* search_by_value(LinkedList* list, int value);
bool insert_by_value(LinkedList* list, int value, ElementType new_data);
bool delete_by_value(LinkedList* list, int value);
// 6 打印链表元素
void printf_linked_list(LinkedList* list);
// 7 销毁链表
void destroy_linked_list(LinkedList* list);
#endif // !LINKED_LIST_H
linked_list.c
#include "linked_list.h"
// 1 建表
LinkedList* create_linked_list() {
return calloc(1, sizeof(LinkedList)); // calloc()自动初始化写入0
}
// 2 头插法
bool head_insertion(LinkedList* list, ElementType new_data) {
Node* new_node = (Node*)malloc(sizeof(Node));
// 动态分配失败的处理
if (new_node == NULL) {
printf("Error: malloc failed in head_insertion.\n");
return false;
}
// 新结点要链接在整个单链表的 头部
new_node->data = new_data;
new_node->next = list->head;
list->head = new_node;
list->size++;
if (list->tail == NULL) {
list->tail = new_node;
}
return true;
}
// 3 尾插法
bool tail_insertion(LinkedList* list, ElementType new_data) {
Node* new_node = (Node*)malloc(sizeof(Node));
// 动态分配失败的处理
if (new_node == NULL) {
printf("Error: malloc failed in head_insertion.\n");
return false;
}
// 新结点要链接在整个单链表的 尾部
new_node->data = new_data;
new_node->next = NULL;
// 链表为空的特殊处理
if (list->head == NULL) {
list->head = list->tail = new_node;
list->size++;
return true;
}
list->tail->next = new_node;
list->tail = new_node;
list->size++;
return true;
}
// 4 按位查找 / 插入 / 删除
// 按位查找
Node* search_by_order(LinkedList* list, int order) {
// 无效位序的处理,有效范围:[1,list->size]
if (order < 1 || order > list->size) {
printf("Error: invalid order in search_by_order.\n");
return NULL;
}
Node* tmp = list->head;
while (order != 1) {
tmp = tmp->next;
order--;
}
return tmp;
}
// 按位插入
// 在位序(1...n)为order的位置插入新结点
bool insert_by_order(LinkedList* list, int order, ElementType new_data) {
// 无效位序的处理,有效范围:[1,list->size+1]
if (order < 1 || order > list->size+1) {
printf("Error: invalid order in insert_by_order.\n");
return false;
}
// 边界位序的处理
if (order == 1) {
return head_insertion(list, new_data);
}
if (order == list->size + 1) {
return tail_insertion(list, new_data);
}
// 常规位序的处理
Node* new_node = (Node*)malloc(sizeof(Node));
// 动态分配失败的处理
if (new_node == NULL) {
printf("Error: malloc failed in insert_by_order.\n");
return false;
}
new_node->data = new_data;
Node* pre = list->head;
while (order != 2) {
pre = pre->next;
order--;
}
new_node->next = pre->next;
pre->next = new_node;
list->size++;
return true;
}
// 按位删除
bool delete_by_order(LinkedList* list, int order) {
// 无效位序的处理,有效范围:[1,list->size]
if (order < 1 || order > list->size) {
printf("Error: invalid order in delete_by_order.\n");
return false;
}
// 边界位序的处理
if (order == 1) {
Node* tmp = list->head;
list->head = tmp->next;
free(tmp);
list->size--;
return true;
}
if (order == list->size) {
Node* pre = list->head;
while (order != 2) {
pre = pre->next;
order--;
}
Node* tmp = pre->next;
pre->next = NULL;
list->tail = pre;
list->size--;
free(tmp);
return true;
}
// 常规位序的处理
Node* pre = list->head;
while (order != 2) {
pre = pre->next;
order--;
}
Node* tmp = pre->next;
pre->next = tmp->next;
free(tmp);
list->size--;
return true;
}
// 5 按值查找 / 插入 / 删除
// 按值查找
Node* search_by_value(LinkedList* list, int value) {
Node* tmp = list->head;
while (tmp) {
if (tmp->data == value) {
return tmp;
}
tmp = tmp->next;
}
printf("Error: node not found in search_by_value.\n");
return NULL;
}
// 按值插入
// 在元素值为value的结点前插入新结点
bool insert_by_value(LinkedList* list, int value, ElementType new_data) {
Node* new_node = (Node*)malloc(sizeof(Node));
// 动态分配失败的处理
if (new_node == NULL) {
printf("Error: malloc failed in insert_by_value.\n");
return false;
}
new_node->data = new_data;
// 边界情况的处理
if (list->head->data == value) {
new_node->next = list->head;
list->head = new_node;
list->size++;
return true;
}
// 常规情况的处理
Node* pre = list->head;
while (pre->next) {
if (pre->next->data == value) {
new_node->next = pre->next;
pre->next = new_node;
list->size++;
return true;
}
pre = pre->next;
}
printf("Error: node not found in insert_by_value.\n");
return false;
}
// 按值删除
bool delete_by_value(LinkedList* list, int value) {
// 边界情况的处理
if (list->head->data == value) {
Node* tmp = list->head;
list->head = tmp->next;
free(tmp);
list->size--;
return true;
}
// 常规位序的处理
Node* pre = list->head;
Node *tmp = pre->next;
while (tmp) {
if (tmp->data == value) {
pre->next = tmp->next;
free(tmp);
list->size--;
return true;
}
pre = tmp;
tmp = tmp->next;
}
printf("Error: node not found in delete_by_value.\n");
return false;
}
// 6 打印链表所有元素
void printf_linked_list(LinkedList* list) {
Node* curr = list->head;
for (int i = 0; i < list->size; i++) {
printf("%d", curr->data);
if (i < list->size-1) {
printf("->");
}
curr = curr->next;
}
printf("\n");
}
// 7 销毁链表
void destroy_linked_list(LinkedList* list) {
Node* curr = list->head;
while (curr != NULL) {
Node* tmp = curr->next;
free(curr);
curr = tmp;
}
free(list);
}
测试代码main.c
#include "linked_list.h"
/*
不带头结点的单链表的功能实现:
1 建表
2 头插法
3 尾插法
4 按位查找/插入/删除
5 按值查找/插入/删除
6 打印链表元素
7 销毁链表
*/
int main(void) {
LinkedList *list = create_linked_list();
// 头插法测试
for (int i = 0; i < 10; i++) {
head_insertion(list, i + 1);
}
printf_linked_list(list);
// 尾插法测试
tail_insertion(list, 99);
printf_linked_list(list);
// 得到位序为5的结点
Node* tmp = search_by_order(list, 5);
printf("search_by_order(list, 5):%d\n", tmp->data);
// 在位序5上插入新结点,值为233
insert_by_order(list, 5, 233);
printf_linked_list(list);
// 删除位序为5的结点
delete_by_order(list, 5);
printf_linked_list(list);
// 得到值为99的结点
tmp = search_by_value(list, 99);
printf("search_by_order(list, 99):%d\n", tmp->data);
// 在元素值为5的结点前插入新结点,值为233
insert_by_value(list, 5, 233);
printf_linked_list(list);
// 删除值为233的结点
delete_by_value(list, 233);
printf_linked_list(list);
// 试图删除不存在的结点的情况测试
delete_by_value(list, 233);
destroy_linked_list(list);
return 0;
}