├── .gitignore ├── 04_Conditional_Statements └── conditionals.cpp ├── 05_Loops ├── homework.cpp └── loops.cpp ├── 06_Patterns ├── pattern slides cpp.pdf └── patterns.cpp ├── 07_Functions └── functions.cpp ├── 08_Binary_Numbers └── binary.cpp ├── 09_Pointers └── pointers.cpp ├── 10_Array_1 └── arrays.cpp ├── 11_Arrays_2 └── arrays2.cpp ├── 12_Basic_Sorting_Algos └── sorting.cpp ├── 13_2DArray └── 2DArrays.cpp ├── 14_String └── strings.cpp ├── 15_Vector └── vectors.cpp ├── 16_Bit_Manipulation └── bit_manip.cpp ├── 17_OOPS1 └── oops1.cpp ├── 18_OOPS2 └── oops2.cpp ├── 19_Recursion1 └── recursion1.cpp ├── 20_Recursion2 └── rec2.cpp ├── 21_Divide_and_Conquer └── divideAndConquer.cpp ├── 24_Backtracking └── backtracking.cpp ├── 25_LinkedList1 └── ll1.cpp ├── 26_LinkedList2 ├── doubly_linkedlist.cpp ├── linkedlist.cpp └── list_using_stl.cpp └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /04_Conditional_Statements/conditionals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | //Print the largest of 2 numbers 7 | int a = 3, b = 5; 8 | if(a >= b) { 9 | cout << "a is larger" << endl; 10 | } else { 11 | cout << "b is larger" << endl; 12 | } 13 | 14 | //Print if a number is Odd or Even 15 | int num = 5; 16 | if(num % 2 == 0) { 17 | cout << "Number is Even" << endl; 18 | } else { 19 | cout << "Number is Odd" << endl; 20 | } 21 | 22 | //Income Tax Calculator 23 | int income = 7; //in Lakhs 24 | float tax; 25 | 26 | if(income < 5) { 27 | tax = 0; 28 | } else if(income <= 10) { 29 | tax = 0.2 * income; 30 | } else { 31 | tax = 0.3 * income; 32 | } 33 | 34 | cout << "Tax = " << (tax * 100000) << endl; 35 | 36 | //Print Largest of 3 Numbers 37 | int x = 2, y = 3, z = 5; 38 | if(x >= y && x >= z) { 39 | cout << "x is largest" << endl; 40 | } else if(y >= z) { 41 | cout << "y is largest" << endl; 42 | } else { 43 | cout << "z is largest" << endl; 44 | } 45 | 46 | //Odd or Even using Ternary Operator 47 | int num = 25; 48 | bool isEven = num % 2 == 0 ? true : false; 49 | 50 | //Calculator using Switch Statement 51 | int op = '*'; 52 | int num1 = 4; 53 | int num2 = 5; 54 | 55 | switch(op) { 56 | case '+' : cout << "a + b = " << (num1 + num2) << endl; 57 | break; 58 | case '-' : cout << "a - b = " << (num1 - num2) << endl; 59 | break; 60 | case '*' : cout << "a * b = " << (num1 * num2) << endl; 61 | break; 62 | case '/' : cout << "a / b = " << (num1 / num2) << endl; 63 | break; 64 | default : cout << "Invalid Operator" << endl; 65 | } 66 | return 0; 67 | } -------------------------------------------------------------------------------- /05_Loops/homework.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | //Qs : Factorial of a number n 6 | int n = 6; 7 | int fact = 1; 8 | for(int i=1; i<=n; i++) { 9 | fact *= i; 10 | } 11 | 12 | cout << "factorial of " << n << " = " << fact << "\n"; 13 | 14 | //Qs : Multiplication Table of n 15 | n = 8; 16 | for(int i=1; i<=10; i++) { 17 | cout << (n * i) << " "; 18 | } 19 | cout << endl; 20 | 21 | //Qs : Check for Armstrong Number 22 | n = 371; 23 | int num = n; 24 | int cubeSum = 0; 25 | 26 | while(num > 0) { 27 | int lastDig = num % 10; 28 | cubeSum += lastDig * lastDig * lastDig; 29 | num /= 10; 30 | } 31 | 32 | if(n == cubeSum) { 33 | cout << "Armstrong number\n"; 34 | } else { 35 | cout << "NOT an Armstrong number\n"; 36 | } 37 | 38 | //Qs : Print Primes from 2 to N 39 | int N = 15; 40 | for(int i=2; i<=N; i++) { 41 | int curr = i; //current number to check for 42 | bool isPrime = true; 43 | for(int j=2; j*j<=i; j++) { 44 | if(curr % j == 0) { 45 | isPrime = false; 46 | } 47 | } 48 | 49 | if(isPrime) { 50 | cout << curr << " "; 51 | } 52 | } 53 | cout << endl; 54 | 55 | //Qs : Print N Fibonacci Numbers 56 | n = 10; 57 | int first = 0, sec = 1; 58 | cout << first << " " << sec << " "; 59 | 60 | for(int i=2; i 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n = 10; 7 | //Print numbers from 1 to n 8 | // for(int i=1; i<=n; i++) { 9 | // cout << i << " "; 10 | // } 11 | // cout << endl; 12 | 13 | //Print numbers from n to 1 (reverse order) 14 | // for(int i=n; i>0; i--) { 15 | // cout << i << " "; 16 | // } 17 | // cout << endl; 18 | 19 | //Print Sum of first N natural numbers 20 | // int sum = 0; 21 | // for(int i=1; i<=n; i++) { 22 | // sum += i; 23 | // } 24 | 25 | // cout << "Sum from 1 to n = " << sum << endl; 26 | 27 | //Square Pattern using For Loop 28 | // for(int i=1; i<=4; i++) { 29 | // cout << "****" << endl; 30 | // } 31 | 32 | //Sum of Digits of a Number 33 | // int digitSum = 0; 34 | // int num = 12345; 35 | 36 | // while(num > 0) { 37 | // digitSum += num % 10; 38 | // num /= 10; 39 | // } 40 | 41 | // cout << "sum of digits = " << digitSum << endl; 42 | 43 | //Sum of Odd Digits of a Number 44 | // int oddDigSum = 0; 45 | // int num = 12345; 46 | // while(num > 0) { 47 | // int lastDig = num % 10; 48 | // if(lastDig % 2 != 0) { 49 | // oddDigSum += lastDig; 50 | // } 51 | // num /= 10; 52 | // } 53 | 54 | // cout << "sum of odd digits = " << oddDigSum << endl; 55 | 56 | //Print a number's Digits in Reverse 57 | // int num = 12345; 58 | // 59 | // while(num > 0) { 60 | // cout << num % 10 << " "; 61 | // num /= 10; 62 | // } 63 | // cout << endl; 64 | 65 | //Reverse the given number & print the result 66 | // int num = 12345; 67 | // int res = 0; 68 | 69 | // while(num > 0) { 70 | // int lastDig = num % 10; 71 | // res = res * 10 + lastDig; 72 | // num /= 10; 73 | // } 74 | // cout << res << endl; 75 | 76 | //Check if a number is Prime or not 77 | int num = 49; 78 | bool isPrime = true; 79 | 80 | //Solution 1 81 | for(int i=2; i 90 | isPrime = true; 91 | for(int i=2; i<=sqrt(num); i++) { 92 | if(num % i == 0) { 93 | isPrime = false; 94 | break; 95 | } 96 | } 97 | cout << isPrime << endl; 98 | return 0; 99 | } -------------------------------------------------------------------------------- /06_Patterns/pattern slides cpp.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apna-college/Cpp-DSAClasses/df2f65cb2bfa221af792f6a63a363a99ad1271ae/06_Patterns/pattern slides cpp.pdf -------------------------------------------------------------------------------- /06_Patterns/patterns.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 4; 6 | 7 | //Number Square Pattern 8 | for(int i=1; i<=n; i++) { 9 | for(int j=1; j<=n; j++) { 10 | cout << i; 11 | } 12 | cout << endl; 13 | } 14 | 15 | // Star Pattern 16 | for(int i=1; i<=n; i++) { 17 | for(int j=1; j<=i; j++) { 18 | cout << "*"; 19 | } 20 | cout << endl; 21 | } 22 | 23 | //Inverted Star Pattern 24 | for(int i=1; i<=n; i++) { 25 | for(int j=1; j<=n-i+1; j++) { 26 | cout << "*"; 27 | } 28 | cout << endl; 29 | } 30 | 31 | // Half Pyramid Pattern 32 | for(int i=1; i<=n; i++) { 33 | for(int j=1; j<=i; j++) { 34 | cout << j; 35 | } 36 | cout << endl; 37 | } 38 | 39 | // Character Pyramid Pattern 40 | char ch = 'A'; 41 | for(int i=1; i<=n; i++) { 42 | for(int j=1; j<=i; j++) { 43 | cout << ch++; 44 | } 45 | cout << endl; 46 | } 47 | 48 | //Hollow Rectangle Pattern 49 | for(int i=1; i<=n; i++) { 50 | cout << "*"; 51 | for(int j=1; j<=n-1; j++) { 52 | if(i == 1 || i == n) { 53 | cout << "*"; 54 | } else { 55 | cout << " "; 56 | } 57 | } 58 | cout<< "*" << endl; 59 | } 60 | 61 | //Inverted & Rotated Half Pyramid 62 | for(int i=1; i<=n; i++) { 63 | //spaces 64 | for(int j=1; j<=n-i; j++) { 65 | cout << " "; 66 | } 67 | //stars 68 | for(int j=1; j<=i; j++) { 69 | cout << "*"; 70 | } 71 | cout << endl; 72 | } 73 | 74 | //Floyd's Triange Pattern 75 | int num = 1; 76 | for(int i=1; i<=5; i++) { 77 | for(int j=1; j<=i; j++) { 78 | cout << num++ << " "; 79 | } 80 | cout << endl; 81 | } 82 | 83 | //Diamond Pattern 84 | for(int i=1; i<=n; i++) { 85 | for(int j=1; j<=n-i; j++) { 86 | cout << " "; 87 | } 88 | for(int j=1; j<=2*i-1; j++) { 89 | cout << "*"; 90 | } 91 | cout << endl; 92 | } 93 | 94 | for(int i=n; i>0; i--) { 95 | for(int j=1; j<=n-i; j++) { 96 | cout << " "; 97 | } 98 | 99 | for(int j=1; j<=2*i-1; j++) { 100 | cout << "*"; 101 | } 102 | cout << endl; 103 | } 104 | 105 | //Butterfly Pattern 106 | for(int i=1; i<=n; i++) { 107 | for(int j=1; j<=i; j++) { 108 | cout << "*"; 109 | } 110 | for(int j=1; j<=2*(n-i); j++) { 111 | cout << " "; 112 | } 113 | for(int j=1; j<=i; j++) { 114 | cout << "*"; 115 | } 116 | cout << endl; 117 | } 118 | 119 | for(int i=n; i>0; i--) { 120 | for(int j=1; j<=i; j++) { 121 | cout << "*"; 122 | } 123 | for(int j=1; j<=2*(n-i); j++) { 124 | cout << " "; 125 | } 126 | for(int j=1; j<=i; j++) { 127 | cout << "*"; 128 | } 129 | cout << endl; 130 | } 131 | 132 | 133 | return 0; 134 | } -------------------------------------------------------------------------------- /07_Functions/functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Simple Function 5 | void sayHello() { 6 | cout << "Hello from Apna College\n"; 7 | } 8 | 9 | //Sum of 2 numbers 10 | int sum(int a, int b) { 11 | return a + b; 12 | } 13 | 14 | //Sum of 2 float - Function Overloading 15 | float sum(float x, float y) { 16 | return x + y; 17 | } 18 | 19 | //Product of 2 numbers 20 | int prod(int a, int b) { 21 | return a + b; 22 | } 23 | 24 | //Is Number Odd or Even 25 | void evenOrOdd(int n) { 26 | if(n % 2 == 0) { 27 | cout << "Even\n"; 28 | } else { 29 | cout << "Odd\n"; 30 | } 31 | } 32 | 33 | //Factorial of n 34 | int factorial(int n) { 35 | int fact = 1; 36 | for(int i=2; i<=n; i++) { 37 | fact = fact * i; 38 | } 39 | 40 | return fact; 41 | } 42 | 43 | //Prime or Not 44 | bool isPrime(int n) { 45 | if(n == 0 || n == 1) { 46 | return false; 47 | } 48 | 49 | for (int i = 2; i*i <= n; i++) { 50 | if(n % i == 0) { 51 | return false; 52 | } 53 | } 54 | 55 | return true; 56 | } 57 | 58 | //Binomial Coefficient nCr 59 | int binCoeff(int n, int r) { 60 | return factorial(n) / (factorial(n-r) * factorial(r)); 61 | } 62 | 63 | //All primes in range [2, n] 64 | void allPrimes(int n) { 65 | for(int i=2; i<=n; i++) { 66 | if(isPrime(i)) { 67 | cout << i << " "; 68 | } 69 | } 70 | 71 | cout << endl; 72 | } 73 | 74 | int main() { 75 | sayHello(); 76 | cout << sum(1, 2) << endl; 77 | cout << sum(1.0f, 2.0f) << endl; 78 | cout << prod(1, 2) << endl; 79 | evenOrOdd(25); 80 | cout << factorial(5) << endl; 81 | cout << isPrime(5) << endl; 82 | cout << binCoeff(4, 2) << endl; 83 | allPrimes(15); 84 | 85 | return 0; 86 | } 87 | 88 | -------------------------------------------------------------------------------- /08_Binary_Numbers/binary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Convert Binary to Decimal 5 | void binToDecimal(int n) { 6 | int res = 0; 7 | int p = 1; 8 | while(n > 0) { 9 | int lastDig = n % 10; 10 | res += lastDig * p; 11 | p = p * 2; 12 | n = n / 10; 13 | } 14 | cout << "Decimal form = " << res << "\n"; 15 | } 16 | 17 | //Convert Decimal to Binary 18 | void decToBinary(int n) { 19 | int res = 0; 20 | int p = 1; 21 | while(n > 0) { 22 | int rem = n % 2; 23 | res += rem * p; 24 | p = p * 10; 25 | n = n / 2; 26 | } 27 | cout << "Binary form = " << res << "\n"; 28 | } 29 | 30 | int main() { 31 | binToDecimal(101); 32 | decToBinary(5); 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /09_Pointers/pointers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void passValue(int param) { 5 | param = 1000; 6 | } 7 | 8 | //using Pointers 9 | void passReference(int *param) { 10 | *param = 1000; 11 | } 12 | 13 | //using Reference Variable 14 | void passReference2(int ¶m) { 15 | param = 5000; 16 | } 17 | 18 | int main() { 19 | int a = 10; 20 | int *ptr = &a; 21 | int **ptr2 = &ptr; 22 | 23 | cout << &a << " = " << ptr << endl; 24 | cout << &ptr << " = " << ptr2 << endl; 25 | 26 | //Dereference Operator 27 | cout << a << " = " << *ptr << " = " << *(&a) << endl; 28 | 29 | *ptr = 20; 30 | cout << a << endl; //should be 20 now 31 | 32 | //Null Pointer 33 | float* nptr = NULL; 34 | cout << nptr << endl; 35 | 36 | //cout << *nptr; // Gives Segmentation Fault 37 | 38 | //Pass by value 39 | int x = 0; 40 | passValue(x); 41 | cout << x << endl; 42 | 43 | //Pass by reference 44 | passReference(&x); 45 | cout << x << endl; 46 | 47 | //Reference Variable 48 | int i = 5; 49 | int &j = i; 50 | j++; 51 | cout << i << endl; 52 | 53 | passReference2(x); 54 | cout << x << endl; 55 | } 56 | 57 | 58 | -------------------------------------------------------------------------------- /10_Array_1/arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printArr(int *arr, int n) { 5 | for(int i=0; i max) { 25 | max = arr[i]; 26 | } 27 | } 28 | 29 | cout << "max = " << max << endl; 30 | } 31 | 32 | //copy & paste method 33 | void reverseArr1(int *arr, int n) { 34 | int newArr[n]; 35 | for(int i=0; i arr[mid]) { 69 | //search in 2nd half 70 | start = mid + 1; 71 | } else { 72 | //search in 1st half 73 | end = mid - 1; 74 | } 75 | } 76 | 77 | return -1; 78 | } 79 | 80 | //Print all Subarrays of an Array 81 | void printSubarrays(int *arr, int n) { 82 | for(int start=0; start 2 | using namespace std; 3 | 4 | //max sum subarray (brute force - O(N^3)) 5 | int maxSumSubarray(int *arr, int n) { 6 | int maxSum = INT_MIN; 7 | for(int start=0; start=0; i--) { 68 | maxRight[i] = max(maxTillNow, heights[i+1]); 69 | maxTillNow = max(maxTillNow, heights[i]); 70 | } 71 | 72 | int water = 0; 73 | for(int i=0; i 0) { 76 | water += ht; 77 | } 78 | } 79 | 80 | cout << "water trapped = " << water << "\n"; 81 | return water; 82 | } 83 | 84 | //Buy & Sell Stocks 85 | int maxProfit(int *prices, int n) { 86 | int bestBuy[100000] = {0}; 87 | bestBuy[0] = INT_MAX; 88 | 89 | for(int i=1; i 2 | using namespace std; 3 | 4 | //Print Array 5 | void printArr(int arr[], int n) { 6 | for(int i=0; i arr[j+1]) { 23 | swap(arr[j], arr[j+1]); 24 | } 25 | } 26 | } 27 | 28 | printArr(arr, n); 29 | } 30 | 31 | void selectionSort(int arr[], int n) { 32 | for(int i=0; i= 0 && arr[prev] > curr) { 52 | arr[prev+1] = arr[prev]; 53 | prev--; 54 | } 55 | 56 | //swap 57 | swap(arr[prev+1], curr); 58 | } 59 | 60 | printArr(arr, n); 61 | } 62 | 63 | void countSort(int arr[], int n) { 64 | int range = INT_MIN; 65 | for(int i=0; i 0) { 76 | arr[i] = i; 77 | freqArr[i]--; 78 | } 79 | } 80 | 81 | printArr(arr, n); 82 | } 83 | 84 | 85 | 86 | void sortChars(char arr[], int n) { 87 | for(int i=1; i= 0 && arr[prev] < curr) { 92 | arr[prev+1] = arr[prev]; 93 | prev--; 94 | } 95 | 96 | swap(arr[prev+1], curr); 97 | } 98 | 99 | printArr(arr, n); 100 | } 101 | 102 | int main() { 103 | int arr[5] = {5, 4, 1, 3, 2}; 104 | //bubbleSort(arr, 5); 105 | //selectionSort(arr, 5); 106 | //insertionSort(arr, 5); 107 | 108 | int arr2[8] = {1, 4, 1, 3, 2, 4, 3, 7}; 109 | //countSort(arr2, 8); 110 | 111 | sort(arr2, arr2+8); 112 | printArr(arr2, 8); 113 | 114 | char ch[] = { 'f', 'b', 'a', 'e', 'c', 'd'}; 115 | sortChars(ch, 6); 116 | 117 | return 0; 118 | } 119 | 120 | 121 | -------------------------------------------------------------------------------- /13_2DArray/2DArrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printSpiral(int matrix[][4], int n, int m) { 5 | int scol = 0, srow = 0; 6 | int ecol = m-1, erow = n-1; 7 | 8 | while(srow <= erow && scol <= ecol) { 9 | //top 10 | for(int j=scol; j<=ecol; j++) { 11 | cout << matrix[srow][j] << " "; 12 | } 13 | 14 | //right 15 | for(int i=srow+1; i<=erow; i++) { 16 | cout << matrix[i][ecol] << " "; 17 | } 18 | 19 | //bottom 20 | for(int j=ecol-1; j>=scol; j--) { 21 | if(srow == erow) { 22 | break; 23 | } 24 | cout << matrix[erow][j] << " "; 25 | } 26 | 27 | //left 28 | for(int i=erow-1; i>=srow+1; i--) { 29 | if(scol == ecol) { 30 | break; 31 | } 32 | cout << matrix[i][scol] << " "; 33 | } 34 | 35 | srow++; scol++; 36 | erow--; ecol--; 37 | } 38 | 39 | cout << endl; 40 | } 41 | 42 | void diagonalSum(int mat[][4], int n) { 43 | int sum = 0; 44 | 45 | //O(n^2) 46 | for(int i=0; i sum = " << sum << endl; 56 | 57 | //O(n) 58 | sum = 0; 59 | for(int i=0; i sum = " << sum << endl; 65 | } 66 | 67 | bool search(int mat[][4], int n, int m, int key) { 68 | int i=0, j=m-1; 69 | while(i < n && j >= 0) { 70 | if(mat[i][j] == key) { 71 | cout << "found at (" << i << "," << j << ")\n"; 72 | return true; 73 | } else if(mat[i][j] > key) { 74 | //go left 75 | j--; 76 | } else { 77 | //go right 78 | i++; 79 | } 80 | } 81 | 82 | cout << "key NOT found\n"; 83 | return false; 84 | } 85 | 86 | void func(int (*ptr)[4]) { 87 | cout << ptr << endl; 88 | cout << ptr+1 << endl; 89 | 90 | //address(pointer) of row 91 | cout << "0th row address" << ptr << endl; 92 | cout << "1st row address" << (ptr+1) << endl; 93 | 94 | //actual row 95 | cout << "0th row start" << *ptr << endl; 96 | cout << "1st row start" << *(ptr+1) << endl; 97 | 98 | //to get j=2 column in i=0 row 99 | cout << "(0, 2) = " << *(*ptr+2) << endl; 100 | 101 | //to get j=2 column in i=1 row 102 | cout << "(1, 2) = " << *(*(ptr+1)+2) << endl; 103 | 104 | } 105 | 106 | int main() { 107 | int matrix[4][4] = {{1, 2, 3, 4}, 108 | {5, 6, 7, 8}, 109 | {9, 10, 11, 12}, 110 | {13, 14, 15, 16}}; 111 | 112 | int matrix2[3][4] = {{1, 2, 3, 4}, 113 | {5, 6, 7, 8}, 114 | {9, 10, 11, 12}}; 115 | 116 | printSpiral(matrix, 4, 4); 117 | //Expected : 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 118 | 119 | printSpiral(matrix2, 3, 4); 120 | 121 | diagonalSum(matrix, 4); 122 | 123 | int mat[4][4] = {{10, 20, 30, 40}, 124 | {15, 25, 35, 45}, 125 | {27, 29, 37, 48}, 126 | {32, 33, 39, 50}}; 127 | 128 | search(mat, 4, 4, 33); 129 | search(mat, 4, 4, 100); 130 | 131 | //Pointers & 2D Arrays 132 | func(matrix); 133 | return 0; 134 | } 135 | 136 | 137 | -------------------------------------------------------------------------------- /14_String/strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Convert to UpperCase 7 | void toUpper(char str[], int n) { 8 | for(int i=0; i= 'A' && str[i] <= 'Z') { 10 | continue; 11 | } else { 12 | int diff = str[i] - 'a'; 13 | str[i] = 'A' + diff; 14 | } 15 | } 16 | } 17 | 18 | //Reverse of a char Array 19 | void reverse(char str[], int n) { 20 | int st = 0, end = n-1; 21 | 22 | while(st < end) { 23 | swap(str[st++], str[end--]); 24 | } 25 | } 26 | 27 | //Valid Palindrome 28 | bool isValid(char str[], int n) { 29 | int st = 0, end = n-1; 30 | 31 | while(st < end) { 32 | if(str[st++] != str[end--]) { 33 | cout << "not valid"; 34 | return false; 35 | } 36 | } 37 | cout << "valid"; 38 | return true; 39 | } 40 | 41 | //Valid Anagram 42 | bool isAnagram(string s, string t) { 43 | if(s.length() != t.length()) { 44 | return false; 45 | } 46 | 47 | int count[26] = {0}; 48 | for(int i=0; i functions 70 | /*strcpy(word, "c++"); 71 | cout << "now your word is : " << word << endl; 72 | 73 | strcat(word, " is my favorite"); 74 | cout << word << endl; 75 | 76 | cout << strcmp("hello", "hello") << endl; 77 | cout << strcmp("abc", "xyz") << endl;*/ 78 | 79 | //C++ Strings 80 | string str; 81 | getline(cin, str); 82 | cout << str << endl; 83 | 84 | //String Member Functions 85 | cout << str.length() << endl; 86 | cout << str.at(0) << endl; 87 | cout << str.substr(2, 3) << endl; 88 | cout << str.find("college") << endl; 89 | cout << str.find("xyz") << endl; 90 | return 0; 91 | } 92 | 93 | 94 | -------------------------------------------------------------------------------- /15_Vector/vectors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // Pair Sum 6 | vector twoSum(vector& nums, int target) { 7 | int st = 0, end = nums.size()-1; 8 | vector ans; 9 | 10 | while(st < end) { 11 | int sum = nums[st] + nums[end]; 12 | if(sum == target) { 13 | ans.push_back(st+1); 14 | ans.push_back(end+1); 15 | break; 16 | } else if(sum > target) { 17 | end--; 18 | } else { 19 | st++; 20 | } 21 | } 22 | 23 | return ans; 24 | } 25 | 26 | int main() { 27 | //Dynamic Allocation & Deallocation 28 | //Example - 1 29 | int *ptr = new int; 30 | *ptr = 100; 31 | cout << *ptr << endl; 32 | delete ptr; 33 | 34 | //1D Dynamic Array 35 | int size; 36 | cout << "enter size of array : "; 37 | cin >> size; 38 | int *arr = new int[size]; 39 | 40 | for(int i=0; i> rows; 52 | cout << "enter cols : "; 53 | cin >> cols; 54 | 55 | int **matrix = new int*[rows]; 56 | for(int i=0; i vec1; 71 | 72 | vector vec2 = {1, 2, 3, 4}; 73 | 74 | vector vec3(5, -1); 75 | 76 | vector vec = {1, 2, 3, 4}; 77 | cout << vec.size() << endl; 78 | cout << vec.capacity() << endl; 79 | 80 | //print elements 81 | for(int i=0; i> matrix = {{1}, {2, 3}, {4, 5, 6}}; 91 | 92 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | bool isPowerOf2(int num) { 6 | 7 | if((num & (num-1)) == 0) { 8 | return true; 9 | } else { 10 | return false; 11 | } 12 | } 13 | 14 | void updateIthBit(int num, int i, int val) { 15 | num = num & ~(1 << i); 16 | 17 | int mask = val << i; 18 | num = num | mask; 19 | 20 | cout << num << endl; 21 | } 22 | 23 | void clearLastIBits(int num, int i) { 24 | int mask = (~0) << i; 25 | num = num & mask; 26 | 27 | cout << num << endl; 28 | } 29 | 30 | void clearBitsInRange(int num, int i, int j) { 31 | int a = (~0) >> (j+1); 32 | int b = (1 << i) - 1; 33 | int mask = a | b; 34 | num = num & mask; 35 | 36 | cout << num << endl; 37 | } 38 | 39 | int countSetBits(int num) { 40 | int count = 0; 41 | 42 | while(num > 0) { 43 | if(num & 1) { 44 | count++; 45 | } 46 | num = num >> 1; 47 | } 48 | 49 | cout << count << endl; 50 | return count; 51 | } 52 | 53 | //calculate x^n 54 | int fastExponentiation(int x, int n) { 55 | int ans = 1; 56 | 57 | while(n > 0) { 58 | int lastBit = n & 1; 59 | if(lastBit) 60 | ans = ans * x; 61 | 62 | x = x*x; 63 | n = n >> 1; 64 | } 65 | 66 | cout << ans << endl; 67 | return ans; 68 | } 69 | 70 | int main() { 71 | /* Binary Operators - AND, OR, XOR */ 72 | int x = 3, y = 5; 73 | 74 | cout << (x & y) << endl; 75 | cout << (x | y) << endl; 76 | cout << (x ^ y) << endl; 77 | 78 | //Binary NOT 79 | cout << ~0 << endl; 80 | cout << ~6 << endl; 81 | 82 | //Binary Shift 83 | cout << (7 << 2) << endl;// same as (7 * 2^2) 84 | cout << (7 >> 2) << endl;// same as (7 / 2^2) 85 | 86 | //Practice Qs 87 | cout << ~4 << endl; 88 | cout << (8 >> 1) << endl; 89 | 90 | //Odd or Even 91 | int num = 7; 92 | bool isOdd = num & 1; 93 | cout << (isOdd ? "odd" : "even") << endl; 94 | 95 | //Get ith bit 96 | num = 6; 97 | int i = 2; 98 | int mask = 1 << i; 99 | if(num & mask) { 100 | cout << "bit is 1\n"; 101 | } else { 102 | cout << "bit is 0\n"; 103 | } 104 | 105 | //Set ith bit 106 | num = 6; 107 | i = 3; 108 | mask = 1 << i; 109 | num = num | mask; 110 | cout << num << endl; //14 is expected; 111 | 112 | //Clear ith Bit 113 | num = 6; 114 | i = 1; 115 | mask = ~(1 << i); 116 | num = num & mask; 117 | cout << num << endl; //4 is expected; 118 | 119 | //Power of 2 120 | cout << isPowerOf2(8) << endl; 121 | cout << isPowerOf2(7) << endl; 122 | 123 | //Update ith Bit 124 | updateIthBit(7, 2, 0); 125 | updateIthBit(7, 3, 1); 126 | 127 | //Clear last i bits 128 | clearLastIBits(15, 2); 129 | 130 | //Count Set Bits; 131 | countSetBits(10); 132 | countSetBits(7); 133 | 134 | //Fast Exponentiation 135 | fastExponentiation(3, 4); 136 | return 0; 137 | } 138 | 139 | 140 | -------------------------------------------------------------------------------- /17_OOPS1/oops1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Student { 6 | private: 7 | float cgpa; 8 | 9 | public: 10 | string name; 11 | 12 | //Setter 13 | void setCgpa(float newCgpa) { 14 | if(newCgpa < 0) { 15 | cout << "Invalid Data\n"; 16 | return; 17 | } 18 | cgpa = newCgpa; 19 | } 20 | 21 | //Getter 22 | float getCgpa() { 23 | return cgpa; 24 | } 25 | 26 | void getPercentage() { 27 | cout << (cgpa * 10) << "% \n"; 28 | } 29 | }; 30 | 31 | class Car { 32 | string name; 33 | int price; 34 | 35 | public: 36 | int *mileage; 37 | //Contructor 38 | Car() { 39 | cout << "Creating & Initializing a new car..\n"; 40 | } 41 | 42 | //Parameterized Contructor 43 | Car(string name, int price) { 44 | cout << "Creating & Initializing a new car..\n"; 45 | this->name = name; 46 | this->price = price; 47 | mileage = new int; 48 | *mileage = 12; 49 | } 50 | 51 | //Custom Copy Constructor 52 | Car(Car &original) { 53 | cout << "copying..\n"; 54 | name = original.name; 55 | price = original.price; 56 | mileage = new int; 57 | *mileage = *original.mileage; 58 | } 59 | 60 | //Getter 61 | string getName() { 62 | return name; 63 | } 64 | 65 | int getPrice() { 66 | return price; 67 | } 68 | 69 | int getMileage() { 70 | return *mileage; 71 | } 72 | 73 | //Setter 74 | // void setMileage(int mileage) { 75 | // mileage = &mileage; 76 | // } 77 | 78 | ~Car() { 79 | cout << "object deletion..\n"; 80 | if(mileage != NULL) { 81 | delete mileage; 82 | mileage = NULL; 83 | } 84 | } 85 | }; 86 | 87 | //Inheritance 88 | class Animal { 89 | string color; 90 | 91 | public: 92 | void eat() { 93 | cout << "eats\n"; 94 | } 95 | 96 | void breathe() { 97 | cout << "breathes\n"; 98 | } 99 | }; 100 | 101 | class Fish : public Animal { 102 | int fins; 103 | 104 | public: 105 | void swim() { 106 | cout << "swims\n"; 107 | } 108 | }; 109 | 110 | int main() { 111 | // Student s1; 112 | // cout << sizeof(s1) << endl; 113 | // s1.name = "shradha"; 114 | // cout << s1.name << endl; 115 | 116 | // //Setter 117 | // s1.setCgpa(9.0); 118 | 119 | // //Getter 120 | // cout << s1.getCgpa() << endl; 121 | // s1.getPercentage(); 122 | 123 | //Contructors 124 | // Car c1("maruti 800", 4); 125 | // cout << c1.getName() << endl; 126 | // cout << c1.getMileage() << endl; 127 | 128 | 129 | //Default Copy Constructor 130 | // Car c2(c1); 131 | // cout << "**************\n"; 132 | // cout << c1.getName() << endl; 133 | // cout << c1.getPrice() << endl; 134 | // cout << c2.getMileage() << endl; 135 | 136 | // *c2.mileage = 15; 137 | // cout << c2.getMileage() << endl; 138 | // cout << c1.getMileage() << endl; 139 | 140 | //Inheritance 141 | Fish f1; 142 | f1.eat(); 143 | f1.swim(); 144 | return 0; 145 | } 146 | 147 | 148 | -------------------------------------------------------------------------------- /18_OOPS2/oops2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | //Function Overloading 6 | class Print { 7 | public: 8 | void show(int x) { 9 | cout << "int : " << x << endl; 10 | } 11 | 12 | void show(string str) { 13 | cout << "string : " << str << endl; 14 | } 15 | }; 16 | 17 | //Operator Overloading 18 | class Complex { 19 | int real; 20 | int img; 21 | 22 | public: 23 | Complex(int r, int i) { 24 | real = r; 25 | img = i; 26 | } 27 | 28 | void showNum() { 29 | cout << real << " + " << img << "i\n"; 30 | } 31 | 32 | Complex operator - (Complex &obj) { 33 | int resReal = this->real - obj.real; 34 | int resImg = this->img - obj.img; 35 | Complex res(resReal, resImg); 36 | return res; 37 | } 38 | 39 | Complex operator + (Complex &obj) { 40 | int resReal = this->real + obj.real; 41 | int resImg = this->img + obj.img; 42 | Complex res(resReal, resImg); 43 | return res; 44 | } 45 | }; 46 | 47 | //Function Overriding & Virtual Function 48 | class Parent { 49 | public: 50 | void show() { 51 | cout << "parent show\n"; 52 | } 53 | 54 | virtual void hello() { 55 | cout << "hello from parent\n"; 56 | } 57 | }; 58 | 59 | class Child : public Parent { 60 | public: 61 | void show() { 62 | cout << "child show\n"; 63 | } 64 | 65 | void hello() { 66 | cout << "hello from child\n"; 67 | } 68 | }; 69 | 70 | //Abstract Classes & Pure Virtual Functions 71 | 72 | class Shape { 73 | //This is an abstract class 74 | public: 75 | virtual void draw() = 0; //Pure Virtual Function 76 | }; 77 | 78 | class Square : public Shape { 79 | public: 80 | void draw() { 81 | cout << "drawing a rectangle\n"; 82 | } 83 | }; 84 | 85 | class Circle : public Shape { 86 | public: 87 | void draw() { 88 | cout << "drawing a circle\n"; 89 | } 90 | }; 91 | 92 | //Static 93 | void counter() { 94 | static int count = 0; 95 | count++; 96 | cout << "count : " << count << endl; 97 | } 98 | 99 | class Example { 100 | public: 101 | Example() { 102 | cout << "constructor\n"; 103 | } 104 | 105 | ~Example() { 106 | cout << "destructor\n"; 107 | } 108 | }; 109 | 110 | //Friend Class & Function 111 | class A { 112 | string secret = "private secret"; 113 | friend class B; 114 | friend void shareSecret(A &obj); 115 | }; 116 | 117 | class B { 118 | public: 119 | void showSecret(A &obj) { 120 | cout << obj.secret << endl; 121 | } 122 | }; 123 | 124 | void shareSecret(A &obj) { 125 | cout << obj.secret << endl; 126 | } 127 | 128 | int main() { 129 | Print p1; 130 | p1.show(50); 131 | p1.show("apnacollege"); 132 | 133 | Complex num1(1, 2); 134 | Complex num2(3, 4); 135 | Complex res = num1 + num2; 136 | res.showNum(); 137 | 138 | Child c1; 139 | c1.show(); 140 | 141 | Child c2; 142 | Parent *par1; 143 | par1 = &c2; //Binding at runtime 144 | par1->hello(); 145 | 146 | Circle cir1; 147 | Square squ1; 148 | cir1.draw(); 149 | squ1.draw(); 150 | 151 | counter(); 152 | counter(); 153 | counter(); 154 | 155 | //Static object 156 | int x = 0; 157 | if(x == 0) { 158 | static Example eg1; 159 | } 160 | cout << "exiting main function.\n"; 161 | 162 | A a1; 163 | B b1; 164 | b1.showSecret(a1); 165 | shareSecret(a1); 166 | return 0; 167 | } 168 | 169 | 170 | -------------------------------------------------------------------------------- /19_Recursion1/recursion1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Simple Recursive Function with Stack Overflow 7 | void func() { 8 | cout << "function call\n"; 9 | func(); 10 | } 11 | 12 | //Factorial 13 | int factorial(int n) { 14 | if(n == 1) { 15 | return 1; 16 | } 17 | return n * factorial(n-1); 18 | } 19 | 20 | //Print Decreasing 21 | void print(int n) { 22 | if(n == 0) { 23 | return; 24 | } 25 | 26 | cout << n << " "; 27 | print(n-1); 28 | } 29 | 30 | //Sum of N Natural Numbers 31 | int sum(int n) { 32 | if(n == 1) { 33 | return 1; 34 | } 35 | 36 | return n + sum(n-1); 37 | } 38 | 39 | //Nth Fibonacci 40 | int fibonacci(int n) { 41 | if(n == 0 || n == 1) { 42 | return n; 43 | } 44 | 45 | return fibonacci(n-1) + fibonacci(n-2); 46 | } 47 | 48 | //Is Array Sorted 49 | bool isSorted(int arr[], int i, int n) { 50 | if(i == n-1) { 51 | return true; 52 | } 53 | 54 | if(arr[i] > arr[i+1]) { 55 | return false; 56 | } 57 | 58 | return isSorted(arr, i+1, n); 59 | } 60 | 61 | //First Occurrence 62 | int firstOccur(vector vec, int target, int i) { 63 | if(i == vec.size()) { 64 | return -1; 65 | } 66 | 67 | if(vec[i] == target) { 68 | return i; 69 | } 70 | 71 | return firstOccur(vec, target, i+1); 72 | } 73 | 74 | //Last Occurrence 75 | int lastOccur(vector vec, int target, int i) { 76 | if(i == vec.size()) { 77 | return -1; 78 | } 79 | 80 | int idxFound = lastOccur(vec, target, i+1); 81 | 82 | if(idxFound == -1 && vec[i] == target) { 83 | return i; 84 | } 85 | 86 | return idxFound; 87 | } 88 | 89 | //X^N 90 | int pow(int x, int n) { 91 | if(n == 0) { 92 | return 1; 93 | } 94 | 95 | int halfPow = pow(x, n/2); 96 | int halfPowSquare = halfPow * halfPow; 97 | if(n % 2 == 0) { 98 | //even 99 | return halfPowSquare; 100 | } else { 101 | return x * halfPowSquare; 102 | } 103 | } 104 | 105 | int main() { 106 | cout << factorial(5) << endl; 107 | 108 | print(6); 109 | cout << endl; 110 | 111 | cout << sum(5) << endl; 112 | 113 | cout << fibonacci(6) << endl; 114 | 115 | int arr1[5] = {1, 2, 3, 4, 5}; 116 | int arr2[5] = {1, 2, 4, 3, 5}; 117 | cout << isSorted(arr1, 0, 5) << endl; 118 | cout << isSorted(arr2, 0, 5) << endl; 119 | 120 | vector vec = {1, 2, 3, 3, 3, 4}; 121 | cout << firstOccur(vec, 3, 0) << endl; 122 | cout << lastOccur(vec, 3, 0) << endl; 123 | 124 | cout << pow(2, 10) << endl; 125 | return 0; 126 | } 127 | 128 | 129 | -------------------------------------------------------------------------------- /20_Recursion2/rec2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | //Tiling Problem 6 | int countWays(int n) { 7 | if(n == 0 || n == 1) { 8 | return 1; 9 | } 10 | 11 | //vertical choice 12 | int ways1 = countWays(n-1); 13 | 14 | //horizonal choice 15 | int ways2 = countWays(n-2); 16 | 17 | return ways1 + ways2; 18 | } 19 | 20 | //Remove Duplicates 21 | void removeDuplicates(string str, int i, bool map[26], string ans) { 22 | if(i == str.length()) { 23 | cout << "ans : " << ans << endl; 24 | return; 25 | } 26 | 27 | int mapIdx = (int)(str.at(i) - 'a'); 28 | 29 | if(map[mapIdx]) { 30 | removeDuplicates(str, i+1, map, ans); 31 | } else { 32 | map[mapIdx] = true; 33 | removeDuplicates(str, i+1, map, ans+str.at(i)); 34 | } 35 | 36 | } 37 | 38 | //Friends Pairing Problem 39 | int pairFriends(int n) { 40 | if(n == 1 || n == 2) { 41 | return n; 42 | } 43 | 44 | return pairFriends(n-1) + (n-1) * pairFriends(n-2); 45 | } 46 | 47 | //Binary Strings Problem 48 | void binStrings(int n, string ans, int lastPlace) { 49 | if(n == 0) { 50 | cout << ans << endl; 51 | return; 52 | } 53 | 54 | binStrings(n-1, ans+'0', 0); 55 | 56 | if(lastPlace != 0) { 57 | binStrings(n-1, ans+'1', 1); 58 | } 59 | } 60 | 61 | void binStrings(int n, string ans) { 62 | if(n == 0) { 63 | cout << ans << endl; 64 | return; 65 | } 66 | 67 | binStrings(n-1, ans+'0'); 68 | 69 | if(ans[ans.size()-1] != '1') { 70 | binStrings(n-1, ans+'1'); 71 | } 72 | } 73 | 74 | int main() { 75 | cout << countWays(4) << endl; 76 | 77 | string ans = ""; 78 | bool map[26] = {false}; 79 | removeDuplicates("appnnacollege", 0, map, ans); //apncoleg 80 | 81 | cout << pairFriends(3) << endl; 82 | 83 | ans = ""; 84 | //binStrings(3, ans, 0); 85 | 86 | binStrings(3, ans); 87 | return 0; 88 | } 89 | 90 | 91 | -------------------------------------------------------------------------------- /21_Divide_and_Conquer/divideAndConquer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void printArr(int arr[], int n) { 6 | for(int i=0; i temp; 14 | int i=si, j=mid+1; 15 | 16 | while(i <= mid && j <= ei) { 17 | if(arr[i] <= arr[j]) { 18 | temp.push_back(arr[i++]); 19 | } else { 20 | temp.push_back(arr[j++]); 21 | } 22 | } 23 | 24 | while(i <= mid) { 25 | temp.push_back(arr[i++]); 26 | } 27 | 28 | while(j <= ei) { 29 | temp.push_back(arr[j++]); 30 | } 31 | 32 | //copy back to original 33 | for(int idx=si, x=0; idx<=ei; idx++) { 34 | arr[idx] = temp[x++]; 35 | } 36 | } 37 | 38 | void mergeSort(int arr[], int si, int ei) { 39 | if(si >= ei) { 40 | return; 41 | } 42 | 43 | int mid = si + (ei - si)/2; 44 | mergeSort(arr, si, mid); 45 | mergeSort(arr, mid+1, ei); 46 | 47 | merge(arr, si, mid, ei); 48 | } 49 | 50 | int partition(int arr[], int si, int ei) { 51 | int pivot = arr[ei]; 52 | int i=si-1; 53 | 54 | for(int j=si; j= ei) { 69 | return; 70 | } 71 | 72 | int pivotIdx = partition(arr, si, ei); 73 | quickSort(arr, si, pivotIdx-1); //left 74 | quickSort(arr, pivotIdx+1, ei); //right 75 | } 76 | 77 | int search(int arr[], int si, int ei, int tar) { 78 | if(si > ei) { 79 | return -1; 80 | } 81 | 82 | int mid = si + (ei - si)/2; 83 | 84 | if(arr[mid] == tar) { 85 | return mid; 86 | } 87 | 88 | if(arr[si] <= arr[mid]) { //Line 1 89 | if(arr[si] <= tar && tar <= arr[mid]) { 90 | //go left 91 | return search(arr, si, mid-1, tar); 92 | } else { 93 | //go right 94 | return search(arr, mid+1, ei, tar); 95 | } 96 | } else { 97 | //Line2 98 | if(tar >= arr[mid] && tar <= arr[ei]) { 99 | //go right 100 | return search(arr, mid+1, ei, tar); 101 | } else { 102 | //go left 103 | return search(arr, si, mid-1, tar); 104 | } 105 | } 106 | } 107 | 108 | int main() { 109 | int arr[6] = {6, 3, 7, 5, 2, 4}; 110 | int n = 6; 111 | 112 | //mergeSort(arr, 0, n-1); 113 | 114 | quickSort(arr, 0, n-1); 115 | printArr(arr, n); 116 | 117 | int arr2[7] = {4, 5, 6, 7, 0, 1, 2}; 118 | int tar = 0; 119 | cout << "idx : " << search(arr2, 0, 6, tar) << endl; 120 | return 0; 121 | } 122 | 123 | 124 | -------------------------------------------------------------------------------- /24_Backtracking/backtracking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void printArr(int arr[], int n) { 7 | for(int i=0; i> board) { 49 | int n = board.size(); 50 | for(int i=0; i> board, int row, int col) { 61 | int n = board.size(); 62 | //vertical 63 | for(int j=0; j=0 && j>=0; i--, j--) { 78 | if(board[i][j] == 'Q') { 79 | return false; 80 | } 81 | } 82 | 83 | //diagonal right 84 | for(int i=row-1, j=col+1; i>=0 && j> board, int row) { 94 | int n = board.size(); 95 | if(row == n) { 96 | printBoard(board); 97 | return 1; 98 | } 99 | 100 | int count = 0; 101 | for(int j=0; j= n || j >= m) { 118 | return 0; 119 | } 120 | 121 | int totWays = 0; 122 | 123 | //go right 124 | totWays += gridWays(i, j+1, n, m); 125 | 126 | //go down 127 | totWays += gridWays(i+1, j, n, m); 128 | 129 | return totWays; 130 | } 131 | 132 | bool isSafe(int sudoku[9][9], int row, int col, int val) { 133 | //same col 134 | for(int i=0; i<9; i++) { 135 | if(sudoku[i][col] == val) { 136 | return false; 137 | } 138 | } 139 | 140 | //same row 141 | for(int j=0; j<9; j++) { 142 | if(sudoku[row][j] == val) { 143 | return false; 144 | } 145 | } 146 | 147 | //same 3x3 grid 148 | int sr = (row/3) * 3; 149 | int sc = (col/3) * 3; 150 | 151 | for(int i=sr; i> board; 216 | 217 | for(int i=0; i newRow; 219 | for(int j=0; j 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | 9 | Node(int data) { 10 | this->data = data; 11 | next = NULL; 12 | } 13 | 14 | ~Node() { 15 | if(next != NULL) { 16 | delete next; 17 | next = NULL; 18 | } 19 | } 20 | }; 21 | 22 | class List { 23 | Node* head; 24 | Node* tail; 25 | 26 | public: 27 | List() { 28 | head = NULL; 29 | tail = NULL; 30 | } 31 | 32 | void push_front(int val) { 33 | Node* newNode = new Node(val); 34 | 35 | if(tail == NULL) { 36 | head = tail = newNode; 37 | } else { 38 | newNode->next = head; 39 | head = newNode; 40 | } 41 | } 42 | 43 | void push_back(int val) { 44 | Node* newNode = new Node(val); 45 | 46 | if(tail == NULL) { 47 | head = tail = newNode; 48 | } else { 49 | tail->next = newNode; 50 | tail = newNode; 51 | } 52 | } 53 | 54 | void printList() { 55 | Node* temp = head; 56 | while(temp != NULL) { 57 | cout << temp->data << " -> "; 58 | temp = temp->next; 59 | } 60 | cout << "NULL\n"; 61 | } 62 | 63 | void insert(int val, int pos) { 64 | if(pos == 0) { 65 | push_front(val); 66 | return; 67 | } 68 | 69 | Node* temp = head; 70 | int i=0; 71 | while(temp != NULL && inext; 73 | i++; 74 | } 75 | 76 | if(temp == NULL) { 77 | cout << "Invalid position\n"; 78 | return; 79 | } 80 | 81 | Node* newNode = new Node(val); 82 | newNode->next = temp->next; 83 | temp->next = newNode; 84 | } 85 | 86 | void pop_front() { 87 | if(head == NULL) { 88 | return; 89 | } 90 | 91 | Node* temp = head; 92 | head = head->next; 93 | 94 | temp->next = NULL; 95 | delete temp; 96 | } 97 | 98 | void pop_back() { 99 | if(head == NULL) { //0 els 100 | return; 101 | } 102 | if(head->next == NULL) { //1 el 103 | delete head; 104 | head = NULL; 105 | return; 106 | } 107 | 108 | Node* temp = head; 109 | while(temp->next->next != NULL) { 110 | temp = temp->next; 111 | } 112 | //temp has tail's prev 113 | temp->next = NULL; 114 | 115 | delete tail; 116 | tail = temp; 117 | } 118 | 119 | ~List() { 120 | if(head != NULL) { 121 | delete head; 122 | head = NULL; 123 | } 124 | } 125 | 126 | int searchItr(int key) { 127 | Node* temp = head; 128 | int idx = 0; 129 | 130 | while(temp != NULL) { 131 | if(temp->data == key) { 132 | return idx; 133 | } 134 | temp = temp->next; 135 | idx++; 136 | } 137 | 138 | return -1; 139 | } 140 | 141 | int searchRec(int key) { 142 | return searchHelper(head, key); 143 | } 144 | 145 | int searchHelper(Node* h, int key) { 146 | if(h == NULL) { 147 | return -1; 148 | } 149 | 150 | if(h-> data == key) { 151 | return 0; //current idx 152 | } 153 | 154 | int ans = searchHelper(h->next, key); 155 | if(ans == -1) { 156 | return -1; 157 | } 158 | 159 | return ans + 1; 160 | } 161 | 162 | void reverseLL() { 163 | Node* prev = NULL; 164 | Node* curr = head; 165 | 166 | while(curr != NULL) { 167 | Node* next = curr->next; 168 | curr->next = prev; 169 | 170 | prev = curr; 171 | curr = next; 172 | } 173 | 174 | head = prev; 175 | } 176 | 177 | int sizeLL() { 178 | Node* temp = head; 179 | int sz = 0; 180 | 181 | while(temp != NULL) { 182 | temp = temp->next; 183 | sz++; 184 | } 185 | return sz; 186 | } 187 | 188 | void removeNth(int n) { 189 | int size = sizeLL(); 190 | cout << "size : " << size << endl; 191 | if(n == size) { 192 | pop_front(); 193 | return; 194 | } 195 | 196 | Node* temp = head; 197 | for(int i=1; inext; 199 | } 200 | 201 | Node* toDel = temp->next; //node to delete 202 | temp->next = temp->next->next; 203 | } 204 | }; 205 | 206 | int main() { 207 | List ll; 208 | 209 | ll.push_front(5); 210 | ll.push_front(4); 211 | ll.push_front(3); 212 | ll.push_front(2); 213 | ll.push_front(1); 214 | 215 | ll.printList(); 216 | ll.removeNth(2); //delete 4 217 | ll.printList(); 218 | return 0; 219 | } 220 | 221 | 222 | 223 | 224 | 225 | 226 | -------------------------------------------------------------------------------- /26_LinkedList2/doubly_linkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | Node* prev; 9 | 10 | Node(int data) { 11 | this->data = data; 12 | next = prev = NULL; 13 | } 14 | }; 15 | 16 | class DoublyList { 17 | public: 18 | Node* head; 19 | Node* tail; 20 | 21 | DoublyList() { 22 | head = NULL; 23 | tail = NULL; 24 | } 25 | 26 | void push_front(int val) { 27 | Node* newNode = new Node(val); 28 | if(head == NULL) { 29 | head = tail = newNode; 30 | } else { 31 | head->prev = newNode; 32 | newNode->next = head; 33 | head = newNode; 34 | } 35 | } 36 | 37 | void printList() { 38 | Node* temp = head; 39 | while(temp != NULL) { 40 | cout << temp->data << " <=> "; 41 | temp = temp->next; 42 | } 43 | cout << "NULL" << endl; 44 | } 45 | 46 | void pop_front() { 47 | cout << "deleting : " << head->data << endl; 48 | Node* temp = head; 49 | head = head->next; 50 | if(head != NULL) { 51 | head->prev = NULL; 52 | } 53 | temp->next = NULL; 54 | delete temp; 55 | } 56 | 57 | 58 | }; 59 | 60 | int main() { 61 | DoublyList dll; 62 | 63 | dll.push_front(3); 64 | dll.push_front(2); 65 | dll.push_front(1); 66 | 67 | dll.printList(); 68 | 69 | dll.pop_front(); 70 | dll.printList(); 71 | } -------------------------------------------------------------------------------- /26_LinkedList2/linkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node* next; 10 | 11 | Node(int data) { 12 | this->data = data; 13 | next = NULL; 14 | } 15 | 16 | // ~Node() { 17 | // if(next != NULL) { 18 | // delete next; 19 | // next = NULL; 20 | // } 21 | // } 22 | }; 23 | 24 | class List { 25 | public: 26 | Node* head; 27 | Node* tail; 28 | 29 | List() { 30 | head = NULL; 31 | tail = NULL; 32 | } 33 | 34 | void push_front(int val) { 35 | Node* newNode = new Node(val); 36 | 37 | if(tail == NULL) { 38 | head = tail = newNode; 39 | } else { 40 | newNode->next = head; 41 | head = newNode; 42 | } 43 | } 44 | 45 | void push_back(int val) { 46 | Node* newNode = new Node(val); 47 | 48 | if(head == NULL) { 49 | head = tail = newNode; 50 | } else { 51 | tail->next = newNode; 52 | tail = newNode; 53 | } 54 | } 55 | 56 | void printList() { 57 | Node* temp = head; 58 | while(temp != NULL) { 59 | cout << temp->data << " -> "; 60 | temp = temp->next; 61 | } 62 | cout << "NULL\n"; 63 | } 64 | 65 | void insert(int val, int pos) { 66 | if(pos == 0) { 67 | push_front(val); 68 | return; 69 | } 70 | 71 | Node* temp = head; 72 | int i=0; 73 | while(temp != NULL && inext; 75 | i++; 76 | } 77 | 78 | if(temp == NULL) { 79 | cout << "Invalid position\n"; 80 | return; 81 | } 82 | 83 | Node* newNode = new Node(val); 84 | newNode->next = temp->next; 85 | temp->next = newNode; 86 | } 87 | 88 | void pop_front() { 89 | if(head == NULL) { 90 | return; 91 | } 92 | 93 | Node* temp = head; 94 | head = head->next; 95 | 96 | temp->next = NULL; 97 | delete temp; 98 | } 99 | 100 | void pop_back() { 101 | if(head == NULL) { //0 els 102 | return; 103 | } 104 | if(head->next == NULL) { //1 el 105 | delete head; 106 | head = NULL; 107 | return; 108 | } 109 | 110 | Node* temp = head; 111 | while(temp->next->next != NULL) { 112 | temp = temp->next; 113 | } 114 | //temp has tail's prev 115 | temp->next = NULL; 116 | 117 | delete tail; 118 | tail = temp; 119 | } 120 | 121 | // ~List() { 122 | // if(head != NULL) { 123 | // delete head; 124 | // head = NULL; 125 | // } 126 | // } 127 | }; 128 | 129 | //cycle detection 130 | bool isCycle(Node* head) { 131 | Node* slow = head; 132 | Node* fast = head; 133 | 134 | while(fast != NULL && fast->next != NULL) { 135 | slow = slow->next; 136 | fast = fast->next->next; 137 | 138 | if(slow == fast) { 139 | return true; 140 | } 141 | } 142 | 143 | return false; 144 | } 145 | 146 | void removeCycle(Node* head) { 147 | Node* slow = head; 148 | Node* fast = head; 149 | bool isCycle = false; 150 | 151 | while(fast != NULL && fast->next != NULL) { 152 | slow = slow->next; 153 | fast = fast->next->next; 154 | 155 | if(slow == fast) { 156 | isCycle = true; 157 | break; 158 | } 159 | } 160 | 161 | if(!isCycle) { 162 | return; 163 | } 164 | 165 | //Removing Loop 166 | slow = head; 167 | 168 | if(slow == fast) { 169 | //special case when tail is connected to head 170 | while(fast->next != slow) { 171 | fast = fast->next; 172 | } 173 | fast->next = NULL; 174 | 175 | } else { 176 | Node* prev = fast; 177 | while(slow != fast) { 178 | prev = fast; 179 | slow = slow->next; 180 | fast = fast->next; 181 | } 182 | 183 | //prev is the last node 184 | prev->next = NULL; 185 | } 186 | } 187 | 188 | Node* splitAtMid(Node* head) { 189 | Node* slow = head; 190 | Node* fast = head; 191 | Node* prev = NULL; 192 | 193 | while(fast != NULL && fast->next != NULL) { 194 | prev = slow; 195 | slow = slow->next; 196 | fast = fast->next->next; 197 | } 198 | 199 | if(prev != NULL) { 200 | prev->next = NULL; 201 | } 202 | 203 | return slow; 204 | } 205 | 206 | Node* merge(Node* h1, Node*h2) { 207 | List ans; 208 | Node* i = h1; 209 | Node* j = h2; 210 | 211 | while(i != NULL && j != NULL) { 212 | if(i->data <= j->data) { 213 | ans.push_back(i->data); 214 | i = i->next; 215 | } else { 216 | ans.push_back(j->data); 217 | j = j->next; 218 | } 219 | } 220 | 221 | while(i != NULL) { 222 | ans.push_back(i->data); 223 | i = i->next; 224 | } 225 | 226 | while(j != NULL) { 227 | ans.push_back(j->data); 228 | j = j->next; 229 | } 230 | return ans.head; 231 | } 232 | 233 | Node* mergeSort(Node* head) { 234 | if(head == NULL || head->next == NULL) { 235 | return head; 236 | } 237 | 238 | Node* rightHead = splitAtMid(head); 239 | Node* sortedLeft = mergeSort(head); 240 | Node* sortedRight =mergeSort(rightHead); 241 | 242 | return merge(sortedLeft, sortedRight); 243 | } 244 | 245 | Node* reverse(Node* head) { 246 | Node* prev = NULL; 247 | Node* curr = head; 248 | 249 | while(curr != NULL) { 250 | Node* next = curr->next; 251 | 252 | curr->next = prev; 253 | prev = curr; 254 | curr = next; 255 | } 256 | 257 | return prev; // prev is head 258 | } 259 | 260 | void printList(Node* head) { 261 | Node* temp = head; 262 | 263 | while(temp != NULL) { 264 | cout << temp->data << " "; 265 | temp = temp->next; 266 | } 267 | cout << endl; 268 | } 269 | 270 | Node* zigzag(Node* head) { 271 | Node* rightHead = splitAtMid(head); 272 | Node* rightRev = reverse(rightHead); 273 | 274 | //alternate merging 275 | Node* left = head; 276 | Node* right = rightRev; 277 | Node* nextLeft = NULL; 278 | Node* nextRight = NULL; 279 | Node* tail = head; 280 | 281 | while(left != NULL && right != NULL) { 282 | nextLeft = left->next; 283 | left->next = right; 284 | 285 | nextRight = right->next; 286 | right->next = nextLeft; 287 | 288 | tail = right; 289 | left = nextLeft; 290 | right = nextRight; 291 | } 292 | 293 | if(right != NULL) { 294 | tail->next = right; 295 | } 296 | return head; 297 | } 298 | 299 | int main(){ 300 | List ll; 301 | 302 | //QS : Cycle Detection & Removal 303 | /* ll.push_front(4); 304 | ll.push_front(3); 305 | ll.push_front(2); 306 | ll.push_front(1); 307 | 308 | ll.tail->next = ll.head; //creating a cycle 309 | 310 | cout << isCycle(ll.head) << endl; 311 | removeCycle(ll.head); 312 | cout << isCycle(ll.head) << endl; 313 | ll.printList(); */ 314 | 315 | //QS : Merge Sort the Linked List 316 | // ll.push_front(1); 317 | // ll.push_front(2); 318 | // ll.push_front(3); 319 | // ll.push_front(4); 320 | // ll.printList(); 321 | 322 | // ll.head = mergeSort(ll.head); 323 | // ll.printList(); 324 | 325 | //QS : Zig Zag Linked List 326 | ll.push_front(5); 327 | ll.push_front(4); 328 | ll.push_front(3); 329 | ll.push_front(2); 330 | ll.push_front(1); 331 | ll.printList(); 332 | 333 | ll.head = zigzag(ll.head); 334 | ll.printList(); 335 | } 336 | -------------------------------------------------------------------------------- /26_LinkedList2/list_using_stl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void printList(list ll) { 7 | list::iterator itr; 8 | for(itr=ll.begin(); itr!=ll.end(); itr++) { 9 | cout << *itr << " "; 10 | } 11 | cout << endl; 12 | } 13 | 14 | int main() { 15 | list ll; 16 | 17 | ll.push_front(2); 18 | ll.push_front(1); 19 | 20 | ll.push_back(3); 21 | ll.push_back(4); 22 | 23 | cout << "size : " << ll.size() << endl; 24 | 25 | printList(ll); 26 | 27 | cout << "head = " << ll.front() << endl; 28 | cout << "tail = " << ll.back() << endl; 29 | 30 | ll.pop_front(); 31 | ll.pop_back(); 32 | 33 | printList(ll); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cpp-DSAClasses 2 | This repo contains the codes of C++ DSA Batch of Apna College. 3 | --------------------------------------------------------------------------------