1. Dynamic Array
#include <vector> int main() { vector<int> arr; // Add elements arr.push_back(1); arr.push_back(2); arr.push_back(3); // Access elements for (int i = 0; i < arr.size(); i++) { cout << arr[i] << endl; } return 0; }
2. Queue
3. Stack
4. Linked List
5. Binary Tree
6. Hash Table
7. Graph
8. Geometry
#include <vector> int main() { vector<int> queue; // Enqueue queue.push_back(1); queue.push_back(2); queue.push_back(3); // Dequeue cout << queue.front() << endl; queue.erase(queue.begin()); return 0; }
#include <vector> int main() { vector<int> stack; // Push stack.push_back(1); stack.push_back(2); stack.push_back(3); // Pop cout << stack.back() << endl; stack.pop_back(); return 0; }
#include <vector> struct Node { int data; Node* next; }; int main() { vector<Node> linkedList; // Create nodes Node node1 = {1, nullptr}; Node node2 = {2, nullptr}; Node node3 = {3, nullptr}; // Link nodes linkedList.push_back(node1); linkedList.push_back(node2); linkedList.push_back(node3); // Iterate over list for (auto& node : linkedList) { cout << node.data << endl; } return 0; }
#include <vector> struct Node { int data; Node* left; Node* right; }; int main() { vector<Node> binaryTree; // Create nodes Node node1 = {1, nullptr, nullptr}; Node node2 = {2, nullptr, nullptr}; Node node3 = {3, nullptr, nullptr}; // Link nodes binaryTree.push_back(node1); binaryTree.push_back(node2); binaryTree.push_back(node3); // Set left and right pointers binaryTree[0].left = &binaryTree[1]; binaryTree[0].right = &binaryTree[2]; // Iterate over tree for (auto& node : binaryTree) { cout << node.data << endl; } return 0; }
#include <vector> template <typename K, typename V> class HashTable { private: vector<pair<K, V>> table; int size; public: HashTable(int size) : size(size) {} void insert(K key, V value) { int index = hash(key); table[index].push_back({key, value}); } V get(K key) { int index = hash(key); for (auto& pair : table[index]) { if (pair.first == key) { return pair.second; } } return nullptr; } private: int hash(K key) { // Calculate hash function here } };
#include <vector> struct Edge { int source; int destination; int weight; }; int main() { vector<vector<Edge>> graph; // Add vertices graph.resize(5); // Add edges graph[0].push_back({0, 1, 1}); graph[0].push_back({0, 2, 2}); graph[1].push_back({1, 2, 3}); // Iterate over graph for (auto& vertex : graph) { for (auto& edge : vertex) { cout << edge.source << " -> " << edge.destination << " (" << edge.weight << ")" << endl; } } return 0; }
#include <vector> struct Point { int x; int y; }; int main() { vector<Point> points; // Add points points.push_back({1, 2}); points.push_back({3, 4}); points.push_back({5, 6}); // Calculate distance between points for (int i = 0; i < points.size