├── README.md ├── 34-2's-complement.c ├── 24-swap-bitwise.c ├── 25-is-n-odd-bitwise.c ├── 19-is-n-power-of-2-bitwise.c ├── 26-count-set-bits.c ├── 36-divisible-by-8-bitwise.c ├── 2-reverse-integer.c ├── 3-euclid-gcd.c ├── 18-lower-to-uppercase-bitwise.c ├── 17-upper-to-lowercase-bitwise.c ├── 14-binary-to-decimal.c ├── 23-lonely-array-item-bitwise.c ├── 9-insertion-sort.c ├── 30-dma-2d-char-array.c ├── 7-bubble-sort.c ├── 13-decimal-to-binary.c ├── 38-catalan-numbers.c ├── 6-fibonacci-recursive.c ├── 8-selection-sort.c ├── 11-fibonacci-dynamic.c ├── 5-fibonacci-iterative.c ├── 37-mmi-brute-force.c ├── 1-primes-basic.c ├── 27-longest-increasing-subsequence-dynamic.c ├── 15-floyd.c ├── 10-union-find-basic.c ├── 16-warshall.c ├── 12-union-find-improvised.c ├── 4-primes-sieve.c ├── 20-ways-to-reach-score-using-3-5-10-dynamic.c ├── 22-subset-sum-dynamic.c ├── 35-permutation-decrease-conquer.c ├── 21-lcs-dynamic.c ├── 32-dfs.c ├── 31-bfs.c ├── 28-stack.c ├── 29-linear-queue.c └── 33-bst-operations.c /README.md: -------------------------------------------------------------------------------- 1 | # day-today-codes 2 | All those needed handy code snippets 3 | -------------------------------------------------------------------------------- /34-2's-complement.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int num = 7; 6 | 7 | /// Two ways to get two's complement 8 | printf("%d\n", -num); 9 | printf("%d\n", ~num + 1); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /24-swap-bitwise.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int x = 20; 5 | int y = 10; 6 | 7 | x = x ^ y; 8 | y = x ^ y; 9 | x = x ^ y; 10 | printf("Swapped Items: x = %d, y = %d", x, y); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /25-is-n-odd-bitwise.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n = 11; 6 | int result = n & 1; 7 | if(result) 8 | printf("Odd number\n"); 9 | else 10 | printf("Even number\n"); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /19-is-n-power-of-2-bitwise.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n, result; 7 | 8 | n = 8; 9 | result = n & (n-1); 10 | printf("%d\n", !result); 11 | 12 | n = 7; 13 | result = n & (n-1); 14 | printf("%d\n", !result); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /26-count-set-bits.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n = 15; 6 | int bits = 0; 7 | 8 | while(n) 9 | { 10 | bits = bits + 1; 11 | n = n & n-1; 12 | } 13 | 14 | printf("The number of set bits are %d\n", bits); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /36-divisible-by-8-bitwise.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int num = 32; 7 | int result = num; 8 | 9 | result = (result >> 3) << 3; 10 | 11 | if (num == result) 12 | printf("Divisible by 8\n"); 13 | else 14 | printf("Not divisible by 8\n"); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /2-reverse-integer.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n, revnum = 0, rem = 0; 5 | 6 | printf("Enter an integer: "); 7 | scanf("%d", &n); 8 | 9 | while(n != 0) 10 | { 11 | rem = n % 10; 12 | revnum = revnum *10 + rem; 13 | n = n / 10; 14 | } 15 | 16 | printf("Reversed Number = %d", revnum); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /3-euclid-gcd.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int gcd(int a, int b) 4 | { 5 | if (a == 0) 6 | return b; 7 | return gcd(b%a, a); 8 | } 9 | 10 | int main() 11 | { 12 | int a, b; 13 | int result = 0; 14 | 15 | printf("Enter 2 numbers\n"); 16 | scanf("%d %d", &a, &b); 17 | result = gcd(a,b); 18 | printf("GCD(%d, %d) = %d\n", a, b, result); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /18-lower-to-uppercase-bitwise.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* 5 | Letter ASCII Binary 6 | --------------------------- 7 | a 97 1100001 8 | A 65 1000001 9 | There is a difference of 32. We AND it with ~32 to convert the case 10 | */ 11 | 12 | int main() 13 | { 14 | char c = 'a'; 15 | int x = 32; 16 | c = c & ~32; 17 | printf("%c\n", c); 18 | } 19 | -------------------------------------------------------------------------------- /17-upper-to-lowercase-bitwise.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* 5 | Letter ASCII Binary 6 | --------------------------- 7 | A 65 1000001 8 | a 97 1100001 9 | 10 | There is a difference of 32. Hence we OR it with 32 to convert the case 11 | */ 12 | int main() 13 | { 14 | char c = 'A'; 15 | int x = 32; 16 | c = c | 32; 17 | printf("%c\n", c); 18 | } 19 | -------------------------------------------------------------------------------- /14-binary-to-decimal.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void binary_to_decimal(int n) 4 | { 5 | int dnum = 0; 6 | int last = 0; 7 | 8 | // Set base to 2^0 9 | int base = 1; 10 | 11 | while(n) 12 | { 13 | last = n % 10; 14 | n = n/10; 15 | dnum += last*base; 16 | base = base*2; 17 | } 18 | 19 | printf("%d\n", dnum); 20 | } 21 | 22 | int main() 23 | { 24 | int n = 101; 25 | binary_to_decimal(n); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /23-lonely-array-item-bitwise.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n = 7; 6 | int a[] = {31, 56, 78, 56, 91, 91, 31}; 7 | int i; 8 | 9 | int temp = 0; 10 | // For the repeated elements, XOR operation flips the bits twice 11 | // and resets back the number with 0. 12 | // What remains is the lonely element 13 | for(i = 0; i < n ; i++) 14 | temp = temp ^ a[i]; 15 | 16 | printf("Lonely element in array is %d\n", temp); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /9-insertion-sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void insertion_sort(int n, int *a) 4 | { 5 | int i, j; 6 | int v; 7 | for(i=1; i <= n-1; i++) { 8 | v = a[i]; 9 | j = i-1; 10 | while( j >= 0 && a[j] > v){ 11 | a[j+1] = a[j]; 12 | j = j - 1; 13 | } 14 | a[j+1] = v; 15 | } 16 | } 17 | 18 | int main() 19 | { 20 | int a[10] = {2, 6, 1, 11, 4}; 21 | int n = 5; 22 | 23 | insertion_sort(n, a); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /30-dma-2d-char-array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | // Say if we want to allocate memory for a 2D char array 7 | // string[10][50]; 8 | 9 | int rows = 10; 10 | int cols = 50; 11 | int i; 12 | 13 | // Allocate for row of pointers 14 | char **string = malloc(rows * sizeof(char *)); 15 | // For each row, get the number of columns 16 | for(i = 0; i < rows; i++) 17 | string[i] = malloc(cols * sizeof(char)); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /7-bubble-sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void bubble_sort(int n, int *a) 4 | { 5 | int i, j, temp =0; 6 | for(i=0; i <= n-2; i++) { 7 | for (j=0; j<= n-2-i; j++) { 8 | if(a[j+1] 2 | 3 | void decimal_to_binary(int n) 4 | { 5 | int bnum[50]; 6 | int i = 0; 7 | int j; 8 | 9 | while (n > 0) { 10 | // Get and store the remainders 11 | bnum[i] = n % 2; 12 | 13 | n = n / 2; 14 | i++; 15 | } 16 | 17 | // Print in the reverse order 18 | for (j = i - 1; j >= 0; j--) 19 | printf("%d", bnum[j]); 20 | } 21 | 22 | int main() 23 | { 24 | int n = 5; 25 | decimal_to_binary(n); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /38-catalan-numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | unsigned long int catalan(int n) { 5 | if (n <= 1) 6 | return 1; 7 | 8 | unsigned long int res = 0; 9 | int i; 10 | for (i=0; i 2 | 3 | int fibo(int n) 4 | { 5 | if (n == 0) 6 | return 0; 7 | else if (n == 1) 8 | return 1; 9 | else 10 | return(fibo(n - 1) + fibo(n - 2)); 11 | } 12 | 13 | 14 | int main() 15 | { 16 | int n; 17 | int result; 18 | printf("Enter the required nth number in fibonacci series: "); 19 | scanf("%d", &n); 20 | 21 | result = fibo(n); 22 | printf("The %d number in fibonacci series is %d\n", n, result); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /8-selection-sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void selection_sort(int n, int *a) 4 | { 5 | int i, j, temp =0; 6 | int min; 7 | for(i=0; i <= n-2; i++) { 8 | min = i; 9 | for (j=i+1; j<= n-1; j++) { 10 | if(a[j] 2 | 3 | int fibo(int n) 4 | { 5 | int fib[n+1]; 6 | int i; 7 | fib[0] = 0; 8 | fib[1] = 1; 9 | 10 | for (i = 2; i < n + 1; i++) 11 | fib[i] = fib[i - 1] + fib[i - 2]; 12 | 13 | return fib[n]; 14 | } 15 | 16 | 17 | int main() 18 | { 19 | int n; 20 | int result; 21 | printf("Enter the required nth number in fibonacci series: "); 22 | scanf("%d", &n); 23 | 24 | if (n == 0) 25 | result = 0; 26 | else 27 | result = fibo(n); 28 | printf("The %d number in fibonacci series is %d\n", n, result); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /5-fibonacci-iterative.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fibo(int n) 4 | { 5 | int previous = 1; 6 | int current = 1; 7 | int next = 1; 8 | int i; 9 | for (i = 3; i <= n; i++) 10 | { 11 | next = current + previous; 12 | previous = current; 13 | current = next; 14 | } 15 | return next; 16 | } 17 | 18 | 19 | int main() 20 | { 21 | int n; 22 | int result; 23 | printf("Enter the required nth number in fibonacci series: "); 24 | scanf("%d", &n); 25 | 26 | if (n == 0) 27 | result = 0; 28 | else 29 | result = fibo(n); 30 | printf("The %d number in fibonacci series is %d\n", n, result); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /37-mmi-brute-force.c: -------------------------------------------------------------------------------- 1 | /* 2 | Modulo Multiplicative Inverse (MMI) of a number y is z iff 3 | (z * y) % M = 1 4 | 5 | Example 1: 6 | If M = 7 and y = 4, 7 | then (z * 4) % 7 = 1 ==> Z = 2 8 | 9 | Example 2: 10 | If M = 11 and y = 7, 11 | then (z * 7) % 11 = 1 ==> z = 8 12 | 13 | */ 14 | 15 | #include 16 | #define M 11 17 | 18 | int mmi_brute_force(int y) 19 | { 20 | int i = 1; 21 | while(i < M) 22 | { 23 | if(((long long)i * y ) % M == 1) 24 | return i; 25 | i++; 26 | } 27 | return -1; 28 | } 29 | 30 | int main() 31 | { 32 | int y = 7; 33 | int mmi = mmi_brute_force(y); 34 | if (mmi == -1) 35 | printf("MMI does not exist\n"); 36 | else 37 | printf("MMI is %d\n", mmi); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /1-primes-basic.c: -------------------------------------------------------------------------------- 1 | /* 2 | A prime number is a number that is divisible only 1 and by itself 3 | Example: 2, 3, 5, 7, 11 etc 4 | As by definition we can see that prime number is generally defined to be any positive number that has 5 | exactly two distinct positive integer divisors. For the same reason 1 is not considered as a prime number. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() 12 | { 13 | int start, end, i, j, flag=0; 14 | printf("Enter starting and end: "); 15 | scanf("%d%d",&start, &end); 16 | 17 | printf("Prime Numbers between %d and %d is :\n", start, end); 18 | for(i=start; i<=end; i++) 19 | { 20 | flag=0; 21 | for(j=2; j 2 | #include 3 | #include 4 | 5 | int longest_increasing_subsequence(int a[], int n) 6 | { 7 | int i, j; 8 | int lis[n]; 9 | int max=0; 10 | 11 | for(i = 0; i < n; i++) 12 | lis[i] = 1; 13 | 14 | for(i = 1; i < n; i++) { 15 | for(j = 0; j < i; j++) { 16 | if(a[i] > a[j] && lis[i] < lis[j]+1) 17 | lis[i] = lis[j]+1; 18 | } 19 | } 20 | 21 | for(i = 0; i < n; i++) 22 | { 23 | if(lis[i] > max) 24 | max = lis[i]; 25 | } 26 | return max; 27 | } 28 | 29 | int main() 30 | { 31 | int n = 6; 32 | int a[] = {5, 11, 3, 15, 30, 25}; 33 | int result = longest_increasing_subsequence(a,n); 34 | 35 | printf("Number of terms in LIS is %d\n", result); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /15-floyd.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n = 4; 7 | int i, j, k; 8 | int D[5][5] = { 9 | {0, 0, 0, 0, 0}, 10 | {0, 0, 999, 3, 999}, 11 | {0, 2, 0, 999, 999}, 12 | {0, 999, 7, 0, 1}, 13 | {0, 6, 999, 999, 0} 14 | }; 15 | 16 | printf("The supplied input is\n"); 17 | for(i = 1; i <= n; i++){ 18 | for(j = 1; j<= n; j++){ 19 | printf("%d\t", D[i][j]); 20 | } 21 | printf("\n"); 22 | } 23 | 24 | 25 | for(k = 1; k <= n; k++) { 26 | for(i = 1; i <= n; i++) { 27 | for(j = 1; j <= n; j++) { 28 | if((D[i][k] + D[k][j]) < D[i][j]) 29 | D[i][j] = D[i][k] + D[k][j]; 30 | } 31 | } 32 | } 33 | 34 | printf("All pair shortest paths:\n"); 35 | for(i = 1; i <= n; i++){ 36 | for(j = 1; j<= n; j++){ 37 | printf("%d\t", D[i][j]); 38 | } 39 | printf("\n"); 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /10-union-find-basic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define n 10 3 | 4 | int arr[n]; 5 | 6 | void initialize() 7 | { 8 | int i; 9 | for(i = 0; i 2 | #include 3 | 4 | int main() 5 | { 6 | int n = 4; 7 | int i, j, k; 8 | int R[5][5] = { 9 | {0, 0, 0, 0, 0}, 10 | {0, 0, 1, 0, 0}, 11 | {0, 0, 0, 0, 1}, 12 | {0, 0, 0, 0, 0}, 13 | {0, 1, 0, 1, 0} 14 | }; 15 | 16 | printf("The supplied input is\n"); 17 | for(i = 1; i <= n; i++){ 18 | for(j = 1; j<= n; j++){ 19 | printf("%d\t", R[i][j]); 20 | } 21 | printf("\n"); 22 | } 23 | 24 | 25 | for(k = 1; k <= n; k++) { 26 | for(i = 1; i <= n; i++) { 27 | for(j = 1; j <= n; j++) { 28 | if(R[i][j] != 1){ 29 | if(R[i][k] == 1 && R[k][j] == 1) 30 | R[i][j] = 1; 31 | } 32 | } 33 | } 34 | } 35 | 36 | printf("Transitive Closure:\n"); 37 | for(i = 1; i <= n; i++){ 38 | for(j = 1; j<= n; j++){ 39 | printf("%d\t", R[i][j]); 40 | } 41 | printf("\n"); 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /12-union-find-improvised.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define n 6 3 | 4 | int arr[n]; 5 | 6 | void initialize() 7 | { 8 | int i; 9 | for(i = 0; i 11 | #include 12 | #include 13 | 14 | int main(){ 15 | long int i,j; 16 | int n = 100; 17 | int *primes; 18 | 19 | primes = (int *)malloc(sizeof(int) * n); 20 | memset(primes, 1, n*sizeof(int)); 21 | 22 | for (i = 2;i < n; i++){ 23 | if (primes[i]) { 24 | for (j = i; i * j < n; j++) 25 | primes[i * j] = 0; 26 | } 27 | } 28 | 29 | printf("\nPrime numbers in range 1 to %d are: \n", n); 30 | for (i = 2;i < n; i++){ 31 | if (primes[i]) 32 | printf("%ld ", i); 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /20-ways-to-reach-score-using-3-5-10-dynamic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | Counts the number of ways in which the given score can be reached using 3, 5 and 10. 7 | Technique used: Dynamic Programming 8 | */ 9 | int count_number_of_ways(int score) 10 | { 11 | int i; 12 | int result[score+1]; 13 | 14 | // Initialize all the entries with 0 15 | memset(result, 0, sizeof(int) * (score+1)); 16 | 17 | // There is one way to reach NO score 18 | result[0] = 1; 19 | 20 | // Count the ways using 3 21 | for(i = 3; i <= score; i++) 22 | result[i] = result[i] + result[i-3]; 23 | 24 | // Count the ways using 3 and 5 25 | for(i = 5; i <= score; i++) 26 | result[i] = result[i] + result[i-5]; 27 | 28 | // Count the ways using 3, 5 and 10 29 | for(i = 10; i <= score; i++) 30 | result[i] = result[i] + result[i-10]; 31 | 32 | return result[score]; 33 | } 34 | 35 | int main() 36 | { 37 | int score = 15; 38 | int result = count_number_of_ways(score); 39 | printf("The number of ways are:\n"); 40 | printf("%d\n", result); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /22-subset-sum-dynamic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int subset_sum(int a[], int n, int sum) 6 | { 7 | int ss[n+1][sum+1]; 8 | int i,j; 9 | 10 | // When sum is 0, empty set is the solution 11 | for(i = 0; i <= n; i++) 12 | ss[i][0] = 1; 13 | 14 | // When there are no array elements, subsets are not possible 15 | for(j = 1; j <= sum; j++) 16 | ss[0][j] = 0; 17 | 18 | for(i = 1; i <= n; i++) { 19 | for(j = 1; j <= sum; j++){ 20 | if(ss[i-1][j] == 1) 21 | ss[i][j] = 1; 22 | else 23 | { 24 | if(a[i-1] > j) 25 | ss[i][j] = 0; 26 | else 27 | ss[i][j] = ss[i - 1][j - a[i-1]]; 28 | } 29 | } 30 | } 31 | 32 | return ss[n][sum]; 33 | } 34 | 35 | int main() 36 | { 37 | int n = 5; 38 | int a[5] = {1, 2, 3, 5, 7}; 39 | int sum = 9; 40 | 41 | int result=subset_sum(a,n,sum); 42 | 43 | if(result) 44 | printf("Subset found\n"); 45 | else 46 | printf("Subset not found\n"); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /35-permutation-decrease-conquer.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Uses the definition of n! to generate the permutations. 4 | 5 | Idea: 6 | ----- 7 | Remove each item from the given n items one at a time and 8 | append it to remaining (n-1)! permutations. 9 | 10 | Efficiency: 11 | ----------- 12 | O(n!) and as well we have expensive swaps 13 | 14 | Strategy used: 15 | -------------- 16 | Decrease and Conquer(decrease by 1) 17 | 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | // Global n 24 | int gn; 25 | 26 | void permute(int a[], int n) 27 | { 28 | if (n == 1) 29 | { 30 | int i; 31 | for(i = 0; i < gn; i++) 32 | printf("%d ", a[i]); 33 | printf("\n"); 34 | return; 35 | } 36 | 37 | int i; 38 | int temp; 39 | for(i = 0; i < n; i++) 40 | { 41 | // Remove the ith item 42 | temp = a[i]; 43 | a[i] = a[n-1]; 44 | a[n-1] = temp; 45 | 46 | permute(a, n-1); 47 | 48 | // Restore it for the next round 49 | temp = a[i]; 50 | a[i] = a[n-1]; 51 | a[n-1] = temp; 52 | 53 | } 54 | } 55 | 56 | int main() 57 | { 58 | int a[5] = {1, 2, 3}; 59 | int n = 3; 60 | gn = n; 61 | 62 | permute(a, n); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /21-lcs-dynamic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | Longest Common Subsequence Problem 7 | 8 | Examples: 9 | --------- 10 | 11 | str1="classic" 12 | str2="music" 13 | LCS=3 for the pattern "sic" 14 | 15 | 16 | str1="eabcde" 17 | str2="aabfcde" 18 | LCS=3 for the pattern "abc" 19 | 20 | */ 21 | 22 | 23 | int get_lcs(char *str1, char *str2, int len1, int len2) 24 | { 25 | int i, j; 26 | int lcs[len1+1][len2+1]; 27 | 28 | // starting with base cases of 29 | // First string is empty 30 | for(i = 0 ; i < len2; i++) 31 | lcs[0][i] = 0; 32 | 33 | // Second string empty 34 | for(i = 0; i <= len1; i++) 35 | lcs[i][0]=0; 36 | 37 | 38 | for(i = 1; i <= len1; i++) { 39 | for(j = 1; j <= len2; j++) { 40 | // When charatcers of both string match 41 | if(str1[i-1] == str2[j-1]) 42 | lcs[i][j] = 1 + lcs[i-1][j-1]; 43 | else 44 | lcs[i][j] = lcs[i-1][j] > lcs[i][j-1] ? lcs[i-1][j] : lcs[i][j-1]; 45 | } 46 | } 47 | 48 | return lcs[len1][len2]; 49 | } 50 | 51 | 52 | int main() 53 | { 54 | char str1[50] = "lmnop"; 55 | char str2[50] = "lmmnoop"; 56 | 57 | int result; 58 | result = get_lcs(str1, str2, strlen(str1), strlen(str2)); 59 | printf("LCS for supplied input is:\n"); 60 | printf("%d\n", result); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /32-dfs.c: -------------------------------------------------------------------------------- 1 | /// PROGRAM TO PRINT DFS TRAVERSAL FROM GIVEN SOURCE VERTEX 2 | 3 | #include 4 | #include 5 | 6 | /// We are going to define some global variables as it involves recursion 7 | /// Variable to hold the number of vertices 8 | int v = 5; 9 | 10 | /// Variable to hold the graph as matrix 11 | /// Variable to hold the graph as matrix 12 | int m[10][10] = 13 | { 14 | {0, 1, 1, 0, 0}, 15 | {1, 0, 0, 1, 1}, 16 | {1, 0, 0, 0, 1}, 17 | {0, 1, 0, 0, 0}, 18 | {0, 1, 1, 0, 0} 19 | }; 20 | 21 | 22 | /// Variable to hold source vertex 23 | int source = 4; 24 | 25 | /// Array to maintain the visited vertices 26 | int visited[10]; 27 | 28 | /* 29 | Funtion Name: dfs 30 | Input Params: Graph input as matrix, number of vertices and source vertex 31 | Return Type: void 32 | Description: performs dfs traversal on the given graph 33 | */ 34 | void dfs(int m[10][10], int v, int source) 35 | { 36 | /// Temporary variable 37 | int i; 38 | 39 | /// Mark the source vertex as visited 40 | visited[source] = 1; 41 | 42 | for(i=0; i 4 | #include 5 | 6 | /* 7 | Funtion Name: bfs 8 | Input Params: Graph input as matrix, number of vertices and source vertex 9 | Return Type: void 10 | Description: performs bfs traversal on the given graph 11 | */ 12 | void bfs(int m[10][10], int v, int source) 13 | { 14 | /// A queue will be used during the traversal 15 | int queue[20]; 16 | int front = 0; 17 | int rear = 0; 18 | 19 | /// Temporary variables 20 | int u; 21 | int i; 22 | 23 | /// Array to maintain the visited vertices 24 | int visited[10]; 25 | 26 | /// Set all the vertices to not visited 27 | for (i= 0; i < v; i ++) 28 | visited[i] = 0; 29 | 30 | /// Initilaize the queue with source and mark it visited 31 | queue[rear] = source; 32 | visited[source] = 1; 33 | 34 | printf("The BFS Traversal is... \n"); 35 | 36 | /// Until the queue is empty 37 | while (front <= rear) 38 | { 39 | /// Pick the vertex from queue front 40 | /// This is also when we mark the traversal and print it 41 | u = queue[front]; 42 | printf("%d\t", u); 43 | front = front + 1; 44 | 45 | /// All the vertices that are reachable from considered vertex 46 | /// and not yet visited, 47 | /// Mark them as visited and Add them to queue 48 | for(i=0;i 2 | #include 3 | #define STACKSIZE 5 4 | #define TRUE 1 5 | #define FALSE 0 6 | 7 | struct stack 8 | { 9 | int top; 10 | int items[STACKSIZE]; 11 | }; 12 | typedef struct stack STACK; 13 | 14 | void push(STACK *); 15 | void pop(STACK *); 16 | void print(STACK *); 17 | void peek(STACK *); 18 | int empty(STACK *); 19 | int full(STACK *); 20 | 21 | int main() 22 | { 23 | STACK S; 24 | S.top = -1; 25 | int choice=0; 26 | 27 | while(1) { 28 | printf("\n Menu\n"); 29 | printf("1-PUSH\n"); 30 | printf("2-POP\n"); 31 | printf("3-PEEK\n"); 32 | printf("4-PRINT\n"); 33 | printf("5-EXIT\n"); 34 | printf("Enter your choice\n"); 35 | scanf("%d", &choice); 36 | switch(choice) { 37 | case 1: push(&S); 38 | break; 39 | case 2: pop(&S); 40 | break; 41 | case 3: peek(&S); 42 | break; 43 | case 4: print(&S); 44 | break; 45 | case 5: printf("Terminating\n"); 46 | exit(1); 47 | } 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | int full(STACK *S) 54 | { 55 | if(S->top == STACKSIZE-1) 56 | return TRUE; 57 | else 58 | return FALSE; 59 | } 60 | 61 | void push(STACK *S) 62 | { 63 | if(full(S)){ 64 | printf("Stack full\n"); 65 | return; 66 | } 67 | 68 | int x; 69 | printf("Enter the item to be pushed\n"); 70 | scanf("%d", &x); 71 | 72 | S->top++; 73 | S->items[S->top] = x; 74 | 75 | } 76 | 77 | int empty(STACK * S) 78 | { 79 | if(S->top == -1) 80 | return TRUE; 81 | else 82 | return FALSE; 83 | } 84 | void pop(STACK *S) 85 | { 86 | if(empty(S)){ 87 | printf("Stack Empty\n"); 88 | return; 89 | } 90 | 91 | int x; 92 | x = S->items[S->top]; 93 | printf("Popped item is %d\n", x); 94 | S->top--; 95 | } 96 | 97 | void peek(STACK *S) 98 | { 99 | if(empty(S)){ 100 | printf("Stack Empty\n"); 101 | return; 102 | } 103 | 104 | int x; 105 | x = S->items[S->top]; 106 | printf("Peeked item is %d\n", x); 107 | } 108 | 109 | void print(STACK *S) 110 | { 111 | if(empty(S)){ 112 | printf("Stack Empty\n"); 113 | return; 114 | } 115 | 116 | int i; 117 | for(i = S->top; i >= 0; i--) 118 | printf("| %d |\n", S->items[i]); 119 | } 120 | -------------------------------------------------------------------------------- /29-linear-queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define QUEUESIZE 5 4 | #define TRUE 1 5 | #define FALSE 0 6 | 7 | struct queue 8 | { 9 | int front; 10 | int rear; 11 | int items[QUEUESIZE]; 12 | }; 13 | typedef struct queue QUEUE; 14 | 15 | void enqueue(QUEUE *); 16 | void dequeue(QUEUE *); 17 | void display(QUEUE *); 18 | int full(QUEUE *); 19 | int empty(QUEUE *); 20 | 21 | int main() 22 | { 23 | QUEUE q; 24 | q.front = 0; 25 | q.rear = -1; 26 | 27 | int choice; 28 | 29 | while(1){ 30 | printf("MENU\n"); 31 | printf("1-Enqueue\n"); 32 | printf("2-Dequeue\n"); 33 | printf("3-Display\n"); 34 | printf("4-Exit\n"); 35 | 36 | printf("\nEnter your choice\n "); 37 | scanf("%d", &choice); 38 | 39 | switch(choice){ 40 | case 1: enqueue(&q); 41 | break; 42 | case 2: dequeue(&q); 43 | break; 44 | case 3: display(&q); 45 | break; 46 | case 4: printf("Terminating\n"); 47 | exit(0); 48 | } 49 | } 50 | 51 | return 0; 52 | } 53 | 54 | /// Function Name: full 55 | /// Description: checks if rear end has reached max position 56 | /// Input Param: Pointer to queue 57 | /// Return type: TRUE if queue full, FALSE otherwise 58 | int full(QUEUE *q) 59 | { 60 | if(q->rear == QUEUESIZE - 1) 61 | return TRUE; 62 | else 63 | return FALSE; 64 | } 65 | 66 | /// Function Name: enqueue 67 | /// Description: enqueue an item inside queue 68 | /// Input Param: Pointer to queue 69 | /// Return type: void 70 | void enqueue(QUEUE *q) 71 | { 72 | if(full(q)){ 73 | printf("Queue full\n"); 74 | return; 75 | } 76 | int x; 77 | printf("Enter the enqueue item\n"); 78 | scanf("%d", &x); 79 | 80 | q->rear++; 81 | q->items[q->rear] = x; 82 | } 83 | 84 | /// Function Name: empty 85 | /// Description: 86 | /// item f r 87 | /// ------------------------------------- 88 | /// initial 0 -1 89 | /// one insertion/deletion 1 0 90 | /// ... 91 | /// n insertions/deletions n n-1` 92 | /// Input Param: Pointer to queue 93 | /// Return type: TRUE if queue empty, FALSE otherwise 94 | int empty(QUEUE *q) 95 | { 96 | if(q->front > q->rear) 97 | return TRUE; 98 | else 99 | return FALSE; 100 | } 101 | 102 | /// Function Name: dequeue 103 | /// Description: dequeue an item from queue 104 | /// Input Param: Pointer to queue 105 | /// Return type: void 106 | void dequeue(QUEUE *q) 107 | { 108 | if(empty(q)){ 109 | printf("Empty queue\n"); 110 | return; 111 | } 112 | int x; 113 | x = q->items[q->front]; 114 | printf("Dequeued Item is %d\n", x); 115 | q->front++; 116 | } 117 | 118 | /// Function Name: display 119 | /// Description: display the items from queue 120 | /// Input Param: Pointer to queue 121 | /// Return type: void 122 | void display(QUEUE *q) 123 | { 124 | if(empty(q)){ 125 | printf("Empty Queue\n"); 126 | return; 127 | } 128 | int i; 129 | for(i = q->front; i<= q->rear; i++) 130 | printf("%d\n", q->items[i]); 131 | } 132 | -------------------------------------------------------------------------------- /33-bst-operations.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct tree 5 | { 6 | int data; 7 | struct tree *left; 8 | struct tree *right; 9 | }; 10 | typedef struct tree TREE; 11 | 12 | TREE * insert_into_bst(TREE *, int); 13 | void inorder(TREE *); 14 | void preorder(TREE *); 15 | void postorder(TREE *); 16 | TREE * delete_from_bst(TREE *, int); 17 | 18 | 19 | /* 20 | Function Name: insert_into_bst 21 | Input Params: Root of the tree and data item to be inserted 22 | Return Type: Updated root of the tree 23 | Description: Inserts a node into a binary search tree at 24 | appropriate position 25 | */ 26 | TREE * insert_into_bst(TREE * root, int data) 27 | { 28 | TREE *newnode,*currnode,*parent; 29 | 30 | // Dynamically allocate the memory using malloc 31 | newnode=(TREE*)malloc(sizeof(TREE)); 32 | 33 | // Check if the memory allocation was successful 34 | if(newnode==NULL) 35 | { 36 | printf("Memory allocation failed\n"); 37 | return NULL; 38 | } 39 | 40 | // Initialize the tree node elements 41 | newnode->data = data; 42 | newnode->left = NULL; 43 | newnode->right = NULL; 44 | 45 | // When the first insertion happens which is the root node 46 | if(root == NULL) 47 | { 48 | root = newnode; 49 | printf("Root node inserted into tree\n"); 50 | return root; 51 | } 52 | 53 | // Traverse through the desired part of the tree using 54 | // currnode and parent pointers 55 | currnode = root; 56 | parent = NULL; 57 | while(currnode != NULL) 58 | { 59 | parent = currnode; 60 | if(newnode->data < currnode->data) 61 | currnode = currnode->left; 62 | else 63 | currnode = currnode->right; 64 | } 65 | 66 | // Attach the node at appropriate place using parent 67 | if(newnode->data < parent->data) 68 | parent->left = newnode; 69 | else 70 | parent->right = newnode; 71 | 72 | // print the successful insertion and return root 73 | printf("Node inserted successfully into the tree\n"); 74 | return root; 75 | } 76 | 77 | /* 78 | Function Name: inorder 79 | Input Params: Root of the tree 80 | Return Type: void 81 | Description: Recursively visits the tree in the order of 82 | Left, Root, Right 83 | */ 84 | void inorder(TREE *troot) 85 | { 86 | if(troot != NULL) 87 | { 88 | inorder(troot->left); 89 | printf("%d\t",troot->data); 90 | inorder(troot->right); 91 | } 92 | } 93 | 94 | /* 95 | Function Name: preorder 96 | Input Params: Root of the tree 97 | Return Type: void 98 | Description: Recursively visits the tree in the order of 99 | Root, Left, Right 100 | */ 101 | void preorder(TREE *troot) 102 | { 103 | if(troot != NULL) 104 | { 105 | printf("%d\t",troot->data); 106 | preorder(troot->left); 107 | preorder(troot->right); 108 | } 109 | } 110 | 111 | 112 | /* 113 | Function Name: postorder 114 | Input Params: Root of the tree 115 | Return Type: void 116 | Description: Recursively visits the tree in the order of 117 | Left, Right, Root 118 | */ 119 | void postorder(TREE *troot) 120 | { 121 | if(troot != NULL) 122 | { 123 | postorder(troot->left); 124 | postorder(troot->right); 125 | printf("%d\t",troot->data); 126 | } 127 | } 128 | 129 | 130 | /* 131 | Function Name: delete_from_bst 132 | Input Params: Root of the tree, item data to be deleted 133 | Return Type: Updated root of the tree 134 | Description: Deletes the specified data and re-adjusts the 135 | tree structure according to bst tree constraints 136 | */ 137 | 138 | TREE * delete_from_bst(TREE * root, int data) 139 | { 140 | TREE * currnode, *parent, *successor, *p; 141 | 142 | // Check if the tree is empty 143 | if(root == NULL) 144 | { 145 | printf("Tree is empty\n"); 146 | return root; 147 | } 148 | 149 | // Traverse and reach the appropriate part of the tree 150 | parent = NULL; 151 | currnode = root; 152 | while (currnode != NULL && data != currnode->data) 153 | { 154 | parent = currnode; 155 | if(data < currnode->data) 156 | currnode = currnode->left; 157 | else 158 | currnode = currnode->right; 159 | } 160 | 161 | // If the data is not present in the tree 162 | if(currnode == NULL) { 163 | printf("Item not found\n"); 164 | return root; 165 | } 166 | 167 | // Check and manipulate if either left subtree is absent, 168 | // or right subtree is absent 169 | // or both are present 170 | if(currnode->left == NULL) 171 | p = currnode->right; 172 | else if (currnode->right == NULL) 173 | p = currnode->left; 174 | else 175 | { 176 | // Process of finding the inorder successor 177 | successor = currnode->right; 178 | while(successor->left != NULL) 179 | successor = successor->left; 180 | 181 | successor->left = currnode->left; 182 | p = currnode->right; 183 | } 184 | 185 | // The case of root deletion 186 | if (parent == NULL) { 187 | free(currnode); 188 | return p; 189 | } 190 | 191 | if(currnode == parent ->left) 192 | parent->left = p; 193 | else 194 | parent->right = p; 195 | 196 | free(currnode); 197 | return root; 198 | } 199 | 200 | int main() 201 | { 202 | TREE * root; 203 | root = NULL; 204 | int choice = 0; 205 | int data = 0; 206 | int count = 0; 207 | 208 | while(1) 209 | { 210 | printf("\n******** Menu *************\n"); 211 | printf("1-Insert into BST\n"); 212 | printf("2-Inorder Traversal\n"); 213 | printf("3-Preorder Traversal\n"); 214 | printf("4-Postorder Traversal\n"); 215 | printf("5-Delete from BST\n"); 216 | printf("Any other option to exit\n"); 217 | printf("*****************************\n"); 218 | 219 | printf("Enter your choice\n"); 220 | scanf("%d", &choice); 221 | 222 | switch(choice) 223 | { 224 | case 1: printf("Enter the item to insert\n"); 225 | scanf("%d", &data); 226 | root = insert_into_bst(root, data); 227 | break; 228 | 229 | case 2: if(root == NULL) 230 | printf("Tree is empty\n"); 231 | else 232 | { 233 | printf("Inorder Traversal is...\n"); 234 | inorder(root); 235 | } 236 | break; 237 | 238 | case 3: if(root == NULL) 239 | printf("Tree is empty\n"); 240 | else 241 | { 242 | printf("Preorder Traversal is...\n"); 243 | preorder(root); 244 | } 245 | break; 246 | 247 | case 4: if(root == NULL) 248 | printf("Tree is empty\n"); 249 | else 250 | { 251 | printf("Postorder Traversal is...\n"); 252 | postorder(root); 253 | } 254 | break; 255 | 256 | case 5: printf("Enter the item to be deleted\n"); 257 | scanf("%d", &data); 258 | root = delete_from_bst(root, data); 259 | break; 260 | 261 | default: printf("Exciting Code.\n"); 262 | exit(0); 263 | } 264 | } 265 | return 0; 266 | } 267 | --------------------------------------------------------------------------------