第二章-双链表

双链表(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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

需要什么私信我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值