├── .gitignore ├── Array ├── 2DArray.cpp ├── BubbleSort.cpp ├── README.md ├── binarySearch.cpp ├── countingSort.cpp ├── insertionSort.cpp └── linearSearch.cpp ├── BST ├── README.md └── bst.cpp ├── Binary Tree ├── README.md ├── binaryTree.cpp └── preorderInordertoBinary.cpp ├── Graph ├── README.md └── graph.cpp ├── Hashing ├── README.md ├── VerticalOrder.cpp ├── freq.cpp └── hashing.cpp ├── Heap and Priority queue ├── README.md ├── heap.cpp ├── mergeKSorted.cpp └── vectorPQSTL.cpp ├── Linked List ├── README.md ├── circularLinkedList.cpp ├── doublyLinkedList.cpp └── linearLinkedList.cpp ├── Queue ├── MYQUEUE.h ├── README.md ├── balancedpar.cpp ├── curmin.cpp └── kreverse.cpp ├── README.md └── Stack ├── MYSTACK.h ├── README.md ├── infixToPrefix.cpp ├── prefixEvaluation.cpp ├── reverseSentence.cpp └── reverseStack.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !/**/ 3 | !*.* 4 | *.exe 5 | .DS_Store 6 | *output -------------------------------------------------------------------------------- /Array/2DArray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This program takes the user input for the dimensions of a 2D array, then prompts the user to enter the elements of the 3 | array. After the array is filled, the program passes the array to a function called printArray, which prints the 4 | contents of the array to the console. 5 | 6 | The printArray function takes three arguments: a pointer to the beginning of the array, the number of rows in the array, 7 | and the number of columns in the array. It then loops through each row and column of the array, printing the contents of 8 | each element to the console. The function uses pointer arithmetic to access the elements of the array. 9 | 10 | Note that the use of #include is generally discouraged in production code, as it includes many headers 11 | that are not needed and can lead to longer compile times. It is better to include only the necessary headers for the 12 | specific functionality required in the program. 13 | */ 14 | 15 | #include 16 | 17 | using namespace std; 18 | 19 | // Function to print a 2D array 20 | void printArray(int *array, int row, int col) { 21 | // Loop through each row 22 | for (int i = 0; i < row; i++) { 23 | // Loop through each column 24 | for (int j = 0; j < col; j++) { 25 | // Print the element at row i, column j 26 | cout << *((array + i * col) + j) << " "; 27 | } 28 | // Print a newline character after each row is printed 29 | cout << endl; 30 | } 31 | 32 | // Print an additional newline character after the entire array is printed 33 | cout << endl; 34 | } 35 | 36 | int main() { 37 | int row, col; 38 | 39 | // Prompt the user to enter the dimensions of the array 40 | cin >> row >> col; 41 | 42 | // Declare a 2D array with the given dimensions 43 | int array[row][col]; 44 | 45 | // Loop through each row and column, and prompt the user to enter the corresponding element 46 | for (int i = 0; i < row; i++) { 47 | for (int j = 0; j < col; j++) { 48 | cin >> array[i][j]; 49 | } 50 | } 51 | 52 | // Pass the array to the printArray function and print the contents of the array 53 | printArray(*array, row, col); 54 | 55 | // Exit the program with status code 0 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Array/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printArray(int array[], int size) 5 | { 6 | for (int i = 0; i < size; i++) 7 | { 8 | cout << array[i] << " "; 9 | } 10 | cout << endl; 11 | } 12 | 13 | int main() 14 | { 15 | 16 | int size; 17 | cin >> size; 18 | 19 | int array[size]; 20 | 21 | for (int i = 0; i < size; i++) 22 | { 23 | cin >> array[i]; 24 | } 25 | 26 | cout << "Before Sort: "; 27 | printArray(array, size); 28 | cout << endl; 29 | 30 | 31 | // Bubble Sort Implementation 32 | for(int i =1; iarray[j+1]){ 37 | int temp = array[j]; 38 | array[j]=array[j+1]; 39 | array[j+1]=temp; 40 | flag = 1; 41 | } 42 | printArray(array, size); 43 | } 44 | cout<< endl; 45 | if(flag == 0) break; 46 | } 47 | 48 | cout << "After Sort: "; 49 | printArray(array, size); 50 | cout << endl; 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Array/README.md: -------------------------------------------------------------------------------- 1 | # **Array** 2 | 3 | An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). 4 | 5 | Array 6 | 7 | Source: [GeeksforGeeks](https://www.geeksforgeeks.org/array-data-structure/) 8 | 9 | ## Code Examples 10 | 11 | - **[2DArray.cpp](2DArray.cpp)** 12 | - **[BubbleSort.cpp](BubbleSort.cpp)** 13 | - **[binarySearch.cpp](binarySearch.cpp)** 14 | - **[countingSort.cpp](countingSort.cpp)** 15 | - **[insertionSort.cpp](insertionSort.cpp)** 16 | - **[linearSearch.cpp](linearSearch.cpp)** 17 | -------------------------------------------------------------------------------- /Array/binarySearch.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This program performs a binary search on a user-defined array of integers to find a specific value. The program prompts 3 | the user to enter the size of the array and then prompts the user to enter the elements of the array. It then prompts 4 | the user to enter the value to search for, and calls the binarySearch function to find the index of the search value in 5 | the array. 6 | 7 | The binarySearch function takes four arguments: an array of integers, the value to search for, the lower bound of the 8 | current range being searched, and the upper bound of the current range being searched. The function recursively searches 9 | the current range of the array for the search value by dividing the range in half at each iteration, and checking if the 10 | midpoint contains the search value. If the search value is not found, the function returns -1. 11 | 12 | Note that the use of #include is generally discouraged in production code, as it includes many headers 13 | that are not needed and can lead to longer compile times. It is better to include only the necessary headers for the 14 | specific functionality required in the program. 15 | */ 16 | 17 | #include 18 | using namespace std; 19 | 20 | // Function to perform binary search on an array 21 | int binarySearch(int array[], int x, int lb, int ub) { 22 | // Check if the lower bound is less than or equal to the upper bound 23 | if (lb <= ub) { 24 | // Calculate the midpoint of the current range 25 | int mid = (lb + ub) / 2; 26 | 27 | // If the midpoint contains the search value, return the index 28 | if (x == array[mid]) return mid; // Centre 29 | 30 | // If the search value is greater than the midpoint, search the right half of the array 31 | else if (x > array[mid]) 32 | binarySearch(array, x, mid + 1, ub); // Right 33 | 34 | // If the search value is less than the midpoint, search the left half of the array 35 | else 36 | binarySearch(array, x, lb, mid - 1); // Left 37 | } 38 | // If the search value is not found, return -1 39 | else 40 | return -1; 41 | } 42 | 43 | int main() { 44 | int size; 45 | 46 | // Prompt the user to enter the size of the array 47 | cin >> size; 48 | 49 | // Declare an array of the given size 50 | int array[size]; 51 | 52 | // Prompt the user to enter the elements of the array 53 | for (int i = 0; i < size; i++) { 54 | cin >> array[i]; 55 | } 56 | 57 | // Prompt the user to enter the value to search for 58 | int checkValue; 59 | cout << "Please Enter the Value You want to Search: "; 60 | cin >> checkValue; 61 | 62 | // Call the binarySearch function to find the index of the search value in the array 63 | int indexNumber; 64 | indexNumber = binarySearch(array, checkValue, 0, size - 1); 65 | 66 | // If the search value is found, print the index and position of the value 67 | if (indexNumber != -1) { 68 | cout << "Index No: " << indexNumber << " Position No: " << indexNumber + 1 << endl; 69 | } 70 | // If the search value is not found, print a message indicating that it was not found 71 | else { 72 | cout << " Not Found"; 73 | } 74 | 75 | // Exit the program with status code 0 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /Array/countingSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void printArray(int array[], int n){ 4 | for(int i=0;i> size; 14 | 15 | int array[size]; 16 | 17 | for (int i = 0; i < size; i++) 18 | { 19 | cin >> array[i]; 20 | } 21 | 22 | cout<<"Before Sort: "; 23 | printArray(array,size); 24 | 25 | // Step 1 : Finding Max 26 | int max = INT_MIN; 27 | for (int i = 0; i < size; i++) 28 | { 29 | if (array[i] > max) 30 | max = array[i]; 31 | } 32 | 33 | //Step 2 // Initialization of 'count' array 34 | int count[max+1]; 35 | 36 | for(int i=0; i<=max;i++){ 37 | count[i]=0; 38 | } 39 | 40 | //Step 3 // Frequency Calculation 41 | for(int i=0; i Backward Traversal of Basic Array 55 | int final[size]; 56 | for(int i=size-1;i>=0;i-- ){ 57 | count[array[i]]--; 58 | int k = count[array[i]]; 59 | final[k] = array[i]; 60 | } 61 | 62 | cout<<"After Sort: "; 63 | printArray(final,size); 64 | 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /Array/insertionSort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This code reads an integer size and an array of size integers from standard input, sorts the array using the Insertion 3 | Sort algorithm, and prints the sorted array. Here are the details: 4 | 5 | The printArray() function prints an array of integers. 6 | The main() function reads the input values for the array, prints the array before sorting, performs the insertion sort 7 | algorithm, and prints the sorted array. The insertion sort algorithm starts from the second element of the array and 8 | compares it with all the elements in the sorted sub-array. If the element is smaller than the key value, it is moved one 9 | position ahead. This process is repeated until the key value is placed in its correct position in the sorted sub-array. 10 | The function printArray() is called after each iteration of the insertion sort algorithm to print the array. 11 | */ 12 | 13 | #include 14 | using namespace std; 15 | 16 | // Function to print an array 17 | void printArray(int array[], int size) { 18 | for (int i = 0; i < size; i++) { 19 | cout << array[i] << " "; 20 | } 21 | cout << endl; 22 | } 23 | 24 | int main() { 25 | int size; 26 | cin >> size; 27 | 28 | // Declare an integer array of given size 29 | int array[size]; 30 | 31 | // Read the input values for the array 32 | for (int i = 0; i < size; i++) { 33 | cin >> array[i]; 34 | } 35 | 36 | // Print the input array 37 | cout << "Before Sorting : "; 38 | printArray(array, size); 39 | cout << endl; 40 | 41 | // Insertion Sort 42 | for (int i = 1; i < size; i++) { 43 | // Store the value of array[i] in key variable 44 | int key = array[i]; 45 | 46 | // Initialize j with i-1, and move the elements of array[0...i-1], that are greater than key, 47 | // to one position ahead of their current position 48 | int j = i - 1; 49 | while (array[j] > key && j >= 0) { 50 | array[j + 1] = array[j]; 51 | j--; 52 | } 53 | 54 | // Place the key value in its correct position in the sorted sub-array 55 | array[j + 1] = key; 56 | 57 | // Print the array after each iteration 58 | printArray(array, size); 59 | } 60 | 61 | // Print the sorted array 62 | cout << endl; 63 | cout << "After Sorting : "; 64 | printArray(array, size); 65 | cout << endl; 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Array/linearSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | int size; 8 | cin >> size; 9 | 10 | int array[size]; 11 | 12 | for (int i = 0; i < size; i++) 13 | { 14 | cin >> array[i]; 15 | } 16 | 17 | char c; 18 | cout << "Do You want to Search: (Y/N) "; 19 | cin >> c; 20 | 21 | while (toupper(c) == 'Y') 22 | { 23 | 24 | int checkvalue; 25 | cout << "Please enter the value you want to search:"; 26 | cin >> checkvalue; 27 | 28 | int flag = 0; 29 | 30 | for (int i = 0; i < size; i++) 31 | { 32 | if (array[i] == checkvalue) 33 | { 34 | flag = 1; 35 | cout << "Index No: " << i << " Position :" << i + 1 << endl; 36 | } 37 | } 38 | 39 | if (flag == 0) 40 | cout << "NOT FOUND" << endl; 41 | 42 | cout << "Do You want to Continue Searching: (Y/N) "; 43 | cin >> c; 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /BST/README.md: -------------------------------------------------------------------------------- 1 | # **Binary Search Tree** 2 | 3 | Binary Search Tree is a node-based binary tree data structure which has the following properties: 4 | 5 | The left subtree of a node contains only nodes with keys lesser than the node’s key. 6 | The right subtree of a node contains only nodes with keys greater than the node’s key. 7 | The left and right subtree each must also be a binary search tree. 8 | 9 | Binary Search Tree 10 | 11 | Source: [GeeksforGeeks](https://www.geeksforgeeks.org/binary-search-tree-data-structure/) 12 | 13 | ## Code Examples 14 | 15 | - **[bst.cpp](bst.cpp)** 16 | -------------------------------------------------------------------------------- /BST/bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class treeNode 6 | { 7 | public: 8 | int data; 9 | treeNode *leftChild; 10 | treeNode *rightChild; 11 | 12 | treeNode(int value) 13 | { 14 | data = value; 15 | leftChild = NULL; 16 | rightChild = NULL; 17 | } 18 | }; 19 | 20 | void printTree(treeNode *root, int level); 21 | void spacePrint(int level); 22 | 23 | void inOrder(treeNode *root, string &chk) // Left Root Right 24 | { 25 | 26 | if (root == NULL) 27 | return; 28 | 29 | inOrder(root->leftChild, chk); 30 | chk += (to_string(root->data) + " "); 31 | inOrder(root->rightChild, chk); 32 | } 33 | 34 | void preOrder(treeNode *root, string &chk) // Root Left Right 35 | { 36 | 37 | if (root == NULL) 38 | return; 39 | 40 | chk += to_string(root->data); 41 | preOrder(root->leftChild, chk); 42 | preOrder(root->rightChild, chk); 43 | } 44 | 45 | void postOrder(treeNode *root, string &chk) // Left Right Root 46 | { 47 | 48 | if (root == NULL) 49 | return; 50 | 51 | postOrder(root->leftChild, chk); 52 | postOrder(root->rightChild, chk); 53 | chk += to_string(root->data); 54 | } 55 | 56 | void printTree(treeNode *root, int level) 57 | { 58 | if (root == NULL) // WHEN THE TREE IS EMPTY 59 | { 60 | return; 61 | } 62 | 63 | if (root->leftChild == NULL && root->rightChild == NULL) // CASE 1 64 | { 65 | cout << root->data << endl; 66 | return; 67 | } 68 | 69 | else // CASE 2 70 | { 71 | cout << endl; 72 | spacePrint(level); 73 | cout << "Root: " << root->data << endl; 74 | } 75 | 76 | if (root->leftChild != NULL) 77 | { 78 | spacePrint(level); 79 | cout << "Left: "; 80 | printTree(root->leftChild, level + 1); 81 | } 82 | 83 | if (root->rightChild != NULL) 84 | { 85 | spacePrint(level); 86 | cout << "Right: "; 87 | printTree(root->rightChild, level + 1); 88 | } 89 | } 90 | 91 | void spacePrint(int level) 92 | { 93 | 94 | for (int i = 0; i < level; i++) 95 | { 96 | cout << " "; 97 | } 98 | } 99 | 100 | int levelOrderTraversal(treeNode *root, string &chk, int k) 101 | { 102 | 103 | if (root == NULL) 104 | { 105 | return -1; 106 | } 107 | int level = 0; 108 | queue q; 109 | q.push(root); 110 | q.push(NULL); 111 | int max = -9999; 112 | 113 | while (!q.empty()) 114 | { 115 | treeNode *chkNode = q.front(); 116 | q.pop(); 117 | if (chkNode != NULL) 118 | { 119 | if (level == k) 120 | { 121 | if (max < chkNode->data) 122 | { 123 | max = chkNode->data; 124 | } 125 | } 126 | cout << chkNode->data << " "; 127 | chk += to_string(chkNode->data); 128 | if (chkNode->leftChild != NULL) 129 | { 130 | q.push(chkNode->leftChild); 131 | } 132 | if (chkNode->rightChild != NULL) 133 | { 134 | q.push(chkNode->rightChild); 135 | } 136 | } 137 | else 138 | { 139 | if (!q.empty()) 140 | { 141 | q.push(NULL); 142 | level++; 143 | } 144 | } 145 | } 146 | 147 | return max; 148 | } 149 | 150 | void printLeaves(treeNode *root) 151 | { 152 | if (root == NULL) 153 | return; 154 | if (root->leftChild == NULL && root->rightChild == NULL) 155 | { 156 | cout << root->data << " "; 157 | return; 158 | } 159 | printLeaves(root->leftChild); 160 | printLeaves(root->rightChild); 161 | } 162 | 163 | void printLeftNonLeaves(treeNode *root) 164 | { 165 | 166 | if (root == NULL) 167 | return; 168 | if (root->leftChild != NULL) 169 | { 170 | cout << root->data << " "; 171 | printLeftNonLeaves(root->leftChild); 172 | } 173 | else if (root->rightChild != NULL) 174 | { 175 | cout << root->data << " "; 176 | printLeftNonLeaves(root->rightChild); 177 | } 178 | } 179 | 180 | void printRightNonLeaves(treeNode *root) 181 | { 182 | if (root == NULL) 183 | return; 184 | if (root->rightChild != NULL) 185 | { 186 | cout << root->data << " "; 187 | printLeftNonLeaves(root->rightChild); 188 | } 189 | else if (root->leftChild != NULL) 190 | { 191 | cout << root->data << " "; 192 | printLeftNonLeaves(root->leftChild); 193 | } 194 | } 195 | 196 | void boundaryTraversal(treeNode *root) 197 | { 198 | 199 | if (root == NULL) 200 | return; 201 | cout << root->data << " "; 202 | 203 | // LB Non-Leaves 204 | printLeftNonLeaves(root->leftChild); 205 | // LB Leaves 206 | printLeaves(root->leftChild); 207 | // RB Leaves 208 | printLeaves(root->rightChild); 209 | // RB Non-Leaves 210 | printRightNonLeaves(root->rightChild); 211 | } 212 | 213 | treeNode *insertionBST(treeNode *root, int value) 214 | { 215 | treeNode *newNode = new treeNode(value); 216 | 217 | if (root == NULL) 218 | { 219 | root = newNode; 220 | return root; 221 | } 222 | // value < root -> data 223 | if (value < root->data) 224 | { 225 | root->leftChild = insertionBST(root->leftChild, value); 226 | } 227 | // value > root-> data 228 | else if (value > root->data) 229 | { 230 | root->rightChild = insertionBST(root->rightChild, value); 231 | } 232 | 233 | return root; 234 | } 235 | 236 | treeNode *searchBST(treeNode *root, int value) 237 | { 238 | 239 | if (root == NULL) 240 | { 241 | return NULL; 242 | } 243 | 244 | if (root->data == value) 245 | { 246 | cout << root->data; 247 | return root; 248 | } 249 | 250 | else if (value < root->data) 251 | { 252 | cout << root->data << " -> "; 253 | searchBST(root->leftChild, value); 254 | } 255 | 256 | else 257 | { 258 | cout << root->data << " -> "; 259 | searchBST(root->rightChild, value); 260 | } 261 | } 262 | 263 | treeNode *inordersucc(treeNode *root) 264 | { 265 | treeNode *curr = root; 266 | while (curr->leftChild != NULL) 267 | { 268 | curr = curr->leftChild; 269 | } 270 | return curr; 271 | } 272 | 273 | treeNode *deletionBST(treeNode *root, int value) 274 | { 275 | if (value < root->data) 276 | { 277 | root->leftChild = deletionBST(root->leftChild, value); 278 | } 279 | else if (value > root->data) 280 | { 281 | root->rightChild = deletionBST(root->rightChild, value); 282 | } 283 | 284 | else 285 | { 286 | // Implementation of 3 case 287 | if (root->leftChild == NULL) 288 | { // CASE 1 | CASE 2 289 | treeNode *temp = root->rightChild; 290 | free(root); 291 | return temp; 292 | } 293 | 294 | else if (root->rightChild == NULL) 295 | { // CASE 2 296 | treeNode *temp = root->leftChild; 297 | free(root); 298 | return temp; 299 | } 300 | else 301 | { // CASE 3 302 | treeNode *temp = inordersucc(root->rightChild); 303 | root->data = temp->data; 304 | root->rightChild = deletionBST(root->rightChild, temp->data); 305 | } 306 | 307 | return root; 308 | } 309 | } 310 | 311 | void zigzagTraversal(treeNode *root) 312 | { 313 | 314 | stack currentLevel; 315 | stack nextLevel; 316 | 317 | bool lefttoRight = true; 318 | 319 | currentLevel.push(root); 320 | 321 | while (!currentLevel.empty()) 322 | { 323 | treeNode *x = currentLevel.top(); 324 | currentLevel.pop(); 325 | 326 | cout << x->data << " "; 327 | 328 | if (lefttoRight) 329 | { 330 | if (x->leftChild) 331 | { 332 | nextLevel.push(x->leftChild); 333 | } 334 | if (x->rightChild) 335 | { 336 | nextLevel.push(x->rightChild); 337 | } 338 | } 339 | 340 | else 341 | { 342 | if (x->rightChild) 343 | { 344 | nextLevel.push(x->rightChild); 345 | } 346 | if (x->leftChild) 347 | { 348 | nextLevel.push(x->leftChild); 349 | } 350 | } 351 | 352 | if(currentLevel.empty()){ 353 | lefttoRight = !lefttoRight; 354 | swap(currentLevel,nextLevel); 355 | } 356 | 357 | } 358 | } 359 | 360 | int main() 361 | { 362 | int n; 363 | cin >> n; 364 | treeNode *root = NULL; 365 | for (int i = 0; i < n; i++) 366 | { 367 | int value; 368 | cin >> value; 369 | root = insertionBST(root, value); 370 | } 371 | string traversal = ""; 372 | inOrder(root, traversal); 373 | cout << traversal << endl; 374 | 375 | zigzagTraversal(root); 376 | // int key; 377 | // cin >> key; 378 | // if (searchBST(root, key) == NULL) 379 | // { 380 | // cout << endl 381 | // << "Value does not exits in the BST"; 382 | // } 383 | // else 384 | // { 385 | // cout << endl 386 | // << "Value exits in the BST"; 387 | // } 388 | 389 | // root = deletionBST(root, key); 390 | // string after = ""; 391 | // inOrder(root, after); 392 | // cout << after; 393 | 394 | cout << endl 395 | << endl; 396 | return 0; 397 | } 398 | /* 399 | 10 400 | 11 5 9 43 34 1 2 7 8 21 401 | */ -------------------------------------------------------------------------------- /Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # **Binary Tree** 2 | 3 | Binary Tree is a special data structure used for data storage purposes. A binary tree has the benefits of both arrays and linked lists. Binary trees are a non-linear data structure. Unlike arrays, linked lists, or stacks, which are linear data structures, trees are hierarchical data structures. A tree data structure can be defined recursively (locally) as a collection of nodes (starting at a root node), where each node is a data structure consisting of a value, together with a list of references to nodes (the "children"), with the constraints that no reference is duplicated, and none points to the root. 4 | 5 | Binary Tree 6 | 7 | Source: [GeeksforGeeks](https://www.geeksforgeeks.org/binary-tree-data-structure/) 8 | 9 | ## Code Examples 10 | 11 | - **[binaryTree.cpp](binaryTree.cpp)** 12 | - **[preorderInordertoBinary.cpp](preorderInordertoBinary.cpp)** 13 | -------------------------------------------------------------------------------- /Binary Tree/binaryTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class treeNode 6 | { 7 | public: 8 | int data; 9 | treeNode *leftChild; 10 | treeNode *rightChild; 11 | 12 | treeNode(int value) 13 | { 14 | data = value; 15 | leftChild = NULL; 16 | rightChild = NULL; 17 | } 18 | }; 19 | 20 | void printTree(treeNode *root, int level); 21 | void spacePrint(int level); 22 | 23 | void inOrder(treeNode *root, string &chk) // Left Root Right 24 | { 25 | 26 | if (root == NULL) 27 | return; 28 | 29 | inOrder(root->leftChild, chk); 30 | chk += to_string(root->data); 31 | inOrder(root->rightChild, chk); 32 | } 33 | 34 | void preOrder(treeNode *root, string &chk) // Root Left Right 35 | { 36 | 37 | if (root == NULL) 38 | return; 39 | 40 | chk += to_string(root->data); 41 | preOrder(root->leftChild, chk); 42 | preOrder(root->rightChild, chk); 43 | } 44 | 45 | void postOrder(treeNode *root, string &chk) // Left Right Root 46 | { 47 | 48 | if (root == NULL) 49 | return; 50 | 51 | postOrder(root->leftChild, chk); 52 | postOrder(root->rightChild, chk); 53 | chk += to_string(root->data); 54 | } 55 | 56 | void printTree(treeNode *root, int level) 57 | { 58 | if (root == NULL) // WHEN THE TREE IS EMPTY 59 | { 60 | return; 61 | } 62 | 63 | if (root->leftChild == NULL && root->rightChild == NULL) // CASE 1 64 | { 65 | cout << root->data << endl; 66 | return; 67 | } 68 | 69 | else // CASE 2 70 | { 71 | cout << endl; 72 | spacePrint(level); 73 | cout << "Root: " << root->data << endl; 74 | } 75 | 76 | if (root->leftChild != NULL) 77 | { 78 | spacePrint(level); 79 | cout << "Left: "; 80 | printTree(root->leftChild, level + 1); 81 | } 82 | 83 | if (root->rightChild != NULL) 84 | { 85 | spacePrint(level); 86 | cout << "Right: "; 87 | printTree(root->rightChild, level + 1); 88 | } 89 | } 90 | 91 | void spacePrint(int level) 92 | { 93 | 94 | for (int i = 0; i < level; i++) 95 | { 96 | cout << " "; 97 | } 98 | } 99 | 100 | int levelOrderTraversal(treeNode *root, string &chk, int k) 101 | { 102 | 103 | if (root == NULL) 104 | { 105 | return -1; 106 | } 107 | int level = 0; 108 | queue q; 109 | q.push(root); 110 | q.push(NULL); 111 | int max = -9999; 112 | 113 | while (!q.empty()) 114 | { 115 | treeNode *chkNode = q.front(); 116 | q.pop(); 117 | if (chkNode != NULL) 118 | { 119 | if (level == k) 120 | { 121 | if (max < chkNode->data) 122 | { 123 | max = chkNode->data; 124 | } 125 | } 126 | cout << chkNode->data << " "; 127 | chk += to_string(chkNode->data); 128 | if (chkNode->leftChild != NULL) 129 | { 130 | q.push(chkNode->leftChild); 131 | } 132 | if (chkNode->rightChild != NULL) 133 | { 134 | q.push(chkNode->rightChild); 135 | } 136 | } 137 | else 138 | { 139 | if (!q.empty()) 140 | { 141 | q.push(NULL); 142 | level++; 143 | } 144 | } 145 | } 146 | 147 | return max; 148 | } 149 | 150 | void printLeaves(treeNode *root) 151 | { 152 | if (root == NULL) 153 | return; 154 | if (root->leftChild == NULL && root->rightChild == NULL) 155 | { 156 | cout << root->data << " "; 157 | return; 158 | } 159 | printLeaves(root->leftChild); 160 | printLeaves(root->rightChild); 161 | } 162 | 163 | void printLeftNonLeaves(treeNode *root) 164 | { 165 | 166 | if (root == NULL) 167 | return; 168 | if (root->leftChild != NULL) 169 | { 170 | cout << root->data << " "; 171 | printLeftNonLeaves(root->leftChild); 172 | } 173 | else if (root->rightChild != NULL) 174 | { 175 | cout << root->data << " "; 176 | printLeftNonLeaves(root->rightChild); 177 | } 178 | } 179 | 180 | void printRightNonLeaves(treeNode *root) 181 | { 182 | if (root == NULL) 183 | return; 184 | if (root->rightChild != NULL) 185 | { 186 | cout << root->data << " "; 187 | printLeftNonLeaves(root->rightChild); 188 | } 189 | else if (root->leftChild != NULL) 190 | { 191 | cout << root->data << " "; 192 | printLeftNonLeaves(root->leftChild); 193 | } 194 | } 195 | 196 | void boundaryTraversal(treeNode *root) 197 | { 198 | 199 | if (root == NULL) 200 | return; 201 | cout << root->data << " "; 202 | 203 | // LB Non-Leaves 204 | printLeftNonLeaves(root->leftChild); 205 | // LB Leaves 206 | printLeaves(root->leftChild); 207 | // RB Leaves 208 | printLeaves(root->rightChild); 209 | // RB Non-Leaves 210 | printRightNonLeaves(root->rightChild); 211 | } 212 | 213 | int main() 214 | { 215 | 216 | int n; 217 | cin >> n; 218 | treeNode *allNodes[n]; 219 | 220 | for (int i = 0; i < n; i++) 221 | { 222 | allNodes[i] = new treeNode(-1); 223 | } 224 | 225 | for (int i = 0; i < n; i++) 226 | { 227 | int value, left, right; 228 | cin >> value >> left >> right; 229 | allNodes[i]->data = value; 230 | if (left > n - 1 || right > n - 1) 231 | { 232 | cout << "Invalid Index" << endl; 233 | break; 234 | } 235 | if (left != -1) 236 | { 237 | allNodes[i]->leftChild = allNodes[left]; 238 | } 239 | if (right != -1) 240 | { 241 | allNodes[i]->rightChild = allNodes[right]; 242 | } 243 | } 244 | 245 | // printTree(allNodes[0], 0); 246 | 247 | string inordertraversal = ""; 248 | string preordertraversal = ""; 249 | string postordertraversal = ""; 250 | string levelordertraversal = ""; 251 | 252 | // inOrder(allNodes[0],inordertraversal); 253 | // preOrder(allNodes[0],preordertraversal); 254 | // postOrder(allNodes[0],postordertraversal); 255 | // int maxValueAtK = levelOrderTraversal(allNodes[0], levelordertraversal, 2); 256 | 257 | boundaryTraversal(allNodes[0]); 258 | 259 | // cout<< "Inorder Traversal : " << inordertraversal << endl; 260 | // cout<< "Preorder Traversal : " << preordertraversal << endl; 261 | // cout<< "Postorder Traversal : " << postordertraversal << endl; 262 | 263 | cout << endl 264 | << endl; 265 | return 0; 266 | } 267 | /* 268 | 9 269 | 0 1 2 270 | 1 3 4 271 | 2 5 6 272 | 3 -1 -1 273 | 4 -1 -1 274 | 5 7 8 275 | 6 -1 -1 276 | 7 -1 -1 277 | 8 -1 -1 278 | 279 | Root: 0 280 | Left: 281 | Root: 1 282 | Left: 3 283 | Right: 4 284 | Right: 285 | Root: 2 286 | Left: 287 | Root: 5 288 | Left: 7 289 | Right: 8 290 | Right:6 291 | */ -------------------------------------------------------------------------------- /Binary Tree/preorderInordertoBinary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class treeNode 6 | { 7 | public: 8 | int data; 9 | treeNode *leftChild; 10 | treeNode *rightChild; 11 | 12 | treeNode(int value) 13 | { 14 | data = value; 15 | leftChild = NULL; 16 | rightChild = NULL; 17 | } 18 | }; 19 | 20 | void printTree(treeNode *root, int level); 21 | void spacePrint(int level); 22 | 23 | void inOrder(treeNode *root, string &chk) // Left Root Right 24 | { 25 | 26 | if (root == NULL) 27 | return; 28 | 29 | inOrder(root->leftChild, chk); 30 | chk += to_string(root->data); 31 | inOrder(root->rightChild, chk); 32 | } 33 | 34 | void preOrderTraversal(treeNode *root, string &chk) // Root Left Right 35 | { 36 | 37 | if (root == NULL) 38 | return; 39 | 40 | chk += to_string(root->data); 41 | preOrderTraversal(root->leftChild, chk); 42 | preOrderTraversal(root->rightChild, chk); 43 | } 44 | 45 | void postOrder(treeNode *root, string &chk) // Left Right Root 46 | { 47 | 48 | if (root == NULL) 49 | return; 50 | 51 | postOrder(root->leftChild, chk); 52 | postOrder(root->rightChild, chk); 53 | chk += to_string(root->data); 54 | } 55 | 56 | void printTree(treeNode *root, int level) 57 | { 58 | if (root == NULL) // WHEN THE TREE IS EMPTY 59 | { 60 | return; 61 | } 62 | 63 | if (root->leftChild == NULL && root->rightChild == NULL) // CASE 1 64 | { 65 | cout << root->data << endl; 66 | return; 67 | } 68 | 69 | else // CASE 2 70 | { 71 | cout << endl; 72 | spacePrint(level); 73 | cout << "Root: " << root->data << endl; 74 | } 75 | 76 | if (root->leftChild != NULL) 77 | { 78 | spacePrint(level); 79 | cout << "Left: "; 80 | printTree(root->leftChild, level + 1); 81 | } 82 | 83 | if (root->rightChild != NULL) 84 | { 85 | spacePrint(level); 86 | cout << "Right: "; 87 | printTree(root->rightChild, level + 1); 88 | } 89 | } 90 | 91 | void spacePrint(int level) 92 | { 93 | 94 | for (int i = 0; i < level; i++) 95 | { 96 | cout << " "; 97 | } 98 | } 99 | 100 | int searchInorder(int inOrder[], int current, int start, int end) 101 | { 102 | for (int i = start; i <= end; i++) 103 | { 104 | if (inOrder[i] == current) 105 | { 106 | return i; 107 | } 108 | } 109 | 110 | return -1; 111 | } 112 | 113 | treeNode *buildTreePreIn(int preOrder[], int inOrder[], int start, int end) 114 | { 115 | 116 | static int id = 0; 117 | 118 | int current = preOrder[id]; 119 | id++; 120 | treeNode *newNode = new treeNode(current); 121 | if (start == end) 122 | { 123 | return newNode; 124 | } 125 | int pos = searchInorder(inOrder, current, start, end); 126 | newNode->leftChild = buildTreePreIn(preOrder, inOrder, start, pos - 1); 127 | newNode->rightChild = buildTreePreIn(preOrder, inOrder, pos + 1, end); 128 | 129 | return newNode; 130 | } 131 | 132 | int main() 133 | { 134 | int n; 135 | cin >> n; 136 | int preOrder[n], inOrder[n]; 137 | for (int i = 0; i < n; i++) 138 | { 139 | cin >> preOrder[i]; 140 | } 141 | for (int i = 0; i < n; i++) 142 | { 143 | cin >> inOrder[i]; 144 | } 145 | 146 | treeNode *root = buildTreePreIn(preOrder, inOrder, 0, n - 1); 147 | string chkPre = ""; 148 | preOrderTraversal(root, chkPre); 149 | cout<< chkPre << endl << endl; 150 | return 0; 151 | } 152 | /* 153 | 9 154 | 0 1 3 4 2 5 7 8 6 155 | 3 1 4 0 7 5 8 2 6 156 | 157 | 158 | 9 159 | 0 1 2 160 | 1 3 4 161 | 2 5 6 162 | 3 -1 -1 163 | 4 -1 -1 164 | 5 7 8 165 | 6 -1 -1 166 | 7 -1 -1 167 | 8 -1 -1 168 | 169 | Root: 0 170 | Left: 171 | Root: 1 172 | Left: 3 173 | Right: 4 174 | Right: 175 | Root: 2 176 | Left: 177 | Root: 5 178 | Left: 7 179 | Right: 8 180 | Right:6 181 | */ -------------------------------------------------------------------------------- /Graph/README.md: -------------------------------------------------------------------------------- 1 | # **Graph** 2 | 3 | Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph can be defined as, a Graph consists of a finite set of vertices(or nodes) and set of Edges which connect a pair of nodes. 4 | 5 | Graph 6 | 7 | Source: [GeeksforGeeks](https://www.geeksforgeeks.org/graph-data-structure-and-algorithms/) 8 | 9 | ## Code Examples 10 | 11 | - **[graph.cpp](graph.cpp)** 12 | -------------------------------------------------------------------------------- /Graph/graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef pair edgeWeightPair; 6 | class Graph 7 | { 8 | int V; 9 | list *adj; 10 | 11 | 12 | public: 13 | Graph(int V) 14 | { 15 | this->V = V; 16 | adj = new list[V]; 17 | } 18 | 19 | void addEdge(int u, int v, int w) 20 | { 21 | adj[u].push_back(make_pair(v, w)); 22 | adj[v].push_back(make_pair(u, w)); 23 | } 24 | 25 | void printNeighbour(int chk) 26 | { 27 | cout << chk << ":"; 28 | for (auto i = adj[chk].begin(); i != adj[chk].end(); i++) 29 | { 30 | cout << " (" << (*i).first << ", " << (*i).second << ")"; 31 | } 32 | } 33 | 34 | void BFS(int source) 35 | { 36 | vectorvisited(V,false); 37 | queue Q; 38 | visited[source] = true; 39 | Q.push(source); 40 | 41 | while (!Q.empty()) 42 | { 43 | int u = Q.front(); 44 | cout << u << " "; 45 | Q.pop(); 46 | for (auto element : adj[u]) 47 | { 48 | int v = element.first; 49 | if (visited[v] != true) 50 | { 51 | visited[v] = true; 52 | Q.push(v); 53 | } 54 | } 55 | } 56 | } 57 | 58 | void DFS(int source) 59 | { 60 | static vectorvisited(V,false); 61 | visited[source] = true; 62 | cout << source << " "; 63 | 64 | for (auto element : adj[source]) 65 | { 66 | int v = element.first; 67 | if (visited[v] != true) 68 | { 69 | DFS(v); 70 | } 71 | } 72 | } 73 | }; 74 | 75 | int main() 76 | { 77 | int V, E, source; 78 | cin >> V >> E >> source; 79 | Graph g(V); 80 | for (int i = 0; i < E; i++) 81 | { 82 | int u, v, w; 83 | cin >> u >> v >> w; 84 | g.addEdge(u, v, w); 85 | } 86 | 87 | for (int i = 0; i < V; i++) 88 | { 89 | g.printNeighbour(i); 90 | cout << endl; 91 | } 92 | 93 | cout << endl 94 | << endl; 95 | 96 | g.BFS(source); 97 | cout << endl 98 | << endl; 99 | g.DFS(source); 100 | 101 | cout << endl 102 | << endl; 103 | return 0; 104 | } 105 | 106 | /* 107 | Bi-Directional Weighted Graph 108 | 7 10 0 109 | 0 1 7 110 | 0 2 1 111 | 0 5 3 112 | 1 3 11 113 | 2 3 3 114 | 3 6 1 115 | 6 5 2 116 | 6 4 4 117 | 5 4 6 118 | 2 5 8 119 | 120 | */ -------------------------------------------------------------------------------- /Hashing/README.md: -------------------------------------------------------------------------------- 1 | # **Hashing** 2 | 3 | Hashing is a technique to convert a range of key values into a range of indexes of an array. The idea is to use a hash function that converts a given key to a small integer which can be used as an index in an array. The hash function is used to map the key to an index in the array. The index is used to insert or search the key in the array. The hash function should be fast to compute and should uniformly distribute the keys and should be deterministic. 4 | 5 | A hash table uses an array to store data. The array is called the hash table. The hash function is used to map the key to an index in the hash table. The index is used to insert or search the key in the hash table. 6 | 7 | Hashing 8 | 9 | Source: [GeeksforGeeks](https://www.geeksforgeeks.org/hashing-data-structure/) 10 | 11 | ## Code Examples 12 | 13 | - **[VerticalOrder.cpp](VerticalOrder.cpp)** 14 | - **[freq.cpp](freq.cpp)** 15 | - **[hashing.cpp](hashing.cpp)** 16 | -------------------------------------------------------------------------------- /Hashing/VerticalOrder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class treeNode 6 | { 7 | public: 8 | int data; 9 | treeNode *leftChild; 10 | treeNode *rightChild; 11 | 12 | treeNode(int value) 13 | { 14 | data = value; 15 | leftChild = NULL; 16 | rightChild = NULL; 17 | } 18 | }; 19 | 20 | void printTree(treeNode *root, int level); 21 | void spacePrint(int level); 22 | 23 | void inOrder(treeNode *root, string &chk) // Left Root Right 24 | { 25 | 26 | if (root == NULL) 27 | return; 28 | 29 | inOrder(root->leftChild, chk); 30 | chk += to_string(root->data); 31 | inOrder(root->rightChild, chk); 32 | } 33 | 34 | void preOrder(treeNode *root, string &chk) // Root Left Right 35 | { 36 | 37 | if (root == NULL) 38 | return; 39 | 40 | chk += to_string(root->data); 41 | preOrder(root->leftChild, chk); 42 | preOrder(root->rightChild, chk); 43 | } 44 | 45 | void postOrder(treeNode *root, string &chk) // Left Right Root 46 | { 47 | 48 | if (root == NULL) 49 | return; 50 | 51 | postOrder(root->leftChild, chk); 52 | postOrder(root->rightChild, chk); 53 | chk += to_string(root->data); 54 | } 55 | 56 | void printTree(treeNode *root, int level) 57 | { 58 | if (root == NULL) // WHEN THE TREE IS EMPTY 59 | { 60 | return; 61 | } 62 | 63 | if (root->leftChild == NULL && root->rightChild == NULL) // CASE 1 64 | { 65 | cout << root->data << endl; 66 | return; 67 | } 68 | 69 | else // CASE 2 70 | { 71 | cout << endl; 72 | spacePrint(level); 73 | cout << "Root: " << root->data << endl; 74 | } 75 | 76 | if (root->leftChild != NULL) 77 | { 78 | spacePrint(level); 79 | cout << "Left: "; 80 | printTree(root->leftChild, level + 1); 81 | } 82 | 83 | if (root->rightChild != NULL) 84 | { 85 | spacePrint(level); 86 | cout << "Right: "; 87 | printTree(root->rightChild, level + 1); 88 | } 89 | } 90 | 91 | void spacePrint(int level) 92 | { 93 | 94 | for (int i = 0; i < level; i++) 95 | { 96 | cout << " "; 97 | } 98 | } 99 | 100 | int levelOrderTraversal(treeNode *root, string &chk, int k) 101 | { 102 | 103 | if (root == NULL) 104 | { 105 | return -1; 106 | } 107 | int level = 0; 108 | queue q; 109 | q.push(root); 110 | q.push(NULL); 111 | int max = -9999; 112 | 113 | while (!q.empty()) 114 | { 115 | treeNode *chkNode = q.front(); 116 | q.pop(); 117 | if (chkNode != NULL) 118 | { 119 | if (level == k) 120 | { 121 | if (max < chkNode->data) 122 | { 123 | max = chkNode->data; 124 | } 125 | } 126 | cout << chkNode->data << " "; 127 | chk += to_string(chkNode->data); 128 | if (chkNode->leftChild != NULL) 129 | { 130 | q.push(chkNode->leftChild); 131 | } 132 | if (chkNode->rightChild != NULL) 133 | { 134 | q.push(chkNode->rightChild); 135 | } 136 | } 137 | else 138 | { 139 | if (!q.empty()) 140 | { 141 | q.push(NULL); 142 | level++; 143 | } 144 | } 145 | } 146 | 147 | return max; 148 | } 149 | 150 | void printLeaves(treeNode *root) 151 | { 152 | if (root == NULL) 153 | return; 154 | if (root->leftChild == NULL && root->rightChild == NULL) 155 | { 156 | cout << root->data << " "; 157 | return; 158 | } 159 | printLeaves(root->leftChild); 160 | printLeaves(root->rightChild); 161 | } 162 | 163 | void printLeftNonLeaves(treeNode *root) 164 | { 165 | 166 | if (root == NULL) 167 | return; 168 | if (root->leftChild != NULL) 169 | { 170 | cout << root->data << " "; 171 | printLeftNonLeaves(root->leftChild); 172 | } 173 | else if (root->rightChild != NULL) 174 | { 175 | cout << root->data << " "; 176 | printLeftNonLeaves(root->rightChild); 177 | } 178 | } 179 | 180 | void printRightNonLeaves(treeNode *root) 181 | { 182 | if (root == NULL) 183 | return; 184 | if (root->rightChild != NULL) 185 | { 186 | cout << root->data << " "; 187 | printLeftNonLeaves(root->rightChild); 188 | } 189 | else if (root->leftChild != NULL) 190 | { 191 | cout << root->data << " "; 192 | printLeftNonLeaves(root->leftChild); 193 | } 194 | } 195 | 196 | void boundaryTraversal(treeNode *root) 197 | { 198 | 199 | if (root == NULL) 200 | return; 201 | cout << root->data << " "; 202 | 203 | // LB Non-Leaves 204 | printLeftNonLeaves(root->leftChild); 205 | // LB Leaves 206 | printLeaves(root->leftChild); 207 | // RB Leaves 208 | printLeaves(root->rightChild); 209 | // RB Non-Leaves 210 | printRightNonLeaves(root->rightChild); 211 | } 212 | 213 | void verticalOrder(treeNode* root, int D, map> &M){ 214 | if(root==NULL) 215 | return; 216 | M[D].push_back(root->data); 217 | verticalOrder(root->leftChild, D-1, M); 218 | verticalOrder(root->rightChild, D+1, M); 219 | } 220 | 221 | int main() 222 | { 223 | 224 | int n; 225 | cin >> n; 226 | treeNode *allNodes[n]; 227 | 228 | for (int i = 0; i < n; i++) 229 | { 230 | allNodes[i] = new treeNode(-1); 231 | } 232 | 233 | for (int i = 0; i < n; i++) 234 | { 235 | int value, left, right; 236 | cin >> value >> left >> right; 237 | allNodes[i]->data = value; 238 | if (left > n - 1 || right > n - 1) 239 | { 240 | cout << "Invalid Index" << endl; 241 | break; 242 | } 243 | if (left != -1) 244 | { 245 | allNodes[i]->leftChild = allNodes[left]; 246 | } 247 | if (right != -1) 248 | { 249 | allNodes[i]->rightChild = allNodes[right]; 250 | } 251 | } 252 | 253 | // printTree(allNodes[0], 0); 254 | 255 | string inordertraversal = ""; 256 | string preordertraversal = ""; 257 | string postordertraversal = ""; 258 | string levelordertraversal = ""; 259 | 260 | // inOrder(allNodes[0],inordertraversal); 261 | // preOrder(allNodes[0],preordertraversal); 262 | // postOrder(allNodes[0],postordertraversal); 263 | // int maxValueAtK = levelOrderTraversal(allNodes[0], levelordertraversal, 2); 264 | 265 | // boundaryTraversal(allNodes[0]); 266 | 267 | // cout<< "Inorder Traversal : " << inordertraversal << endl; 268 | // cout<< "Preorder Traversal : " << preordertraversal << endl; 269 | // cout<< "Postorder Traversal : " << postordertraversal << endl; 270 | 271 | map> M; 272 | 273 | verticalOrder(allNodes[0], 0, M); 274 | 275 | // map> :: iterator it; 276 | 277 | // for(it = M.begin(); it!=M.end(); it++){ 278 | // for(int i =0; i<(it->second)) 279 | // } 280 | 281 | for(auto i : M){ 282 | cout << i.first << ": "; 283 | for(int j=0; j<(i.second).size(); j++){ 284 | cout<< (i.second)[j] << " "; 285 | } 286 | cout<< endl; 287 | } 288 | 289 | 290 | cout << endl 291 | << endl; 292 | return 0; 293 | } 294 | /* 295 | 9 296 | 0 1 2 297 | 1 3 4 298 | 2 5 6 299 | 3 -1 -1 300 | 4 -1 -1 301 | 5 7 8 302 | 6 -1 -1 303 | 7 -1 -1 304 | 8 -1 -1 305 | 306 | Root: 0 307 | Left: 308 | Root: 1 309 | Left: 3 310 | Right: 4 311 | Right: 312 | Root: 2 313 | Left: 314 | Root: 5 315 | Left: 7 316 | Right: 8 317 | Right:6 318 | */ -------------------------------------------------------------------------------- /Hashing/freq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n; 8 | cin>>n; 9 | int array[n]; 10 | 11 | for(int i=0;i> array[i]; 13 | } 14 | 15 | map M; 16 | 17 | for(int i=0;i :: iterator it; 22 | 23 | for(it=M.begin();it!=M.end();it++){ 24 | cout<< it->first << " = " << it->second << endl; 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | /* 31 | 10 32 | 2 1 1 2 2 300 4 9 4 3 5 33 | */ -------------------------------------------------------------------------------- /Hashing/hashing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | unordered_map M; 8 | 9 | M[3] = 9; 10 | M[2] = 3; 11 | M.insert(make_pair(1,4)); 12 | 13 | cout<< "key" << " | " << "Value" << endl; 14 | for(auto i: M){ 15 | cout << i.first << " | " << i.second << endl; 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Heap and Priority queue/README.md: -------------------------------------------------------------------------------- 1 | # **Heap and Priority Queue** 2 | 3 | Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. A heap is a useful data structure when it is necessary to repeatedly remove the object with the highest (or lowest) priority. The most common implementation of a priority queue is the binary heap. 4 | 5 | Priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue. 6 | 7 | Source: [OpenDSA](https://opendsa-server.cs.vt.edu/ODSA/Books/Everything/html/Heaps.html) 8 | 9 | ## Code Examples 10 | 11 | - **[heap.cpp](heap.cpp)** 12 | - **[mergeKSorted.cpp](mergeKSorted.cpp)** 13 | - **[vectorPQSTL.cpp](vectorPQSTL.cpp)** 14 | -------------------------------------------------------------------------------- /Heap and Priority queue/heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void swap(int *a, int *b){ 6 | int temp; 7 | temp = *a; 8 | *a=*b; 9 | *b=temp; 10 | } 11 | 12 | void heapify (int array[], int n, int current){ 13 | 14 | int largest = current; 15 | int leftChild = 2*current +1; 16 | int rightChild = 2*current+2; 17 | 18 | if(leftChild array[largest]){ 19 | largest = leftChild; 20 | } 21 | 22 | if(rightChild< n && array[rightChild]>array[largest]){ 23 | largest = rightChild; 24 | } 25 | 26 | if(largest!=current){ 27 | swap(array[current],array[largest]); 28 | heapify(array,n,largest); 29 | } 30 | } 31 | 32 | void printArray(int array[], int size){ 33 | cout << endl; 34 | for(int i=0; i=0; i--){ 44 | swap(array[0], array[i]); 45 | //Print the Array 46 | heapify(array,i,0); 47 | } 48 | } 49 | int main(){ 50 | 51 | int n; 52 | cin>>n; 53 | int array[n]; 54 | for(int i=0; i>array[i]; 56 | } 57 | cout << "Before Heapify: "; 58 | printArray(array,n); 59 | // Heapify 60 | int nonLeafStart =(n/2)-1; 61 | 62 | for(int i =nonLeafStart; i>=0; i--){ 63 | heapify(array, n, i); 64 | } 65 | 66 | cout << "After Heapify: "; 67 | printArray(array,n); 68 | 69 | heapsort(array,n); 70 | cout << "After the HeapSort: "; 71 | printArray(array,n); 72 | 73 | return 0; 74 | 75 | 76 | 77 | } -------------------------------------------------------------------------------- /Heap and Priority queue/mergeKSorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | typedef pair PAIR; 5 | 6 | int main() 7 | { 8 | 9 | int k; 10 | cin >> k; 11 | 12 | vector> allValues(k); 13 | 14 | //Input 15 | for (int i = 0; i < k; i++) 16 | { 17 | int size; 18 | cin >> size; 19 | allValues[i] = vector(size); 20 | for (int k = 0; k < size; k++) 21 | { 22 | cin >> allValues[i][k]; 23 | } 24 | } 25 | 26 | vectorindexTracker(k,0); 27 | priority_queue, greater> PQ; 28 | 29 | //Initialisation of PQ 30 | for(int i=0; ians; 35 | 36 | // Main Logic of Heap 37 | while(!PQ.empty()){ 38 | 39 | PAIR x = PQ.top(); 40 | PQ.pop(); 41 | 42 | ans.push_back(x.first); 43 | 44 | if(indexTracker[x.second]+1 < allValues[x.second].size()){ //increment position Validity Check 45 | PQ.push(make_pair(allValues[x.second][indexTracker[x.second]+1], x.second)); 46 | //We are Creating a pair: (increment position value, increment position array identity ) 47 | //Then Push in PQ 48 | } 49 | indexTracker[x.second]+=1; 50 | } 51 | 52 | //Print Ans 53 | for(auto element:ans){ 54 | cout<< element << " "; 55 | } 56 | 57 | cout<< endl < 2 | 3 | using namespace std; 4 | 5 | typedef pair PAIR; 6 | 7 | int main(){ 8 | 9 | vector V (3,-1); 10 | for(int i = 0;i :: iterator it; 18 | for(it=V.begin();it!=V.end();it++){ 19 | cout << *it << " "; 20 | } 21 | cout << endl << endl; 22 | //'auto' Register in range based traversal 23 | 24 | for(auto element: V){ 25 | cout<< element << " "; 26 | } 27 | cout << endl << endl; 28 | // auto register in place of iterator 29 | for(auto it=V.begin();it!=V.end();it++){ 30 | cout << *it << " "; 31 | } 32 | 33 | cout << endl << endl; 34 | 35 | 36 | priority_queue> PQ; 37 | PQ.push(make_pair(1,9)); 38 | PQ.push(make_pair(6,2)); 39 | PQ.push(make_pair(4,1)); 40 | 41 | while(!PQ.empty()){ 42 | cout << PQ.top().first << "|" << PQ.top().second << endl; 43 | PQ.pop(); 44 | } 45 | 46 | 47 | cout << endl << endl; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Linked List/README.md: -------------------------------------------------------------------------------- 1 | # **Linked List** 2 | 3 | Linked List is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. 4 | 5 | There are two types of linked list: 6 | 7 | - **Singly Linked List**: Each node of a singly linked list contains two items - the data and a reference to the next node. The last node has a reference to null. The entry point into a linked list is called the head of the list. It should be noted that head is not a separate node, but the reference to the first node. If the list is empty then the head is a null reference. 8 | 9 | - **Doubly Linked List**: Each node of a doubly linked list contains three items - the data, a reference (link) to the next node and a reference to the previous node. The last node has a reference to null. The entry point into a doubly linked list is called the head of the list. It should be noted that head is not a separate node, but the reference to the first node. If the list is empty then the head is a null reference. 10 | 11 | Linked List 12 | 13 | Source: [GeeksforGeeks](https://www.geeksforgeeks.org/data-structures/linked-list/) 14 | 15 | ## Code Examples 16 | 17 | - **[circularLinkedList.cpp](circularLinkedList.cpp)** 18 | - **[doublyLinkedList.cpp](doublyLinkedList.cpp)** 19 | - **[linearLinkedList.cpp](linearLinkedList.cpp)** 20 | -------------------------------------------------------------------------------- /Linked List/circularLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int value; 9 | Node *Next; 10 | 11 | Node(int val) 12 | { 13 | value = val; 14 | Next = NULL; 15 | } 16 | }; 17 | 18 | void insertAtTail(Node *&head, int val); 19 | void insertAtHead(Node *&head, int val); 20 | void display(Node *n); 21 | int countLength(Node *&head); 22 | void deletionAtHead(Node *&head); 23 | void deletionAtTail(Node *&head); 24 | void deletionAtSpecificPosition(Node *&head, int position); 25 | void deletionByValueUnique(Node *&head, int value); 26 | 27 | void insertAtTail(Node *&head, int val) 28 | { 29 | Node *newNode = new Node(val); 30 | 31 | if (head == NULL) 32 | { 33 | head = newNode; 34 | newNode->Next = head; 35 | return; 36 | } 37 | 38 | Node *temp = head; 39 | 40 | while (temp->Next != head) 41 | { 42 | temp = temp->Next; 43 | } 44 | newNode->Next = head; // newNode->Next = temp->Next 45 | temp->Next = newNode; 46 | } 47 | 48 | void insertAtHead(Node *&head, int val) 49 | { 50 | // s1: newNode creation 51 | Node *newNode = new Node(val); 52 | // s2: Update of newNode->Next 53 | newNode->Next = head; 54 | // s3: Update the Tail with New head 55 | if (head == NULL) 56 | { 57 | head = newNode; 58 | newNode->Next = head; 59 | return; 60 | } 61 | Node *temp = head; 62 | while (temp->Next != head) 63 | { 64 | temp = temp->Next; 65 | } 66 | temp->Next = newNode; 67 | // s4: Update of head 68 | head = newNode; 69 | } 70 | 71 | void display(Node *head) 72 | { 73 | if (head == NULL) 74 | { 75 | cout << "The Linked List is Empty" << endl; 76 | return; 77 | } 78 | Node *temp = head; 79 | do 80 | { 81 | cout << temp->value; 82 | temp = temp->Next; 83 | if (temp != head) 84 | cout << " -> "; 85 | } while (temp != head); 86 | 87 | cout << endl; 88 | } 89 | 90 | int countLength(Node *&head) 91 | { 92 | int count = 0; 93 | Node *temp = head; 94 | do 95 | { 96 | count++; 97 | temp = temp->Next; 98 | } while (temp != head); 99 | 100 | return count; 101 | } 102 | 103 | void insertionAtSpecificPosition(Node *&head, int pos, int val) 104 | { 105 | int i = 0; 106 | Node *temp = head; 107 | 108 | while (i < pos - 2) 109 | { 110 | temp = temp->Next; // 2 3 111 | i++; // 1 2 112 | } 113 | 114 | Node *newNode = new Node(val); 115 | newNode->Next = temp->Next; 116 | temp->Next = newNode; 117 | } 118 | 119 | void deletionAtHead(Node *&head) 120 | { 121 | Node *temp = head; 122 | if (temp != NULL) 123 | { 124 | Node *delNode = temp; 125 | while (temp->Next != head) 126 | { 127 | temp = temp->Next; 128 | } 129 | temp->Next = delNode->Next; 130 | head = delNode->Next; 131 | delete delNode; 132 | } 133 | else 134 | { 135 | cout << "There is No Value in the Linked List" << endl; 136 | } 137 | } 138 | 139 | void deletionAtTail(Node *&head) 140 | { 141 | Node *temp = head; 142 | if (temp != NULL && temp->Next != head) 143 | { 144 | while (temp->Next->Next != head) 145 | { 146 | temp = temp->Next; 147 | } 148 | Node *delNode = temp->Next; 149 | temp->Next = delNode->Next; 150 | delete delNode; 151 | } 152 | 153 | else 154 | { 155 | // Head is Null 156 | if (temp == NULL) 157 | cout << "There is No Value in the Linked List" << endl; 158 | 159 | // Head is Tail 160 | else 161 | deletionAtHead(head); 162 | } 163 | } 164 | 165 | void deletionAtSpecificPosition(Node *&head, int position) 166 | { 167 | 168 | Node *temp = head; 169 | if (position <= countLength(head)) 170 | { 171 | 172 | if (position == 1) 173 | { 174 | deletionAtHead(head); 175 | } 176 | else if (position == countLength(head)) 177 | { 178 | deletionAtTail(head); 179 | } 180 | else 181 | { 182 | int i = 1; 183 | while (i < position - 1) 184 | { 185 | temp = temp->Next; 186 | i++; 187 | } 188 | 189 | Node *delNode = temp->Next; 190 | temp->Next = delNode->Next; 191 | delete delNode; 192 | } 193 | } 194 | else 195 | { 196 | // Position is Out of Range 197 | // if (position > countLength(head)) 198 | // { 199 | cout << "Position Out of Bound"; 200 | // } 201 | // // LL is NULL 202 | // else 203 | // cout << "There is No Value in the Linked List" << endl; 204 | // 205 | } 206 | } 207 | 208 | int main() 209 | { 210 | Node *head = NULL; 211 | 212 | int value, position; 213 | cout << "Choice 1: Insertion at Head" << endl 214 | << "Choice 2: Insertion at Tail" << endl 215 | << "Choice 3: Insertion at Specific Position" << endl 216 | << "Choice 4: Deletion at Head" << endl 217 | << "Choice 5: Deletion at Tail" << endl 218 | << "Choice 6: Deletion at a Specific Position" << endl 219 | << "Choice 0: Exit" << endl 220 | << endl; 221 | cout << "Next Choice: "; 222 | int choice; 223 | cin >> choice; 224 | 225 | while (choice != 0) 226 | { 227 | switch (choice) 228 | { 229 | case 1: 230 | cout << "Enter the Value: "; 231 | cin >> value; 232 | insertAtHead(head, value); 233 | break; 234 | case 2: 235 | cout << "Enter the Value: "; 236 | cin >> value; 237 | insertAtTail(head, value); 238 | break; 239 | case 4: 240 | deletionAtHead(head); 241 | break; 242 | case 5: 243 | deletionAtTail(head); 244 | break; 245 | default: 246 | break; 247 | } 248 | cout << "Next Choice: "; 249 | cin >> choice; 250 | } 251 | 252 | cout << endl 253 | << "Linked List: "; 254 | display(head); 255 | cout << "Length of the List: " << countLength(head) << endl; 256 | 257 | return 0; 258 | } -------------------------------------------------------------------------------- /Linked List/doublyLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class doublyNode{ 6 | public: 7 | int val; 8 | doublyNode* prev; 9 | doublyNode* next; 10 | 11 | doublyNode(int val){ 12 | this->val=val; 13 | prev = NULL; 14 | next = NULL; 15 | } 16 | 17 | }; 18 | 19 | void insertAtTail(doublyNode *&head, int val) 20 | { 21 | doublyNode *newNode = new doublyNode(val); 22 | 23 | if (head == NULL) 24 | { 25 | head = newNode; 26 | return; 27 | } 28 | 29 | doublyNode *temp = head; 30 | 31 | while (temp->next != NULL) 32 | { 33 | temp = temp->next; 34 | } 35 | 36 | temp->next = newNode; 37 | newNode->prev=temp; 38 | } 39 | 40 | void insertAtHead(doublyNode* &head, int val){ 41 | // s1: newNode creation 42 | doublyNode *newNode = new doublyNode(val); 43 | //s2: Update of head->prev 44 | head->prev = newNode; 45 | // s3: Update of newNode->Next 46 | newNode->next = head; 47 | // s4: Update of head 48 | head = newNode; 49 | } 50 | 51 | void display(doublyNode* n) 52 | { 53 | while (n != NULL) 54 | { 55 | cout << n-> val ; 56 | if (n->next != NULL) 57 | cout << " ---> "; 58 | n = n->next; 59 | } 60 | cout << endl 61 | << endl; 62 | } 63 | 64 | void displayReverse(doublyNode* &head) 65 | { 66 | doublyNode* temp = head; 67 | while(temp->next!=NULL){ 68 | temp=temp->next; 69 | } 70 | while (temp != NULL) 71 | { 72 | cout << temp-> val ; 73 | if (temp->prev != NULL) 74 | cout << " ---> "; 75 | temp = temp->prev; 76 | } 77 | cout << endl 78 | << endl; 79 | } 80 | 81 | int countLength(doublyNode *&head) 82 | { 83 | int count = 0; 84 | doublyNode *temp = head; 85 | while (temp != NULL) 86 | { 87 | count++; 88 | temp = temp->next; 89 | } 90 | 91 | return count; 92 | } 93 | 94 | 95 | int main() 96 | { 97 | doublyNode *head = NULL; 98 | 99 | int value, position; 100 | // Choice 1: Insertion at Head 101 | // Choice 2: Insertion at Tail 102 | cout << "Choice 1: Insertion at Head" << endl 103 | << "Choice 2: Insertion at Tail" << endl 104 | << "Choice 3: Reverse Print" << endl 105 | << "Choice 0: Exit" << endl 106 | << endl; 107 | cout << "Next Choice: "; 108 | int choice; 109 | cin >> choice; 110 | 111 | while (choice != 0) 112 | { 113 | switch (choice) 114 | { 115 | case 1: 116 | cout << "Enter the Value: "; 117 | cin >> value; 118 | insertAtHead(head, value); 119 | break; 120 | case 2: 121 | cout << "Enter the Value: "; 122 | cin >> value; 123 | insertAtTail(head, value); 124 | break; 125 | case 3: 126 | displayReverse(head); 127 | break; 128 | default: 129 | break; 130 | } 131 | cout << "Next Choice: "; 132 | cin >> choice; 133 | } 134 | 135 | cout << endl << "Doubly Linked List: "; 136 | display(head); 137 | cout << "Length of the Doubly Linked List: " << countLength(head) << endl; 138 | 139 | return 0; 140 | } -------------------------------------------------------------------------------- /Linked List/linearLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int value; 9 | Node *Next; 10 | 11 | // Constructer Creation 12 | Node(int val) 13 | { 14 | value = val; 15 | Next = NULL; 16 | } 17 | }; 18 | 19 | struct Test 20 | { 21 | int position[1000]; 22 | }; 23 | 24 | void insertAtTail(Node *&head, int val); 25 | void insertAtHead(Node *&head, int val); 26 | void display(Node *n); 27 | int countLength(Node *&head); 28 | void insertionAtSpecificPosition(Node *&head, int pos, int val); 29 | int searchByValueUnique(Node *&head, int key); 30 | void searchByValueDuplicate(Node *&head, int key); 31 | Test searchByValueDuplicateReturn(Node *&head, int key); 32 | void insertionByValueUnique(Node *&head, int searchValue, int value); 33 | void deletionAtHead(Node *&head); 34 | void deletionAtTail(Node *&head); 35 | void deletionAtSpecificPosition(Node *&head, int position); 36 | void deletionByValueUnique(Node *&head, int value); 37 | 38 | void insertAtTail(Node *&head, int val) 39 | { 40 | Node *newNode = new Node(val); 41 | 42 | if (head == NULL) 43 | { 44 | head = newNode; 45 | return; 46 | } 47 | 48 | Node *temp = head; 49 | 50 | while (temp->Next != NULL) 51 | { 52 | temp = temp->Next; 53 | } 54 | 55 | temp->Next = newNode; 56 | } 57 | 58 | void insertAtHead(Node *&head, int val) 59 | { 60 | // s1: newNode creation 61 | Node *newNode = new Node(val); 62 | // s2: Update of newNode->Next 63 | newNode->Next = head; 64 | // s3: Update of head 65 | head = newNode; 66 | } 67 | 68 | void display(Node *n) 69 | { 70 | while (n != NULL) 71 | { 72 | cout << n->value; 73 | if (n->Next != NULL) 74 | cout << " -> "; 75 | n = n->Next; 76 | } 77 | cout << endl 78 | << endl; 79 | } 80 | 81 | int countLength(Node *&head) 82 | { 83 | int count = 0; 84 | Node *temp = head; 85 | while (temp != NULL) 86 | { 87 | count++; 88 | temp = temp->Next; 89 | } 90 | 91 | return count; 92 | } 93 | 94 | void insertionAtSpecificPosition(Node *&head, int pos, int val) 95 | { 96 | int i = 0; 97 | Node *temp = head; 98 | 99 | while (i < pos - 2) 100 | { 101 | temp = temp->Next; // 2 3 102 | i++; // 1 2 103 | } 104 | 105 | Node *newNode = new Node(val); 106 | newNode->Next = temp->Next; 107 | temp->Next = newNode; 108 | } 109 | 110 | int searchByValueUnique(Node *&head, int key) 111 | { 112 | Node *temp = head; // 1 2 3 4 5 6 9 Case 1 | ---- 9 Case 2 113 | int count = 1; 114 | if (temp == NULL) 115 | { 116 | return -1; 117 | } 118 | 119 | while (temp->value != key) 120 | { 121 | if (temp->Next == NULL) 122 | { 123 | return -1; 124 | } 125 | temp = temp->Next; // 2 3 4 5 6 126 | count++; 127 | } 128 | 129 | return count; 130 | } 131 | 132 | void searchByValueDuplicate(Node *&head, int key) 133 | { 134 | 135 | Node *temp = head; 136 | int size; 137 | size = countLength(head); 138 | int position[size + 1], k = 1; 139 | int count = 1; 140 | int flag = 0; 141 | 142 | while (temp != NULL) 143 | { 144 | if (temp->value == key) 145 | { 146 | // cout << count << " "; 147 | position[k] = count; 148 | k++; 149 | flag = 1; 150 | } 151 | temp = temp->Next; 152 | count++; 153 | } 154 | if (flag == 0) 155 | cout << "The Searched Value is not yet in the List" << endl; 156 | else 157 | { 158 | position[0] = k; 159 | cout << "The value is found at Position: "; 160 | for (int i = 1; i < position[0]; i++) 161 | { 162 | cout << position[i]; 163 | if (i < position[0] - 1) 164 | cout << ","; 165 | } 166 | cout << endl; 167 | } 168 | } 169 | 170 | Test searchByValueDuplicateReturn(Node *&head, int key) 171 | { 172 | Node *temp = head; 173 | Test T; 174 | int k = 1; 175 | int count = 1; 176 | 177 | while (temp != NULL) 178 | { 179 | if (temp->value == key) 180 | { 181 | // cout << count << " "; 182 | T.position[k] = count; 183 | k++; 184 | } 185 | temp = temp->Next; 186 | count++; 187 | } 188 | 189 | T.position[0] = k; 190 | return T; 191 | } 192 | 193 | void insertionByValueUnique(Node *&head, int searchValue, int value) 194 | { 195 | 196 | // Step 1: Search the Position of the searchValue 197 | int position; 198 | position = searchByValueUnique(head, searchValue); 199 | 200 | // Step 2: Insert the value at the Position+1 201 | insertionAtSpecificPosition(head, position + 1, value); 202 | } 203 | 204 | void deletionAtHead(Node *&head) 205 | { 206 | Node *temp = head; 207 | if (temp != NULL) 208 | { 209 | head = temp->Next; 210 | delete temp; 211 | } 212 | else 213 | { 214 | cout << "There is No Value in the Linked List" << endl; 215 | } 216 | } 217 | 218 | void deletionAtTail(Node *&head) 219 | { 220 | Node *temp = head; 221 | if (temp != NULL && temp->Next != NULL) 222 | { 223 | while (temp->Next->Next != NULL) 224 | { 225 | temp = temp->Next; 226 | } 227 | Node *delNode = temp->Next; 228 | temp->Next = NULL; 229 | delete delNode; 230 | } 231 | 232 | else 233 | { 234 | // Head is Null 235 | if (temp == NULL) 236 | cout << "There is No Value in the Linked List" << endl; 237 | 238 | // Head is Tail 239 | else 240 | deletionAtHead(head); 241 | } 242 | } 243 | 244 | void deletionAtSpecificPosition(Node *&head, int position) 245 | { 246 | 247 | Node *temp = head; 248 | if (position <= countLength(head)) 249 | { 250 | 251 | if (position == 1) 252 | { 253 | deletionAtHead(head); 254 | } 255 | else if (position == countLength(head)) 256 | { 257 | deletionAtTail(head); 258 | } 259 | else 260 | { 261 | int i = 1; 262 | while (i < position - 1) 263 | { 264 | temp = temp->Next; 265 | i++; 266 | } 267 | 268 | Node *delNode = temp->Next; 269 | temp->Next = delNode->Next; 270 | delete delNode; 271 | } 272 | } 273 | else 274 | { 275 | // Position is Out of Range 276 | // if (position > countLength(head)) 277 | // { 278 | cout << "Position Out of Bound"; 279 | // } 280 | // // LL is NULL 281 | // else 282 | // cout << "There is No Value in the Linked List" << endl; 283 | // 284 | } 285 | } 286 | 287 | void deletionByValueUnique(Node *&head, int value) 288 | { 289 | // Find the Position of the Value 290 | int position; 291 | position = searchByValueUnique(head, value); 292 | 293 | // Delete the Node at that Position 294 | if (position == -1) 295 | { 296 | cout << "Value not Found in the Linked List" << endl; 297 | } 298 | else 299 | { 300 | deletionAtSpecificPosition(head, position); 301 | } 302 | } 303 | 304 | Node *reverseNonRecursive(Node *&head) 305 | { 306 | Node *prev = NULL; 307 | Node *current = head; 308 | if (head == NULL) 309 | { 310 | cout << "The Linked List is Empty " << endl; 311 | return head; 312 | } 313 | Node *next = head->Next; 314 | 315 | while (true) 316 | { 317 | current->Next = prev; 318 | prev = current; 319 | current = next; 320 | if (current == NULL) 321 | break; 322 | next = next->Next; 323 | } 324 | 325 | return prev; 326 | } 327 | 328 | Node *reverseRecursive(Node *&head) 329 | { 330 | 331 | // BASE CALL 332 | if (head == NULL || head->Next == NULL) 333 | { 334 | if (head == NULL) 335 | cout << "The Linked List is Empty " << endl; 336 | return head; 337 | } 338 | 339 | // RECURSIVE CALL 340 | Node *newHead = reverseRecursive(head->Next); 341 | head->Next->Next = head; 342 | head->Next = NULL; 343 | 344 | return newHead; 345 | } 346 | 347 | int findMid(Node *&head) 348 | { 349 | // Case 1: Head Empty 350 | if (head == NULL) 351 | { 352 | return -1; 353 | } 354 | Node *slow = head; 355 | Node *fast = head; 356 | 357 | while (fast != NULL && fast->Next != NULL) 358 | { 359 | slow = slow->Next; 360 | fast = fast->Next->Next; 361 | } 362 | 363 | return slow->value; 364 | } 365 | 366 | void makeCycle(Node* &head, int pos){ 367 | Node* temp = head; 368 | Node* startNode; 369 | int count = 1; 370 | 371 | while(temp->Next!=NULL){ 372 | 373 | if(count==pos) startNode=temp; 374 | temp=temp->Next; 375 | count++; 376 | } 377 | 378 | temp->Next=startNode; 379 | } 380 | 381 | bool detectCycle (Node * &head){ 382 | Node* slow =head; 383 | Node* fast = head; 384 | 385 | while (fast != NULL && fast->Next != NULL) 386 | { 387 | slow = slow->Next; 388 | fast = fast->Next->Next; 389 | 390 | //Cycle Check 391 | if(slow->Next==fast->Next){ 392 | return true; 393 | } 394 | } 395 | 396 | return false; 397 | } 398 | 399 | void removeCycle(Node* &head){ 400 | Node* slow = head; 401 | Node* fast = head; 402 | 403 | //Step 1: fast = slow 404 | do{ 405 | slow=slow->Next; 406 | fast=fast->Next->Next; 407 | 408 | }while(slow!=fast); 409 | 410 | //Step 2: Re Initialization of fast 411 | fast = head; 412 | 413 | //Step 3: fast -> Next = slow-> Next 414 | while(fast->Next!= slow->Next){ 415 | slow=slow->Next; 416 | fast=fast->Next; 417 | } 418 | //Step 4: 419 | slow->Next=NULL; 420 | } 421 | 422 | int main() 423 | { 424 | Node *head = NULL; 425 | 426 | int value, position; 427 | // Choice 1: Insertion at Head 428 | // Choice 2: Insertion at Tail 429 | // Choice 3: Insertion at a Certain Position 430 | cout << "Choice 1: Insertion at Head" << endl 431 | << "Choice 2: Insertion at Tail" << endl 432 | << "Choice 3: Insertion at Specific Position" << endl 433 | << "Choice 4: Search a value (Unique List)" << endl 434 | << "Choice 5: Search a value (Duplication enabled List)" << endl 435 | << "Choice 6: Insertion after a specific value (Unique List)" << endl 436 | << "Choice 7: Deletion at Head" << endl 437 | << "Choice 8: Deletion at Tail" << endl 438 | << "Choice 9: Deletion at a Specific Position" << endl 439 | << "Choice 10: Deletion by Value (Unique List)" << endl 440 | << "Choice 11: Reversal of List Non-Recursive" << endl 441 | << "Choice 12: Finding the Mid (Slow-Fast Pointer Method)" << endl 442 | << "Choice 13: Make a Cycle at K Position" <> choice; 450 | 451 | while (choice != 0) 452 | { 453 | switch (choice) 454 | { 455 | case 1: 456 | cout << "Enter the Value: "; 457 | cin >> value; 458 | insertAtHead(head, value); 459 | break; 460 | case 2: 461 | cout << "Enter the Value: "; 462 | cin >> value; 463 | insertAtTail(head, value); 464 | break; 465 | case 3: 466 | cout << "Enter the Desired Position: "; 467 | cin >> position; 468 | cout << "Enter the Value: "; 469 | case 4: 470 | cout << "Enter the Value to Search: "; 471 | cin >> value; 472 | position = searchByValueUnique(head, value); 473 | if (position != -1) 474 | { 475 | cout << "The number is at Position " << position << endl; 476 | } 477 | else 478 | { 479 | cout << "The number is not yet in the List" << endl; 480 | } 481 | break; 482 | case 5: 483 | cout << "Enter the Value to Search: "; 484 | cin >> value; 485 | // searchByValueDuplicate(head, value); 486 | Test T; 487 | T = searchByValueDuplicateReturn(head, value); 488 | if (T.position[0] == 1) 489 | { 490 | cout << "The Searched Value is not yet in the List" << endl; 491 | } 492 | else 493 | { 494 | int size = T.position[0]; 495 | cout << "The value is found at Position: "; 496 | for (int i = 1; i < size; i++) 497 | { 498 | cout << T.position[i]; 499 | if (i < size - 1) 500 | cout << ", "; 501 | } 502 | cout << endl; 503 | } 504 | break; 505 | case 6: 506 | cout << "Enter the value to search: "; 507 | int searchValue; 508 | cin >> searchValue; 509 | cout << "Enter the value to insert: "; 510 | cin >> value; 511 | insertionByValueUnique(head, searchValue, value); 512 | break; 513 | case 7: 514 | deletionAtHead(head); 515 | break; 516 | case 8: 517 | deletionAtTail(head); 518 | break; 519 | case 9: 520 | if (head == NULL) 521 | { 522 | cout << "There is No Value in the Linked List" << endl; 523 | break; 524 | } 525 | cout << "Enter the Desired Position: "; 526 | cin >> position; 527 | deletionAtSpecificPosition(head, position); 528 | // cout << endl; 529 | // display(head); 530 | break; 531 | case 10: 532 | cout << "Enter the value to Delete: "; 533 | int delValue; 534 | cin >> delValue; 535 | deletionByValueUnique(head, delValue); 536 | break; 537 | case 11: 538 | // head = reverseNonRecursive(head); 539 | head = reverseRecursive(head); 540 | break; 541 | case 12: 542 | int mid; 543 | mid = findMid(head); 544 | if (mid == -1) 545 | cout << "The LL is empty" << endl; 546 | else 547 | cout << "Mid value is " << mid << endl; 548 | break; 549 | case 13: 550 | cout << "Enter the Desired Position to Create Cycle: "; 551 | cin >> position; 552 | makeCycle(head, position); 553 | break; 554 | case 14: 555 | bool cycleStatus; 556 | cycleStatus=detectCycle(head); 557 | if(cycleStatus==true){ 558 | cout<< "There is a Cycle in the List" << endl; 559 | } 560 | else{ 561 | cout<< "There is NO Cycle in the List" << endl; 562 | } 563 | break; 564 | case 15: 565 | cycleStatus=detectCycle(head); 566 | if(cycleStatus==true){ 567 | removeCycle(head); 568 | } 569 | else{ 570 | cout<< "There is NO Cycle in the List" << endl; 571 | } 572 | break; 573 | default: 574 | break; 575 | } 576 | cout << "Next Choice: "; 577 | cin >> choice; 578 | } 579 | 580 | cout << endl 581 | << "Linked List: "; 582 | display(head); 583 | cout << "Length of the List: " << countLength(head) << endl; 584 | 585 | return 0; 586 | } -------------------------------------------------------------------------------- /Queue/MYQUEUE.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template class NodeQueue 6 | { 7 | 8 | public: 9 | N value; 10 | NodeQueue *next; 11 | 12 | NodeQueue(N val) 13 | { 14 | value = val; 15 | next = NULL; 16 | } 17 | }; 18 | 19 | template < typename Q> class Queue 20 | { 21 | NodeQueue *front; 22 | NodeQueue *rear; 23 | 24 | public: 25 | Queue() 26 | { 27 | front = NULL; 28 | rear = NULL; 29 | } 30 | 31 | // enqueue --> push (val) 32 | 33 | void push(Q val) 34 | { 35 | 36 | NodeQueue *newNode = new NodeQueue (val); 37 | 38 | if (front == NULL) 39 | { 40 | front = newNode; 41 | rear = newNode; 42 | return; 43 | } 44 | 45 | rear->next = newNode; 46 | rear = rear->next; 47 | } 48 | 49 | // deque --> pop () 50 | 51 | Q pop() 52 | { 53 | Q chk; 54 | if (rear == NULL) 55 | { 56 | cout << "Queue Underflow | There is no element in the Queue" << endl; 57 | return chk; 58 | } 59 | NodeQueue *delNode; 60 | delNode = front; 61 | front = front->next; 62 | if (front == NULL) 63 | { 64 | rear = NULL; 65 | } 66 | chk = delNode->value; 67 | delete delNode; 68 | return chk; 69 | } 70 | 71 | // peek ---> front() back() 72 | 73 | Q Front() 74 | { 75 | 76 | Q chk; 77 | chk = front->value; 78 | return chk; 79 | } 80 | 81 | Q Back() 82 | { 83 | 84 | Q chk; 85 | chk = rear->value; 86 | return chk; 87 | } 88 | 89 | // empty ---> empty() 90 | bool empty() 91 | { 92 | 93 | if (front == NULL && rear == NULL) 94 | return true; 95 | else 96 | return false; 97 | } 98 | }; 99 | -------------------------------------------------------------------------------- /Queue/README.md: -------------------------------------------------------------------------------- 1 | # **Queue** 2 | 3 | Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added. 4 | 5 | Queue 6 | 7 | Source: [GeeksforGeeks](https://www.geeksforgeeks.org/queue-data-structure/) 8 | 9 | ## Code Examples 10 | 11 | - **[MYQUEUE.h](MYQUEUE.h)** 12 | - **[balancedpar.cpp](balancedpar.cpp)** 13 | - **[curmin.cpp](curmin.cpp)** 14 | - **[kreverse.cpp](kreverse.cpp)** 15 | -------------------------------------------------------------------------------- /Queue/balancedpar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "MYSTACK.h" 3 | 4 | using namespace std; 5 | 6 | bool balancedParenthesis(string s) 7 | { 8 | //[{(){}{()}}] 9 | int n = s.size(); 10 | Stack st; 11 | bool isBalanced = true; 12 | 13 | if (s[0] == ')' || s[0] == '}' || s[0] == ']') 14 | { 15 | return false; 16 | } 17 | for (int i = 0; i < n; i++) 18 | { 19 | // Opening Bracket -> ( { [ 20 | if (s[i] == '(' || s[i] == '{' || s[i] == '[') 21 | { 22 | st.push(s[i]); 23 | } 24 | 25 | // Closing Bracket 26 | // ) --> st.top == ( st.pop else isBalanced = False Break 27 | else if (s[i] == ')') 28 | { 29 | if (st.Top() == '(') 30 | { 31 | st.pop(); 32 | } 33 | else 34 | { 35 | isBalanced = false; 36 | break; 37 | } 38 | } 39 | 40 | else if (s[i] == '}') 41 | { 42 | if (st.Top() == '{') 43 | { 44 | st.pop(); 45 | } 46 | else 47 | { 48 | isBalanced = false; 49 | break; 50 | } 51 | } 52 | 53 | else if (s[i] == ']') 54 | { 55 | if (st.Top() == '[') 56 | { 57 | st.pop(); 58 | } 59 | else 60 | { 61 | isBalanced = false; 62 | break; 63 | } 64 | } 65 | } 66 | 67 | if (!st.empty()) 68 | { 69 | isBalanced = false; 70 | } 71 | 72 | return isBalanced; 73 | } 74 | 75 | int main() 76 | { 77 | 78 | string chk; 79 | cin >> chk; 80 | if (balancedParenthesis(chk)) 81 | cout << "YES"; 82 | else 83 | cout << "NO"; 84 | cout << endl; 85 | 86 | return 0; 87 | } -------------------------------------------------------------------------------- /Queue/curmin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "MYSTACK.h" 3 | 4 | using namespace std; 5 | 6 | /* 7 | 5 8 | 1 2 3 4 5 9 | 7 10 | 1 6 43 1 2 0 5 11 | 10 12 | 6 5 5 5 4 7 1 2 1 2 13 | */ 14 | 15 | 16 | int main() 17 | { 18 | int n; 19 | cin>>n; 20 | int array[n]; 21 | 22 | for(int i=0; i>array[i]; 24 | } 25 | 26 | Stack st; 27 | Stack minStack; 28 | 29 | int min =INT_MAX; 30 | 31 | for(int i=0; i 2 | #include "MYSTACK.h" 3 | #include "MYQUEUE.h" 4 | 5 | using namespace std; 6 | 7 | /* 8 | 11 3 9 | 1 2 3 4 5 6 7 8 9 10 11 10 | ---> 3 2 1 6 5 4 9 8 7 11 10 11 | --> #3 2 1 6 5 4 9 8 7 10 11# 12 | */ 13 | 14 | void reverseKElements(int n, int k, Queue &q) 15 | { 16 | int x = n / k; 17 | int y = n % k; 18 | 19 | Stack st; 20 | 21 | while (x > 0) 22 | { 23 | int chk = k; 24 | while (chk > 0) 25 | { 26 | st.push(q.pop()); 27 | chk--; 28 | } 29 | while (!st.empty()) 30 | { 31 | q.push(st.pop()); 32 | } 33 | 34 | x--; 35 | } 36 | 37 | while (y > 0) 38 | { 39 | st.push(q.pop()); 40 | y--; 41 | } 42 | while (!st.empty()) 43 | { 44 | q.push(st.pop()); 45 | } 46 | } 47 | 48 | int main() 49 | { 50 | int n, k; 51 | cin >> n >> k; 52 | Queue q; 53 | for (int i = 0; i < n; i++) 54 | { 55 | int val; 56 | cin >> val; 57 | q.push(val); 58 | } 59 | 60 | reverseKElements(n, k, q); 61 | while (!q.empty()) 62 | { 63 | cout << q.pop() << " "; 64 | } 65 | cout << endl 66 | << endl; 67 | 68 | return 0; 69 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **Basic-Data-Structure** 2 | 3 | This repository contains the basic data structure and algorithms in C++. The code is written in a way that it can be easily understood by beginners. 4 | 5 | ## **Table of Contents** 6 | 7 | - **[Array](#1-array)** 8 | - **[Linked List](#2-linked-list)** 9 | - **[Stack](#3-stack)** 10 | - **[Queue](#4-queue)** 11 | - **[Binary Tree](#5-binary-tree)** 12 | - **[Binary Search Tree](#6-binary-search-tree)** 13 | - **[Heap and Priority Queue](#7-heap-and-priority-queue)** 14 | - **[Graph](#8-graph)** 15 | - **[Hashing](#9-hashing)** 16 | 17 | ## **[1. Array](./Array)** 18 | 19 | Array is a collection of similar type of data items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). 20 | 21 | - **[2DArray.cpp](./Array/2DArray.cpp)** 22 | - **[BubbleSort.cpp](./Array/BubbleSort.cpp)** 23 | - **[binarySearch.cpp](./Array/binarySearch.cpp)** 24 | - **[countingSort.cpp](./Array/countingSort.cpp)** 25 | - **[insertionSort.cpp](./Array/insertionSort.cpp)** 26 | - **[linearSearch.cpp](./Array/linearSearch.cpp)** 27 | 28 | ## **[2. Linked List](./Linked%20List)** 29 | 30 | A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. 31 | 32 | - **[circularLinkedList.cpp](./Linked%20List/circularLinkedList.cpp)** 33 | - **[doublyLinkedList.cpp](./Linked%20List/doublyLinkedList.cpp)** 34 | - **[linearLinkedList.cpp](./Linked%20List/linearLinkedList.cpp)** 35 | 36 | ## **[3. Stack](./Stack)** 37 | 38 | A stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). Mainly the following three basic operations are performed in the stack: Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. Peek or Top: Returns top element of stack. IsEmpty: Returns true if stack is empty, else false. 39 | 40 | - **[MYSTACK.h](./Stack/MYSTACK.h)** 41 | - **[infixToPrefix.cpp](./Stack/infixToPrefix.cpp)** 42 | - **[prefixEvaluation.cpp](./Stack/prefixEvaluation.cpp)** 43 | - **[reverseSentence.cpp](./Stack/reverseSentence.cpp)** 44 | - **[reverseStack.cpp](./Stack/reverseStack.cpp)** 45 | 46 | ## **[4. Queue](./Queue)** 47 | 48 | A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added. 49 | 50 | - **[MYQUEUE.h](./Queue/MYQUEUE.h)** 51 | - **[balancedpar.cpp](./Queue/balancedpar.cpp)** 52 | - **[curmin.cpp](./Queue/curmin.cpp)** 53 | - **[kreverse.cpp](./Queue/kreverse.cpp)** 54 | 55 | ## **[5. Binary Tree](./Binary%20Tree)** 56 | 57 | A binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child. A node with no children is called a leaf. A node with no parent is called the root. A binary tree with a single node is called a degenerate or pathological tree. A binary tree is either empty or consists of a root node and two binary trees, called the left subtree and the right subtree. 58 | 59 | - **[binaryTree.cpp](./Binary%20Tree/binaryTree.cpp)** 60 | - **[preorderInordertoBinary.cpp](./Binary%20Tree/preorderInordertoBinary.cpp)** 61 | 62 | ## **[6. Binary Search Tree](./BST)** 63 | 64 | Binary Search Tree is a node-based binary tree data structure which has the following properties: The left subtree of a node contains only nodes with keys lesser than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. The left and right subtree each must also be a binary search tree. There must be no duplicate nodes. 65 | 66 | - **[bst.cpp](./BST/bst.cpp)** 67 | 68 | ## **[7. Heap and Priority Queue](./Heap%20and%20Priority%20queue)** 69 | 70 | A heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. 71 | 72 | - **[heap.cpp](./Heap%20and%20Priority%20queue/heap.cpp)** 73 | - **[mergeKSorted.cpp](./Heap%20and%20Priority%20queue/mergeKSorted.cpp)** 74 | - **[vectorPQSTL.cpp](./Heap%20and%20Priority%20queue/vectorPQSTL.cpp)** 75 | 76 | ## **[8. Graph](./Graph)** 77 | 78 | A graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph can be defined as, A Graph consists of a finite set of vertices(or nodes) and set of Edges which connect a pair of nodes. 79 | 80 | - **[graph.cpp](./Graph/graph.cpp)** 81 | 82 | ## **[9. Hashing](./Hashing)** 83 | 84 | Hashing is a technique to convert a range of key values into a range of indexes of an array. The idea is to use a hash function that converts a given key to a smaller number and uses the small number as an index in a table called hash table. 85 | 86 | - **[VerticalOrder.cpp](./Hashing/VerticalOrder.cpp)** 87 | - **[freq.cpp](./Hashing/freq.cpp)** 88 | - **[hashing.cpp](./Hashing/hashing.cpp)** 89 | -------------------------------------------------------------------------------- /Stack/MYSTACK.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template class Node 6 | { 7 | 8 | public: 9 | N value; 10 | Node *next; 11 | Node *prev; 12 | 13 | Node(N val) 14 | { 15 | value = val; 16 | next = NULL; 17 | prev = NULL; 18 | } 19 | }; 20 | // Stack st; 21 | template class Stack 22 | { 23 | 24 | Node *head; 25 | Node *top; 26 | int count = 0; 27 | 28 | public: 29 | 30 | Stack(){ 31 | head = NULL; 32 | top = NULL; 33 | } 34 | 35 | // PUSH 36 | void push(S val) 37 | { 38 | 39 | Node *newNode = new Node (val); 40 | 41 | if (head == NULL) 42 | { 43 | head = top = newNode; 44 | count++; 45 | return; 46 | } 47 | 48 | top->next = newNode; 49 | newNode->prev = top; 50 | top = newNode; 51 | count++; 52 | } 53 | // POP 54 | 55 | S pop() 56 | { 57 | Node *delNode; 58 | delNode = top; 59 | S chk; 60 | if (head == NULL) 61 | { // There is No Element in the Stack 62 | cout << "Stack Underflow " << endl; 63 | return chk; 64 | } 65 | 66 | if (top == head) // There is only 1 element 67 | { 68 | head = top = NULL; 69 | } 70 | else // There is More than 1 element 71 | { 72 | top = delNode->prev; 73 | top->next = NULL; 74 | } 75 | 76 | chk = delNode->value; 77 | delete delNode; 78 | count--; 79 | return chk; 80 | } 81 | 82 | // EMPTY 83 | bool empty() 84 | { 85 | if (head == NULL) 86 | return true; 87 | else 88 | return false; 89 | } 90 | // SIZE 91 | int size() 92 | { 93 | return count; 94 | } 95 | // TOP 96 | S Top() 97 | { 98 | S chk; 99 | if (top == NULL) 100 | { 101 | cout << "Stack Underflow | There is no Element in Top " << endl; 102 | } 103 | else 104 | chk = top->value; 105 | 106 | return chk; 107 | } 108 | }; 109 | 110 | 111 | -------------------------------------------------------------------------------- /Stack/README.md: -------------------------------------------------------------------------------- 1 | # **Stack** 2 | 3 | Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). Mainly the following three basic operations are performed in the stack: 4 | 5 | - Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. 6 | - Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. 7 | 8 | Stack 9 | 10 | Source: [GeeksforGeeks](https://www.geeksforgeeks.org/stack-data-structure/) 11 | 12 | ## Code Examples 13 | 14 | - **[MYSTACK.h](MYSTACK.h)** 15 | - **[infixToPrefix.cpp](infixToPrefix.cpp)** 16 | - **[prefixEvaluation.cpp](prefixEvaluation.cpp)** 17 | - **[reverseSentence.cpp](reverseSentence.cpp)** 18 | - **[reverseStack.cpp](reverseStack.cpp)** 19 | -------------------------------------------------------------------------------- /Stack/infixToPrefix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "MYSTACK.h" 3 | 4 | using namespace std; 5 | 6 | int precisionCalc(char c) 7 | { 8 | if (c == '^') 9 | { 10 | return 3; 11 | } 12 | else if (c == '/' || c == '*') 13 | { 14 | return 2; 15 | } 16 | else if (c == '+' || c == '-') 17 | { 18 | return 1; 19 | } 20 | else 21 | return -1; 22 | } 23 | 24 | string infixToPrefix(string chk) 25 | { 26 | 27 | reverse(chk.begin(), chk.end()); // ) --> Opening AND ( --> Closing 28 | Stack st; 29 | string result; 30 | 31 | for (int i = 0; i < chk.length(); i++) 32 | { 33 | 34 | if ((chk[i] >= 'a' && chk[i] <= 'z') || (chk[i] >= 'A' && chk[i] <= 'Z'))//(chk[i] >= '0' && chk[i] <= '9') // 35 | { 36 | result += chk[i]; 37 | } 38 | else if (chk[i] == ')') 39 | { 40 | st.push(chk[i]); 41 | } 42 | else if (chk[i] == '(') 43 | { 44 | while (!st.empty() && st.Top() != ')') 45 | { 46 | result += st.pop(); 47 | } 48 | if (!st.empty()) 49 | st.pop(); 50 | } 51 | else 52 | { 53 | while (!st.empty() && precisionCalc(st.Top()) >= precisionCalc(chk[i])) 54 | { 55 | result += st.pop(); 56 | } 57 | st.push(chk[i]); 58 | } 59 | } 60 | 61 | while(!st.empty()){ 62 | result+=st.pop(); 63 | } 64 | 65 | reverse(result.begin(),result.end()); 66 | return result; 67 | 68 | } 69 | 70 | int prefixEvaluation(string chk) 71 | { 72 | 73 | Stack st; 74 | 75 | for (int i = chk.length() - 1; i >= 0; i--) 76 | { 77 | if (chk[i] >= '0' && chk[i] <= '9') // chk[i] 0 to 9 --> Operand 78 | { 79 | st.push(chk[i] - '0'); 80 | } 81 | else // chk[i] ---> Operator 82 | { 83 | int a = st.pop(); 84 | int b = st.pop(); 85 | 86 | switch (chk[i]) 87 | { 88 | case '+': 89 | st.push(a + b); 90 | break; 91 | case '-': 92 | st.push(a - b); 93 | break; 94 | case '*': 95 | st.push(a * b); 96 | break; 97 | case '/': 98 | st.push(a / b); 99 | break; 100 | case '^': 101 | st.push(pow(a, b)); 102 | break; 103 | default: 104 | break; 105 | } 106 | } 107 | } 108 | return st.Top(); 109 | } 110 | 111 | /* 112 | +*423 113 | -+7*45+20 114 | (7+(4*5))-(2+0) 115 | ((A^B*C)-D)+((E/F)/(G+H)) 116 | */ 117 | 118 | int main() 119 | { 120 | string infix = "((A^B*C)-D)+((E/F)/(G+H))"; 121 | string prefix; 122 | prefix = infixToPrefix(infix); 123 | cout << endl << prefix << endl; 124 | // << prefixEvaluation(prefix) << endl 125 | // << endl; 126 | } -------------------------------------------------------------------------------- /Stack/prefixEvaluation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "MYSTACK.h" 3 | 4 | using namespace std; 5 | 6 | int prefixEvaluation(string chk) 7 | { 8 | 9 | Stack st; 10 | 11 | for (int i = chk.length() - 1; i >= 0; i--) 12 | { 13 | if (chk[i] >= '0' && chk[i] <= '9') // chk[i] 0 to 9 --> Operand 14 | { 15 | st.push(chk[i] - '0'); 16 | } 17 | else // chk[i] ---> Operator 18 | { 19 | int a = st.pop(); 20 | int b = st.pop(); 21 | 22 | switch (chk[i]) 23 | { 24 | case '+': 25 | st.push(a + b); 26 | break; 27 | case '-': 28 | st.push(a - b); 29 | break; 30 | case '*': 31 | st.push(a * b); 32 | break; 33 | case '/': 34 | st.push(a / b); 35 | break; 36 | case '^': 37 | st.push(pow(a, b)); 38 | break; 39 | default: 40 | break; 41 | } 42 | } 43 | } 44 | return st.Top(); 45 | 46 | } 47 | 48 | /* 49 | +*423 50 | -+7*45+20 51 | */ 52 | 53 | int main() 54 | { 55 | cout << endl << prefixEvaluation("-+7*45+20") << endl << endl; 56 | } -------------------------------------------------------------------------------- /Stack/reverseSentence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"MYSTACK.h" 3 | 4 | using namespace std; 5 | 6 | void reveseSentence (string chk){ 7 | 8 | Stack st; 9 | 10 | for(int i =0; i 2 | #include"MYSTACK.h" 3 | 4 | using namespace std; 5 | 6 | void insertAtBottom(Stack&chk, int chkElement){ 7 | 8 | if(chk.empty()){ 9 | chk.push(chkElement); 10 | return; 11 | } 12 | 13 | int topElement = chk.Top(); 14 | chk.pop(); 15 | insertAtBottom(chk,chkElement); 16 | 17 | chk.push(topElement); 18 | } 19 | 20 | void reverseStack (Stack &chk){ 21 | 22 | if(chk.empty()){ 23 | return; 24 | } 25 | 26 | int topElement=chk.Top(); 27 | chk.pop(); 28 | reverseStack(chk); 29 | insertAtBottom(chk,topElement); 30 | 31 | 32 | 33 | } 34 | 35 | int main(){ 36 | 37 | Stackst; 38 | st.push(1); 39 | st.push(2); 40 | st.push(3); 41 | st.push(4); 42 | st.push(5); 43 | 44 | reverseStack(st); 45 | 46 | while(!st.empty()){ 47 | cout << st.pop() << endl; 48 | } 49 | 50 | return 0; 51 | } --------------------------------------------------------------------------------