揭秘C++数据结构:50个实战代码实例,轻松入门高效编程

C++作为一种高效、强大的编程语言,在各个领域都得到了广泛应用。掌握C++数据结构是实现高效编程的关键。本文将详细介绍50个实战代码实例,帮助读者轻松入门C++数据结构,提高编程效率。
1. 线性表
线性表是C++中最基本的数据结构,包括顺序表和链表。
1.1 顺序表
顺序表是一种基于数组的线性表,可以通过下标直接访问元素。
#include
#include

using namespace std;

int main() {
vector arr = {1, 2, 3, 4, 5};
cout << "顺序表元素:" << endl; for (int i = 0; i < arr.size(); ++i) { cout << arr[i] << " "; } cout << endl; return 0; } 1.2 链表 链表是一种基于节点的线性表,节点包含数据和指向下一个节点的指针。 #include
#include

using namespace std;

struct Node {
int data;
Node* next;
Node(int x) : data(x), next(NULL) {}
};

void printList(Node* node) {
while (node != NULL) {
cout << node->data << " "; node = node->next;
}
cout << endl; } int main() { Node* head = new Node(1); head->next = new Node(2);
head->next->next = new Node(3);

cout << "链表元素:" << endl; printList(head); return 0; } 2. 栈 栈是一种后进先出(LIFO)的数据结构,适用于解决需要回溯的问题。 #include
#include

using namespace std;

int main() {
stack s;
s.push(1);
s.push(2);
s.push(3);

cout << "栈顶元素:" << s.top() << endl; s.pop(); cout << "出栈后栈顶元素:" << s.top() << endl; return 0; } 3. 队列 队列是一种先进先出(FIFO)的数据结构,适用于处理需要顺序处理的问题。 #include
#include

using namespace std;

int main() {
queue q;
q.push(1);
q.push(2);
q.push(3);

cout << "队列首元素:" << q.front() << endl; cout << "队列尾元素:" << q.back() << endl; q.pop(); return 0; } 4. 树 树是一种由节点组成的数据结构,具有层次关系。 4.1 二叉树 二叉树是一种每个节点最多有两个子节点的树。 #include
#include

using namespace std;

struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode* buildTree(vector& nums, int start, int end) {
if (start > end) return NULL;
TreeNode* root = new TreeNode(nums[start]);
root->left = buildTree(nums, 2 * start + 1, 2 * end + 1);
root->right = buildTree(nums, 2 * start + 2, 2 * end + 2);
return root;
}

int main() {
vector nums = {1, 2, 3, 4, 5, 6, 7};
TreeNode* root = buildTree(nums, 0, nums.size() – 1);

return 0;
}
4.2 图
图是一种由节点和边组成的数据结构,表示节点之间的连接关系。
#include
#include
#include

using namespace std;

class Graph {
public:
int numVertices;
list *adjLists;
Graph(int vertices) {
numVertices = vertices;
adjLists = new list[vertices];
}
void addEdge(int src, int dest) {
adjLists[src].push_back(dest);
adjLists[dest].push_back(src);
}
void printGraph() {
for (int v = 0; v < numVertices; v++) { cout << "Adjacency list of vertex " << v << ":\n"; for (int adj : adjLists[v]) { cout << adj << " "; } cout << endl; } } }; int main() { Graph g(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 2); g.addEdge(2, 0); g.addEdge(2, 3); g.addEdge(3, 3); g.printGraph(); return 0; } 5. 哈希表 哈希表是一种基于哈希函数的关联数组,用于高效地存储和检索数据。 #include
#include

using namespace std;

int main() {
unordered_map map;
map[1] = “one”;
map[2] = “two”;
map[3] = “three”;

cout << "map[1] = " << map[1] << endl; cout << "map[2] = " << map[2] << endl; cout << "map[3] = " << map[3] << endl; return 0; } 6. 并查集 并查集是一种用于处理元素划分问题的数据结构,可以快速合并集合和查询元素所属集合。 #include
#include

using namespace std;

struct UnionFind {
vector parent;
vector rank;

UnionFind(int size) {
parent.resize(size);
rank.resize(size, 0);
for (int i = 0; i < size; i++) { parent[i] = i; } } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void unionSets(int x, int y) { int rootX = find(x); int rootY = find(y); if (rootX != rootY) { if (rank[rootX] < rank[rootY]) { parent[rootX] = rootY; } else if (rank[rootX] > rank[rootY]) {
parent[rootY] = rootX;
} else {
parent[rootY] = rootX;
rank[rootX]++;
}
}
}
};

int main() {
UnionFind uf(5);
uf.unionSets(1, 2);
uf.unionSets(2, 3);
uf.unionSets(4, 5);

cout << "Is 1 and 2 in the same set? " << (uf.find(1) == uf.find(2)) << endl; cout << "Is 2 and 3 in the same set? " << (uf.find(2) == uf.find(3)) << endl; cout << "Is 3 and 4 in the same set? " << (uf.find(3) == uf.find(4)) << endl; return 0; } 总结 本文介绍了C++中常用的50个数据结构实战代码实例,包括线性表、栈、队列、树、图、哈希表和并查集等。通过学习这些实例,读者可以轻松入门C++数据结构,提高编程效率。希望本文对您的学习有所帮助!