您好,登录后才能下订单哦!
双向链表(Doubly Linked List)是一种常见的数据结构,它由一系列节点组成,每个节点包含两个指针:一个指向前一个节点(prev
),另一个指向下一个节点(next
)。与单向链表不同,双向链表可以从任意一个节点向前或向后遍历整个链表。
双向链表的主要特点包括: - 双向遍历:可以从头到尾或从尾到头遍历链表。 - 插入和删除操作高效:由于每个节点都保存了前驱和后继节点的信息,插入和删除操作的时间复杂度为 O(1)。 - 占用更多内存:每个节点需要额外的指针来存储前驱节点的地址,因此比单向链表占用更多的内存。
在C语言中,双向链表的节点通常定义为一个结构体,包含数据域和两个指针域:
struct Node {
int data; // 数据域
struct Node* prev; // 指向前一个节点的指针
struct Node* next; // 指向下一个节点的指针
};
首先,我们需要一个函数来创建新的节点:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void insertAtHead(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
}
void insertAtTail(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}
void insertAtPosition(struct Node** head, int data, int position) {
if (position < 1) {
printf("Invalid position\n");
return;
}
if (position == 1) {
insertAtHead(head, data);
return;
}
struct Node* newNode = createNode(data);
struct Node* temp = *head;
for (int i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
return;
}
newNode->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = newNode;
}
temp->next = newNode;
newNode->prev = temp;
}
void deleteAtHead(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
}
void deleteAtTail(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
if (temp->prev != NULL) {
temp->prev->next = NULL;
} else {
*head = NULL;
}
free(temp);
}
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL || position < 1) {
printf("List is empty or invalid position\n");
return;
}
struct Node* temp = *head;
if (position == 1) {
deleteAtHead(head);
return;
}
for (int i = 1; i < position && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
return;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
free(temp);
}
void printListForward(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
void printListBackward(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty\n");
return;
}
while (temp->next != NULL) {
temp = temp->next;
}
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->prev;
}
printf("NULL\n");
}
为了避免内存泄漏,我们需要在程序结束时释放链表占用的内存:
void freeList(struct Node** head) {
struct Node* temp;
while (*head != NULL) {
temp = *head;
*head = (*head)->next;
free(temp);
}
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void insertAtHead(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
}
void insertAtTail(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}
void insertAtPosition(struct Node** head, int data, int position) {
if (position < 1) {
printf("Invalid position\n");
return;
}
if (position == 1) {
insertAtHead(head, data);
return;
}
struct Node* newNode = createNode(data);
struct Node* temp = *head;
for (int i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
return;
}
newNode->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = newNode;
}
temp->next = newNode;
newNode->prev = temp;
}
void deleteAtHead(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
}
void deleteAtTail(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
if (temp->prev != NULL) {
temp->prev->next = NULL;
} else {
*head = NULL;
}
free(temp);
}
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL || position < 1) {
printf("List is empty or invalid position\n");
return;
}
struct Node* temp = *head;
if (position == 1) {
deleteAtHead(head);
return;
}
for (int i = 1; i < position && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
return;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
free(temp);
}
void printListForward(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
void printListBackward(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty\n");
return;
}
while (temp->next != NULL) {
temp = temp->next;
}
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->prev;
}
printf("NULL\n");
}
void freeList(struct Node** head) {
struct Node* temp;
while (*head != NULL) {
temp = *head;
*head = (*head)->next;
free(temp);
}
}
int main() {
struct Node* head = NULL;
insertAtHead(&head, 10);
insertAtHead(&head, 20);
insertAtTail(&head, 30);
insertAtPosition(&head, 40, 2);
printf("Forward traversal: ");
printListForward(head);
printf("Backward traversal: ");
printListBackward(head);
deleteAtHead(&head);
deleteAtTail(&head);
deleteAtPosition(&head, 2);
printf("Forward traversal after deletion: ");
printListForward(head);
freeList(&head);
return 0;
}
双向链表是一种功能强大的数据结构,它允许我们在链表中进行双向遍历,并且在插入和删除操作时具有较高的效率。通过本文的介绍和示例代码,你应该已经掌握了如何在C语言中实现双向链表的基本操作。希望这篇文章对你理解和使用双向链表有所帮助!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。