C++实现的数据结构示例,涵盖链表、数组、树和图

[复制链接]
发表于 2025-10-12 23:26:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
利用C++实现的数据结构示例,涵盖链表、数组、树和图的根本操纵:

  • 链表(单向链表)
  1. #include <iostream>
  2. using namespace std;
  3. struct Node {
  4.     int data;
  5.     Node* next;
  6.     Node(int val) : data(val), next(nullptr) {}
  7. };
  8. class LinkedList {
  9. private:
  10.     Node* head;
  11. public:
  12.     LinkedList() : head(nullptr) {}
  13.     void insert(int val) {
  14.         Node* newNode = new Node(val);
  15.         if (!head) {
  16.             head = newNode;
  17.             return;
  18.         }
  19.         Node* temp = head;
  20.         while (temp->next) {
  21.             temp = temp->next;
  22.         }
  23.         temp->next = newNode;
  24.     }
  25.     void remove(int val) {
  26.         if (!head) return;
  27.         if (head->data == val) {
  28.             Node* temp = head;
  29.             head = head->next;
  30.             delete temp;
  31.             return;
  32.         }
  33.         Node* curr = head;
  34.         while (curr->next && curr->next->data != val) {
  35.             curr = curr->next;
  36.         }
  37.         if (curr->next) {
  38.             Node* temp = curr->next;
  39.             curr->next = curr->next->next;
  40.             delete temp;
  41.         }
  42.     }
  43.     void print() {
  44.         Node* temp = head;
  45.         while (temp) {
  46.             cout << temp->data << " ";
  47.             temp = temp->next;
  48.         }
  49.         cout << endl;
  50.     }
  51. };
复制代码

  • 动态数组
  1. class DynamicArray {
  2. private:
  3.     int* arr;
  4.     int capacity;
  5.     int size;
  6.    
  7.     void resize() {
  8.         capacity *= 2;
  9.         int* newArr = new int[capacity];
  10.         for (int i = 0; i < size; i++) {
  11.             newArr[i] = arr[i];
  12.         }
  13.         delete[] arr;
  14.         arr = newArr;
  15.     }
  16. public:
  17.     DynamicArray() : capacity(2), size(0) {
  18.         arr = new int[capacity];
  19.     }
  20.     void push_back(int val) {
  21.         if (size == capacity) resize();
  22.         arr[size++] = val;
  23.     }
  24.     void pop_back() {
  25.         if (size > 0) size--;
  26.     }
  27.     int& operator[](int index) {
  28.         return arr[index];
  29.     }
  30.     int getSize() {
  31.         return size;
  32.     }
  33. };
复制代码

  • 二叉树(二叉搜索树)
  1. class TreeNode {
  2. public:
  3.     int val;
  4.     TreeNode* left;
  5.     TreeNode* right;
  6.     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  7. };
  8. class BinarySearchTree {
  9. private:
  10.     TreeNode* root;
  11.     TreeNode* insertNode(TreeNode* node, int val) {
  12.         if (!node) return new TreeNode(val);
  13.         if (val < node->val) {
  14.             node->left = insertNode(node->left, val);
  15.         } else {
  16.             node->right = insertNode(node->right, val);
  17.         }
  18.         return node;
  19.     }
  20.     void inorder(TreeNode* node) {
  21.         if (!node) return;
  22.         inorder(node->left);
  23.         cout << node->val << " ";
  24.         inorder(node->right);
  25.     }
  26. public:
  27.     BinarySearchTree() : root(nullptr) {}
  28.     void insert(int val) {
  29.         root = insertNode(root, val);
  30.     }
  31.     void printInorder() {
  32.         inorder(root);
  33.         cout << endl;
  34.     }
  35. };
复制代码

  • 图(毗邻矩阵实现)
  1. #include <vector>
  2. class Graph {
  3. private:
  4.     vector<vector<bool>> adjMatrix;
  5.     int numVertices;
  6.     void DFSutil(int v, vector<bool>& visited) {
  7.         visited[v] = true;
  8.         cout << v << " ";
  9.         for (int i = 0; i < numVertices; i++) {
  10.             if (adjMatrix[v][i] && !visited[i]) {
  11.                 DFSutil(i, visited);
  12.             }
  13.         }
  14.     }
  15. public:
  16.     Graph(int n) : numVertices(n) {
  17.         adjMatrix.resize(n, vector<bool>(n, false));
  18.     }
  19.     void addEdge(int i, int j) {
  20.         adjMatrix[i][j] = true;
  21.         adjMatrix[j][i] = true;
  22.     }
  23.     void DFS(int start) {
  24.         vector<bool> visited(numVertices, false);
  25.         DFSutil(start, visited);
  26.         cout << endl;
  27.     }
  28. };
复制代码
利用示例:
  1. int main() {
  2.     // 链表测试
  3.     LinkedList list;
  4.     list.insert(1);
  5.     list.insert(2);
  6.     list.insert(3);
  7.     list.remove(2);
  8.     list.print();  // 输出: 1 3
  9.     // 动态数组测试
  10.     DynamicArray arr;
  11.     arr.push_back(10);
  12.     arr.push_back(20);
  13.     cout << arr[1] << endl;  // 输出: 20
  14.     // 二叉搜索树测试
  15.     BinarySearchTree bst;
  16.     bst.insert(5);
  17.     bst.insert(3);
  18.     bst.insert(7);
  19.     bst.printInorder();  // 输出: 3 5 7
  20.     // 图测试
  21.     Graph g(4);
  22.     g.addEdge(0, 1);
  23.     g.addEdge(0, 2);
  24.     g.addEdge(1, 3);
  25.     g.DFS(0);  // 输出: 0 1 3 2
  26.     return 0;
  27. }
复制代码
这些实现展示了数据结构的根本操纵:

  • 链表的插入、删除和遍历
  • 动态数组的主动扩容机制
  • 二叉搜索树的递归插入和中序遍历
  • 图的毗邻矩阵存储和深度优先搜索
每个实现都遵照数据结构的焦点原理,同时保持了代码的简便性和可读性。可以根据具体需求在这些根本实现上添加更多功能或举行优化。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表