{"id":21831750,"url":"https://github.com/nil369/akash_cpp_dsa_notes","last_synced_at":"2025-06-24T07:36:52.095Z","repository":{"id":248392226,"uuid":"806514234","full_name":"Nil369/Akash_Cpp_DSA_Notes","owner":"Nil369","description":"This is my C++ \u0026 DSA notes. This Repo is helpful for Learning C++ Programming language and also Data Structures \u0026 Algorithm (DSA)","archived":false,"fork":false,"pushed_at":"2024-12-25T15:42:07.000Z","size":1236,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-21T13:26:48.753Z","etag":null,"topics":["akash-halder","cpp","cpp20","data-structures","dsa"],"latest_commit_sha":null,"homepage":"https://github.com/Nil369/Akash_Cpp_DSA_Notes/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Nil369.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-27T10:46:19.000Z","updated_at":"2024-12-25T15:42:10.000Z","dependencies_parsed_at":"2024-12-25T16:23:40.335Z","dependency_job_id":"7b6e2647-5dc1-4a14-bfc5-46ab25a40dc7","html_url":"https://github.com/Nil369/Akash_Cpp_DSA_Notes","commit_stats":null,"previous_names":["nil369/akash_cpp_notes"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Nil369/Akash_Cpp_DSA_Notes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nil369%2FAkash_Cpp_DSA_Notes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nil369%2FAkash_Cpp_DSA_Notes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nil369%2FAkash_Cpp_DSA_Notes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nil369%2FAkash_Cpp_DSA_Notes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Nil369","download_url":"https://codeload.github.com/Nil369/Akash_Cpp_DSA_Notes/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nil369%2FAkash_Cpp_DSA_Notes/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261630375,"owners_count":23187217,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["akash-halder","cpp","cpp20","data-structures","dsa"],"created_at":"2024-11-27T19:15:10.217Z","updated_at":"2025-06-24T07:36:52.063Z","avatar_url":"https://github.com/Nil369.png","language":"C++","readme":"\u003cimg src=\"https://pro2-bar-s3-cdn-cf1.myportfolio.com/97b1d4bc028e3890ce75267deb159e95/24e71a4a26b5d319d6ac79ee_rw_600.gif?h=4c5ec98e37c4fea0b6776ef182152b43\" width=\"1500px\"\u003e\n\n\n# C++ \u0026 DSA Notes by Akash Halder\n\nWelcome to my C++ notes repo! Here, you'll find comprehensive and easy-to-understand notes on C++ programming language, along with simple code examples to help you grasp the fundamental concepts. Whether you're a beginner looking to get started with C++ or someone who wants to brush up on their knowledge, this repository is designed to cater to your learning needs.\n\n\u003cimg src=\"https://i.redd.it/xxodzo30yoab1.gif\" width=\"300px\" alt=\"C++\" align=\"right\"\u003e\n\n## Introduction to C++\n\nC++ is a powerful general-purpose programming language that is widely used for developing applications that require performance, efficiency, and versatility. It supports various programming paradigms including procedural, object-oriented, and generic programming. C++ is an extension of the C programming language and provides additional features such as classes, inheritance, polymorphism, templates, and exception handling.\n\n\n### Basic Concepts\n\n1. **Variables and Data Types**\n2. **Control Statements**\n3. **Loops**\n4. **Functions**\n5. **Arrays**\n6. **Pointers**\n7. **Structures and Unions**\n8. **Dynamic Memory Allocation**\n9.  **File I/O**\n10. **Object-Oriented Programming (OOP)**\n11. **Templates \u0026 Standard Template Library (STL)**\n\n    \n### Easy-to-Understand Code Examples\n\nBelow are some basic examples to illustrate these concepts. For a more detailed explanation, please refer to the corresponding code files.\n\n#### 1. Variables and Data Types\n\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nint main() {\n    int age = 20;\n    double height = 5.9;\n    char grade = 'A';\n    bool isStudent = true;\n\n    cout \u003c\u003c \"Age: \" \u003c\u003c age \u003c\u003c endl;\n    cout \u003c\u003c \"Height: \" \u003c\u003c height \u003c\u003c endl;\n    cout \u003c\u003c \"Grade: \" \u003c\u003c grade \u003c\u003c endl;\n    cout \u003c\u003c \"Is Student: \" \u003c\u003c boolalpha \u003c\u003c isStudent \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n#### 2. Control Statements\n\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nint main() {\n    int number;\n    cout \u003c\u003c \"Enter a number: \";\n    cin \u003e\u003e number;\n\n    if (number \u003e 0) {\n        cout \u003c\u003c \"The number is positive.\" \u003c\u003c endl;\n    } else if (number \u003c 0) {\n        cout \u003c\u003c \"The number is negative.\" \u003c\u003c endl;\n    } else {\n        cout \u003c\u003c \"The number is zero.\" \u003c\u003c endl;\n    }\n\n    return 0;\n}\n```\n\n#### 3. Loops\n\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nint main() {\n    for (int i = 1; i \u003c= 5; ++i) {\n        cout \u003c\u003c \"Iteration \" \u003c\u003c i \u003c\u003c endl;\n    }\n\n    return 0;\n}\n```\n\n#### 4. Functions\n\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nint add(int a, int b) {\n    return a + b;\n}\n\nint main() {\n    int result = add(5, 3);\n    cout \u003c\u003c \"Sum: \" \u003c\u003c result \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n#### 5. Arrays\n\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nint main() {\n    int numbers[5] = {1, 2, 3, 4, 5};\n\n    for (int i = 0; i \u003c 5; ++i) {\n        cout \u003c\u003c \"Element \" \u003c\u003c i \u003c\u003c \": \" \u003c\u003c numbers[i] \u003c\u003c endl;\n    }\n\n    return 0;\n}\n```\n\n#### 6. Pointers\n\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nint main() {\n    int number = 10;\n    int *ptr = \u0026number;\n\n    cout \u003c\u003c \"Value: \" \u003c\u003c number \u003c\u003c endl;\n    cout \u003c\u003c \"Pointer: \" \u003c\u003c ptr \u003c\u003c endl;\n    cout \u003c\u003c \"Dereferenced: \" \u003c\u003c *ptr \u003c\u003c endl;\n\n    return 0;\n}\n```\n### 7. Structures and Unions\n\n**Structures:**\n- Structures are user-defined data types that allow grouping different data types together.\n- Each element in a structure is called a member.\n- Members can be of different types (e.g., int, float, char).\n\n**Example of Structures:**\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nstruct Student {\n    string name;\n    int age;\n    float gpa;\n};\n\nint main() {\n    Student student1;\n    student1.name = \"John\";\n    student1.age = 20;\n    student1.gpa = 3.5;\n\n    cout \u003c\u003c \"Name: \" \u003c\u003c student1.name \u003c\u003c endl;\n    cout \u003c\u003c \"Age: \" \u003c\u003c student1.age \u003c\u003c endl;\n    cout \u003c\u003c \"GPA: \" \u003c\u003c student1.gpa \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n**Unions:**\n- Unions are similar to structures but with a key difference: all members share the same memory location.\n- Only one member can contain a value at any given time.\n- Useful for memory-saving when storing different types of data in the same memory location.\n\n**Example of Unions:**\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nunion Data {\n    int i;\n    float f;\n    char str[20];\n};\n\nint main() {\n    Data data;\n    data.i = 10;\n    cout \u003c\u003c \"Data as integer: \" \u003c\u003c data.i \u003c\u003c endl;\n\n    data.f = 220.5;\n    cout \u003c\u003c \"Data as float: \" \u003c\u003c data.f \u003c\u003c endl;\n\n    strcpy(data.str, \"C++ Programming\");\n    cout \u003c\u003c \"Data as string: \" \u003c\u003c data.str \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n### 8. Dynamic Memory Allocation\n\n- Dynamic memory allocation allows the program to allocate memory at runtime using pointers.\n- This is useful when the size of data is not known at compile time.\n\n**Functions for Dynamic Memory Allocation:**\n- `malloc()`: Allocates a block of memory.\n- `calloc()`: Allocates a block of memory and initializes it to zero.\n- `free()`: Deallocates a previously allocated block of memory.\n- `new`: Allocates memory (C++).\n- `delete`: Deallocates memory (C++).\n\n**Example of Dynamic Memory Allocation:**\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nint main() {\n    int* ptr;\n    int n;\n\n    cout \u003c\u003c \"Enter number of elements: \";\n    cin \u003e\u003e n;\n\n    // Dynamically allocate memory using new\n    ptr = new int[n];\n\n    // Check if memory has been allocated successfully\n    if (!ptr) {\n        cout \u003c\u003c \"Memory allocation failed\" \u003c\u003c endl;\n        return 1;\n    }\n\n    cout \u003c\u003c \"Enter elements: \";\n    for (int i = 0; i \u003c n; i++) {\n        cin \u003e\u003e ptr[i];\n    }\n\n    cout \u003c\u003c \"You entered: \";\n    for (int i = 0; i \u003c n; i++) {\n        cout \u003c\u003c ptr[i] \u003c\u003c \" \";\n    }\n    cout \u003c\u003c endl;\n\n    // Deallocate memory\n    delete[] ptr;\n\n    return 0;\n}\n```\n\n### 9. File I/O\n\n- File I/O allows a program to read from and write to files.\n- Use file streams (`ifstream` for input, `ofstream` for output, and `fstream` for both).\n\n**Example of File I/O:**\n```cpp\n#include \u003ciostream\u003e\n#include \u003cfstream\u003e\nusing namespace std;\n\nint main() {\n    // Writing to a file\n    ofstream outFile(\"example.txt\");\n    if (outFile.is_open()) {\n        outFile \u003c\u003c \"Hello, file!\" \u003c\u003c endl;\n        outFile \u003c\u003c \"C++ File I/O example.\" \u003c\u003c endl;\n        outFile.close();\n    } else {\n        cout \u003c\u003c \"Unable to open file for writing\" \u003c\u003c endl;\n    }\n\n    // Reading from a file\n    ifstream inFile(\"example.txt\");\n    if (inFile.is_open()) {\n        string line;\n        while (getline(inFile, line)) {\n            cout \u003c\u003c line \u003c\u003c endl;\n        }\n        inFile.close();\n    } else {\n        cout \u003c\u003c \"Unable to open file for reading\" \u003c\u003c endl;\n    }\n\n    return 0;\n}\n```\n\nIn the examples above:\n- **Structures**: The `Student` structure groups different types of data together.\n- **Unions**: The `Data` union shares memory among its members.\n- **Dynamic Memory Allocation**: Memory is dynamically allocated for an array of integers and then deallocated.\n- **File I/O**: Demonstrates writing to and reading from a text file.\n\n\u003cbr\u003e\n\n### 10. Object Oriented Programming (OOP)\n\nObject Oriented Programming (OOP) is a programming paradigm that uses objects and classes to design and develop applications. OOP aims to implement real-world entities like inheritance, polymorphism, encapsulation, and abstraction in programming. The main principles of OOP are:\n\n1. **Encapsulation**: Wrapping data and methods into a single unit (class).\n2. **Abstraction**: Hiding the complex implementation details and showing only the essential features.\n3. **Inheritance**: The mechanism by which one class can inherit properties and behaviors from another class.\n4. **Polymorphism**: The ability to use a single interface to represent different underlying forms (data types).\n\n### Basic Example of OOP in C++\n\nHere is a simple example to illustrate OOP concepts in C++:\n\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\n// Base class\nclass Animal {\npublic:\n    void eat() {\n        cout \u003c\u003c \"Eating...\" \u003c\u003c endl;\n    }\n};\n\n// Derived class\nclass Dog : public Animal {\npublic:\n    void bark() {\n        cout \u003c\u003c \"Barking...\" \u003c\u003c endl;\n    }\n};\n\nint main() {\n    Dog dog;\n    dog.eat();  // Inherited from Animal class\n    dog.bark(); // Specific to Dog class\n\n    return 0;\n}\n```\n\nIn this example:\n- **Encapsulation**: The `Animal` and `Dog` classes encapsulate data and methods.\n- **Inheritance**: The `Dog` class inherits the `eat` method from the `Animal` class.\n- **Polymorphism**: Although not directly shown here, polymorphism would allow different classes to be treated as instances of the same class through inheritance.\n\u003cbr\u003e\n### Difference Between Procedural Oriented Programming (POP) and Object Oriented Programming (OOP)\n\n| `Feature `                        |` Procedural Oriented Programming (POP)` | `Object Oriented Programming (OOP)`    |\n|---------------------------------|---------------------------------------|--------------------------------------|\n| **Approach**                    | Follows a top-down approach           | Follows a bottom-up approach         |\n| **Structure**                   | Program is divided into functions     | Program is divided into objects      |\n| **Data Access**                 | Data is global and shared by functions| Data is encapsulated in objects      |\n| **Data Security**               | Less secure as data is exposed        | More secure due to encapsulation     |\n| **Focus**                       | Focuses on functions                  | Focuses on objects                   |\n| **Code Reusability**            | Less reusability                      | High reusability through inheritance |\n| **Inheritance**                 | Does not support inheritance          | Supports inheritance                 |\n| **Polymorphism**                | Does not support polymorphism         | Supports polymorphism                |\n| **Examples**                    | C, Pascal                             | C++, Java, Python                    |\n\n\u003cbr\u003e\u003cbr\u003e\n\n### 11. C++ Templates \u0026 STL\n\nTemplates in C++ allow writing generic and reusable code. They enable functions and classes to operate with different data types without rewriting the entire code for each type.\n\n**Function Templates:**\n- Function templates define a generic function that can work with any data type.\n\n**Example of Function Template:**\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\ntemplate \u003ctypename T\u003e\nT add(T a, T b) {\n    return a + b;\n}\n\nint main() {\n    cout \u003c\u003c \"Sum of integers: \" \u003c\u003c add(3, 4) \u003c\u003c endl;          // int\n    cout \u003c\u003c \"Sum of doubles: \" \u003c\u003c add(3.5, 4.5) \u003c\u003c endl;      // double\n    cout \u003c\u003c \"Sum of strings: \" \u003c\u003c add(string(\"Hello, \"), string(\"World!\")) \u003c\u003c endl; // string\n\n    return 0;\n}\n```\n\n**Class Templates:**\n- Class templates define a blueprint for a class that can handle different data types.\n\n**Example of Class Template:**\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\ntemplate \u003ctypename T\u003e\nclass Calculator {\npublic:\n    T add(T a, T b) {\n        return a + b;\n    }\n    T subtract(T a, T b) {\n        return a - b;\n    }\n};\n\nint main() {\n    Calculator\u003cint\u003e intCalc;\n    cout \u003c\u003c \"Integer addition: \" \u003c\u003c intCalc.add(5, 3) \u003c\u003c endl;\n    cout \u003c\u003c \"Integer subtraction: \" \u003c\u003c intCalc.subtract(5, 3) \u003c\u003c endl;\n\n    Calculator\u003cdouble\u003e doubleCalc;\n    cout \u003c\u003c \"Double addition: \" \u003c\u003c doubleCalc.add(5.5, 3.3) \u003c\u003c endl;\n    cout \u003c\u003c \"Double subtraction: \" \u003c\u003c doubleCalc.subtract(5.5, 3.3) \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n### C++ Standard Template Library (STL)\n\nThe C++ Standard Template Library (STL) is a powerful library that provides generic classes and functions. It includes four main components:\n1. **Algorithms**: Functions for sorting, searching, and manipulating data.\n2. **Containers**: Data structures like vectors, lists, and maps that store collections of objects.\n3. **Iterators**: Objects that point to elements within containers and are used to traverse the containers.\n4. **Function Objects**: Objects that can be called as if they are ordinary functions.\n\n**Example of STL with Vectors:**\n```cpp\n#include \u003ciostream\u003e\n#include \u003cvector\u003e\nusing namespace std;\n\nint main() {\n    vector\u003cint\u003e numbers = {1, 2, 3, 4, 5};\n\n    // Add elements to the vector\n    numbers.push_back(6);\n    numbers.push_back(7);\n\n    // Access elements\n    cout \u003c\u003c \"Vector elements: \";\n    for (int i = 0; i \u003c numbers.size(); ++i) {\n        cout \u003c\u003c numbers[i] \u003c\u003c \" \";\n    }\n    cout \u003c\u003c endl;\n\n    // Use iterator to traverse the vector\n    cout \u003c\u003c \"Vector elements using iterator: \";\n    for (vector\u003cint\u003e::iterator it = numbers.begin(); it != numbers.end(); ++it) {\n        cout \u003c\u003c *it \u003c\u003c \" \";\n    }\n    cout \u003c\u003c endl;\n\n    // Use algorithm to sort the vector\n    sort(numbers.begin(), numbers.end());\n\n    cout \u003c\u003c \"Sorted vector elements: \";\n    for (int i = 0; i \u003c numbers.size(); ++i) {\n        cout \u003c\u003c numbers[i] \u003c\u003c \" \";\n    }\n    cout \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n**Example of STL with Maps:**\n```cpp\n#include \u003ciostream\u003e\n#include \u003cmap\u003e\nusing namespace std;\n\nint main() {\n    map\u003cstring, int\u003e ageMap;\n\n    // Insert elements into the map\n    ageMap[\"Alice\"] = 30;\n    ageMap[\"Bob\"] = 25;\n    ageMap[\"Charlie\"] = 35;\n\n    // Access elements\n    cout \u003c\u003c \"Alice's age: \" \u003c\u003c ageMap[\"Alice\"] \u003c\u003c endl;\n    cout \u003c\u003c \"Bob's age: \" \u003c\u003c ageMap[\"Bob\"] \u003c\u003c endl;\n\n    // Use iterator to traverse the map\n    cout \u003c\u003c \"All elements in the map:\" \u003c\u003c endl;\n    for (map\u003cstring, int\u003e::iterator it = ageMap.begin(); it != ageMap.end(); ++it) {\n        cout \u003c\u003c it-\u003efirst \u003c\u003c \": \" \u003c\u003c it-\u003esecond \u003c\u003c endl;\n    }\n\n    return 0;\n}\n```\n\nIn these examples:\n- **Function Templates**: `add` function template works with different data types.\n- **Class Templates**: `Calculator` class template performs addition and subtraction for different data types.\n- **STL with Vectors**: Demonstrates vector operations including adding, accessing, and sorting elements.\n- **STL with Maps**: Demonstrates map operations including inserting, accessing, and iterating over elements.\n\n\u003cbr\u003e\u003cbr\u003e\n\n\n---\n\n# Data Structures and Algorithms (DSA)\n\n## Table of Contents\n\n1. [Arrays](#arrays)\n2. [Strings](#strings)\n3. [Linked Lists](#linked-lists)\n4. [Stacks](#stacks)\n5. [Queues](#queues)\n6. [Trees](#trees)\n7. [Graphs](#graphs)\n8. [Hashing](#hashing)\n9. [Recursion and Backtracking](#recursion-and-backtracking)\n10. [Dynamic Programming](#dynamic-programming)\n11. [Sorting Algorithms](#sorting-algorithms)\n12. [Searching Algorithms](#searching-algorithms)\n\n## Arrays\n\n### Key Concepts\n- Fixed-size sequential collection of elements of the same type.\n- Access elements via indices.\n- Common operations: Traversal, Insertion, Deletion, Searching, Sorting.\n\n### Example Problem: Find the Maximum Element\n```cpp\n#include \u003ciostream\u003e\n#include \u003cvector\u003e\nusing namespace std;\n\nint findMax(const vector\u003cint\u003e\u0026 arr) {\n    int maxElement = arr[0];\n    for (int i = 1; i \u003c arr.size(); ++i) {\n        if (arr[i] \u003e maxElement) {\n            maxElement = arr[i];\n        }\n    }\n    return maxElement;\n}\n\nint main() {\n    vector\u003cint\u003e arr = {1, 3, 5, 2, 4, 6};\n    cout \u003c\u003c \"Maximum Element: \" \u003c\u003c findMax(arr) \u003c\u003c endl;\n    return 0;\n}\n```\n\n## Strings\n\n### Key Concepts\n- Sequence of characters.\n- Common operations: Length, Concatenation, Substring, Comparison, Searching, Pattern Matching.\n\n### Example Problem: Reverse a String\n```cpp\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\nstring reverseString(const string\u0026 str) {\n    string reversedStr = str;\n    int n = str.length();\n    for (int i = 0; i \u003c n / 2; ++i) {\n        swap(reversedStr[i], reversedStr[n - i - 1]);\n    }\n    return reversedStr;\n}\n\nint main() {\n    string str = \"Hello, World!\";\n    cout \u003c\u003c \"Reversed String: \" \u003c\u003c reverseString(str) \u003c\u003c endl;\n    return 0;\n}\n```\n\n## Linked Lists\n\n### Key Concepts\n- Sequence of elements where each element points to the next element.\n- Types: Singly Linked List, Doubly Linked List, Circular Linked List.\n- Common operations: Traversal, Insertion, Deletion, Searching.\n\n### Example Problem: Reverse a Singly Linked List\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nstruct ListNode {\n    int val;\n    ListNode* next;\n    ListNode(int x) : val(x), next(nullptr) {}\n};\n\nListNode* reverseList(ListNode* head) {\n    ListNode* prev = nullptr;\n    ListNode* curr = head;\n    while (curr != nullptr) {\n        ListNode* nextTemp = curr-\u003enext;\n        curr-\u003enext = prev;\n        prev = curr;\n        curr = nextTemp;\n    }\n    return prev;\n}\n\nvoid printList(ListNode* head) {\n    while (head != nullptr) {\n        cout \u003c\u003c head-\u003eval \u003c\u003c \" \";\n        head = head-\u003enext;\n    }\n    cout \u003c\u003c endl;\n}\n\nint main() {\n    ListNode* head = new ListNode(1);\n    head-\u003enext = new ListNode(2);\n    head-\u003enext-\u003enext = new ListNode(3);\n    head-\u003enext-\u003enext-\u003enext = new ListNode(4);\n    head-\u003enext-\u003enext-\u003enext-\u003enext = new ListNode(5);\n\n    cout \u003c\u003c \"Original List: \";\n    printList(head);\n\n    head = reverseList(head);\n\n    cout \u003c\u003c \"Reversed List: \";\n    printList(head);\n\n    return 0;\n}\n```\n\n## Stacks\n\n### Key Concepts\n- Last In First Out (LIFO) data structure.\n- Operations: Push, Pop, Peek, IsEmpty.\n\n### Example Problem: Check for Balanced Parentheses\n```cpp\n#include \u003ciostream\u003e\n#include \u003cstack\u003e\nusing namespace std;\n\nbool isBalanced(string str) {\n    stack\u003cchar\u003e s;\n    for (char c : str) {\n        if (c == '(' || c == '{' || c == '[') {\n            s.push(c);\n        } else {\n            if (s.empty()) return false;\n            char top = s.top();\n            if ((c == ')' \u0026\u0026 top == '(') || (c == '}' \u0026\u0026 top == '{') || (c == ']' \u0026\u0026 top == '[')) {\n                s.pop();\n            } else {\n                return false;\n            }\n        }\n    }\n    return s.empty();\n}\n\nint main() {\n    string str = \"{[()]}\";\n    cout \u003c\u003c (isBalanced(str) ? \"Balanced\" : \"Not Balanced\") \u003c\u003c endl;\n    return 0;\n}\n```\n\n## Queues\n\n### Key Concepts\n- First In First Out (FIFO) data structure.\n- Operations: Enqueue, Dequeue, Front, IsEmpty.\n\n### Example Problem: Implement a Queue using Two Stacks\n```cpp\n#include \u003ciostream\u003e\n#include \u003cstack\u003e\nusing namespace std;\n\nclass Queue {\n    stack\u003cint\u003e s1, s2;\n\npublic:\n    void enqueue(int x) {\n        s1.push(x);\n    }\n\n    int dequeue() {\n        if (s2.empty()) {\n            while (!s1.empty()) {\n                s2.push(s1.top());\n                s1.pop();\n            }\n        }\n        int x = s2.top();\n        s2.pop();\n        return x;\n    }\n\n    bool isEmpty() {\n        return s1.empty() \u0026\u0026 s2.empty();\n    }\n};\n\nint main() {\n    Queue q;\n    q.enqueue(1);\n    q.enqueue(2);\n    q.enqueue(3);\n\n    cout \u003c\u003c q.dequeue() \u003c\u003c endl;\n    cout \u003c\u003c q.dequeue() \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n## Trees\n\n### Key Concepts\n- Hierarchical data structure with nodes.\n- Types: Binary Tree, Binary Search Tree, AVL Tree, Red-Black Tree.\n- Common operations: Traversal (Inorder, Preorder, Postorder), Insertion, Deletion, Searching.\n\n### Example Problem: Inorder Traversal of a Binary Tree\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nstruct TreeNode {\n    int val;\n    TreeNode* left;\n    TreeNode* right;\n    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n\nvoid inorderTraversal(TreeNode* root) {\n    if (root == nullptr) return;\n    inorderTraversal(root-\u003eleft);\n    cout \u003c\u003c root-\u003eval \u003c\u003c \" \";\n    inorderTraversal(root-\u003eright);\n}\n\nint main() {\n    TreeNode* root = new TreeNode(1);\n    root-\u003eright = new TreeNode(2);\n    root-\u003eright-\u003eleft = new TreeNode(3);\n\n    cout \u003c\u003c \"Inorder Traversal: \";\n    inorderTraversal(root);\n\n    return 0;\n}\n```\n\n## Graphs\n\n### Key Concepts\n- Collection of nodes and edges.\n- Types: Directed, Undirected, Weighted, Unweighted.\n- Representations: Adjacency Matrix, Adjacency List.\n- Common operations: Traversal (DFS, BFS), Shortest Path (Dijkstra, Bellman-Ford), Minimum Spanning Tree (Kruskal, Prim).\n\n### Example Problem: Depth-First Search (DFS)\n```cpp\n#include \u003ciostream\u003e\n#include \u003cvector\u003e\nusing namespace std;\n\nvoid DFS(int v, vector\u003cbool\u003e\u0026 visited, const vector\u003cvector\u003cint\u003e\u003e\u0026 adj) {\n    visited[v] = true;\n    cout \u003c\u003c v \u003c\u003c \" \";\n    for (int u : adj[v]) {\n        if (!visited[u]) {\n            DFS(u, visited, adj);\n        }\n    }\n}\n\nint main() {\n    int V = 4;\n    vector\u003cvector\u003cint\u003e\u003e adj(V);\n    adj[0].push_back(1);\n    adj[0].push_back(2);\n    adj[1].push_back(2);\n    adj[2].push_back(0);\n    adj[2].push_back(3);\n    adj[3].push_back(3);\n\n    vector\u003cbool\u003e visited(V, false);\n\n    cout \u003c\u003c \"DFS Traversal starting from vertex 2: \";\n    DFS(2, visited, adj);\n\n    return 0;\n}\n```\n\n## Hashing\n\n### Key Concepts\n- Mapping keys to values using a hash function.\n- Common operations: Insertion, Deletion, Searching.\n\n### Example Problem: Implement a Simple Hash Table\n```cpp\n#include \u003ciostream\u003e\n#include \u003clist\u003e\nusing namespace std;\n\nclass HashTable {\n    int BUCKET;\n    list\u003cint\u003e* table;\n\npublic:\n    HashTable(int V) {\n        BUCKET = V;\n        table = new list\u003cint\u003e[BUCKET];\n    }\n\n    void insertItem(int key) {\n        int index = key % BUCKET;\n        table[index].push_back(key);\n    }\n\n    void deleteItem(int key) {\n        int index = key % BUCKET;\n        table[index].remove(key);\n    }\n\n    bool searchItem(int key) {\n        int index = key % BUCKET;\n        for (auto x : table[index]) {\n            if (x ==\n\n key) return true;\n        }\n        return false;\n    }\n\n    void displayHash() {\n        for (int i = 0; i \u003c BUCKET; i++) {\n            cout \u003c\u003c i;\n            for (auto x : table[i])\n                cout \u003c\u003c \" --\u003e \" \u003c\u003c x;\n            cout \u003c\u003c endl;\n        }\n    }\n};\n\nint main() {\n    int keys[] = {15, 11, 27, 8, 12};\n    int n = sizeof(keys) / sizeof(keys[0]);\n\n    HashTable h(7);\n    for (int i = 0; i \u003c n; i++) {\n        h.insertItem(keys[i]);\n    }\n\n    h.displayHash();\n\n    return 0;\n}\n```\n\n## Recursion and Backtracking\n\n### Key Concepts\n- Recursion: Function calling itself.\n- Backtracking: Trying out all possible solutions and eliminating invalid ones.\n\n### Example Problem: Solving N-Queens Problem\n```cpp\n#include \u003ciostream\u003e\n#include \u003cvector\u003e\nusing namespace std;\n\nbool isSafe(vector\u003cvector\u003cint\u003e\u003e\u0026 board, int row, int col) {\n    int i, j;\n    int N = board.size();\n\n    for (i = 0; i \u003c col; i++)\n        if (board[row][i])\n            return false;\n\n    for (i = row, j = col; i \u003e= 0 \u0026\u0026 j \u003e= 0; i--, j--)\n        if (board[i][j])\n            return false;\n\n    for (i = row, j = col; j \u003e= 0 \u0026\u0026 i \u003c N; i++, j--)\n        if (board[i][j])\n            return false;\n\n    return true;\n}\n\nbool solveNQUtil(vector\u003cvector\u003cint\u003e\u003e\u0026 board, int col) {\n    int N = board.size();\n    if (col \u003e= N)\n        return true;\n\n    for (int i = 0; i \u003c N; i++) {\n        if (isSafe(board, i, col)) {\n            board[i][col] = 1;\n\n            if (solveNQUtil(board, col + 1))\n                return true;\n\n            board[i][col] = 0;\n        }\n    }\n\n    return false;\n}\n\nvoid solveNQ(int N) {\n    vector\u003cvector\u003cint\u003e\u003e board(N, vector\u003cint\u003e(N, 0));\n\n    if (solveNQUtil(board, 0)) {\n        for (int i = 0; i \u003c N; i++) {\n            for (int j = 0; j \u003c N; j++)\n                cout \u003c\u003c board[i][j] \u003c\u003c \" \";\n            cout \u003c\u003c endl;\n        }\n    } else {\n        cout \u003c\u003c \"Solution does not exist\" \u003c\u003c endl;\n    }\n}\n\nint main() {\n    int N = 4;\n    solveNQ(N);\n    return 0;\n}\n```\n\n## Dynamic Programming\n\n### Key Concepts\n- Solving problems by breaking them into simpler subproblems and storing the results to avoid redundant calculations.\n\n### Example Problem: Fibonacci Sequence\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nint fib(int n) {\n    int* f = new int[n + 1];\n    f[0] = 0;\n    f[1] = 1;\n\n    for (int i = 2; i \u003c= n; i++) {\n        f[i] = f[i - 1] + f[i - 2];\n    }\n\n    return f[n];\n}\n\nint main() {\n    int n = 10;\n    cout \u003c\u003c \"Fibonacci number is \" \u003c\u003c fib(n) \u003c\u003c endl;\n    return 0;\n}\n```\n\n## Sorting Algorithms\n\n### Key Concepts\n- Arranging elements in a particular order.\n- Common algorithms: Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort, Heap Sort.\n\n### Example Problem: Quick Sort\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nvoid swap(int* a, int* b) {\n    int t = *a;\n    *a = *b;\n    *b = t;\n}\n\nint partition(int arr[], int low, int high) {\n    int pivot = arr[high];\n    int i = (low - 1);\n\n    for (int j = low; j \u003c= high - 1; j++) {\n        if (arr[j] \u003c pivot) {\n            i++;\n            swap(\u0026arr[i], \u0026arr[j]);\n        }\n    }\n    swap(\u0026arr[i + 1], \u0026arr[high]);\n    return (i + 1);\n}\n\nvoid quickSort(int arr[], int low, int high) {\n    if (low \u003c high) {\n        int pi = partition(arr, low, high);\n\n        quickSort(arr, low, pi - 1);\n        quickSort(arr, pi + 1, high);\n    }\n}\n\nvoid printArray(int arr[], int size) {\n    for (int i = 0; i \u003c size; i++)\n        cout \u003c\u003c arr[i] \u003c\u003c \" \";\n    cout \u003c\u003c endl;\n}\n\nint main() {\n    int arr[] = {10, 7, 8, 9, 1, 5};\n    int n = sizeof(arr) / sizeof(arr[0]);\n    quickSort(arr, 0, n - 1);\n    cout \u003c\u003c \"Sorted array: \";\n    printArray(arr, n);\n    return 0;\n}\n```\n\n## Searching Algorithms\n\n### Key Concepts\n- Finding an element in a collection.\n- Common algorithms: Linear Search, Binary Search.\n\n### Example Problem: Binary Search\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nint binarySearch(int arr[], int l, int r, int x) {\n    while (l \u003c= r) {\n        int m = l + (r - l) / 2;\n\n        if (arr[m] == x)\n            return m;\n\n        if (arr[m] \u003c x)\n            l = m + 1;\n        else\n            r = m - 1;\n    }\n\n    return -1;\n}\n\nint main() {\n    int arr[] = {2, 3, 4, 10, 40};\n    int n = sizeof(arr) / sizeof(arr[0]);\n    int x = 10;\n    int result = binarySearch(arr, 0, n - 1, x);\n    if (result != -1)\n        cout \u003c\u003c \"Element is present at index \" \u003c\u003c result \u003c\u003c endl;\n    else\n        cout \u003c\u003c \"Element is not present in array\" \u003c\u003c endl;\n    return 0;\n}\n```\n\n\nFeel free to customize the content and add more examples or explanations as needed. This `README.md` provides a comprehensive guide for anyone preparing for technical interviews with a focus on essential DSA topics.\nFor more detailed notes and examples, please explore the code files in this repository. Each file is dedicated to a specific topic and provides in-depth explanations and additional examples.\n\n`THANKS FOR READING 🙂`\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnil369%2Fakash_cpp_dsa_notes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnil369%2Fakash_cpp_dsa_notes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnil369%2Fakash_cpp_dsa_notes/lists"}