提示信息

C++ 中 List 的介绍与常用操作的模拟实现

韦兰 已关注

在 C++ 中,std::list 是一个双向链表(doubly linked list)实现的容器,它属于标准模板库(STL)的一部分。与 std::vector 不同,std::list 允许在常数时间内插入或删除元素(只要位置是已知的),而代价是它不支持随机访问。下面是 C++ 中 std::list 的一些常用操作和其模拟实现。

std::list 的基本特性

  • 双向链表:每个节点都知道其前一个和后一个节点。
  • 动态大小:能够高效地插入和删除元素。
  • 双向迭代:支持正向和反向遍历。

常用操作

下面是一些 std::list 的常用操作:

  1. 定义和初始化

    1. std::list<int> myList; // 空链表
    2. std::list<int> myList(5, 10); // 长度为5,所有元素都是10
    3. std::list<int> anotherList = {1, 2, 3}; // 使用初始化列表
  2. 插入元素

    1. myList.push_back(20); // 在末尾插入
    2. myList.push_front(5); // 在头部插入
    3. // 在某个位置插入
    4. auto it = myList.begin();
    5. std::advance(it, 2); // 移动迭代器到第三个元素
    6. myList.insert(it, 15); // 在此位置插入15
  3. 删除元素

    1. myList.pop_back(); // 删除末尾元素
    2. myList.pop_front(); // 删除头部元素
    3. // 删除特定位置的元素
    4. auto it = myList.begin();
    5. std::advance(it, 1); // 移动到第二个元素
    6. myList.erase(it);
    7. // 删除特定值的所有元素
    8. myList.remove(10);
  4. 访问元素

    1. int first = myList.front(); // 访问第一个元素
    2. int last = myList.back(); // 访问最后一个元素
  5. 遍历

    1. for (int value : myList) {
    2. std::cout << value << " ";
    3. }
    4. // 使用迭代器
    5. for (auto it = myList.begin(); it != myList.end(); ++it) {
    6. std::cout << *it << " ";
    7. }
  6. 其它操作

    1. myList.clear(); // 清空列表
    2. myList.reverse(); // 反转列表
    3. myList.sort(); // 排序列表
    4. myList.unique(); // 去除连续相等的元素
    5. myList.merge(anotherList); // 合并另一个已排序的列表

模拟实现的简单示例

虽然 C++ 提供了标准的 std::list 实现,但你可以从头模拟一个简单的链表来更好地理解它的工作原理。

  1. #include <iostream>
  2. // 节点结构
  3. struct Node {
  4. int data;
  5. Node* next;
  6. Node* prev;
  7. Node(int value) : data(value), next(nullptr), prev(nullptr) {}
  8. };
  9. // 双向链表类
  10. class DoublyLinkedList {
  11. public:
  12. DoublyLinkedList() : head(nullptr), tail(nullptr) {}
  13. // 添加到尾部
  14. void push_back(int value) {
  15. Node* newNode = new Node(value);
  16. if (!tail) {
  17. head = tail = newNode;
  18. } else {
  19. tail->next = newNode;
  20. newNode->prev = tail;
  21. tail = newNode;
  22. }
  23. }
  24. // 打印链表
  25. void print() {
  26. Node* current = head;
  27. while (current) {
  28. std::cout << current->data << " ";
  29. current = current->next;
  30. }
  31. std::cout << std::endl;
  32. }
  33. // 删除链表中的所有节点
  34. void clear() {
  35. Node* current = head;
  36. while (current) {
  37. Node* next = current->next;
  38. delete current;
  39. current = next;
  40. }
  41. head = tail = nullptr;
  42. }
  43. ~DoublyLinkedList() {
  44. clear();
  45. }
  46. private:
  47. Node* head;
  48. Node* tail;
  49. };
  50. int main() {
  51. DoublyLinkedList list;
  52. list.push_back(1);
  53. list.push_back(2);
  54. list.push_back(3);
  55. std::cout << "List contents: ";
  56. list.print();
  57. list.clear();
  58. return 0;
  59. }

这段代码展示了一个简单的双向链表的数据结构及其基本操作。通过这种方式,你可以深入理解 std::list 在底层是如何运作的。

    遇到难题? "AI大模型GPT4.0、GPT" 是你的私人解答专家! 点击按钮去提问......
韦兰 关注 已关注

最近一次登录:2024-11-20 23:09:01   

暂时还没有签名,请关注我或评论我的文章
×
免费图表工具,画流程图、架构图