双链表(Double Linked List)是一种常见的线性数据结构,它在单链表的基础上,为每个节点增加了一个指向前一个节点的指针。因此,双链表中的每个节点都有两个指针:一个指向前一个节点,另一个指向下一个节点。
双链表的优点:
1. 支持双向访问:由于双链表中的节点具有前驱和后继指针,因此我们可以从头节点开始,沿着链表访问到尾节点,也可以从尾节点开始,沿着链表访问到头节点。这一点在需要双向访问场景中具有优势。
2. 更容易实现插入和删除操作:在双链表中,插入和删除一个节点只需要改变相邻节点之间的指针指向关系。相比单链表,双链表在插入和删除操作时更加灵活和高效。
3. 更适合实现一些高级数据结构:例如循环链表、双向循环链表等。
双链表的缺点:
1. 占用更多内存:由于每个节点都需要存储前驱和后继指针,因此双链表比单链表占用更多的内存空间。
2. 复杂程度稍高:由于双链表有多个指针,因此实现相对复杂,需要对边界条件进行更多考虑。
双链表的应用:
1. 缓存替换算法:双链表可以用于实现LRU(最近最少使用)等缓存替换算法。
2. 解决约瑟夫问题:双链表可以用来解决著名的约瑟夫问题(Josephus problem)。
3. 实现数据结构:双链表还可以用于实现其他数据结构,如循环链表、双向循环链表等。
下面是双链表(Doubly Linked List)的各个基本操作的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
// 初始化双链表
void initList(Node** head) {
*head = NULL;
}
// 判断双链表是否为空
int isEmpty(Node* head) {
return head == NULL;
}
// 在链表头部插入元素
void insertAtHead(Node** head, int element) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->prev = NULL;
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;
}
*head = newNode;
}
// 在链表尾部插入元素
void insertAtTail(Node** head, int element) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
// 在指定位置插入元素
void insertAtPosition(Node** head, int position, int element) {
if (position <= 0) {
printf("Error: Invalid position\n");
return;
}
if (position == 1) {
insertAtHead(head, element);
} else {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
Node* current = *head;
int count = 1;
while (current != NULL && count < position - 1) {
current = current->next;
count++;
}
if (current == NULL) {
printf("Error: Invalid position\n");
return;
}
newNode->prev = current;
newNode->next = current->next;
if (current->next != NULL) {
current->next->prev = newNode;
}
current->next = newNode;
}
}
// 删除链表中第一个出现的指定元素
void deleteElement(Node** head, int element) {
Node* current = *head;
while (current != NULL && current->data != element) {
current = current->next;
}
if (current == NULL) {
printf("Error: Element not found\n");
return;
}
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
*head = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
}
// 获取链表长度
int getLength(Node* head) {
int length = 0;
Node* current = head;
while (current != NULL) {
length++;
current = current->next;
}
return length;
}
// 打印链表中的元素
void printList(Node* head) {
printf("Linked List: ");
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// 销毁链表,释放内存
void destroyList(Node** head) {
Node* current = *head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
*head = NULL;
}
int main() {
Node* head;
initList(&head);
insertAtHead(&head, 10);
insertAtHead(&head, 20);
insertAtTail(&head, 30);
insertAtPosition(&head, 2, 40);
printList(head);
deleteElement(&head, 20);
printList(head);
int length = getLength(head);
printf("Length of the linked list: %d\n", length);
destroyList(&head);
return 0;
}
```
这段代码实现了双链表的基本操作,包括初始化双链表 `initList`,判断双链表是否为空 `isEmpty`,在链表头部插入元素 `insertAtHead`,在链表尾部插入元素 `insertAtTail`,在指定位置插入元素 `insertAtPosition`,删除链表中第一个出现的指定元素 `deleteElement`,获取链表长度 `getLength`,打印链表中的元素 `printList`,以及销毁链表,释放内存 `destroyList`。
在 `main` 函数中,我们演示了如何使用这些基本操作来创建链表、插入元素、删除元素、获取链表长度,并打印链表的内容。
需要注意的是,双链表相比单链表多了一个指向前一个节点的指针 `prev`,在插入和删除操作中需要同时更新前一个节点的 `next` 指针,以保持链表的完整性。此外,这只是一个简单的示例,实际应用中可能需要根据具体需求进行修改和扩展。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
// 初始化双链表
void initList(Node** head) {
*head = NULL;
}
// 判断双链表是否为空
int isEmpty(Node* head) {
return head == NULL;
}
// 在链表头部插入元素
void insertAtHead(Node** head, int element) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->prev = NULL;
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;
}
*head = newNode;
}
// 在链表尾部插入元素
void insertAtTail(Node** head, int element) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
// 在指定位置插入元素
void insertAtPosition(Node** head, int position, int element) {
if (position <= 0) {
printf("Error: Invalid position\n");
return;
}
if (position == 1) {
insertAtHead(head, element);
} else {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
Node* current = *head;
int count = 1;
while (current != NULL && count < position - 1) {
current = current->next;
count++;
}
if (current == NULL) {
printf("Error: Invalid position\n");
return;
}
newNode->prev = current;
newNode->next = current->next;
if (current->next != NULL) {
current->next->prev = newNode;
}
current->next = newNode;
}
}
// 删除链表中第一个出现的指定元素
void deleteElement(Node** head, int element) {
Node* current = *head;
while (current != NULL && current->data != element) {
current = current->next;
}
if (current == NULL) {
printf("Error: Element not found\n");
return;
}
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
*head = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
}
// 获取链表长度
int getLength(Node* head) {
int length = 0;
Node* current = head;
while (current != NULL) {
length++;
current = current->next;
}
return length;
}
// 打印链表中的元素
void printList(Node* head) {
printf("Linked List: ");
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// 销毁链表,释放内存
void destroyList(Node** head) {
Node* current = *head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
*head = NULL;
}
int main() {
Node* head;
initList(&head);
insertAtHead(&head, 10);
insertAtHead(&head, 20);
insertAtTail(&head, 30);
insertAtPosition(&head, 2, 40);
printList(head);
deleteElement(&head, 20);
printList(head);
int length = getLength(head);
printf("Length of the linked list: %d\n", length);
destroyList(&head);
return 0;
}