当前位置:   article > 正文

数据结构之链表(算法之初)

数据结构之链表(算法之初)

首先脑补一下单向链表和双向链表:

  • 单向链表(C语言实现):

    • 每个节点只包含一个指向下一个节点的指针。
    • 只能从头节点开始向后遍历,无法回溯。
  • 双向链表(C++实现):

    • 每个节点包含两个指针,一个指向下一个节点,一个指向前一个节点。
    • 可以在链表中前后遍历,增加了操作的灵活性。

下面是具体的代码展示,通过代码展示如何操作,我就不写注释了,完全的代码附上,都用了标准的变量和函数命名,方便大家阅读,希望大家能在标准代码中更深入的理解链表难关,大家也可以把代码复制下来在自己电脑运行和修改,最后,祝大家能够有所收获!

C语言实现单向链表

1. 定义节点结构和链表初始化
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct ListNode {
  4. int value;
  5. struct ListNode *next;
  6. } ListNode;
  7. ListNode* create_node(int value) {
  8. ListNode *new_node = (ListNode*)malloc(sizeof(ListNode));
  9. new_node->value = value;
  10. new_node->next = NULL;
  11. return new_node;
  12. }
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. typedef struct ListNode {
  16. int value;
  17. struct ListNode *next;
  18. } ListNode;
  19. ListNode* create_node(int value) {
  20. ListNode *new_node = (ListNode*)malloc(sizeof(ListNode));
  21. new_node->value = value;
  22. new_node->next = NULL;
  23. return new_node;
  24. }
2. 插入节点(头插法和尾插法)
  1. void insert_at_beginning(ListNode **head, int value) {
  2. ListNode *new_node = create_node(value);
  3. new_node->next = *head;
  4. *head = new_node;
  5. }
  6. void insert_at_end(ListNode **head, int value) {
  7. ListNode *new_node = create_node(value);
  8. if (*head == NULL) {
  9. *head = new_node;
  10. return;
  11. }
  12. ListNode *temp = *head;
  13. while (temp->next != NULL) {
  14. temp = temp->next;
  15. }
  16. temp->next = new_node;
  17. }
3. 删除节点和查找节点
  1. void delete_node(ListNode **head, int key) {
  2. ListNode *temp = *head, *prev = NULL;
  3. if (temp != NULL && temp->value == key) {
  4. *head = temp->next;
  5. free(temp);
  6. return;
  7. }
  8. while (temp != NULL && temp->value != key) {
  9. prev = temp;
  10. temp = temp->next;
  11. }
  12. if (temp == NULL) return;
  13. prev->next = temp->next;
  14. free(temp);
  15. }
  16. int search(ListNode *head, int key) {
  17. ListNode *current = head;
  18. while (current != NULL) {
  19. if (current->value == key) return 1;
  20. current = current->next;
  21. }
  22. return 0;
  23. }
4. 打印链表
  1. void print_list(ListNode *head) {
  2. ListNode *current = head;
  3. while (current != NULL) {
  4. printf("%d -> ", current->value);
  5. current = current->next;
  6. }
  7. printf("NULL\n");
  8. }
  9. int main() {
  10. ListNode *head = NULL;
  11. insert_at_beginning(&head, 3);
  12. insert_at_beginning(&head, 2);
  13. insert_at_end(&head, 4);
  14. print_list(head); // 输出: 2 -> 3 -> 4 -> NULL
  15. printf("%d\n", search(head, 3)); // 输出: 1
  16. delete_node(&head, 3);
  17. print_list(head); // 输出: 2 -> 4 -> NULL
  18. return 0;
  19. }

C++实现双向链表

1. 定义节点结构和链表初始化
  1. #include <iostream>
  2. struct DoubleListNode {
  3. int value;
  4. DoubleListNode* next;
  5. DoubleListNode* prev;
  6. DoubleListNode(int val) : value(val), next(nullptr), prev(nullptr) {}
  7. };
  8. class DoublyLinkedList {
  9. public:
  10. DoublyLinkedList() : head(nullptr) {}
  11. void insert_at_beginning(int value);
  12. void insert_at_end(int value);
  13. void delete_node(int key);
  14. bool search(int key);
  15. void print_list();
  16. private:
  17. DoubleListNode* head;
  18. };
2. 插入节点(头插法和尾插法) 
  1. void DoublyLinkedList::insert_at_beginning(int value) {
  2. DoubleListNode* new_node = new DoubleListNode(value);
  3. new_node->next = head;
  4. if (head != nullptr) head->prev = new_node;
  5. head = new_node;
  6. }
  7. void DoublyLinkedList::insert_at_end(int value) {
  8. DoubleListNode* new_node = new DoubleListNode(value);
  9. if (head == nullptr) {
  10. head = new_node;
  11. return;
  12. }
  13. DoubleListNode* temp = head;
  14. while (temp->next != nullptr) temp = temp->next;
  15. temp->next = new_node;
  16. new_node->prev = temp;
  17. }
3. 删除节点和查找节点
  1. void DoublyLinkedList::delete_node(int key) {
  2. DoubleListNode* temp = head;
  3. while (temp != nullptr && temp->value != key) {
  4. temp = temp->next;
  5. }
  6. if (temp == nullptr) return;
  7. if (temp->prev != nullptr) temp->prev->next = temp->next;
  8. else head = temp->next;
  9. if (temp->next != nullptr) temp->next->prev = temp->prev;
  10. delete temp;
  11. }
  12. bool DoublyLinkedList::search(int key) {
  13. DoubleListNode* current = head;
  14. while (current != nullptr) {
  15. if (current->value == key) return true;
  16. current = current->next;
  17. }
  18. return false;
  19. }
4. 打印链表
  1. void DoublyLinkedList::print_list() {
  2. DoubleListNode* current = head;
  3. while (current != nullptr) {
  4. std::cout << current->value << " <-> ";
  5. current = current->next;
  6. }
  7. std::cout << "NULL" << std::endl;
  8. }
  9. int main() {
  10. DoublyLinkedList dll;
  11. dll.insert_at_beginning(3);
  12. dll.insert_at_beginning(2);
  13. dll.insert_at_end(4);
  14. dll.print_list(); // 输出: 2 <-> 3 <-> 4 <-> NULL
  15. std::cout << dll.search(3) << std::endl; // 输出: 1
  16. dll.delete_node(3);
  17. dll.print_list(); // 输出: 2 <-> 4 <-> NULL
  18. return 0;
  19. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/669809
推荐阅读
相关标签
  

闽ICP备14008679号