链表
单链表
线性表:
- 有限的序列
- 序列中的每个元素都有唯一的前驱和后继,除了开头和结尾两个节点
线性表的两种实现:
顺序表:分配一块 连续的内存 去存放这些元素,例如编程语言中的数组
链表: 内存是不连续 的,元素会各自被分配一块内存,内存和内存之间用指针进行相连
单链表操作:
-
增加
- 头插法
- 尾插法
-
删除:只需要找到对应节点,将对应节点的前一个节点指向这个结点的后继,只操作1个指针
头节点
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
//数据域
int data;
//指针域
struct Node* next;
}Node;
//初始化链表头节点
Node* initList()
{
Node* list = (Node*)malloc(sizeof(Node));
// 检查内存分配是否成功
if (list == NULL)
{
printf("内存分配失败\n");
return NULL;
}
list -> data = 0;
list -> next = NULL;
return list;
}
//头插法
void headInsert(Node* list,int data)
{
//创建一个新的节点并分配内存
Node* node = (Node*)malloc(sizeof(Node));
// 检查内存分配是否成功
if (node == NULL)
{
printf("内存分配失败\n");
return;
}
//设置新节点的数据字段
node -> data = data;
//将新节点的 next 字段指向当前链表的第一个节点
node -> next = list -> next;
//将链表的头节点的 next 字段指向新节点
list -> next = node;
//增加链表头节点的数据字段以表示链表长度
list -> data ++;
}
//尾插法
void tailInsert(Node* list,int data)
{
Node* head = list;
//创建一个新的节点并分配内存
Node* node = (Node*)malloc(sizeof(Node));
// 检查内存分配是否成功
if (node == NULL)
{
printf("内存分配失败\n");
return;
}
//设置新节点的数据字段
node -> data = data;
node -> next = NULL;
//遍历链表找到最后一个节点
while (list->next != NULL)
{
list = list->next;
}
//将最后一个节点的 next 字段指向新节点
list -> next = node;
//增加链表头节点的数据字段以表示链表长度
head -> data ++;
}
//删除
void delete(Node* list, int data)
{
//初始化两个指针pre和current,分别指向链表的头节点和第一个节点
Node* pre = list;
Node* current = list -> next;
//遍历链表,查找数据字段等于data的节点
while(current)
{
//如果找到匹配的节点,将前一个节点的next字段指向当前节点的next字段
if(current -> data == data)
{
pre -> next = current -> next;
//释放当前节点的内存
free(current);
//更新链表长度
list->data--;
break;
}
pre = current;
current = current -> next;
}
}
//打印链表
void printList(Node* list)
{
//将指针移动到链表的第一个节点(跳过头节点)
list = list -> next;
//遍历链表,直到list为NULL
while(list)
{
printf("%d",list -> data);
list = list -> next;
}
}
int main()
{
Node* list = initList();
headInsert(list,1);
headInsert(list,2);
headInsert(list,3);
headInsert(list,4);
headInsert(list,5);
tailInsert(list,6);
printList(list);
return 0;
}
单循环链表
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
//数据域
int data;
//指针域
struct Node* next;
}Node;
//初始化单循环链表
Node* initList()
{
Node* L = (Node*)malloc(sizeof(Node));
if (L == NULL)
{
printf("内存分配失败\n");
return NULL;
}
L -> data = 0;
L -> next = L; //指向自身,形成循环
return L;
}
//头插法
void headInsert(Node* L,int data)
{
Node* node = (Node*)malloc(sizeof(Node));
if (node == NULL)
{
printf("内存分配失败\n");
return;
}
node -> data = data;
node -> next = L -> next;
L -> next = node;
L->data++;
}
// 尾插法
void tailInsert(Node* L, int data)
{
Node* head = L;
Node* node = (Node*)malloc(sizeof(Node));
if (node == NULL)
{
printf("内存分配失败\n");
return;
}
node->data = data;
node->next = head; //新节点指向头节点,形成循环
while (L->next != head)
{
L = L->next;
}
L->next = node;
head->data++;
}
// 删除节点
void delete(Node* L, int data)
{
Node* pre = L;
Node* current = L->next;
while (current != L)
{
if (current->data == data)
{
pre->next = current->next;
free(current);
L->data--;
break;
}
pre = current;
current = current->next;
}
}
// 打印链表
void printList(Node* L)
{
Node* start = L->next;
while (start != L)
{
printf("%d ", start->data);
start = start->next;
}
printf("\n");
}
int main()
{
Node* list = initList();
headInsert(list, 1);
headInsert(list, 2);
tailInsert(list, 3);
tailInsert(list, 4);
printList(list);
delete(list, 3);
printList(list);
return 0;
}
双链表
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node* next;
struct Node* prev;
} Node;
// 初始化双链表
Node* initList()
{
Node* L = (Node*)malloc(sizeof(Node));
if (L == NULL)
{
printf("内存分配失败\n");
return NULL;
}
L->data = 0;
L->next = NULL;
L->prev = NULL;
return L;
}
// 头插法
void headInsert(Node* L, int data)
{
// 分配内存并检查是否成功
Node* node = (Node*)malloc(sizeof(Node));
if (node == NULL)
{
printf("内存分配失败\n");
return;
}
// 设置新节点的数据字段
node->data = data;
// 设置新节点的 next 和 prev 字段
node->next = L->next;
node->prev = L;
// 更新原链表第一个节点的 prev 字段
if (L->next != NULL)
{
L->next->prev = node;
}
// 将头节点的 next 字段指向新节点
L->next = node;
// 更新链表长度
L->data++;
}
// 尾插法
void tailInsert(Node* L, int data)
{
// 保存头节点的指针
Node* head = L;
// 分配内存并检查是否成功
Node* node = (Node*)malloc(sizeof(Node));
if (node == NULL)
{
printf("内存分配失败\n");
return;
}
// 设置新节点的数据字段
node->data = data;
// 设置新节点的 next 字段为 NULL
node->next = NULL;
// 遍历链表,找到最后一个节点
while (L->next != NULL)
{
L = L->next;
}
// 将最后一个节点的 next 字段指向新节点
L->next = node;
// 设置新节点的 prev 字段指向最后一个节点
node->prev = L;
// 更新链表长度
head->data++;
}
// 删除节点
void delete(Node* L, int data)
{
// 初始化指针 current,指向链表的第一个节点
Node* current = L->next;
// 遍历链表,查找数据字段等于 data 的节点
while (current != NULL)
{
// 如果找到匹配的节点
if (current->data == data)
{
// 如果当前节点不是最后一个节点,更新下一个节点的 prev 字段
if (current->next != NULL)
{
current->next->prev = current->prev;
}
// 更新前一个节点的 next 字段
current->prev->next = current->next;
// 释放当前节点的内存
free(current);
// 更新链表长度
L->data--;
// 退出循环
break;
}
// 移动到下一个节点
current = current->next;
}
}
// 打印链表
void printList(Node* L)
{
Node* start = L->next;
while (start != NULL)
{
printf("%d ", start->data);
start = start->next;
}
printf("\n");
}
int main()
{
Node* list = initList();
headInsert(list, 1);
headInsert(list, 2);
tailInsert(list, 3);
tailInsert(list, 4);
printList(list);
delete(list, 3);
printList(list);
return 0;
}
双循环链表
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node* next;
struct Node* prev;
} Node;
// 初始化双循环链表
Node* initList()
{
// 分配内存并检查是否成功
Node* L = (Node*)malloc(sizeof(Node));
if (L == NULL)
{
printf("内存分配失败\n");
return NULL;
}
// 初始化头节点
L->data = 0;
L->next = L; // 指向自身,形成循环
L->prev = L; // 指向自身,形成循环
return L;
}
// 头插法
void headInsert(Node* L, int data)
{
// 分配内存并检查是否成功
Node* node = (Node*)malloc(sizeof(Node));
if (node == NULL)
{
printf("内存分配失败\n");
return;
}
// 设置新节点的数据字段
node->data = data;
// 设置新节点的 next 和 prev 字段
node->next = L->next;
node->prev = L;
// 更新原链表第一个节点的 prev 字段
L->next->prev = node;
// 将头节点的 next 字段指向新节点
L->next = node;
// 更新链表长度
L->data++;
}
// 尾插法
void tailInsert(Node* L, int data)
{
// 分配内存并检查是否成功
Node* node = (Node*)malloc(sizeof(Node));
if (node == NULL)
{
printf("内存分配失败\n");
return;
}
// 设置新节点的数据字段
node->data = data;
// 设置新节点的 next 和 prev 字段
node->next = L;
node->prev = L->prev;
// 更新原链表最后一个节点的 next 字段
L->prev->next = node;
// 将头节点的 prev 字段指向新节点
L->prev = node;
// 更新链表长度
L->data++;
}
// 删除节点
void delete(Node* L, int data)
{
// 初始化指针 current,指向链表的第一个节点
Node* current = L->next;
// 遍历链表,查找数据字段等于 data 的节点
while (current != L)
{
// 如果找到匹配的节点
if (current->data == data)
{
// 更新前一个节点的 next 字段
current->prev->next = current->next;
// 更新下一个节点的 prev 字段
current->next->prev = current->prev;
// 释放当前节点的内存
free(current);
// 更新链表长度
L->data--;
// 退出循环
break;
}
// 移动到下一个节点
current = current->next;
}
}
// 打印链表
void printList(Node* L)
{
// 初始化指针 start,指向链表的第一个节点
Node* start = L->next;
// 遍历链表,直到回到头节点
while (start != L)
{
// 打印当前节点的数据字段
printf("%d ", start->data);
// 移动到下一个节点
start = start->next;
}
// 打印换行符
printf("\n");
}
int main()
{
Node* list = initList();
headInsert(list, 1);
headInsert(list, 2);
tailInsert(list, 3);
tailInsert(list, 4);
delete(list, 3);
printList(list);
return 0;
}