DEV Community

Codes With Pankaj
Codes With Pankaj

Posted on

Comprehensive Guide to C++ Data Structures : @codeswithpankaj

Welcome to this extensive tutorial on C++ data structures, where we will explore the fundamental building blocks that empower efficient and organized programming. Data structures are essential for managing and storing data, and a solid understanding of them is crucial for developing robust and optimized software. In this tutorial, we'll cover various data structures, providing complete examples for each topic to deepen your comprehension.

Table of Contents

  1. Introduction to C++ Data Structures
  2. Arrays
  3. Linked Lists
  4. Stacks
  5. Queues
  6. Trees
  7. Graphs
  8. Hash Tables
  9. Advanced Data Structures
  10. Practical Example
  11. Conclusion

1. Introduction to C++ Data Structures

Data structures are containers that hold, organize, and manage data. In C++, various data structures cater to different needs, providing efficiency in terms of time and space complexity.

2. Arrays

Arrays are a fundamental data structure, representing a collection of elements stored in contiguous memory locations.

#include <iostream>

int main() {
    // Declaration and Initialization
    int numbers[5] = {1, 2, 3, 4, 5};

    // Accessing Elements
    std::cout << "Third element: " << numbers[2] << std::endl;

    // ... (rest of the code)
}
Enter fullscreen mode Exit fullscreen mode

3. Linked Lists

Linked lists consist of nodes, each containing data and a reference to the next node.

#include <iostream>

struct Node {
    int data;
    Node* next;
};

int main() {
    // Creating a Linked List
    Node* head = new Node{1, nullptr};
    head->next = new Node{2, nullptr};
    head->next->next = new Node{3, nullptr};

    // ... (rest of the code)
}
Enter fullscreen mode Exit fullscreen mode

4. Stacks

Stacks follow the Last In, First Out (LIFO) principle, where the last element added is the first to be removed.

#include <iostream>
#include <stack>

int main() {
    std::stack<int> stack;

    // Pushing Elements onto the Stack
    stack.push(1);
    stack.push(2);
    stack.push(3);

    // Popping Elements from the Stack
    while (!stack.empty()) {
        std::cout << "Popped: " << stack.top() << std::endl;
        stack.pop();
    }

    // ... (rest of the code)
}
Enter fullscreen mode Exit fullscreen mode

5. Queues

Queues follow the First In, First Out (FIFO) principle, where the first element added is the first to be removed.

#include <iostream>
#include <queue>

int main() {
    std::queue<int> queue;

    // Enqueuing Elements
    queue.push(1);
    queue.push(2);
    queue.push(3);

    // Dequeuing Elements
    while (!queue.empty()) {
        std::cout << "Dequeued: " << queue.front() << std::endl;
        queue.pop();
    }

    // ... (rest of the code)
}
Enter fullscreen mode Exit fullscreen mode

6. Trees

Trees are hierarchical data structures with a root node and branches.

#include <iostream>

struct TreeNode {
    int data;
    TreeNode* left;
    TreeNode* right;
};

int main() {
    // Creating a Binary Tree
    TreeNode* root = new TreeNode{1, nullptr, nullptr};
    root->left = new TreeNode{2, nullptr, nullptr};
    root->right = new TreeNode{3, nullptr, nullptr};

    // ... (rest of the code)
}
Enter fullscreen mode Exit fullscreen mode

7. Graphs

Graphs consist of vertices and edges, representing relationships between entities.

#include <iostream>
#include <vector>

struct Graph {
    int vertices;
    std::vector<std::vector<int>> adjacencyList;
};

int main() {
    // Creating a Graph
    Graph graph{4, {{1, 2}, {2, 3}, {3, 4}, {4, 1}}};

    // ... (rest of the code)
}
Enter fullscreen mode Exit fullscreen mode

8. Hash Tables

Hash tables store data in key-value pairs, providing fast retrieval based on keys.

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> hashMap;

    // Inserting Key-Value Pairs
    hashMap["one"] = 1;
    hashMap["two"] = 2;
    hashMap["three"] = 3;

    // Accessing Values
    std::cout << "Value for key 'two': " << hashMap["two"] << std::endl;

    // ... (rest of the code)
}
Enter fullscreen mode Exit fullscreen mode

9. Advanced Data Structures

Explore advanced data structures like AVL trees, B-trees, and more based on specific application requirements.

// Example of AVL Tree
#include <iostream>

struct AVLNode {
    int data;
    AVLNode* left;
    AVLNode* right;
    int height;
};

// ... (rest of the code)
Enter fullscreen mode Exit fullscreen mode

10. Practical Example

Let's apply our knowledge to a practical example: a program that uses a hash table to store and retrieve student information.

#include <iostream>
#include <unordered_map>
#include <string>

struct Student {
    std::string name;
    int age;
    double gpa;
};

int main() {
    std::unordered_map<std::string, Student> studentTable;

    // Inserting Student Information
    studentTable["John"] = {"John Doe", 20, 3.8};
    studentTable["Jane"] = {"Jane Smith", 21, 3.9};
    studentTable["Bob"] = {"Bob Johnson", 19, 3.5};

    // Retrieving and Displaying Information
    std::string searchName = "Jane";
    if (studentTable.find(searchName) != studentTable.end()) {
        Student& foundStudent = studentTable[searchName];
        std::cout << "Student Information for " << searchName << ":\n";
        std::cout << "Name: " << foundStudent.name << "\n";
        std::cout << "Age: " << foundStudent.age << "\n";
        std::cout << "GPA: " << foundStudent.gpa << "\n";
    } else {
        std::cout << "Student not found.\n";
    }

    // ... (rest of the code)
}
Enter fullscreen mode Exit fullscreen mode

11. Conclusion <a name="

conclusion">

Congratulations! You've now delved into the realm of C++ data structures, mastering the essential concepts and gaining practical experience through examples. These foundational elements are key to writing efficient and organized programs.

For more programming tutorials and resources, visit codeswithpankaj.com. Happy coding!

Top comments (0)