├── Algorithms ├── Finding_prime_in_sqrt(n) .c ├── Sieve_Of_Erastothenes.c ├── sparse_matrix_representation.c └── square_root.c ├── Array_Problems ├── 10_Element_odd_times.c ├── 11_printRepeating.c ├── 1_Common_Elements.c ├── 1_Duplicate_Num.c ├── 1_Missing.c ├── 1_Recursive_Even_num.c ├── 2_First_Duplicate.c ├── 2_Iterative_Even_num.c ├── 2_Strictly_smaller_num.c ├── 2a_First_NonRepeating.c ├── 2b_Smallest_Num.c ├── 3_Non_Recursive_Fib.c ├── 3_Sum_Recursive.c ├── 3_Sum_k.c ├── 3_arrange.c ├── 4_Recursive_Sum.c ├── 4_Sum_closestToZero.c ├── 4_num_of_Zeros.c ├── 4_sum_equal_zero.c ├── 5_Duplicates.c ├── 5_Max_Repeating.c ├── 5a_Iterative_Sum_of_digit.c ├── 5b_Recursive_Sum_of_digit.c ├── 6_Maximum.c ├── 6_Recursion_Fib_N_Calls.c ├── 6_Strictly_greater_num.c ├── 6_remove_element.c ├── 7_Num_of_occurrence.c ├── 7_Recursive_Binary.c ├── 7_Repeating_k_times.c ├── 8_Row_with_Max_0.c ├── 8a_Trivial_GCD.c ├── 8b_Euclidean_GCD.c ├── 9_Missing.c ├── Maximum-Sum-Subarray-[Kadanes-Algorithm] ├── missing_ranges.py └── spiral_matrix.py ├── Binary_Tree ├── 1_Binary_tree.c ├── 2_LookUp.c ├── 3_Num_of_Nodes.c ├── 4_Delete_Node.c ├── 5_Min_Max_data_value.c ├── BST.h ├── Balanced_Binary_Tree │ └── AVL_Tree.c ├── Delete_Node.h ├── Leaf_NonLeaf_Level.c ├── Level_order_traversal.c ├── Min_Max_value_n_Delete.c ├── Queue.h ├── Range Sum of BST.java └── implementation.cpp ├── Competitive_Programming ├── Arranging Coins.java ├── Binary Tree Level Order Traversal II.java ├── Game_Time.py ├── Game_of_stones_4.py ├── Knapsack_Problem.cpp ├── Largest Divisible Subset.java ├── Least Number of Unique Integers after K Removals.java ├── Maximum Length of Repeated Subarray.java ├── Maximum Points You Can Obtain from Cards.java ├── Pokemon_1.py ├── SPOJ - MMASS.cpp ├── Sort Characters By Frequency.java ├── Sort Colors.java ├── Stepping_stones_3.py ├── cadet_vs_Lecturer.py └── spoj - AGGRCOW.cpp ├── Graphs ├── AdjLinked_List_Implementation.c ├── Bellman ford algorithm.cpp ├── DetectCycleInADirectedGraph.cpp └── graph_topological_dfs.cpp ├── LICENSE ├── Linked_List ├── 11_Reverse.c ├── 11_Reverse_Recursion.c ├── 12_Stack_n_Queue.c ├── 14_LENGHT.c ├── 1_Count.c ├── 2_GetNth.c ├── 3.1_Delete_list.c ├── 3.2_InsertNth.c ├── 4_SortedInsert.c ├── 6_Append.c ├── 7_FrontBackSplit.c ├── 7_print_lots.c ├── 8_RemoveDuplicates.c ├── Implementation.cpp ├── Linked_List.c ├── cll.cpp ├── dll.cpp ├── input.txt └── input2.txt ├── Queue ├── dequeue.c ├── dispqueue.c └── enqueue.c ├── README.md ├── Stacks └── Implementing_two_stack_in_one_array.c ├── _config.yml └── media └── Data_Structure_n_Algorithms.png /Algorithms/Finding_prime_in_sqrt(n) .c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | printf("\nEnter The Positive Integer, n\n"); 5 | int n; 6 | scanf("%d", &n); 7 | int i; 8 | int count = 0; 9 | if (n == 0 || n == 1) { 10 | printf("n is not a Prime Number\n"); 11 | } 12 | for(i = 2; i <= sqrt(n); i++ ) { 13 | if(n%i == 0 && n>2) { 14 | printf("n is not a Prime Number\n"); 15 | count++; 16 | } 17 | } 18 | if(count == 0 && n> 2) { 19 | printf("n is a Prime Number\n"); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Algorithms/Sieve_Of_Erastothenes.c: -------------------------------------------------------------------------------- 1 | // Algorithm to Find all The Prime Number before the Input Number 2 | 3 | #include 4 | #include 5 | #include 6 | int main() { 7 | int n; 8 | printf("\nEnter Value of n\n"); 9 | scanf("%d", &n); 10 | int *A = (int*)malloc(n*sizeof(int)); 11 | int i, j; 12 | // Intializing the Array A which will contain prime number at the end by 1 at all index 13 | for(i = 2; i<=n; i++) { 14 | A[i] = 1; 15 | } 16 | for(i = 2; i <=sqrt(n); i++ ) { 17 | if(A[i] == 1) { 18 | for(j = i*2; j <=n; j = j + i) { //Crossing out all the multiple of i by changing its value to 0 19 | A[j] = 0; 20 | } 21 | } 22 | } 23 | printf("Prime Number Before %d :\n", n); 24 | // printing the prime Numbers 25 | for(i = 2; i<=n; i++) { 26 | if(A[i] == 1) { 27 | printf("%d ", i); 28 | } 29 | } 30 | printf("\n"); 31 | } 32 | 33 | // To Compile use "gcc 5_Sieve_Of_Erastothenes.c -lm" 34 | 35 | // Time Complexity O(sqrt(n)loglogn) 36 | -------------------------------------------------------------------------------- /Algorithms/sparse_matrix_representation.c: -------------------------------------------------------------------------------- 1 | #include 2 | void getMatrix(int r,int c,int arr[][10]) 3 | { 4 | printf("Enter matrix elements\n"); 5 | for(int i=0;i 2 | 3 | float Q_rsqrt( float number ) 4 | { 5 | long i; 6 | float x2, y; 7 | const float threehalfs = 1.5; 8 | 9 | x2 = number * 0.5; 10 | y = number; 11 | i = * ( long * ) &y; // evil floating point bit level hacking 12 | printf("%ld\n" ,i); 13 | i = 0x5f3759df - ( i >> 1 ); 14 | printf("%ld\n" ,i); 15 | y = * ( float * ) &i; 16 | printf("%f\n" ,y); 17 | y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration 18 | y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed 19 | 20 | return y; 21 | } 22 | 23 | int main(int argc,char* argv[]){ 24 | float x ; 25 | printf("enter your number: "); 26 | scanf("%f",&x); 27 | printf("%f\n",Q_rsqrt(x)); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Array_Problems/10_Element_odd_times.c: -------------------------------------------------------------------------------- 1 | //Given an array of positive integers, all numbers occurs even number of times except 2 | //one number which occurs odd number of times. Find the number in O(n) time. 3 | 4 | #include 5 | #include 6 | // Function to swap two variable 7 | // Here on Appling XOR operator when it encounter same number again it became zero 8 | // else it will return the same value 9 | // Therefore he we can transverse the complete array 10 | // For all Even occurance of Element it became zero 11 | // and for odd occurance of Element it return that Element 12 | 13 | int Element_odd_times(int A[], int size) 14 | { 15 | int i; 16 | int result = 0; 17 | for (i = 0; i < size; i++) 18 | result = result ^ A[i]; 19 | 20 | return result; 21 | } 22 | int main() 23 | { 24 | int i,n; 25 | printf("Enter the number of Elements to be Entered\n"); 26 | scanf("%d", &n); 27 | int *A = (int*)malloc(n*sizeof(int)); 28 | printf("Enter the Elements of Array\n"); 29 | for(i=0;i 6 | #include 7 | // Function to find the Repeating numbers 8 | void printRepeating(int A[], int size) 9 | { 10 | int *count = (int *)malloc((size - 2)*sizeof(int)); // Creating a Temporary array count to store Index 11 | int i; 12 | 13 | printf("Repeating elements are "); 14 | // Traverse the array once. 15 | for(i = 0; i < size; i++) 16 | { 17 | if(count[A[i]] == 1) //While traversing, keep track of count of all elements in the array using a temp array count[] of size n 18 | printf(" %d ", A[i]); 19 | else 20 | count[A[i]]++; 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | int i,n; 27 | printf("Enter the number of Elements to be Entered\n"); 28 | scanf("%d", &n); 29 | int *A = (int*)malloc(n*sizeof(int)); 30 | printf("Enter the Elements of Array\n"); 31 | for(i=0;i 4 | void Common_Elements(int A[], int B[], int C[], int a, int b, int c) { 5 | int i = 0,j = 0, k = 0; // Intializing the Index 6 | // Transverse Through all three arrays simultaneously using indexes i,j,k. 7 | printf("Comman Elements are :\n"); 8 | while(i 5 | #include 6 | 7 | // Function used to swap two variable in quickSort 8 | void swap(int* a, int* b) 9 | { 10 | int t = *a; 11 | *a = *b; 12 | *b = t; 13 | } 14 | int partition (int arr[], int low, int high) 15 | { 16 | int pivot = arr[high]; // pivot 17 | int i = (low - 1); 18 | int j; 19 | for (j = low; j <= high- 1; j++) 20 | { 21 | if (arr[j] <= pivot) 22 | { 23 | i++; 24 | swap(&arr[i], &arr[j]); 25 | } 26 | } 27 | swap(&arr[i + 1], &arr[high]); 28 | return (i + 1); 29 | } 30 | // Function to perform quick quickSort 31 | void quickSort(int arr[], int low, int high) 32 | { 33 | if (low < high) 34 | { 35 | int pi = partition(arr, low, high); 36 | quickSort(arr, low, pi - 1); 37 | quickSort(arr, pi + 1, high); 38 | } 39 | } 40 | 41 | // Function two find all the Duplicate Number in the Array 42 | void DuplicateNum(int A[], int size) 43 | { 44 | int i; 45 | quickSort(A, 0, size-1); 46 | printf("The repeating elements are: \n"); 47 | for (i = 0; i < size;) 48 | { 49 | if(A[i] == A[i+1]) // After sorting if the Element is matches with Next Element 50 | { // then this Element is Duplicate 51 | printf(" %d ", A[i]); 52 | i++; 53 | } 54 | else 55 | { 56 | i++; 57 | } 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | int i,n; 64 | printf("Enter the number of Elements to be Entered\n"); 65 | scanf("%d", &n); 66 | int *A = (int*)malloc(n*sizeof(int)); 67 | printf("Enter the Elements of Array\n"); 68 | for(i=0;i 8 | #include 9 | 10 | // Function to check the Missing Number 11 | int Missing_ele(int *A, int n) 12 | { 13 | int sum=0; 14 | int i; 15 | 16 | for(i=1;i<=n;i++) 17 | { 18 | sum = sum + i; // Calculating the sum of 1 to n integers 19 | } 20 | 21 | for(i=0;i<(n-1);i++) 22 | { 23 | sum = sum - *(A+i); // Substracting the elements of the Array from this Number 24 | } 25 | 26 | int Missing_Element; 27 | Missing_Element = sum; 28 | return Missing_Element; 29 | 30 | } 31 | 32 | int main() 33 | { 34 | int i,n; 35 | 36 | printf("Enter the number of Elements to be Entered\n"); 37 | scanf("%d", &n); 38 | 39 | int *A = (int*)malloc(n*sizeof(int)); 40 | 41 | printf("Enter the Elements of Array\n"); 42 | 43 | for(i=0;i<(n-1);i++) 44 | { 45 | scanf("%d", &A[i]); 46 | } 47 | 48 | int Num; 49 | Num = Missing_ele(A,n); 50 | printf("\nMissing Element: %d\n\n", Num); 51 | } 52 | 53 | 54 | /* 55 | Sample: 56 | Input: 57 | Enter the number of Elements to be Entered 58 | 7 59 | Enter the Elements of Array 60 | 1 61 | 7 62 | 5 63 | 4 64 | 3 65 | 6 66 | 67 | Output: 68 | Missing Element: 2 69 | */ 70 | -------------------------------------------------------------------------------- /Array_Problems/1_Recursive_Even_num.c: -------------------------------------------------------------------------------- 1 | // Recursive Algorithm to find the kth Even Natural Number 2 | 3 | #include 4 | // Function to find the kth Even Natural Number 5 | int Num(int k) 6 | { 7 | if(k > 0) 8 | return Num(k-1) + 2; //Num is reducing and addition of 2 each time 9 | else 10 | return 0; //Loop Terminating Condition 11 | } 12 | 13 | int main() 14 | { 15 | int n; 16 | printf("Enter the Value of K:\n "); 17 | scanf("%d", &n); 18 | printf("The kth Even Natural Number is %d\n", Num(n)); // Printing The kth Even Natural Number 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Array_Problems/2_First_Duplicate.c: -------------------------------------------------------------------------------- 1 | // An algorithm for finding the first element in the array 2 | // which is repeated 3 | 4 | #include 5 | #include 6 | // Function used to find first Duplicate element 7 | void First_Duplicate(int A[], int size) 8 | { 9 | int i, j; 10 | printf("The First repeating elements is: \n"); 11 | // Traversing the array to find the First Duplicate number 12 | for (i = 0; i < size-1; i++) 13 | { 14 | for(j =i+1; j 3 | 4 | // Function to find the kth Even Natural Number 5 | int main() { 6 | int k,i,num; 7 | printf("Enter the value of K to Find Kth even Natural Number\n"); 8 | scanf("%d",&k); 9 | i = 1; 10 | num = 0; 11 | while(i <= k) { //Till i is less then or equal to the entered number k it will add 2 each time 12 | num = num + 2; 13 | i++; 14 | } 15 | printf("The kth even Natural Number: %d\n", num); // Printing the kth Even Natural Number 16 | } 17 | -------------------------------------------------------------------------------- /Array_Problems/2_Strictly_smaller_num.c: -------------------------------------------------------------------------------- 1 | // 17CSE1029 - ARCHIT 2 | 3 | // Given a sorted array and a target value, find the first element that is strictly smaller than 4 | // given element. 5 | 6 | #include 7 | #include 8 | 9 | //Trivial Function find the first element that is strictly smaller than 10 | // given element. 11 | /*int Strictly_smaller_num_trivial(int A[], int n, int k) { 12 | int i; 13 | for(i=0;i k && A[i - 1] < k ) ) { 15 | return A[i-1]; 16 | break; 17 | } 18 | } 19 | }*/ 20 | 21 | //Efficient Function find the first element that is strictly smaller than 22 | // given element. 23 | // Using Binary Search Concept. 24 | int Strictly_smaller_num(int A[], int first, int last, int k) { 25 | if(A[first] > k ) { 26 | printf("No Smaller Element then target Element present\n"); 27 | return -1; 28 | } 29 | else if(A[last] < k) { 30 | return A[last]; 31 | } 32 | else { 33 | while(last>=first) 34 | { 35 | int middle = (first + last)/2; 36 | if(A[middle]==k || (A[middle] > k && A[middle - 1] < k )) { 37 | return A[middle-1]; 38 | } 39 | 40 | // Above I created it like that if in case targeted element is not from the given elements 41 | // Then also it will find first element that is strictly smaller than targeted Value 42 | // By checking after dividing matrix if middle element is equal to targeted element 43 | // or if middle is greater but middle - 1 element is smaller then it will return next element 44 | 45 | else if(A[middle]>k){ 46 | return Strictly_smaller_num(A, first, middle-1, k); 47 | } 48 | else 49 | return Strictly_smaller_num(A, middle+1, last, k); 50 | } 51 | } 52 | } 53 | 54 | int main() 55 | { 56 | int i,n; 57 | 58 | printf("Enter the number of Elements to be Entered\n"); 59 | scanf("%d", &n); 60 | 61 | int *A = (int*)malloc(n*sizeof(int)); 62 | 63 | printf("Enter the Elements of Array (sorted)\n"); 64 | 65 | for(i=0;i 4 | #include 5 | // Function used to find first non - repeating element 6 | int First_NonRepeating(int A[], int n) 7 | { 8 | int i, j; 9 | printf("The First Non repeating elements is: \n"); 10 | // Traversing the array 11 | for (i=0;i 3 | #include 4 | int Smallest_Num(int A[], int size) 5 | { 6 | int i; 7 | int k = 1; // Initialize result 8 | // Traverse the array and increment 'k' if arr[i] is 9 | // smaller than or equal to 'k'. 10 | for(i = 0;i < size && A[i] <= k; i++) 11 | k = k + A[i]; 12 | return k; 13 | } 14 | 15 | int main() 16 | { 17 | int i,n; 18 | printf("Enter the number of Elements to be Entered\n"); 19 | scanf("%d", &n); 20 | int *A = (int*)malloc(n*sizeof(int)); 21 | printf("Enter the Elements of Array\n"); 22 | for(i=0;i 4 | 5 | int fib(int n) { 6 | //Declare an array to store Fibonacci numbers. 7 | int f[n+1]; // 1 extra space for n = 0 8 | int i; 9 | f[0] = 0; // Initial Values of Series 10 | f[1] = 1; 11 | for (i = 2; i <= n; i++) { 12 | f[i] = f[i-1] + f[i-2]; // Add the previous 2 numbers in the series and store it 13 | } 14 | return f[n]; 15 | } 16 | int main () 17 | { 18 | int n; 19 | printf("Which nth Fibonnaci Number have to print\n"); 20 | scanf("%d",&n); 21 | printf("Nth Fibonacci Number is %d\n", fib(n)); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Array_Problems/3_Sum_Recursive.c: -------------------------------------------------------------------------------- 1 | // 17CSE1029 - ARCHIT 2 | 3 | // To Find the sum of the integers from 1 through n. Using recursion. 4 | 5 | #include 6 | 7 | // Function to compute the sum of first n Integers 8 | // Here i use long long in data type to Calculating sum of large till large values of n 9 | long long int Sum(long long int n) 10 | { 11 | if(n > 0) 12 | return n + Sum(n-1); // Decrement in value of n to find sum using recursion. 13 | 14 | return n; 15 | } 16 | 17 | int main() 18 | { 19 | long long int n; 20 | 21 | printf("Enter the n value: "); 22 | scanf("%lld", &n); 23 | 24 | printf("\nThe sum of the integers from 1 through %lld : %lld\n\n", n , Sum(n)); 25 | return 0; 26 | } 27 | 28 | /* 29 | Sample 1: 30 | Input: 31 | Enter the n value: 8 32 | 33 | 34 | Output: 35 | The sum of the integers from 1 through 8 : 36 36 | 37 | ------------------------------------------------------------------ 38 | 39 | Sample 2: 40 | Input: 41 | Enter the n value: 78945 42 | 43 | Output: 44 | The sum of the integers from 1 through 78945 : 3116195985 45 | 46 | */ 47 | -------------------------------------------------------------------------------- /Array_Problems/3_Sum_k.c: -------------------------------------------------------------------------------- 1 | // Find two elements in the array such that their sum is equal to given element k. 2 | 3 | #include 4 | #include 5 | // Function to swap two variable 6 | void swap(int* a, int* b) 7 | { 8 | int t = *a; 9 | *a = *b; 10 | *b = t; 11 | } 12 | 13 | int partition (int arr[], int low, int high) 14 | { 15 | int pivot = arr[high]; // pivot 16 | int i = (low - 1); 17 | int j; 18 | for (j = low; j <= high- 1; j++) 19 | { 20 | if (arr[j] <= pivot) 21 | { 22 | i++; 23 | swap(&arr[i], &arr[j]); 24 | } 25 | } 26 | swap(&arr[i + 1], &arr[high]); 27 | return (i + 1); 28 | } 29 | // Function to perform Quick Sort 30 | void quickSort(int arr[], int low, int high) 31 | { 32 | if (low < high) 33 | { 34 | int pi = partition(arr, low, high); 35 | quickSort(arr, low, pi - 1); 36 | quickSort(arr, pi + 1, high); 37 | } 38 | } 39 | 40 | // Function to find the two Elements such that their sum is equal to given element k 41 | void Sum_k(int A[], int size, int k) 42 | { 43 | quickSort(A,0,size); //Quick sort is used to sort the elements of array 44 | int first_i = 0; 45 | int last_i = size-1; 46 | while(first_i < last_i) // Loop will transerse till first_i = last_i 47 | { 48 | int t; 49 | t = A[first_i] + A[last_i]; 50 | if(t == k) // Will print the value 51 | { 52 | printf("The two elements are : %d and %d \n", A[first_i], A[last_i]); 53 | } 54 | if(t < k) 55 | first_i++; 56 | else 57 | last_i--; 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | int i,n; 64 | printf("Enter the number of Elements to be Entered\n"); 65 | scanf("%d", &n); 66 | int *A = (int*)malloc(n*sizeof(int)); 67 | printf("Enter the Elements of Array\n"); 68 | for(i=0;i 3 | #include 4 | 5 | void arrange(int A[], int n) 6 | { 7 | printf("Output: \n"); 8 | int* temp = (int*)malloc(n*sizeof(int)); // Create a temp array to separate and store -ve and +ve number 9 | int l = 0, h = n-1; 10 | int a; 11 | for(a=0;a=l) { 20 | printf(" %d ", temp[neg++]); 21 | printf(" %d ", temp[pos--]); 22 | } 23 | while(neg<=l) { 24 | printf(" %d ", temp[neg++]); 25 | } 26 | while(pos>=l) { 27 | printf(" %d ", temp[pos--]); 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | int i,n; 34 | printf("Enter the number of Elements to be Entered\n"); 35 | scanf("%d", &n); 36 | int *A = (int*)malloc(n*sizeof(int)); 37 | printf("Enter the Elements of Array\n"); 38 | for(i=0;i 4 | // Function to compute the sum of first n Integers 5 | int Sum(int n) 6 | { 7 | if(n > 0) 8 | return n + Sum(n-1); // Reducing value of n also adding the value of n to create Recursion 9 | else 10 | return n; 11 | } 12 | 13 | int main() 14 | { 15 | int num; 16 | printf("Enter a Integer: "); 17 | scanf("%d", &num); 18 | printf("Sum of first %d Numbers is %d\n",num ,Sum(num)); // Printing the Sum of first n Integer 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Array_Problems/4_Sum_closestToZero.c: -------------------------------------------------------------------------------- 1 | //Given an array with both positive and negative numbers. We need to find the two 2 | //elements such that their sum is closest to zero. 3 | 4 | #include 5 | #include 6 | 7 | // Function used to swap two variable in quickSort 8 | void swap(int* a, int* b) 9 | { 10 | int t = *a; 11 | *a = *b; 12 | *b = t; 13 | } 14 | int partition (int arr[], int low, int high) 15 | { 16 | int pivot = arr[high]; // pivot 17 | int i = (low - 1); 18 | int j; 19 | for (j = low; j <= high- 1; j++) 20 | { 21 | if (arr[j] <= pivot) 22 | { 23 | i++; 24 | swap(&arr[i], &arr[j]); 25 | } 26 | } 27 | swap(&arr[i + 1], &arr[high]); 28 | return (i + 1); 29 | } 30 | // Function to perform quick quickSort 31 | void quickSort(int arr[], int low, int high) 32 | { 33 | if (low < high) 34 | { 35 | int pi = partition(arr, low, high); 36 | quickSort(arr, low, pi - 1); 37 | quickSort(arr, pi + 1, high); 38 | } 39 | } 40 | // Function to print pair of elements having minimum sum 41 | int Sum_closestToZero(int A[], int n) 42 | { 43 | int sum, min_sum; 44 | int l = 0, r = n-1; // Here l and r is 1st and last Index of an array 45 | int min_l = l, min_r = n-1; 46 | if(n < 2) // if only 2 elements are there 47 | { 48 | return 0; 49 | } 50 | 51 | // Sort the elements 52 | quickSort(A, l, r); 53 | while(l < r) 54 | { 55 | sum = A[l] + A[r]; 56 | if(abs(sum) < abs(min_sum)) 57 | { 58 | min_sum = sum; 59 | min_l = l; 60 | min_r = r; 61 | } 62 | if(sum < 0) 63 | l++; 64 | else 65 | r--; 66 | } 67 | 68 | printf(" The two elements whose sum is minimum are %d and %d",A[min_l], A[min_r]); 69 | } 70 | int main() 71 | { 72 | int i,n; 73 | printf("Enter the number of Elements to be Entered\n"); 74 | scanf("%d", &n); 75 | int *A = (int*)malloc(n*sizeof(int)); 76 | printf("Enter the Elements of Array\n"); 77 | for(i=0;i 6 | #include 7 | 8 | //Function to Count Number of zeros 9 | int Count_Zeros(int *A, int n, int countZ) 10 | { 11 | if(n==-1) 12 | return countZ; 13 | else 14 | { 15 | if(A[n]==0) 16 | countZ=1+Count_Zeros(A,n-1,countZ); 17 | else 18 | countZ=0+Count_Zeros(A,n-1,countZ); 19 | } 20 | 21 | } 22 | 23 | int main() 24 | { 25 | int i,n; 26 | 27 | printf("Enter the number of Elements to be Entered\n"); 28 | scanf("%d", &n); 29 | 30 | int *A = (int*)malloc(n*sizeof(int)); 31 | 32 | printf("Enter the Elements of Array \n"); 33 | 34 | for(i=0;i 3 | #include 4 | void sum_equal_zero(int A[], int n) { 5 | int i,j; 6 | for(i=0;i 4 | #include 5 | // Function to remove Duplicates 6 | int Duplicates(int A[], int size) 7 | { 8 | int i,j=0; 9 | for(i=0; i < size-1; i++) { 10 | if (A[i] != A[i+1]) { 11 | A[j++] = A[i]; 12 | } 13 | } 14 | A[j++] = A[size-1]; 15 | return j; // Return Lenght of New Array 16 | } 17 | int main() 18 | { 19 | int i,n; 20 | printf("Enter the number of Elements to be Entered\n"); 21 | scanf("%d", &n); 22 | int *A = (int*)malloc(n*sizeof(int)); 23 | printf("Enter the Elements of Array\n"); 24 | for(i=0;i 5 | #include 6 | 7 | // Function used to swap two variable in quickSort 8 | void swap(int* a, int* b) 9 | { 10 | int t = *a; 11 | *a = *b; 12 | *b = t; 13 | } 14 | int partition (int arr[], int low, int high) 15 | { 16 | int pivot = arr[high]; // pivot 17 | int i = (low - 1); 18 | int j; 19 | for (j = low; j <= high- 1; j++) 20 | { 21 | if (arr[j] <= pivot) 22 | { 23 | i++; 24 | swap(&arr[i], &arr[j]); 25 | } 26 | } 27 | swap(&arr[i + 1], &arr[high]); 28 | return (i + 1); 29 | } 30 | // Function to perform quick quickSort 31 | void quickSort(int arr[], int low, int high) 32 | { 33 | if (low < high) 34 | { 35 | int pi = partition(arr, low, high); 36 | quickSort(arr, low, pi - 1); 37 | quickSort(arr, pi + 1, high); 38 | } 39 | } 40 | // Function for finding the element which 41 | // appears maximum number of times in the array 42 | int Max_Repeating(int A[], int size) 43 | { 44 | quickSort(A, 0, size-1); // Quick sort is used to sort the elements of array 45 | int i; 46 | int max_repeat = 0, max_repeat_i = 0; 47 | int count; 48 | for(i=0;i max_repeat) 57 | { 58 | max_repeat = count; 59 | max_repeat_i = i; 60 | } 61 | } 62 | return A[max_repeat_i]; 63 | } 64 | 65 | int main() 66 | { 67 | int i,n; 68 | printf("Enter the number of Elements to be Entered\n"); 69 | scanf("%d", &n); 70 | int *A = (int*)malloc(n*sizeof(int)); 71 | printf("Enter the Elements of Array\n"); 72 | for(i=0;i 5 | // Function to Find the sum of digit without using Recursion 6 | int Sum(int n) { 7 | int Sum_of_digit = 0; //Intialize the Sum_of_digit to zero 8 | int digit; //To store the last Digit of 7-Digit Number 9 | while(n > 0) 10 | { 11 | digit = n%10; // Take Mod of input number n by 10 to get remainder 12 | Sum_of_digit = Sum_of_digit + digit; // add the remainder to Sum_of_digit 13 | n/=10; // Divide the input number by 10 to get Quotient 14 | } 15 | return Sum_of_digit; // Returning Sum_of_digit after repeating steps till Quotient became zero 16 | } 17 | int main() { 18 | int num; 19 | printf("Enter a 7-digit Number:\n"); 20 | scanf("%d", &num); 21 | Sum(num); 22 | printf("Sum of digits of the 7-digit number: %d\n", Sum(num)); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Array_Problems/5b_Recursive_Sum_of_digit.c: -------------------------------------------------------------------------------- 1 | // Sum of digits of the 7-digit number: 2 | // Using Recursion 3 | #include 4 | // Function to Find the sum of digit using recursion 5 | int Sum(int n) { 6 | if (n == 0) 7 | return 0; // Terminating Condition 8 | else 9 | return (n % 10 + Sum(n / 10)); // Recursive call to add next last digit of the Number 10 | } 11 | 12 | 13 | int main() { 14 | int num; 15 | printf("Enter a 7-digit Number:\n"); 16 | scanf("%d", &num); 17 | Sum(num); 18 | printf("Sum of digits of the 7-digit number: %d\n", Sum(num)); // Printing The Sum of Digit of the Number 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Array_Problems/6_Maximum.c: -------------------------------------------------------------------------------- 1 | // Let A be an array of n distinct integers. Suppose A has the following property: there 2 | // exists an index 1≤ k ≤ n such that A[1], . . . , A[k] is an increasing sequence and 3 | // A[k+1], . . . ,A[n] is a decreasing sequence. Design an algorithm for finding k. 4 | #include 5 | #define SIZE 10 6 | // Function to find Max element 7 | int Maximum(int A[], int first, int last) { 8 | if (first == last) // Checking the case when there is only 1 element 9 | return A[first]; 10 | if ((last == first + 1) && A[first] >= A[last]) // when there is only 2 Elements 11 | return A[first]; 12 | if ((last == first + 1) && A[first] < A[last]) 13 | return A[last]; 14 | int mid = (first + last)/2; 15 | if (A[mid] > A[mid + 1] && A[mid] > A[mid - 1]) // if mid is the Max element 16 | return A[mid]; 17 | if (A[mid] > A[mid + 1] && A[mid] < A[mid - 1]) // removing left side or right side element of mid to find max element 18 | return Maximum(A, first, mid - 1); 19 | else 20 | return Maximum(A, mid + 1, last); 21 | } 22 | 23 | int main() { 24 | int A[SIZE]; 25 | int i; 26 | printf("Enter the Elements of Array upto 10\n"); 27 | for(i=0;i 5 | int Call_Counter = 0; // Global Variable to find the Number of calls required 6 | // Function to Find Fibonacci (n) using Recursion 7 | int fib(int n) 8 | { 9 | Call_Counter++; 10 | if(n<=1) 11 | return n; // For First Two Number of Series 12 | else 13 | { 14 | return fib(n-1) + fib(n-2); // Recursive call of the Fibonacci Function 15 | } 16 | } 17 | int main() 18 | { 19 | int n, a; 20 | printf("Write the Value Of N\n"); 21 | scanf("%d", &n); // getting the Number as Input 22 | a=fib(n); // Calling above function to find Binary Equivalent of the number 23 | printf("Nth Fibonacci Number: %d ",a); // Printing the Binary Equivalent of the Number 24 | printf("is Obtained in %d Calls\n",Call_Counter); // Printing Numbers Of Calls 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Array_Problems/6_Strictly_greater_num.c: -------------------------------------------------------------------------------- 1 | // 17CSE1029 - ARCHIT 2 | 3 | //Given a sorted array and a target value, find the first element that is strictly greater than 4 | //given element. 5 | 6 | #include 7 | #include 8 | 9 | //Trivial Function find the first element that is strictly greater than 10 | // given element. 11 | /*int Strictly_greater_num_trivial(int A[], int n, int k) { 12 | int i; 13 | for(i=0;i k) { 15 | return A[i]; 16 | break; 17 | } 18 | } 19 | }*/ 20 | 21 | //Efficient Function find the first element that is strictly greater than 22 | // given element. 23 | // Using Binary Search Concept. 24 | int Strictly_greater_num(int A[], int first, int last, int k) { 25 | if(A[first] > k) { 26 | return A[first]; 27 | } 28 | else if(A[last] < k) { 29 | printf("Error: No such Element Found\n"); 30 | return -1; 31 | } 32 | else { 33 | while(last>=first) 34 | { 35 | int middle = (first + last)/2; 36 | if(A[middle]==k || (A[middle] < k && A[middle + 1] > k )) { 37 | return A[middle+1]; 38 | } 39 | 40 | // Above I created it like that if in case targeted element is not from the given elements 41 | // Then also it will find first element that is strictly greater than targeted Value 42 | // By checking after dividing matrix if middle element is equal to targeted element 43 | // or if middle is less but middle + 1 element is greater then it will return next element 44 | 45 | else if(A[middle]>k) 46 | return Strictly_greater_num(A, first, middle, k); 47 | else 48 | return Strictly_greater_num(A, middle+1, last, k); 49 | } 50 | } 51 | 52 | // It should never reach this point, but the compiler still expects a return 53 | // value, otherwise it throws error warnings. 54 | printf("Error: something bad happened!\n"); 55 | return -1; 56 | } 57 | 58 | int main() 59 | { 60 | int i,n; 61 | 62 | printf("Enter the number of Elements to be Entered\n"); 63 | scanf("%d", &n); 64 | 65 | int *A = (int*)malloc(n*sizeof(int)); 66 | 67 | printf("Enter the Elements of Array (sorted)\n"); 68 | 69 | for(i=0;i= A[i+1]) 78 | { 79 | printf("\n\nInvalid input. Please, enter a sorted array without" 80 | " repeated numbers.\n\n"); 81 | return -1; 82 | } 83 | } 84 | 85 | int target_value; 86 | printf("\nEnter the Target Value:"); 87 | scanf("%d", &target_value); 88 | 89 | int Num; 90 | Num = Strictly_greater_num(A, 0, n-1, target_value); 91 | printf("\n\nThe first element that is strictly greater than %d: %d\n\n", target_value, Num); 92 | 93 | return 0; 94 | } 95 | 96 | 97 | /* 98 | Sample: 99 | Input: 100 | Enter the number of Elements to be Entered 101 | 7 102 | Enter the Elements of Array (sorted) 103 | 1 104 | 4 105 | 7 106 | 8 107 | 9 108 | 15 109 | 60 110 | 111 | Output: 112 | The first element that is strictly greater than 14: 15 113 | 114 | */ 115 | -------------------------------------------------------------------------------- /Array_Problems/6_remove_element.c: -------------------------------------------------------------------------------- 1 | //To Remove the given Element from the array 2 | #include 3 | #include 4 | // Function to print Array Elements 5 | void print_array(int *A, int n) { 6 | int i; 7 | printf("Enter the Elements of Array\n"); 8 | for (i=0; i0 && k<(n-1)) // if we have to remove any middle one 32 | { 33 | for (i=0; i 4 | #include 5 | // Function to Find occurrences of given Element 6 | int Num_of_occurrence(int A[], int size, int k) 7 | { 8 | int i; 9 | int count = 0; 10 | for(i=0;i 5 | // Function to Find the binary equivalent of the number using recursion 6 | int Binary(int n) { 7 | if (n == 0) 8 | return 0; 9 | else { 10 | return (n % 2 + 10*Binary(n / 2)); // Recursive call of the Function 11 | } 12 | } 13 | 14 | 15 | int main() { 16 | int num; 17 | printf("Enter a Number:\n"); 18 | scanf("%d", &num); // getting the Number as Input 19 | Binary(num); // Calling above function to find Binary Equivalent of the number 20 | printf("Binary equivalent of the Number : %d\n", Binary(num)); // Printing the Binary Equivalent of the Number 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Array_Problems/7_Repeating_k_times.c: -------------------------------------------------------------------------------- 1 | // 17CSE1029 - ARCHIT 2 | 3 | //Given a list of N integers A = [a 1 , a 2 ,..., a N ], you have to find those integers which are 4 | //repeated at least K times. In case no such element exists you have to print -1. If there 5 | //are multiple elements in A which are repeated at least K times, then print these elements 6 | //ordered by their first occurrence in the list. 7 | 8 | #include 9 | #include 10 | // Function used to find first Duplicate element 11 | void Duplicates(int A[], int size, int k) 12 | { 13 | int i, j; 14 | int count; 15 | printf("\nElements which are Repeating %d times are\n", k); 16 | // Traversing the array to find the Duplicate numbers 17 | // Here I simply check the next element of ith element and count this and make it zero 18 | // Then print the element which is positive and is counted k times. 19 | for (i = 0; i < size-1; i++) 20 | { 21 | count = 1; 22 | for(j =i+1; j = k && A[i] > 0) { 31 | printf("%d\t", A[i]); 32 | } 33 | } 34 | printf("\n"); 35 | } 36 | int main() 37 | { 38 | int i,n; 39 | printf("Enter the number of Elements to be Entered\n"); 40 | scanf("%d", &n); 41 | int *A = (int*)malloc(n*sizeof(int)); 42 | printf("Enter the Elements of Array\n"); 43 | for(i=0;i 4 | #define n 4 5 | int Row_with_Max_0(int Mat[n][n]) { 6 | int i = 0, j = n-1; 7 | int Max_0_row = 0; 8 | while( i < n && j > 0 ) 9 | { 10 | if ( Mat[i][j] == 0 ) // Checking the Element is zero or not 11 | { 12 | j--; // if zero then traverse back to previous column 13 | Max_0_row = i; 14 | } 15 | else { 16 | i++; // else go to next row 17 | } 18 | } 19 | return i; 20 | } 21 | int main() 22 | { 23 | int Mat[n][n] = { {1, 1, 0, 0}, 24 | {1, 1, 1, 0}, 25 | {1, 0, 0, 0}, 26 | {1, 1, 1, 1}}; 27 | printf("Index of row with maximum 0s is %d ", Row_with_Max_0(Mat)); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Array_Problems/8a_Trivial_GCD.c: -------------------------------------------------------------------------------- 1 | // To compute the greatest common divisor (GCD) of any two numbers 2 | // Trivial Approach 3 | #include 4 | // Function to compute the greatest common divisor (GCD) of two numbers 5 | int GCD_Func(int a, int b) { 6 | int i, gcd; 7 | for(i=1;i<=a && i<=b;i++) 8 | { 9 | if(a%i==0 && b%i==0) // if both a and b are exactly divisible by i, the value of i is assigned to gcd 10 | gcd = i; 11 | } 12 | return gcd; 13 | } 14 | int main() 15 | { 16 | int a, b, GCD; 17 | printf("Enter the two Numbers:\n"); 18 | scanf("%d %d", &a, &b); 19 | GCD = GCD_Func(a,b); // Calling the GCD_Func 20 | printf("GCD of Entered Two Number is %d\n",GCD); // Printing the GCD of the Entered 2 Numbers 21 | return 0; 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /Array_Problems/8b_Euclidean_GCD.c: -------------------------------------------------------------------------------- 1 | // Euclidean algorithm to compute the GCD of two numbers 2 | #include 3 | // Function to return GCD of a and b 4 | int GCD_Func(int a, int b) 5 | { 6 | if (a == 0) 7 | return b; // The algorithm stops when we find remainder of b/a = 0 8 | else 9 | return GCD_Func(b%a, a); // If we Divide smaller number from larger (we reduce larger number), GCD doesn’t change 10 | } 11 | int main() 12 | { 13 | int a, b, GCD; 14 | printf("Enter the two Numbers:\n"); 15 | scanf("%d %d", &a, &b); 16 | GCD = GCD_Func(a,b); // Calling the GCD_Func 17 | printf("GCD of Entered Two Number is %d\n",GCD); // Printing the GCD of the Entered 2 Numbers 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Array_Problems/9_Missing.c: -------------------------------------------------------------------------------- 1 | //We are given a list of n-1 integers and these integers are in the range of 1 to n. There are 2 | //no duplicates in list. One of the integers is missing in the list. Given an algorithm to find 3 | //the missing integer. 4 | 5 | #include 6 | #include 7 | // Function to check the Missing Number 8 | int Missing(int *A, int n) 9 | { 10 | int sum; 11 | int i; 12 | sum = n*(n+1)/2; // Formula to found the sum of n numbers 13 | for(i=0;i<(n-1);i++) 14 | { 15 | sum = sum - *(A+i); // Substracting the elements of the Array from this Number 16 | } 17 | int MissingElement; 18 | MissingElement = sum; 19 | return MissingElement; 20 | 21 | } 22 | int main() 23 | { 24 | int i,n; 25 | printf("Enter the number of Elements to be Entered\n"); 26 | scanf("%d", &n); 27 | int *A = (int*)malloc(n*sizeof(int)); 28 | printf("Enter the Elements of Array\n"); 29 | for(i=0;i<(n-1);i++) 30 | { 31 | scanf("%d", &A[i]); 32 | } 33 | int Num; 34 | Num = Missing(A,n); 35 | printf("%d", Num); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Array_Problems/Maximum-Sum-Subarray-[Kadanes-Algorithm]: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of integers containing positive as well as negative integers. Find the sum of subarray which has maximum sum, in the given array. 3 | 4 | Input : 2,-3,4,5,-1 5 | Output : 9 (Subarray - [2,3]) 6 | 7 | This is O(N) in time complexity. 8 | */ 9 | 10 | #include 11 | using namespace std; 12 | 13 | int maxSubArray(vector& arr) { 14 | int curr_sum=0, max_sum=INT_MIN; 15 | for(int i=0; i arr{2,-3,4,5,-1}; 26 | int res = maxSubArray(arr); 27 | cout<<"Sum of Maximum sum subarray is "< answer: [(1, 2), (4, 4), (6, 10)] 4 | """ 5 | 6 | def missing_ranges(arr, lo, hi): 7 | 8 | res = [] 9 | start = lo 10 | 11 | for n in arr: 12 | 13 | if n == start: 14 | start += 1 15 | elif n > start: 16 | res.append((start, n-1)) 17 | start = n + 1 18 | 19 | if start <= hi: # after done iterating thru array, 20 | res.append((start, hi)) # append remainder to list 21 | 22 | return res 23 | -------------------------------------------------------------------------------- /Array_Problems/spiral_matrix.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a matrix of m x n elements (m rows, n columns), 3 | return all elements of the matrix in spiral order. 4 | For example, 5 | Given the following matrix: 6 | [ 7 | [ 1, 2, 3 ], 8 | [ 4, 5, 6 ], 9 | [ 7, 8, 9 ] 10 | ] 11 | You should return [1,2,3,6,9,8,7,4,5]. 12 | """ 13 | 14 | 15 | def spiral_traversal(matrix): 16 | res = [] 17 | if len(matrix) == 0: 18 | return res 19 | row_begin = 0 20 | row_end = len(matrix) - 1 21 | col_begin = 0 22 | col_end = len(matrix[0]) - 1 23 | 24 | while row_begin <= row_end and col_begin <= col_end: 25 | for i in range(col_begin, col_end+1): 26 | res.append(matrix[row_begin][i]) 27 | row_begin += 1 28 | 29 | for i in range(row_begin, row_end+1): 30 | res.append(matrix[i][col_end]) 31 | col_end -= 1 32 | 33 | if row_begin <= row_end: 34 | for i in range(col_end, col_begin-1, -1): 35 | res.append(matrix[row_end][i]) 36 | row_end -= 1 37 | 38 | if col_begin <= col_end: 39 | for i in range(row_end, row_begin-1, -1): 40 | res.append(matrix[i][col_begin]) 41 | col_begin += 1 42 | 43 | return res 44 | 45 | 46 | if __name__ == "__main__": 47 | mat = [[1, 2, 3], 48 | [4, 5, 6], 49 | [7, 8, 9]] 50 | print(spiral_traversal(mat)) 51 | -------------------------------------------------------------------------------- /Binary_Tree/1_Binary_tree.c: -------------------------------------------------------------------------------- 1 | // Implement Binary Search Tree Write a Function To Insert a node 2 | 3 | // To traverse the tree using all the methods i.e., inorder, preorder and postorder. 4 | 5 | // Given a non-empty binary search tree (an ordered binary tree), return the 6 | // minimum data value found in that tree. 7 | 8 | // Given a non-empty binary tree, count the number of leaf nodes in the tree. 9 | 10 | // Given a non-empty binary tree, write a function to find the height of the tree. 11 | 12 | 13 | #include 14 | #include 15 | 16 | struct Node { 17 | int data; 18 | struct Node *left; 19 | struct Node *right; 20 | }; 21 | typedef struct Node NODE; 22 | 23 | NODE *getnode(int x) { 24 | NODE *newnode = (NODE*)malloc(sizeof(NODE)); 25 | newnode->data = x; 26 | newnode->left = newnode->right = NULL; 27 | return newnode; 28 | } 29 | 30 | // Function to insert in BST, return the Node 31 | NODE *Insert(NODE *new, int x) { 32 | if(new == NULL) { // Empty tree 33 | new = getnode(x); 34 | return new; 35 | } 36 | else if(x <= new->data) { 37 | new->left = Insert(new->left, x); 38 | } 39 | else if(x >= new->data) { 40 | new->right = Insert(new->right, x); 41 | } 42 | return new; 43 | } 44 | 45 | // Preorder traversal 46 | void preorder_display(NODE *temp) { 47 | if(temp == NULL) { 48 | return; 49 | } 50 | else { 51 | printf(" %d ", temp->data); 52 | preorder_display(temp->left); 53 | preorder_display(temp->right); 54 | } 55 | return; 56 | } 57 | 58 | // Postorder traversal 59 | void postorder_display(NODE *temp) { 60 | if(temp == NULL) { 61 | return; 62 | } 63 | else { 64 | postorder_display(temp->left); 65 | postorder_display(temp->right); 66 | printf(" %d ", temp->data); 67 | } 68 | return; 69 | } 70 | 71 | // Inorder Traversal 72 | void inorder_display(NODE *temp) { 73 | if(temp == NULL) { 74 | return; 75 | } 76 | else { 77 | inorder_display(temp->left); 78 | printf(" %d ", temp->data); 79 | inorder_display(temp->right); 80 | } 81 | return; 82 | } 83 | 84 | //Minimum Data value 85 | void Min(NODE *temp) { 86 | if(temp == NULL) { 87 | printf("\nTree is Empty\n"); 88 | return; 89 | } 90 | while(temp->left != NULL) { 91 | temp = temp->left; 92 | } 93 | printf("Minimum Data Value in Tree; %d", temp->data); 94 | return; 95 | } 96 | 97 | // Height of Tree 98 | int Height(NODE *temp) { 99 | if(temp == NULL) { 100 | return 0; 101 | } 102 | int LT = Height(temp->left); 103 | int RT = Height(temp->right); 104 | if(LT>RT) { 105 | return LT + 1; 106 | } 107 | else { 108 | return RT + 1; 109 | } 110 | } 111 | 112 | // Number of Leaf 113 | int Num_leaf(NODE *temp) { 114 | if(temp == NULL) { 115 | return 0; 116 | } 117 | if(temp->left == NULL && temp->right == NULL) { 118 | return 1; 119 | } 120 | else { 121 | return Num_leaf(temp->left) + Num_leaf(temp->right); 122 | } 123 | } 124 | void main() { 125 | int a; 126 | int ele; 127 | NODE *root = NULL; 128 | while(a!=0) { 129 | printf("\n1: To Insert Nodes"); 130 | printf("\n2: Preorder Display Tree"); 131 | printf("\n3: Postorder Display Tree"); 132 | printf("\n4: Inorder Display Tree"); 133 | printf("\n5: Minimum data Value"); 134 | printf("\n6: Height of Tree"); 135 | printf("\n7: Number of Leaf Node"); 136 | printf("\n0: Quit\n"); 137 | scanf("%d", &a); 138 | if(a==0) { 139 | printf("-------------------------Thank You----------------------------\n"); 140 | return; 141 | } 142 | switch(a) { 143 | case 1 : 144 | { 145 | printf("Enter the Data\n"); 146 | scanf("%d", &ele); 147 | root = Insert(root, ele); 148 | break; 149 | } 150 | case 2 : 151 | { 152 | preorder_display(root); 153 | break; 154 | } 155 | case 3 : 156 | { 157 | postorder_display(root); 158 | break; 159 | } 160 | case 4 : 161 | { 162 | inorder_display(root); 163 | break; 164 | } 165 | case 5 : 166 | { 167 | Min(root); 168 | break; 169 | } 170 | case 6 : 171 | { 172 | int h; 173 | h = Height(root); 174 | printf("Height of Tree: %d", h); 175 | break; 176 | } 177 | case 7 : 178 | { 179 | int l; 180 | l = Num_leaf(root); 181 | printf("Number of leaf Node: %d", l); 182 | break; 183 | } 184 | default : 185 | { 186 | printf("Wrong Value Detacted\n"); 187 | break; 188 | } 189 | return; 190 | } 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /Binary_Tree/2_LookUp.c: -------------------------------------------------------------------------------- 1 | //Implement the LookUp () operation where given a binary search tree and a 2 | // “target” value, searches the tree to see if it contains the target. 3 | 4 | // Include Insert node Function 5 | // Include Inorder Traversal Function 6 | 7 | 8 | 9 | #include 10 | #include 11 | 12 | struct Node { 13 | int data; 14 | struct Node *left; 15 | struct Node *right; 16 | }; 17 | typedef struct Node NODE; 18 | 19 | NODE *getnode(int x) { 20 | NODE *newnode = (NODE*)malloc(sizeof(NODE)); 21 | newnode->data = x; 22 | newnode->left = newnode->right = NULL; 23 | return newnode; 24 | } 25 | 26 | // Function to insert in BST, return the Node 27 | NODE *Insert(NODE *new, int x) { 28 | if(new == NULL) { // Empty tree 29 | new = getnode(x); 30 | return new; 31 | } 32 | else if(x <= new->data) { 33 | new->left = Insert(new->left, x); 34 | } 35 | else if(x >= new->data) { 36 | new->right = Insert(new->right, x); 37 | } 38 | return new; 39 | } 40 | 41 | 42 | // Function to LookUp the Given Element in BST, return FOUND or NOTFOUND 43 | void LookUp(NODE *new, int x) { 44 | if(new == NULL) { 45 | printf("\nNOT FOUND\n"); 46 | return; 47 | } 48 | else if(new->data == x) { 49 | printf("\nFOUND\n"); 50 | return; 51 | } 52 | else if(x <= new->data) { 53 | return LookUp(new->left, x); 54 | } 55 | else if(x >= new->data){ 56 | return LookUp(new->right, x); 57 | } 58 | else { 59 | printf("\nNOT FOUND\n"); 60 | return; 61 | } 62 | } 63 | 64 | // Inorder Traversal 65 | void inorder_display(NODE *temp) { 66 | if(temp == NULL) { 67 | return; 68 | } 69 | else { 70 | inorder_display(temp->left); 71 | printf(" %d ", temp->data); 72 | inorder_display(temp->right); 73 | } 74 | return; 75 | } 76 | 77 | 78 | void main() { 79 | int a; 80 | int ele; 81 | NODE *root = NULL; 82 | while(a!=0) { 83 | printf("\n1: To Insert Nodes"); 84 | printf("\n2: Inorder Display Tree"); 85 | printf("\n3: LookUp the Element"); 86 | printf("\n0: Quit\n"); 87 | scanf("%d", &a); 88 | if(a==0) { 89 | printf("-------------------------Thank You----------------------------\n"); 90 | return; 91 | } 92 | switch(a) { 93 | case 1 : 94 | { 95 | printf("Enter the Data\n"); 96 | scanf("%d", &ele); 97 | root = Insert(root, ele); 98 | break; 99 | } 100 | case 2 : 101 | { 102 | inorder_display(root); 103 | break; 104 | } 105 | case 3 : 106 | { 107 | printf("Enter the Data\n"); 108 | scanf("%d", &ele); 109 | LookUp(root, ele); 110 | break; 111 | } 112 | 113 | default : 114 | { 115 | printf("Wrong Value Detacted\n"); 116 | break; 117 | } 118 | return; 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /Binary_Tree/3_Num_of_Nodes.c: -------------------------------------------------------------------------------- 1 | // Given a binary tree, count the number of nodes in the tree. 2 | 3 | // Include Insert node Function 4 | // Include Inorder Traversal Function 5 | 6 | 7 | #include 8 | #include 9 | 10 | struct Node { 11 | int data; 12 | struct Node *left; 13 | struct Node *right; 14 | }; 15 | typedef struct Node NODE; 16 | 17 | NODE *getnode(int x) { 18 | NODE *newnode = (NODE*)malloc(sizeof(NODE)); 19 | newnode->data = x; 20 | newnode->left = newnode->right = NULL; 21 | return newnode; 22 | } 23 | 24 | // Function to insert in BST, return the Node 25 | NODE *Insert(NODE *new, int x) { 26 | if(new == NULL) { // Empty tree 27 | new = getnode(x); 28 | return new; 29 | } 30 | else if(x <= new->data) { 31 | new->left = Insert(new->left, x); 32 | } 33 | else if(x >= new->data) { 34 | new->right = Insert(new->right, x); 35 | } 36 | return new; 37 | } 38 | // Function to count the Number of nodes in a tree 39 | int count(NODE *temp) { 40 | int c = 1; 41 | if(temp == NULL) { 42 | return 0; 43 | } 44 | else { 45 | c = c + count(temp->left); 46 | c = c + count(temp->right); 47 | } 48 | return c; 49 | } 50 | 51 | 52 | // Inorder Traversal of a tree 53 | void inorder_display(NODE *temp) { 54 | if(temp == NULL) { 55 | return; 56 | } 57 | else { 58 | inorder_display(temp->left); 59 | printf(" %d ", temp->data); 60 | inorder_display(temp->right); 61 | } 62 | return; 63 | } 64 | 65 | 66 | void main() { 67 | int a; 68 | int ele; 69 | NODE *root = NULL; 70 | while(a!=0) { 71 | printf("\n1: To Insert Nodes"); 72 | printf("\n2: Inorder Display Tree"); 73 | printf("\n3: To Count Number of Nodes"); 74 | printf("\n0: Quit\n"); 75 | scanf("%d", &a); 76 | if(a==0) { 77 | printf("-------------------------Thank You----------------------------\n"); 78 | return; 79 | } 80 | switch(a) { 81 | case 1 : 82 | { 83 | printf("Enter the Data\n"); 84 | scanf("%d", &ele); 85 | root = Insert(root, ele); 86 | break; 87 | } 88 | case 2 : 89 | { 90 | inorder_display(root); 91 | break; 92 | } 93 | case 3 : 94 | { 95 | int nodes; 96 | nodes = count(root); 97 | printf("Number of Nodes: %d", nodes); 98 | break; 99 | } 100 | default : 101 | { 102 | printf("Wrong Value Detacted\n"); 103 | break; 104 | } 105 | return; 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /Binary_Tree/4_Delete_Node.c: -------------------------------------------------------------------------------- 1 | //Write a program with a function to delete a node from Binary Search Tree. 2 | 3 | #include 4 | #include 5 | 6 | struct node { 7 | int data; 8 | struct node *left; 9 | struct node *right; 10 | }; 11 | 12 | typedef struct node NODE; 13 | 14 | NODE *newnode(int ele) { 15 | NODE *new = (NODE*)malloc(sizeof(NODE)); 16 | new->data = ele; 17 | new->left = NULL; 18 | new->right = NULL; 19 | return new; 20 | } 21 | 22 | NODE *Insert(NODE *new, int ele) { 23 | if(new == NULL) { 24 | return newnode(ele); 25 | } 26 | if(ele < new->data ) { 27 | new->left = Insert(new->left, ele); 28 | } 29 | else if(ele > new->data) { 30 | new->right = Insert(new->right, ele); 31 | } 32 | return new; 33 | } 34 | 35 | NODE *FindMin(NODE *temp) { 36 | if(temp == NULL) { 37 | printf("\nEmpty Tree\n"); 38 | return NULL; 39 | } 40 | while(temp->left != NULL) { 41 | temp = temp->left; 42 | } 43 | return temp; 44 | } 45 | 46 | NODE *DeleteNode(NODE *temp, int ele) { 47 | if(temp == NULL) { 48 | printf("\nEmpty Tree\n"); 49 | return temp; 50 | } 51 | if(ele < temp->data) { 52 | temp->left = DeleteNode(temp->left , ele); 53 | } 54 | else if(ele > temp->data) { 55 | temp->right = DeleteNode(temp->right, ele); 56 | } 57 | else { 58 | if(temp->left == NULL) { 59 | NODE *temp1 = temp->right; 60 | free(temp); 61 | return temp1; 62 | } 63 | if(temp->right == NULL) { 64 | NODE *temp2 = temp->left; 65 | free(temp); 66 | return temp2; 67 | } 68 | NODE *temp2 = FindMin(temp->right); 69 | temp->data = temp2->data; 70 | temp->right = DeleteNode(temp->right, temp2->data); 71 | } 72 | return temp; 73 | } 74 | 75 | 76 | void inorder_display(NODE *temp) { 77 | if(temp == NULL) { 78 | return; 79 | } 80 | else { 81 | inorder_display(temp->left); 82 | printf(" %d ", temp->data); 83 | inorder_display(temp->right); 84 | } 85 | return; 86 | } 87 | 88 | void main() { 89 | int a; 90 | int ele; 91 | NODE *root = NULL; 92 | while(a!=0) { 93 | printf("\n1: To Insert Nodes"); 94 | printf("\n2: Inorder Display Tree"); 95 | printf("\n3: Delete Node"); 96 | printf("\n0: Quit\n"); 97 | scanf("%d", &a); 98 | if(a==0) { 99 | printf("-------------------------Thank You----------------------------\n"); 100 | return; 101 | } 102 | switch(a) { 103 | case 1 : 104 | { 105 | printf("Enter the Data\n"); 106 | scanf("%d", &ele); 107 | root = Insert(root, ele); 108 | break; 109 | } 110 | 111 | case 2 : 112 | { 113 | inorder_display(root); 114 | break; 115 | } 116 | 117 | case 3 : 118 | { 119 | printf("Enter a node to Delete\n"); 120 | scanf("%d", &ele); 121 | DeleteNode(root, ele); 122 | break; 123 | } 124 | 125 | 126 | default : 127 | { 128 | printf("Wrong Value Detacted\n"); 129 | break; 130 | } 131 | return; 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /Binary_Tree/5_Min_Max_data_value.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node { 5 | int data; 6 | struct node *left; 7 | struct node *right; 8 | }; 9 | 10 | typedef struct node NODE; 11 | 12 | NODE *newnode(int ele) { 13 | NODE *new = (NODE*)malloc(sizeof(NODE)); 14 | new->data = ele; 15 | new->left = NULL; 16 | new->right = NULL; 17 | return new; 18 | } 19 | 20 | NODE *Insert(NODE *new, int ele) { 21 | if(new == NULL) { 22 | return newnode(ele); 23 | } 24 | if(ele < new->data ) { 25 | new->left = Insert(new->left, ele); 26 | } 27 | else if(ele > new->data) { 28 | new->right = Insert(new->right, ele); 29 | } 30 | return new; 31 | } 32 | 33 | void FindMin(NODE *temp) { 34 | if(temp == NULL) { 35 | printf("\nEmpty Tree\n"); 36 | return; 37 | } 38 | while(temp->left != NULL) { 39 | temp = temp->left; 40 | } 41 | printf("Minimum Value: %d", temp->data); 42 | return; 43 | } 44 | 45 | void FindMax(NODE *temp) { 46 | if(temp == NULL) { 47 | printf("\nEmpty Tree\n"); 48 | return; 49 | } 50 | while(temp->right != NULL) { 51 | temp = temp->right; 52 | } 53 | printf("Maximum Value: %d", temp->data); 54 | return; 55 | } 56 | 57 | void inorder_display(NODE *temp) { 58 | if(temp == NULL) { 59 | return; 60 | } 61 | else { 62 | inorder_display(temp->left); 63 | printf(" %d ", temp->data); 64 | inorder_display(temp->right); 65 | } 66 | return; 67 | } 68 | 69 | void main() { 70 | int a; 71 | int ele; 72 | NODE *root = NULL; 73 | while(a!=0) { 74 | printf("\n1: To Insert Nodes"); 75 | printf("\n2: Inorder Display Tree"); 76 | printf("\n3: Minimum Value"); 77 | printf("\n4: Maximum Value"); 78 | printf("\n0: Quit\n"); 79 | scanf("%d", &a); 80 | if(a==0) { 81 | printf("-------------------------Thank You----------------------------\n"); 82 | return; 83 | } 84 | switch(a) { 85 | case 1 : 86 | { 87 | printf("Enter the Data\n"); 88 | scanf("%d", &ele); 89 | root = Insert(root, ele); 90 | break; 91 | } 92 | case 2 : 93 | { 94 | inorder_display(root); 95 | break; 96 | } 97 | case 3 : 98 | { 99 | FindMin(root); 100 | break; 101 | } 102 | case 4 : 103 | { 104 | FindMax(root); 105 | break; 106 | } 107 | default : 108 | { 109 | printf("Wrong Value Detacted\n"); 110 | break; 111 | } 112 | return; 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /Binary_Tree/BST.h: -------------------------------------------------------------------------------- 1 | struct node{ 2 | int data; 3 | struct node* left; 4 | struct node* right; 5 | 6 | }; 7 | 8 | typedef struct node Node; 9 | Node* root=NULL; 10 | 11 | Node* createTreeNode(int data){ 12 | Node* newNode=malloc(sizeof(Node)); 13 | newNode->data=data; 14 | newNode->left=NULL; 15 | newNode->right=NULL; 16 | return newNode; 17 | } 18 | void Insert(int data){ 19 | Node *newNode=createTreeNode(data); 20 | if(root==NULL){ 21 | root=newNode; 22 | return; 23 | } 24 | Node* temp=root; 25 | while(1){ 26 | if(temp->data==data){ 27 | printf("Cannot enter duplicate items in a BST\n"); 28 | return; 29 | } 30 | 31 | else if(datadata && temp->left!=NULL) 32 | temp=temp->left; 33 | 34 | else if(data>temp->data && temp->right!=NULL) 35 | temp=temp->right; 36 | 37 | else if(datadata){ 38 | temp->left=newNode; 39 | return; 40 | } 41 | 42 | else{ 43 | temp->right=newNode; 44 | return; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Binary_Tree/Balanced_Binary_Tree/AVL_Tree.c: -------------------------------------------------------------------------------- 1 | //Implemenation of AVL Trees in C 2 | // -- Archit 3 | 4 | #include 5 | #include 6 | 7 | struct AVLNode { 8 | struct AVLNode *left; 9 | struct AVLNode *right; 10 | int data; 11 | int height; 12 | }; 13 | typedef struct AVLNode ANODE; 14 | 15 | ANODE *getnode(int x) { 16 | ANODE *newnode = (ANODE*)malloc(sizeof(ANODE)); 17 | newnode->data = x; 18 | newnode->height = 0; 19 | newnode->left = newnode->right = NULL; 20 | return newnode; 21 | } 22 | 23 | int max(int a, int b) { 24 | if(a > b) { 25 | return a; 26 | } 27 | else { 28 | return b; 29 | } 30 | } 31 | 32 | int height(ANODE *root) { 33 | if(!root) { 34 | return -1; 35 | } 36 | return root->height; 37 | } 38 | 39 | ANODE *singlerotateleft(ANODE *X) { 40 | ANODE *W = X->left; 41 | X->left = W->right; 42 | W->right = X; 43 | X->height = max(height(X->left), height(X->right))+1; 44 | W->height = max(height(W->left), X->height)+1; 45 | return W; 46 | } 47 | 48 | ANODE *singlerotateright(ANODE *X) { 49 | ANODE *W = X->right; 50 | X->right = W->left; 51 | W->left = X; 52 | X->height = max(height(X->left), height(X->right))+1; 53 | W->height = max(height(W->right), X->height)+1; 54 | return W; 55 | } 56 | 57 | ANODE *Doubleleft(ANODE *X) { 58 | X->left = singlerotateright(X->left); 59 | return singlerotateleft(X); 60 | } 61 | 62 | ANODE *Doubleright(ANODE *X) { 63 | X->right = singlerotateleft(X->right); 64 | return singlerotateright(X); 65 | } 66 | 67 | ANODE *Insert(ANODE *root,int x) { 68 | if(root == NULL) { // Empty tree 69 | root = getnode(x); 70 | return root; 71 | } 72 | if(x < root->data) { 73 | root->left = Insert(root->left, x); 74 | if(height(root->left)-height(root->right) == 2) { 75 | if(xleft->data) { 76 | root = singlerotateleft(root); 77 | } 78 | else { 79 | root = Doubleleft(root); 80 | } 81 | } 82 | } 83 | if(x > root->data) { 84 | root->right = Insert(root->right,x); 85 | if(height(root->right)-height(root->left) == 2) { 86 | if(xright->data) { 87 | root = singlerotateright(root); 88 | } 89 | else { 90 | root = Doubleright(root); 91 | } 92 | } 93 | } 94 | root->height = max(height(root->left), height(root->right)) + 1; 95 | return root; 96 | } 97 | 98 | void inorder_display(ANODE *temp) { 99 | if(temp == NULL) { 100 | return; 101 | } 102 | else { 103 | inorder_display(temp->left); 104 | printf(" %d ", temp->data); 105 | inorder_display(temp->right); 106 | } 107 | return; 108 | } 109 | void main() 110 | { 111 | int a; 112 | int ele; 113 | ANODE *root = NULL; 114 | while(a!=0) { 115 | printf("\n\n"); 116 | printf("Enter 1 -> Insert Node in AVL\n"); 117 | printf("Enter 2 -> Inorder\n"); 118 | printf("Enter 0 -> Exit\n"); 119 | scanf("%d", &a); 120 | if(a==0) { 121 | printf("-------------------------Thank You----------------------------\n"); 122 | return; 123 | } 124 | switch(a) { 125 | case 1 : 126 | printf("Enter the Element to Insert\n"); 127 | { 128 | scanf("%d", &ele); 129 | root = Insert(root, ele); 130 | break; 131 | } 132 | case 2 : 133 | { 134 | printf("inorder_display:\n"); 135 | inorder_display(root); 136 | break; 137 | } 138 | default : 139 | { 140 | printf("Wrong Value Detacted\n"); 141 | break; 142 | } 143 | return; 144 | } 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /Binary_Tree/Delete_Node.h: -------------------------------------------------------------------------------- 1 | struct node { 2 | int data; 3 | struct node *left; 4 | struct node *right; 5 | }; 6 | 7 | typedef struct node NODE; 8 | 9 | NODE *newnode(int ele) { 10 | NODE *new = (NODE*)malloc(sizeof(NODE)); 11 | new->data = ele; 12 | new->left = NULL; 13 | new->right = NULL; 14 | return new; 15 | } 16 | 17 | NODE *Insert(NODE *new, int ele) { 18 | if(new == NULL) { 19 | return newnode(ele); 20 | } 21 | if(ele < new->data ) { 22 | new->left = Insert(new->left, ele); 23 | } 24 | else if(ele > new->data) { 25 | new->right = Insert(new->right, ele); 26 | } 27 | return new; 28 | } 29 | 30 | NODE *FindMin(NODE *temp) { 31 | if(temp == NULL) { 32 | printf("\nEmpty Tree\n"); 33 | return NULL; 34 | } 35 | while(temp->left != NULL) { 36 | temp = temp->left; 37 | } 38 | return temp; 39 | } 40 | 41 | NODE *DeleteNode(NODE *temp, int ele) { 42 | if(temp == NULL) { 43 | printf("\nEmpty Tree\n"); 44 | return temp; 45 | } 46 | if(ele < temp->data) { 47 | temp->left = DeleteNode(temp->left , ele); 48 | } 49 | else if(ele > temp->data) { 50 | temp->right = DeleteNode(temp->right, ele); 51 | } 52 | else { 53 | if(temp->left == NULL) { 54 | NODE *temp1 = temp->right; 55 | free(temp); 56 | return temp1; 57 | } 58 | if(temp->right == NULL) { 59 | NODE *temp2 = temp->left; 60 | free(temp); 61 | return temp2; 62 | } 63 | NODE *temp2 = FindMin(temp->right); 64 | temp->data = temp2->data; 65 | temp->right = DeleteNode(temp->right, temp2->data); 66 | } 67 | return temp; 68 | } 69 | -------------------------------------------------------------------------------- /Binary_Tree/Leaf_NonLeaf_Level.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node { 5 | int data; 6 | struct node *left; 7 | struct node *right; 8 | }; 9 | 10 | typedef struct node NODE; 11 | 12 | NODE *newnode(int ele) { 13 | NODE *new = (NODE*)malloc(sizeof(NODE)); 14 | new->data = ele; 15 | new->left = NULL; 16 | new->right = NULL; 17 | return new; 18 | } 19 | 20 | NODE *Insert(NODE *new, int ele) { 21 | if(new == NULL) { 22 | return newnode(ele); 23 | } 24 | if(ele < new->data ) { 25 | new->left = Insert(new->left, ele); 26 | } 27 | else if(ele > new->data) { 28 | new->right = Insert(new->right, ele); 29 | } 30 | return new; 31 | } 32 | 33 | void Leaf_NonLeaf_Level(NODE* root,int level){ 34 | if(root->left == NULL && root->right == NULL){ 35 | printf("%d\tis at Leaf NODE\t\tLevel: %d\n",root->data,level); 36 | return; 37 | } 38 | printf("%d\tis at Non-leaf NODE\tLevel: %d\n",root->data,level); 39 | if(root->left) 40 | Leaf_NonLeaf_Level(root->left,level+1); 41 | if(root->right) 42 | Leaf_NonLeaf_Level(root->right,level+1); 43 | } 44 | 45 | void inorder_display(NODE *temp) { 46 | if(temp == NULL) { 47 | return; 48 | } 49 | else { 50 | inorder_display(temp->left); 51 | printf(" %d ", temp->data); 52 | inorder_display(temp->right); 53 | } 54 | return; 55 | } 56 | 57 | void main() { 58 | int a; 59 | int ele; 60 | NODE *root = NULL; 61 | while(a!=0) { 62 | printf("\n1: To Insert Nodes"); 63 | printf("\n2: Inorder Display Tree"); 64 | printf("\n3: Leaf_NonLeaf_Level"); 65 | printf("\n0: Quit\n"); 66 | scanf("%d", &a); 67 | if(a==0) { 68 | printf("-------------------------Thank You----------------------------\n"); 69 | return; 70 | } 71 | switch(a) { 72 | case 1 : 73 | { 74 | printf("Enter the Data\n"); 75 | scanf("%d", &ele); 76 | root = Insert(root, ele); 77 | break; 78 | } 79 | 80 | case 2 : 81 | { 82 | inorder_display(root); 83 | break; 84 | } 85 | 86 | case 3 : 87 | { 88 | Leaf_NonLeaf_Level(root, 0); 89 | break; 90 | } 91 | 92 | 93 | default : 94 | { 95 | printf("Wrong Value Detacted\n"); 96 | break; 97 | } 98 | return; 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /Binary_Tree/Level_order_traversal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include"Queue.h" 4 | 5 | 6 | void Level_order(Node *root){ 7 | enqueue(root); 8 | Node *temp; 9 | while(temp=dequeue()){ 10 | if(temp->left) 11 | enqueue(temp->left); 12 | if(temp->right) 13 | enqueue(temp->right); 14 | printf("%d ",temp->data); 15 | 16 | } 17 | printf("\n"); 18 | } 19 | 20 | void inorder_display(Node *temp) { 21 | if(temp == NULL) { 22 | return; 23 | } 24 | else { 25 | inorder_display(temp->left); 26 | printf(" %d ", temp->data); 27 | inorder_display(temp->right); 28 | } 29 | return; 30 | } 31 | 32 | void main() { 33 | int a; 34 | int ele; 35 | while(a!=0) { 36 | printf("\n1: To Insert Nodes"); 37 | printf("\n2: Inorder Display Tree"); 38 | printf("\n3: Level Order the Element"); 39 | printf("\n0: Quit\n"); 40 | scanf("%d", &a); 41 | if(a==0) { 42 | printf("-------------------------Thank You----------------------------\n"); 43 | return; 44 | } 45 | switch(a) { 46 | case 1 : 47 | { 48 | printf("Enter the Data\n"); 49 | scanf("%d", &ele); 50 | Insert(ele); 51 | break; 52 | } 53 | case 2: 54 | { 55 | inorder_display(root); 56 | break; 57 | } 58 | 59 | case 3 : 60 | { 61 | Level_order(root); 62 | break; 63 | } 64 | default : 65 | { 66 | printf("Wrong Value Detacted\n"); 67 | break; 68 | } 69 | return; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Binary_Tree/Min_Max_value_n_Delete.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Delete_Node.h" 4 | 5 | 6 | int findMin(NODE *temp) { 7 | if(temp == NULL) { 8 | printf("\nEmpty Tree\n"); 9 | return 0; 10 | } 11 | while(temp->left != NULL) { 12 | temp = temp->left; 13 | } 14 | return temp->data; 15 | } 16 | 17 | void delMin(NODE *root){ 18 | int min=findMin(root); 19 | root=DeleteNode(root,min); 20 | } 21 | 22 | int findMax(NODE *temp) { 23 | if(temp == NULL) { 24 | printf("\nEmpty Tree\n"); 25 | return 0; 26 | } 27 | while(temp->right != NULL) { 28 | temp = temp->right; 29 | } 30 | return temp->data; 31 | } 32 | 33 | void delMax(NODE *root){ 34 | int max=findMax(root); 35 | root=DeleteNode(root,max); 36 | } 37 | 38 | void inorder_display(NODE *temp) { 39 | if(temp == NULL) { 40 | return; 41 | } 42 | else { 43 | inorder_display(temp->left); 44 | printf(" %d ", temp->data); 45 | inorder_display(temp->right); 46 | } 47 | return; 48 | } 49 | 50 | void main() { 51 | int a; 52 | int ele; 53 | NODE *root = NULL; 54 | while(a!=0) { 55 | printf("\n1: To Insert Nodes"); 56 | printf("\n2: Inorder Display Tree"); 57 | printf("\n3: Minimum Value"); 58 | printf("\n4: Maximum Value"); 59 | printf("\n5: Delete Minimum Value"); 60 | printf("\n6: Delete Maximum Value"); 61 | printf("\n0: Quit\n"); 62 | scanf("%d", &a); 63 | if(a==0) { 64 | printf("-------------------------Thank You----------------------------\n"); 65 | return; 66 | } 67 | switch(a) { 68 | case 1 : 69 | { 70 | printf("Enter the Data\n"); 71 | scanf("%d", &ele); 72 | root = Insert(root, ele); 73 | break; 74 | } 75 | case 2 : 76 | { 77 | inorder_display(root); 78 | break; 79 | } 80 | case 3 : 81 | { 82 | int min = findMin(root); 83 | printf("\nMinimum Value: %d\n", min); 84 | break; 85 | } 86 | case 4 : 87 | { 88 | int max = findMax(root); 89 | printf("\nMaximum Value: %d\n", max); 90 | break; 91 | } 92 | case 5 : 93 | { 94 | delMin(root); 95 | break; 96 | } 97 | case 6 : 98 | { 99 | delMax(root); 100 | break; 101 | } 102 | default : 103 | { 104 | printf("Wrong Value Detacted\n"); 105 | break; 106 | } 107 | return; 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /Binary_Tree/Queue.h: -------------------------------------------------------------------------------- 1 | // Circular Queue Basic Function C Library 2 | #include"BST.h" 3 | struct Qnode{ 4 | struct node* item; 5 | struct Qnode *next; 6 | }; 7 | 8 | typedef struct Qnode QNode; 9 | QNode *last=NULL; 10 | 11 | QNode* createNode(Node *item){ 12 | QNode* newNode=(QNode*)malloc(sizeof(QNode)); 13 | newNode->item=item; 14 | newNode->next=NULL; 15 | return newNode; 16 | } 17 | 18 | 19 | void enqueue(Node* item){ 20 | QNode *newNode=createNode(item); 21 | if(last==NULL){ 22 | last=newNode; 23 | last->next=newNode; 24 | return; 25 | } 26 | newNode->next=last->next; 27 | last->next=newNode; 28 | last=newNode; 29 | } 30 | 31 | 32 | Node* dequeue(){ 33 | if(last==NULL){ 34 | return NULL; 35 | } 36 | if(last->next==last){ 37 | Node *item=last->item; 38 | free(last); 39 | last=NULL; 40 | return item; 41 | } 42 | Node* item; 43 | QNode *temp=last->next; 44 | last->next=(last->next)->next; 45 | item=temp->item; 46 | free(temp); 47 | return item; 48 | } 49 | -------------------------------------------------------------------------------- /Binary_Tree/Range Sum of BST.java: -------------------------------------------------------------------------------- 1 | /*Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). 2 | 3 | The binary search tree is guaranteed to have unique values. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: root = [10,5,15,3,7,null,18], L = 7, R = 15 10 | Output: 32 11 | Example 2: 12 | 13 | Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10 14 | Output: 23 15 | 16 | 17 | Note: 18 | 19 | The number of nodes in the tree is at most 10000. 20 | The final answer is guaranteed to be less than 2^31. 21 | */ 22 | class Solution { 23 | int sum=0; 24 | public int rangeSumBST(TreeNode root, int L, int R) { 25 | if(root==null) 26 | return 0; 27 | 28 | range(root,L,R); 29 | return sum; 30 | 31 | } 32 | void range(TreeNode root,int L,int R){ 33 | if(root==null) 34 | return; 35 | if(root.val>=L&&root.val<=R) 36 | sum=sum+root.val; 37 | range(root.left,L,R); 38 | range(root.right,L,R); 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Binary_Tree/implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Trees{ 5 | public: 6 | int val; 7 | Trees* left; 8 | Trees* right; 9 | Trees(int val){ 10 | this->val = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | 16 | void insert(Trees* &treenode, int val){ 17 | 18 | if(treenode == NULL){ 19 | treenode = new Trees(val); 20 | } 21 | 22 | else if(treenode->val > val){ 23 | if(treenode->left!=NULL) 24 | insert(treenode->left, val); 25 | else 26 | treenode->left = new Trees(val); 27 | } 28 | else if(treenode->val < val){ 29 | if(treenode->right!=NULL) 30 | insert(treenode->right, val); 31 | else 32 | treenode->right = new Trees(val); 33 | } 34 | } 35 | 36 | void inorder(Trees* treenode){ 37 | if (treenode == NULL) 38 | return; 39 | 40 | inorder(treenode->left); 41 | cout << treenode->val << " "; 42 | inorder(treenode->right); 43 | } 44 | 45 | void preorder(Trees* treenode){ 46 | if (treenode == NULL) 47 | return; 48 | 49 | cout << treenode->val << " "; 50 | preorder(treenode->left); 51 | preorder(treenode->right); 52 | } 53 | 54 | void postorder(Trees* treenode){ 55 | if (treenode == NULL) 56 | return; 57 | 58 | postorder(treenode->left); 59 | postorder(treenode->right); 60 | cout << treenode->val << " "; 61 | } 62 | 63 | 64 | int sizeoftree(Trees* node){ 65 | if(node == NULL) return 0; 66 | return sizeoftree(node->left) + 1 + sizeoftree(node->right); 67 | } 68 | 69 | int issame(Trees* node1, Trees* node2){ 70 | if(node1 == NULL and node2 == NULL) return 1; 71 | if(node1 != NULL and node2 != NULL) 72 | return (issame(node1->left, node2->left) and 73 | node1->val == node2->val and 74 | issame(node1->right, node2->right)); 75 | 76 | return 0; 77 | } 78 | 79 | 80 | 81 | int main(){ 82 | Trees* t = NULL; 83 | insert(t, 3); 84 | insert(t, 1); 85 | insert(t, 4); 86 | insert(t, 2); 87 | insert(t, 5); 88 | insert(t, 8); 89 | 90 | inorder(t); 91 | cout << endl; 92 | preorder(t); 93 | cout << endl; 94 | postorder(t); 95 | cout << endl; 96 | cout << "Size of Tree: " << sizeoftree(t)<< endl; 97 | 98 | Trees* tt = NULL; 99 | 100 | insert(tt, 3); 101 | insert(tt, 1); 102 | insert(tt, 4); 103 | insert(tt, 2); 104 | insert(tt, 5); 105 | insert(tt, 8); 106 | 107 | inorder(tt); 108 | cout << endl; 109 | 110 | if(issame(t, tt)) cout << "Same Trees"<=i){ 36 | n=n-i; 37 | count++; 38 | } 39 | else 40 | break; 41 | } 42 | return count; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Competitive_Programming/Binary Tree Level Order Traversal II.java: -------------------------------------------------------------------------------- 1 | /*Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). 2 | 3 | For example: 4 | Given binary tree [3,9,20,null,null,15,7], 5 | 3 6 | / \ 7 | 9 20 8 | / \ 9 | 15 7 10 | return its bottom-up level order traversal as: 11 | [ 12 | [15,7], 13 | [9,20], 14 | [3] 15 | ]*/ 16 | 17 | /** 18 | * Definition for a binary tree node. 19 | * public class TreeNode { 20 | * int val; 21 | * TreeNode left; 22 | * TreeNode right; 23 | * TreeNode() {} 24 | * TreeNode(int val) { this.val = val; } 25 | * TreeNode(int val, TreeNode left, TreeNode right) { 26 | * this.val = val; 27 | * this.left = left; 28 | * this.right = right; 29 | * } 30 | * } 31 | */ 32 | class Solution { 33 | public List> levelOrderBottom(TreeNode root) { 34 | if(root==null) 35 | return new ArrayList<>(); 36 | List> res=new ArrayList<>(); 37 | Queuequ=new LinkedList<>(); 38 | qu.add(root); 39 | 40 | while(!qu.isEmpty()){ 41 | Listcur=new ArrayList<>(); 42 | 43 | int size=qu.size(); 44 | 45 | for(int i=0;i 34 | #include 35 | 36 | using namespace std; 37 | 38 | int SolveRec(vector> value,int maxWeight, int currentWeight=0,int index=0, int maxValue=0){ 39 | // Base Case 40 | if(index>=value.size()){ 41 | return maxValue; 42 | } 43 | 44 | int indexWeight=value[index].first; 45 | if(currentWeight+indexWeight>maxWeight){ 46 | return maxValue; 47 | } 48 | 49 | // Solve for taking current and not taking current separately 50 | int including=SolveRec(value,maxWeight,currentWeight+indexWeight,index+1,maxValue+value[index].second); 51 | int excluding=SolveRec(value,maxWeight,currentWeight,index+1,maxValue); 52 | 53 | // Return Max of including and excluding current item 54 | maxValue=max(including,excluding); 55 | return maxValue; 56 | } 57 | 58 | int SolveRecMem(vector> value,int maxWeight,vector> &dp, int currentWeight=0,int index=0){ 59 | // Base Case 60 | if(index>=value.size()){ 61 | return 0; 62 | } 63 | 64 | int indexWeight=value[index].first; 65 | 66 | // If solved for before, return saved value 67 | if(dp[index][currentWeight]!=-1){ 68 | return dp[index][currentWeight]; 69 | } 70 | 71 | // Solve for taking current and not taking current separately 72 | int including=0; 73 | if(currentWeight+indexWeight<=maxWeight){ 74 | including=value[index].second + SolveRecMem(value,maxWeight,dp,currentWeight+indexWeight,index+1); 75 | } 76 | int excluding=SolveRecMem(value,maxWeight,dp,currentWeight,index+1); 77 | 78 | // Save in DP array 79 | dp[index][currentWeight]=max(including,excluding); 80 | return dp[index][currentWeight]; 81 | } 82 | 83 | int SolveTab(vector> value,int maxWeight){ 84 | 85 | vector> dp(value.size(),vector(maxWeight+1,0)); 86 | 87 | //Base Case 88 | for(int j=1;j<=maxWeight;j++){ 89 | if(value[0].first>j){ 90 | dp[0][j]=0; 91 | } 92 | else{ 93 | dp[0][j]=value[0].second; 94 | } 95 | } 96 | 97 | // Loop through in reverse manner of recursion + memoization 98 | for(int i=1;i> value, int maxWeight){ 113 | // Note that at any point answer is only dependent on one array worth of values 114 | vector current(maxWeight+1,0); 115 | 116 | //Base Case 117 | for(int j=1;j<=maxWeight;j++){ 118 | if(value[0].first>j){ 119 | current[j]=0; 120 | } 121 | else{ 122 | current[j]=value[0].second; 123 | } 124 | } 125 | 126 | for(int i=1;i=1;j--){ 128 | 129 | int including=0; 130 | if(value[i].first<=j){ 131 | including= value[i].second + current[j-value[i].first]; 132 | } 133 | current[j]=max(including,current[j]); 134 | 135 | } 136 | } 137 | 138 | return current[maxWeight]; 139 | } 140 | 141 | int Question1(vector> value,int maxWeight){ 142 | // int answer1=SolveRec(value,maxWeight); Too Long 143 | 144 | // vector> dp(value.size(),vector(maxWeight+1,-1)); 145 | // int answer2 = SolveRecMem(value,maxWeight,dp); 146 | 147 | // int answer3=SolveTab(value,maxWeight); 148 | 149 | int answer4=SolveEfficient(value,maxWeight); 150 | return answer4; 151 | } 152 | 153 | int main(){ 154 | 155 | int n,maxWeight; 156 | cin>>n>>maxWeight; 157 | 158 | vector> value; 159 | for(int i=0;i>a>>b; 162 | 163 | value.push_back(make_pair(a,b)); 164 | } 165 | 166 | /* Test Example 167 | vector> value; 168 | for(int i=0;i<10;i++){ 169 | value.push_back(make_pair(1,5)); 170 | value.push_back(make_pair(2,4)); 171 | value.push_back(make_pair(4,13)); 172 | value.push_back(make_pair(5,6)); 173 | } 174 | int maxWeight=20; 175 | */ 176 | 177 | cout<<"Maximum Value he can steal: "< largestDivisibleSubset(int[] nums) { 18 | 19 | 20 | Listnu=new ArrayList<>(); 21 | if(nums.length==0) 22 | return nu; 23 | 24 | if(nums.length==1) 25 | { 26 | nu.add(nums[0]); 27 | return nu; 28 | } 29 | Arrays.sort(nums); 30 | int dp[]=new int[nums.length]; 31 | for(int i=0;i=0;j--){ 40 | if(nums[i]%nums[j]==0) 41 | { 42 | if(dp[i]min){ 55 | min=dp[i]; 56 | r=i; 57 | } 58 | } 59 | int pa=p[r]; 60 | nu.add(nums[r]); 61 | while(pa!=-1){ 62 | nu.add(nums[pa]); 63 | pa=p[pa]; 64 | } 65 | Collections.reverse(nu); 66 | 67 | return nu; 68 | 69 | 70 | 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Competitive_Programming/Least Number of Unique Integers after K Removals.java: -------------------------------------------------------------------------------- 1 | /*Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: arr = [5,5,4], k = 1 8 | Output: 1 9 | Explanation: Remove the single 4, only 5 is left. 10 | Example 2: 11 | Input: arr = [4,3,1,1,3,3,2], k = 3 12 | Output: 2 13 | Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left. 14 | 15 | 16 | Constraints: 17 | 18 | 1 <= arr.length <= 10^5 19 | 1 <= arr[i] <= 10^9 20 | 0 <= k <= arr.length 21 | */ 22 | 23 | 24 | class Solution { 25 | public int findLeastNumOfUniqueInts(int[] arr, int k) { 26 | if(arr.length==0) 27 | return -1; 28 | HashMaphm=new HashMap<>(); 29 | int size=0; 30 | for(int i=0;i>li=new LinkedList<>(hm.entrySet()); 40 | Collections.sort(li,(o1,o2)->o1.getValue()-o2.getValue()); 41 | for(Map.Entryi:li){ 42 | if(i.getValue()<=k){ 43 | size--; 44 | k-=i.getValue(); 45 | } 46 | else 47 | break; 48 | 49 | } 50 | return size; 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Competitive_Programming/Maximum Length of Repeated Subarray.java: -------------------------------------------------------------------------------- 1 | /*Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. 2 | 3 | Example 1: 4 | 5 | Input: 6 | A: [1,2,3,2,1] 7 | B: [3,2,1,4,7] 8 | Output: 3 9 | Explanation: 10 | The repeated subarray with maximum length is [3, 2, 1]. 11 | 12 | 13 | Note: 14 | 15 | 1 <= len(A), len(B) <= 1000 16 | 0 <= A[i], B[i] < 100 17 | */ 18 | class Solution { 19 | public int findLength(int[] A, int[] B) { 20 | int dp[][]=new int[A.length+1][B.length+1]; 21 | int max=-1; 22 | for(int i=1;imax) 29 | max=dp[i][j]; 30 | 31 | 32 | } 33 | } 34 | return max; 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Competitive_Programming/Maximum Points You Can Obtain from Cards.java: -------------------------------------------------------------------------------- 1 | /*There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. 2 | 3 | In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. 4 | 5 | Your score is the sum of the points of the cards you have taken. 6 | 7 | Given the integer array cardPoints and the integer k, return the maximum score you can obtain. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: cardPoints = [1,2,3,4,5,6,1], k = 3 14 | Output: 12 15 | Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. 16 | Example 2: 17 | 18 | Input: cardPoints = [2,2,2], k = 2 19 | Output: 4 20 | Explanation: Regardless of which two cards you take, your score will always be 4. 21 | Example 3: 22 | 23 | Input: cardPoints = [9,7,7,9,7,7,9], k = 7 24 | Output: 55 25 | Explanation: You have to take all the cards. Your score is the sum of points of all cards. 26 | Example 4: 27 | 28 | Input: cardPoints = [1,1000,1], k = 1 29 | Output: 1 30 | Explanation: You cannot take the card in the middle. Your best score is 1. 31 | Example 5: 32 | 33 | Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 34 | Output: 202 35 | 36 | 37 | Constraints: 38 | 39 | 1 <= cardPoints.length <= 10^5 40 | 1 <= cardPoints[i] <= 10^4 41 | 1 <= k <= cardPoints.length 42 | */ 43 | 44 | class Solution { 45 | public int maxScore(int[] cardPoints, int k) { 46 | int sum = 0 ; 47 | for( int i = 0 ; i< k ; i ++) 48 | { 49 | sum+=cardPoints[i]; //sum=6 50 | } 51 | int e =cardPoints.length-1; 52 | int s =k-1; //s=2 53 | int ans =0 ; 54 | ans =sum; 55 | while(s >=0) 56 | { 57 | sum-=cardPoints[s];//sum=3 58 | sum+=cardPoints[e]; //sum=4 59 | ans =Math.max(sum,ans); 60 | s--; 61 | e--; 62 | } 63 | return ans; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Competitive_Programming/Pokemon_1.py: -------------------------------------------------------------------------------- 1 | """ 2 | Obama has always wanted to be the best Pokemon trainer in this world. And he thinks he has achieved his goal, so he 3 | wants to quickly go and meet Professor Oak and verify this fact. But like all Pokemon trainers, he has a weird habit, too. 4 | He catches Pokemons which can go through evolution to become a better one. After roaming in the Pokeworld for days, he has 5 | finally managed to catch k such Pokemons. 6 | The way he can make a Pokemon go through evolution is NOT by making them fight battles, but by using an evolution stone. 7 | Since he has k Pokemons, he naturally needs k evolution stones for every one of them, as well. 8 | Now it takes Obama one complete day, to use the evolution stone on one Pokemon. And for each Pokemon, he knows how many days 9 | will they take to evolute after the evolution stone has been used on them. 10 | He will go to meet Professor Oak, the very next day, once all his Pokemons have gone through evolution. 11 | He can select the order of applying evolution stones as he likes, so he wants to do it in such a way that he gets to 12 | meet Professor Oak as soon as possible! 13 | 14 | Input Format 15 | The input has two lines. The first line will contain an integer k, which denotes the number of Pokemons. Then, a line with k integers follows, where the i-th integer denotes the number of days it takes for the i-th Pokemon to evolve. 16 | 17 | Constraints 18 | The days are numbered 1, 2, 3, 4, 5, 6... 19 | 1 ≤ k ≤ 10^5. 20 | 1 ≤ Number of days ≤ 10^6. 21 | 22 | Output Format 23 | You need to print the earliest day when little Arihant can go meet Professor Oak. 24 | 25 | """ 26 | 27 | a = lambda: map(int, input().strip().split()) 28 | def main(): 29 | k = int(input()) 30 | A = sorted(a(), reverse = True) 31 | print(max(i + A[i] + 2 for i in range(k))) 32 | 33 | if __name__ == "__main__": 34 | main() 35 | -------------------------------------------------------------------------------- /Competitive_Programming/SPOJ - MMASS.cpp: -------------------------------------------------------------------------------- 1 | // Problem : spoj MMASS 2 | // Link : https://www.spoj.com/problems/MMASS/ 3 | 4 | /* 5 | A molecule can be defined as a sequence of atoms and represented 6 | by a chemical formula consisting of letters denoting these atoms. 7 | E.g. letter H denotes atom of hydrogen, C denotes atom of carbon, 8 | O denotes atom of oxygen, formula COOH represents molecule consisting 9 | of one atom of carbon, two atoms of oxygen and one atom of hydrogen. 10 | 11 | To write some formulas efficiently, we use the following rules. 12 | Letters denoting some atoms can be grouped by enclosing in 13 | parentheses, e.g. formula CH(OH) contains group OH. 14 | Groups can be nested – a group can also contain other groups. 15 | To simplify a formula, consecutive occurrences of the same letter 16 | can be replaced with that letter followed by a number of these 17 | occurrences. E.g. formula COOHHH can be written as CO2H3 and it 18 | represents a molecule consisting of one atom of carbon, 19 | two atoms of oxygen and three atoms of hydrogen. Furthermore, 20 | consecutive occurrences of the same group can be replaced with that 21 | group followed by a number of these occurrences. E.g. formula 22 | CH (CO2H) (CO2H) (CO2H) can be written as CH(CO2H)3 and molecule 23 | represented by both those formulas consists of four atoms of carbon, 24 | four atoms of hydrogen and six atoms of oxygen. A number written 25 | after a letter or a group is always greater than or equal to 2 and 26 | less than or equal to 9. A mass of a molecule is a sum of masses of 27 | all its atoms. One atom of hydrogen has mass 1, one atom of carbon 28 | has mass 12 and one atom of oxygen has mass 16. 29 | 30 | Write a program that will calculate a mass of a molecule. 31 | 32 | Input 33 | The first and only line of input file contains a formula of a molecule 34 | whose mass needs to be determined. A formula of a molecule will consist 35 | of characters H, C, O, (, ) , 2, 3, ..., 9 only. Its length will be 36 | less or equal to 100 characters. 37 | 38 | Output 39 | The first and only line of output file should contain a mass of a 40 | molecule represented with a given formula. The result will always 41 | be less than or equal to 10,000. 42 | 43 | Sample Input 44 | COOH 45 | 46 | Sample Output 47 | 45 48 | 49 | Sample Input 50 | CH(CO2H)3 51 | 52 | Sample Output 53 | 148 54 | 55 | Sample Input 56 | ((CH)2(OH2H)(C(H))O)3 57 | 58 | Sample Output 59 | 222 60 | 61 | */ 62 | 63 | #include 64 | #include 65 | using namespace std; 66 | 67 | bool isCHO(char c) { 68 | if (c == 'C' or c == 'H' or c == 'O') 69 | return true; 70 | else 71 | return false; 72 | } 73 | 74 | bool isDigit(char c) { 75 | if (c >= '1' and c <= '9') 76 | return true; 77 | else 78 | return false; 79 | } 80 | 81 | int getWeight(char c) { 82 | if (c == 'C') 83 | return 12; 84 | else if (c == 'H') 85 | return 1; 86 | else if (c == 'O') 87 | return 16; 88 | else 89 | return 0; 90 | } 91 | 92 | int getDigit(char c) { 93 | return c - '0'; 94 | } 95 | 96 | int main() { 97 | 98 | // input expression 99 | string exp; 100 | cin >> exp; 101 | 102 | stack stk; 103 | 104 | // use -10101 to denote ( 105 | 106 | for (int i = 0 ; i < exp.length(); i++) { 107 | if (exp[i] == '(') 108 | stk.push(-10101); 109 | 110 | else if (exp[i] == ')') { 111 | int accum = 0; 112 | 113 | while (stk.top() != -10101) { 114 | accum += stk.top(); 115 | stk.pop(); 116 | } 117 | 118 | stk.pop(); 119 | stk.push(accum); 120 | } 121 | 122 | else if (isCHO(exp[i])) { 123 | stk.push(getWeight(exp[i])); 124 | } 125 | 126 | else if (isDigit(exp[i])) { 127 | int x = stk.top(); stk.pop(); 128 | x = x * getDigit(exp[i]); 129 | stk.push(x); 130 | } 131 | } 132 | 133 | int ans = 0; 134 | 135 | while (stk.empty() == false) { 136 | ans += stk.top(); 137 | stk.pop(); 138 | } 139 | 140 | cout << ans << "\n"; 141 | 142 | return 0; 143 | } 144 | -------------------------------------------------------------------------------- /Competitive_Programming/Sort Characters By Frequency.java: -------------------------------------------------------------------------------- 1 | 2 | /*Given a string, sort it in decreasing order based on the frequency of characters. 3 | 4 | Example 1: 5 | 6 | Input: 7 | "tree" 8 | 9 | Output: 10 | "eert" 11 | 12 | Explanation: 13 | 'e' appears twice while 'r' and 't' both appear once. 14 | So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. 15 | Example 2: 16 | 17 | Input: 18 | "cccaaa" 19 | 20 | Output: 21 | "cccaaa" 22 | 23 | Explanation: 24 | Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer. 25 | Note that "cacaca" is incorrect, as the same characters must be together. 26 | Example 3: 27 | 28 | Input: 29 | "Aabb" 30 | 31 | Output: 32 | "bbAa" 33 | 34 | Explanation: 35 | "bbaA" is also a valid answer, but "Aabb" is incorrect. 36 | Note that 'A' and 'a' are treated as two different characters. 37 | */ 38 | class Solution { 39 | public String frequencySort(String s) { 40 | if(s.isEmpty()) 41 | return ""; 42 | HashMaphm=new HashMap<>(); 43 | char str[]=s.toCharArray(); 44 | for(Character i:str){ 45 | if(!hm.containsKey(i)) 46 | hm.put(i,1); 47 | else 48 | hm.put(i,hm.get(i)+1); 49 | } 50 | PriorityQueuemaxheap=new PriorityQueue<>((o1,o2)->hm.get(o2)-hm.get(o1)); 51 | for(Character c:hm.keySet()){ 52 | 53 | maxheap.add(c); 54 | } 55 | StringBuilder res = new StringBuilder(); 56 | for(int i = maxheap.size()-1; i >= 0 ; i--){ 57 | char ch = maxheap.poll(); 58 | int c = hm.get(ch); 59 | while(c != 0){ 60 | res.append(ch); 61 | c--; 62 | } 63 | } 64 | return res.toString(); 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Competitive_Programming/Sort Colors.java: -------------------------------------------------------------------------------- 1 | /*Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. 2 | 3 | Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. 4 | 5 | Follow up: 6 | 7 | Could you solve this problem without using the library's sort function? 8 | Could you come up with a one-pass algorithm using only O(1) constant space? 9 | 10 | 11 | Example 1: 12 | 13 | Input: nums = [2,0,2,1,1,0] 14 | Output: [0,0,1,1,2,2] 15 | Example 2: 16 | 17 | Input: nums = [2,0,1] 18 | Output: [0,1,2] 19 | Example 3: 20 | 21 | Input: nums = [0] 22 | Output: [0] 23 | Example 4: 24 | 25 | Input: nums = [1] 26 | Output: [1] 27 | 28 | 29 | Constraints: 30 | 31 | n == nums.length 32 | 1 <= n <= 300 33 | nums[i] is 0, 1, or 2. 34 | */ 35 | 36 | 37 | class Solution { 38 | public void sortColors(int[] nums) { 39 | int l=0; 40 | int medium=0; 41 | int high=nums.length-1; 42 | while(medium<=high){ 43 | if(nums[medium]==0){ 44 | int temp=nums[medium]; 45 | nums[medium]=nums[l]; 46 | nums[l]=temp; 47 | l++; 48 | medium++; 49 | 50 | } 51 | else if(nums[medium]==1){ 52 | medium++; 53 | } 54 | else if(nums[medium]==2){ 55 | int temp=nums[medium]; 56 | nums[medium]=nums[high]; 57 | nums[high]=temp; 58 | high--; 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Competitive_Programming/Stepping_stones_3.py: -------------------------------------------------------------------------------- 1 | """ 2 | Vasu is running up a stone staircase with N stones, and can hop(jump) either 1 step, 2 steps or 3 steps at a time. 3 | You have to count, how many possible ways Vasu can run up to the stone stairs. 4 | 5 | Input Format: 6 | Input contains integer N that is number of steps 7 | 8 | Constraints: 9 | 1<= N <=30 10 | 11 | Output Format: 12 | Output for each integer N the no of possible ways w. 13 | 14 | """ 15 | 16 | def hop(N) : 17 | if (N == 1 or N == 0) : 18 | return 1 19 | elif (N == 2) : 20 | return 2 21 | 22 | else : 23 | return hop(N - 3) + hop(N - 2) + hop(N - 1) 24 | 25 | 26 | N = int(input()) 27 | print(hop(N)) 28 | 29 | -------------------------------------------------------------------------------- /Competitive_Programming/cadet_vs_Lecturer.py: -------------------------------------------------------------------------------- 1 | """ 2 | One day a Lecturer of CSE got angry with a ProjectX member for coming late in project report submission. 3 | He gave a tough task to the member only after completion of which he will get his report signed. 4 | The member solved the problem in less than 10 minutes abd walked out with swag ;). 5 | Can you solve this and prove your worth to ProjectX? 6 | 7 | "Given a string S consisting of letters 'A' and 'B'. Calculate the minimum number of changes required such that all A's 8 | are adjacent to each other and all B's are adjacent to each other." 9 | 10 | One change is equivalent to swapping two neighbouring letters. 11 | 12 | Input Format 13 | The first line contains t - the number of test cases . 14 | Each test case consists of a string S consisting of characters 'A' and 'B' only. 15 | 16 | Constraints 17 | 1<=t<=10 18 | 1<=|S|<= 10^6 19 | 20 | Output Format 21 | Desired O/p 22 | 23 | """ 24 | 25 | a1 = 0 26 | a2 = 0 27 | for t in range(int(input())): 28 | ar = list(input()) 29 | 30 | a1 = 0 31 | p1 = -1 32 | p2 = -1 33 | arr = ar.copy() 34 | for ch in range(len(arr)): 35 | if arr[ch]=='A' and p1==p2 : 36 | p1 = p1 + 1 37 | p2 = p2 + 1 38 | if arr[ch] =='B' and p1>=p2 : 39 | p1 = p1 + 1 40 | if arr[ch] =='A' and p1!=p2 : 41 | p1 = p1 + 1 42 | p2 = p2 + 1 43 | a1 = a1 + p1-p2 44 | temp = arr[p1] 45 | arr[p1] = arr[p2] 46 | arr[p2] = temp 47 | 48 | a2 = 0 49 | p1 = -1 50 | p2 = -1 51 | arr2 = ar.copy() 52 | for ch in range(len(arr2)): 53 | if arr2[ch]=='B' and p1==p2 : 54 | p1 = p1 + 1 55 | p2 = p2 + 1 56 | if arr2[ch] =='A' and p1>=p2 : 57 | p1 = p1 + 1 58 | if arr2[ch] =='B' and p1!=p2 : 59 | p1 = p1 + 1 60 | p2 = p2 + 1 61 | a2 = a2 + p1-p2 62 | temp = arr2[p1] 63 | arr2[p1] = arr2[p2] 64 | arr2[p2] = temp 65 | 66 | print(min(a1,a2)) 67 | -------------------------------------------------------------------------------- /Competitive_Programming/spoj - AGGRCOW.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Problem : AGGRCOW - Aggressive cows 3 | // Link : https://www.spoj.com/problems/AGGRCOW/ 4 | 5 | /* 6 | Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. 7 | The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). 8 | 9 | His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. 10 | To prevent the cows from hurting each other, FJ wants to assign the cows to the stalls, such that the minimum distance 11 | between any two of them is as large as possible. What is the largest minimum distance? 12 | 13 | Input 14 | t – the number of test cases, then t test cases follows. 15 | Line 1: Two space-separated integers: N and C 16 | Lines 2..N+1: Line i+1 contains an integer stall location, xi 17 | 18 | Output 19 | For each test case output one integer: the largest minimum distance. 20 | 21 | Sample Input: 22 | 23 | 1 24 | 5 3 25 | 1 26 | 2 27 | 8 28 | 4 29 | 9 30 | 31 | Sample Output: 32 | 33 | 3 34 | 35 | */ 36 | 37 | 38 | // Algo used - binary search 39 | 40 | #include 41 | #include 42 | #include 43 | #include 44 | using namespace std; 45 | 46 | #define all(x) x.begin(), x.end() 47 | #define ll long long int 48 | 49 | bool canCCowsBePlaced (vector& arr, ll c, ll dist) { 50 | ll size = arr.size(); 51 | 52 | ll i = 0; 53 | ll j = 1; 54 | 55 | c--; 56 | 57 | while (i < j and j < size) { 58 | ll sep = arr[j] - arr[i]; 59 | 60 | if (sep >= dist) { 61 | i = j; 62 | j++; 63 | c--; 64 | 65 | if (c == 0) 66 | return true; 67 | } 68 | else 69 | j++; 70 | } 71 | 72 | return false; 73 | } 74 | 75 | int main() { 76 | 77 | ll t; 78 | cin >> t; 79 | 80 | while (t--) { 81 | ll n, c; 82 | cin >> n >> c; 83 | 84 | vector arr(n); 85 | 86 | for (int i = 0; i < n; ++i) 87 | cin >> arr[i]; 88 | 89 | 90 | sort(all(arr)); 91 | 92 | ll start = 0; 93 | ll end = arr[n - 1] - arr[0]; 94 | ll ans = 0; 95 | 96 | while (start <= end) { 97 | ll mid = (start + end) / 2; 98 | 99 | bool valid = canCCowsBePlaced(arr, c, mid); 100 | 101 | if (valid) { 102 | ans = mid; 103 | start = mid + 1; 104 | } 105 | else { 106 | end = mid - 1; 107 | } 108 | } 109 | 110 | cout << ans << "\n"; 111 | } 112 | 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /Graphs/AdjLinked_List_Implementation.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | struct AdjListNode 6 | { 7 | int address; 8 | struct AdjListNode *next; 9 | }; 10 | typedef struct AdjListNode NODE; 11 | 12 | struct AdjList 13 | { 14 | NODE *head; 15 | }; 16 | typedef struct AdjList LIST; 17 | 18 | struct Graph 19 | { 20 | int V; // Number of Vertex in graph 21 | LIST *array; 22 | }; 23 | typedef struct Graph GRAPH; 24 | 25 | NODE *newAdjListNode(int address) 26 | { 27 | NODE *newNode =(NODE*) malloc(sizeof(NODE)); 28 | newNode->address = address; 29 | newNode->next = NULL; 30 | return newNode; 31 | } 32 | 33 | 34 | GRAPH* createGraph(int V) 35 | { 36 | GRAPH* graph = (GRAPH*) malloc(sizeof(GRAPH)); 37 | graph->V = V; 38 | graph->array = (LIST*) malloc(V * sizeof(LIST)); 39 | int i; 40 | for (i = 0; i < V; ++i) 41 | graph->array[i].head = NULL; 42 | return graph; 43 | } 44 | 45 | 46 | void addEdge(GRAPH* graph, int src, int address) 47 | { 48 | NODE* newNode = newAdjListNode(address); 49 | newNode->next = graph->array[src].head; 50 | graph->array[src].head = newNode; 51 | 52 | newNode = newAdjListNode(src); 53 | newNode->next = graph->array[address].head; 54 | graph->array[address].head = newNode; 55 | } 56 | 57 | void printGraph(GRAPH* graph) 58 | { 59 | int v; 60 | for (v = 0; v < graph->V; v++) 61 | { 62 | NODE* temp = graph->array[v].head; 63 | printf("\n\t Adjacency list of vertex %d\n\t head ", v); 64 | while (temp) 65 | { 66 | printf("-> %d", temp->address); 67 | temp = temp->next; 68 | } 69 | printf("\n"); 70 | } 71 | } 72 | 73 | void main() 74 | { 75 | 76 | int V = 5; 77 | GRAPH* graph = createGraph(V); 78 | int a; 79 | while(a!=0) { 80 | printf("\n\n"); 81 | printf("Enter 1 -> Add Edge\n"); 82 | printf("Enter 2 -> Print Graph\n"); 83 | printf("Enter 0 -> Exit\n"); 84 | scanf("%d", &a); 85 | if(a==0) { 86 | printf("-------------------------Thank You----------------------------\n"); 87 | return; 88 | } 89 | switch(a) { 90 | case 1 : 91 | { 92 | int u, v; 93 | printf("Enter u, v\n"); 94 | scanf("%d %d", &u, &v); 95 | addEdge(graph, u, v); 96 | break; 97 | } 98 | case 2 : 99 | { 100 | printGraph(graph); 101 | break; 102 | } 103 | default : 104 | { 105 | printf("Wrong Value Detacted\n"); 106 | break; 107 | } 108 | return; 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /Graphs/Bellman ford algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define inf 1e9 3 | using namespace std; 4 | 5 | class Edge 6 | { 7 | public: 8 | int src, dest, weight; 9 | }; 10 | 11 | class Graph 12 | { 13 | public: 14 | int v, e; 15 | Edge *edge; 16 | 17 | Graph(int v, int e) 18 | { 19 | this->v = v; 20 | this->e = e; 21 | edge = new Edge[e]; 22 | } 23 | 24 | void addEgde(int u, int v, int w, int count) 25 | { 26 | edge[count].src = u; 27 | edge[count].dest = v; 28 | edge[count].weight = w; 29 | } 30 | 31 | void BellmanFord(int src) 32 | { 33 | int distance[v]; 34 | for (int i = 0; i < v; i++) 35 | { 36 | if (i == src) 37 | { 38 | distance[i] = 0; 39 | } 40 | else 41 | { 42 | distance[i] = inf; 43 | } 44 | } 45 | 46 | for (int i = 0; i < v - 1; i++) 47 | { 48 | for (int i = 0; i < e; i++) 49 | { 50 | int src = edge[i].src; 51 | int dest = edge[i].dest; 52 | int weight = edge[i].weight; 53 | 54 | if (distance[src] != inf and distance[src] + weight < distance[dest]) 55 | { 56 | distance[dest] = distance[src] + weight; 57 | } 58 | } 59 | } 60 | 61 | for (int i = 0; i < e; i++) 62 | { 63 | int src = edge[i].src; 64 | int dest = edge[i].dest; 65 | int weight = edge[i].weight; 66 | 67 | if (distance[src] != inf and distance[src] + weight < distance[dest]) 68 | { 69 | cout << "negetive edge cycle present"; 70 | return; 71 | } 72 | } 73 | 74 | for (int i = 0; i < v; i++) 75 | { 76 | cout << i << " - " << distance[i] << endl; 77 | } 78 | } 79 | }; 80 | 81 | int main() 82 | { 83 | int v, e; 84 | cin >> v >> e; 85 | Graph g(v, e); 86 | for (int i = 0; i < e; i++) 87 | { 88 | int u, v, wt; 89 | cin >> u >> v >> wt; 90 | g.addEgde(u, v, wt, i); 91 | } 92 | 93 | g.BellmanFord(0); 94 | } -------------------------------------------------------------------------------- /Graphs/DetectCycleInADirectedGraph.cpp: -------------------------------------------------------------------------------- 1 | //Given a Directed Graph. Check whether it contains any cycle or not. 2 | 3 | //Input: The first line of the input contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of two lines. Description of testcases is as follows: The First line of each test case contains two integers 'N' and 'M' which denotes the no of vertices and no of edges respectively. The Second line of each test case contains 'M' space separated pairs u and v denoting that there is an uni-directed edge from u to v . 4 | 5 | //Output: 6 | //The method should return 1 if there is a cycle else it should return 0. 7 | 8 | //User task: 9 | //You don't need to read input or print anything. Your task is to complete the function isCyclic which takes the Graph and the number of vertices and inputs and returns true if the given directed graph contains a cycle. Else, it returns false. 10 | 11 | //Expected Time Complexity: O(V + E). 12 | //Expected Auxiliary Space: O(V). 13 | 14 | //Constraints: 15 | //1 <= T <= 1000 16 | //1<= N,M <= 1000 17 | //0 <= u,v <= N-1 18 | 19 | //Example: 20 | //Input: 21 | //3 22 | //2 2 23 | //0 1 0 0 24 | //4 3 25 | //0 1 1 2 2 3 26 | //4 3 27 | //0 1 2 3 3 2 28 | //Output: 29 | //1 30 | //0 31 | //1 32 | 33 | #include 34 | using namespace std; 35 | bool cyclic(int i, int V, vector adj[],bool visited[], bool inTheCall[]) 36 | { 37 | visited[i]=true; 38 | inTheCall[i]=true; 39 | for(int j =0;j adj[]) 54 | { 55 | bool visited[V]; 56 | bool inTheCall[V]; 57 | for(int i=0;i> t; 75 | while(t--){ 76 | int v, e; 77 | cin >> v >> e; 78 | vector adj[v]; 79 | for(int i =0;i> u >> v; 82 | adj[u].push_back(v); 83 | } 84 | cout << isCyclic(v, adj) << endl; 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /Graphs/graph_topological_dfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | template 8 | class Graph{ 9 | 10 | map > adjList; 11 | 12 | public: 13 | Graph(){ 14 | 15 | } 16 | void addEdge(T u, T v,bool bidir=true){ 17 | 18 | adjList[u].push_back(v); 19 | if(bidir){ 20 | adjList[v].push_back(u); 21 | } 22 | } 23 | 24 | void print(){ 25 | 26 | //Iterate over the map 27 | for(auto i:adjList){ 28 | cout<"; 29 | 30 | //i.second is LL 31 | for(T entry:i.second){ 32 | cout< &visited,list &ordering){ 38 | 39 | visited[node] = true; 40 | 41 | //Will call dfs on the unvisted neighbours of the current node 42 | for(T neighbour:adjList[node]){ 43 | if(!visited[neighbour]){ 44 | dfsHelper(neighbour, visited,ordering); 45 | } 46 | } 47 | //Add 1 line for TS 48 | // At this point, all the children of current node have been visited 49 | // so we can add current node to the list 50 | ordering.push_front(node); 51 | } 52 | 53 | 54 | void dfsToplogicalSort(){ 55 | map visited; 56 | list ordering; 57 | 58 | for(auto i: adjList){ 59 | //i is pair (node,list of nodes) 60 | T node = i.first; 61 | if(!visited[node]){ 62 | dfsHelper(node,visited,ordering); 63 | } 64 | } 65 | //Print all the elements in ordering 66 | for(T element:ordering){ 67 | cout<"; 68 | } 69 | 70 | } 71 | }; 72 | 73 | int main(){ 74 | 75 | Graph g; 76 | g.addEdge("English","Programming Logic",false); 77 | g.addEdge("Maths","Programming Logic",false); 78 | g.addEdge("Programming Logic","HTML",false); 79 | g.addEdge("Programming Logic","Python",false); 80 | g.addEdge("Programming Logic","Java",false); 81 | g.addEdge("Programming Logic","JS",false); 82 | g.addEdge("Python","Web Dev",false); 83 | g.addEdge("HTML","CSS",false); 84 | g.addEdge("CSS","JS",false); 85 | g.addEdge("JS","Web Dev",false); 86 | g.addEdge("Java","Web Dev",false); 87 | g.addEdge("Python","Web Dev",false); 88 | 89 | g.dfsToplogicalSort(); 90 | 91 | 92 | return 0; 93 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Archit 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Linked_List/11_Reverse.c: -------------------------------------------------------------------------------- 1 | // Write a function to reverse a given linked list. You should not use the separate list and you should 2 | // work on the original list and reverse the same. 3 | 4 | #include 5 | #include 6 | #include "Linked_List.c" 7 | 8 | void Reverse() { 9 | if(head == NULL) { 10 | return; 11 | } 12 | 13 | NODE *curr = head; // Point to Current node of the List 14 | NODE *next; // Point to Next of Current node of the list 15 | NODE *temp = NULL; 16 | while(curr != NULL){ 17 | next = curr->next; 18 | curr->next = temp; // Curr will Detached from next and point to temp 19 | temp = curr; // Changing Temp to curr therefore next Element Detached will point to Current 20 | curr = next; // Hence list will reverse after execuation of while loop. 21 | } 22 | head = temp; 23 | } 24 | 25 | void main() { 26 | int a; 27 | while(a!=0) { 28 | printf("\n\n"); 29 | printf("Enter 1 -> Insert_front\n"); 30 | printf("Enter 2 -> Delete_front\n"); 31 | printf("Enter 3 -> Insert_end\n"); 32 | printf("Enter 4 -> Delete_end\n"); 33 | printf("Enter 5 -> Display_LL\n"); 34 | printf("Enter 6 -> Reverse()\n"); 35 | printf("Enter 0 -> Exit\n"); 36 | scanf("%d", &a); 37 | if(a==0) { 38 | printf("-------------------------Thank You----------------------------\n"); 39 | return; 40 | } 41 | switch(a) { 42 | case 1 : 43 | { 44 | Insert_front(); 45 | Display_LL(); 46 | break; 47 | } 48 | case 2 : 49 | { 50 | Delete_front(); 51 | Display_LL(); 52 | break; 53 | } 54 | case 3 : 55 | { 56 | Insert_end(); 57 | Display_LL(); 58 | break; 59 | } 60 | case 4 : 61 | { 62 | Delete_end(); 63 | Display_LL(); 64 | break; 65 | } 66 | case 5 : 67 | { 68 | Display_LL(); 69 | break; 70 | } 71 | case 6 : 72 | { 73 | Reverse(); 74 | printf("\nAfter reversing:\n"); 75 | Display_LL(); 76 | break; 77 | } 78 | default : 79 | { 80 | printf("Wrong Value Detacted\n"); 81 | break; 82 | } 83 | return; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Linked_List/11_Reverse_Recursion.c: -------------------------------------------------------------------------------- 1 | // Write a function to reverse a given linked list Recusrively. 2 | 3 | #include 4 | #include 5 | #include "Linked_List.c" 6 | 7 | // Reverse_Recursion Implementation 8 | void Reverse_Recursion(NODE *p) { 9 | if(p->next == NULL) { 10 | head = p; 11 | return; 12 | } 13 | Reverse_Recursion(p->next); 14 | NODE *temp = p->next; 15 | temp->next = p; 16 | p->next = NULL; 17 | } 18 | 19 | void main() { 20 | int a; 21 | while(a!=0) { 22 | printf("\n\n"); 23 | printf("Enter 1 -> Insert_front\n"); 24 | printf("Enter 2 -> Delete_front\n"); 25 | printf("Enter 3 -> Insert_end\n"); 26 | printf("Enter 4 -> Delete_end\n"); 27 | printf("Enter 5 -> Display_LL\n"); 28 | printf("Enter 6 -> Reverse()\n"); 29 | printf("Enter 0 -> Exit\n"); 30 | scanf("%d", &a); 31 | if(a==0) { 32 | printf("-------------------------Thank You----------------------------\n"); 33 | return; 34 | } 35 | switch(a) { 36 | case 1 : 37 | { 38 | Insert_front(); 39 | Display_LL(); 40 | break; 41 | } 42 | case 2 : 43 | { 44 | Delete_front(); 45 | Display_LL(); 46 | break; 47 | } 48 | case 3 : 49 | { 50 | Insert_end(); 51 | Display_LL(); 52 | break; 53 | } 54 | case 4 : 55 | { 56 | Delete_end(); 57 | Display_LL(); 58 | break; 59 | } 60 | case 5 : 61 | { 62 | Display_LL(); 63 | break; 64 | } 65 | case 6 : 66 | { 67 | Reverse_Recursion(head); 68 | printf("\nAfter Reversing Recusrively:\n"); 69 | Display_LL(); 70 | break; 71 | } 72 | default : 73 | { 74 | printf("Wrong Value Detacted\n"); 75 | break; 76 | } 77 | return; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Linked_List/12_Stack_n_Queue.c: -------------------------------------------------------------------------------- 1 | //Simulate the Stack and Queue using linked lists. 2 | #include 3 | #include 4 | struct node { 5 | int data; 6 | struct node *next; 7 | }; 8 | typedef struct node NODE; 9 | 10 | // To create the NODE 11 | 12 | NODE *getnode() { 13 | NODE *newnode; 14 | newnode = (NODE*)malloc(sizeof(NODE)); 15 | int ele; 16 | printf("Enter the Element\n"); 17 | scanf("%d", &ele); 18 | newnode->data = ele; 19 | newnode->next = NULL; 20 | return newnode; 21 | } 22 | 23 | // Intiallize top as NULL for Stack 24 | NODE *top = NULL; 25 | 26 | // Intiallize front and rare as NULL for Queue 27 | NODE *front = NULL; 28 | NODE *rare = NULL; 29 | 30 | // Stack Function to push element in stack work like insert_front in linked list 31 | 32 | void push() { 33 | NODE *newnode = getnode(); 34 | if(top == NULL) { 35 | newnode->next = NULL; // if stack is empty then newnode will join to NULL 36 | } 37 | else { 38 | newnode->next = top; // pushing newnode in stack 39 | } 40 | top = newnode; 41 | } 42 | 43 | // Stack Function to pop a element from stack work like delete_front in linked list 44 | 45 | void pop() { 46 | if(top == NULL) { 47 | printf("Stack is Empty\n"); 48 | return; 49 | } 50 | NODE *temp = top; 51 | top = top->next; 52 | free(temp); 53 | } 54 | 55 | // Queue Function to Enqueue the element in queue 56 | 57 | void Enqueue() { 58 | 59 | NODE *newnode; 60 | newnode = getnode(); 61 | if(rare == NULL) { // If rare is null then front and rare both linked with newnode 62 | front = rare = newnode; 63 | return; 64 | } 65 | rare->next = newnode; // otherwise rare will shift to newnode 66 | rare = newnode; 67 | return; 68 | } 69 | 70 | // Queue Function to Dequeue the element from Queue 71 | 72 | void Dequeue() { 73 | if(front == NULL) { // if front is null list is empty 74 | printf("Queue is Empty\n"); 75 | return; 76 | } 77 | NODE *temp = front; // Work like delete_front in linked list 78 | front = front->next; 79 | free(temp); 80 | if(front == NULL) { // if new position of front is null then link rare with null as list is empty in this condition 81 | rare = NULL; 82 | return; 83 | } 84 | } 85 | 86 | // To Display Stack 87 | 88 | void Display_stack() { 89 | if(top == NULL) { 90 | printf("\nStack is Empty\n"); 91 | } 92 | NODE *temp = top; 93 | while(temp != NULL) { 94 | printf("%d --> ", temp->data); 95 | temp = temp->next; 96 | } 97 | } 98 | 99 | //To Display Queue 100 | 101 | void Display_queue() { 102 | if(rare == NULL) { 103 | printf("\nQueue is Empty\n"); 104 | } 105 | NODE *temp = front; 106 | while(temp != NULL) { 107 | printf("%d --> ", temp->data); 108 | temp = temp->next; 109 | } 110 | } 111 | 112 | void main() { 113 | int a; 114 | int b; 115 | printf("Enter 1 : Simulate the Stack\n"); 116 | printf("Enter 2 : Simulate the Queue\n"); 117 | scanf("%d", &b); 118 | if(b==1) { 119 | while(a!=0) { 120 | printf("\n\n"); 121 | printf("Enter 1 -> PUSH\n"); 122 | printf("Enter 2 -> POP\n"); 123 | printf("Enter 3 -> Display\n"); 124 | printf("Enter 0 -> Exit\n"); 125 | scanf("%d", &a); 126 | if(a==0) { 127 | printf("-------------------------Thank You----------------------------\n"); 128 | return; 129 | } 130 | switch(a) { 131 | case 1 : 132 | { 133 | push(); 134 | Display_stack(); 135 | break; 136 | } 137 | case 2 : 138 | { 139 | pop(); 140 | Display_stack(); 141 | break; 142 | } 143 | case 3 : 144 | { 145 | Display_stack(); 146 | break; 147 | } 148 | 149 | default : 150 | { 151 | printf("Wrong Value Detacted\n"); 152 | break; 153 | } 154 | return; 155 | } 156 | } 157 | } 158 | else if(b==2) { 159 | while(a!=0) { 160 | printf("\n\n"); 161 | printf("Enter 1 -> Enqueue\n"); 162 | printf("Enter 2 -> Dequeue\n"); 163 | printf("Enter 3 -> Display_LL\n"); 164 | printf("Enter 0 -> Exit\n"); 165 | scanf("%d", &a); 166 | if(a==0) { 167 | printf("-------------------------Thank You----------------------------\n"); 168 | return; 169 | } 170 | switch(a) { 171 | case 1 : 172 | { 173 | Enqueue(); 174 | Display_queue(); 175 | break; 176 | } 177 | case 2 : 178 | { 179 | Dequeue(); 180 | Display_queue(); 181 | break; 182 | } 183 | case 3 : 184 | { 185 | Display_queue(); 186 | break; 187 | } 188 | 189 | default : 190 | { 191 | printf("Wrong Value Detacted\n"); 192 | break; 193 | } 194 | return; 195 | } 196 | } 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /Linked_List/14_LENGHT.c: -------------------------------------------------------------------------------- 1 | // Write a function LENGTH(X) to count the number of nodes in a singly linked list P, where P 2 | // points to the first node in the list. The last node has link field NULL. 3 | 4 | 5 | #include 6 | #include 7 | #include "Linked_List.c" 8 | 9 | int LENGHT() { 10 | NODE *curr = head; // Will point to Current Element in Linked_List 11 | 12 | // To Find the Number of Nodes 13 | int num_nodes = 0; 14 | while(curr != NULL) { 15 | num_nodes++; // Finding the Number of Nodes in the Linked_List 16 | curr = curr->next; 17 | } 18 | return num_nodes; 19 | } 20 | 21 | 22 | 23 | int main() { 24 | int a; 25 | while(a!=0) { 26 | printf("\n\n"); 27 | printf("Enter 1 -> Insert_front\n"); 28 | printf("Enter 2 -> Delete_front\n"); 29 | printf("Enter 3 -> Insert_end\n"); 30 | printf("Enter 4 -> Delete_end\n"); 31 | printf("Enter 5 -> Display_LL\n"); 32 | printf("Enter 6 -> LENGHT()\n"); 33 | printf("Enter 0 -> Exit\n"); 34 | scanf("%d", &a); 35 | if(a==0) { 36 | printf("-------------------------Thank You----------------------------\n"); 37 | return 0; 38 | } 39 | switch(a) { 40 | case 1 : 41 | { 42 | Insert_front(); 43 | Display_LL(); 44 | break; 45 | } 46 | case 2 : 47 | { 48 | Delete_front(); 49 | Display_LL(); 50 | break; 51 | } 52 | case 3 : 53 | { 54 | Insert_end(); 55 | Display_LL(); 56 | break; 57 | } 58 | case 4 : 59 | { 60 | Delete_end(); 61 | Display_LL(); 62 | break; 63 | } 64 | case 5 : 65 | { 66 | Display_LL(); 67 | break; 68 | } 69 | case 6 : 70 | { 71 | int len = LENGHT(); 72 | printf("\nNumber of Nodes in this List: %d\n", len); 73 | return 0; 74 | } 75 | default : 76 | { 77 | printf("Wrong Value Detacted\n"); 78 | break; 79 | } 80 | return 0; 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Linked_List/1_Count.c: -------------------------------------------------------------------------------- 1 | //Write a Count() function that counts the number of times a given int occurs in a linked list. 2 | 3 | #include 4 | #include 5 | #include "Linked_List.c" 6 | 7 | void Count() { 8 | if(head == NULL) { 9 | printf("\nError: Linked_List is Empty\n"); 10 | return; 11 | } 12 | int count = 0; // To Count the Number of times given key aka given Integer occurs 13 | NODE *temp = head; // dereference head to get the head on temp NODE 14 | int key; // Given Integer 15 | printf("Enter the Number\n"); 16 | scanf("%d", &key); 17 | while(temp != NULL) { // Traverse the Linked List till last NODE 18 | if(key == temp->data) { // If key = data at that node execute count++ to count it 19 | count++; 20 | } 21 | temp = temp->next; 22 | } 23 | printf("\nThe Number of times %d occurs: %d\n",key, count); 24 | } 25 | 26 | void main() { 27 | int a; 28 | while(a!=0) { 29 | printf("\n\n"); 30 | printf("Enter 1 -> Insert_front\n"); 31 | printf("Enter 2 -> Delete_front\n"); 32 | printf("Enter 3 -> Insert_end\n"); 33 | printf("Enter 4 -> Delete_end\n"); 34 | printf("Enter 5 -> Display_LL\n"); 35 | printf("Enter 6 -> Count()\n"); 36 | printf("Enter 0 -> Exit\n"); 37 | scanf("%d", &a); 38 | if(a==0) { 39 | printf("-------------------------Thank You----------------------------\n"); 40 | return; 41 | } 42 | switch(a) { 43 | case 1 : 44 | { 45 | Insert_front(); 46 | Display_LL(); 47 | break; 48 | } 49 | case 2 : 50 | { 51 | Delete_front(); 52 | Display_LL(); 53 | break; 54 | } 55 | case 3 : 56 | { 57 | Insert_end(); 58 | Display_LL(); 59 | break; 60 | } 61 | case 4 : 62 | { 63 | Delete_end(); 64 | Display_LL(); 65 | break; 66 | } 67 | case 5 : 68 | { 69 | Display_LL(); 70 | break; 71 | } 72 | case 6 : 73 | { 74 | Count(); 75 | break; 76 | } 77 | default : 78 | { 79 | printf("Wrong Value Detacted\n"); 80 | break; 81 | } 82 | return; 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Linked_List/2_GetNth.c: -------------------------------------------------------------------------------- 1 | //Write a GetNth() function that takes a linked list and an integer index and returns the data value 2 | //stored in the node at that index position. 3 | 4 | #include 5 | #include 6 | #include "Linked_List.c" 7 | 8 | void GetNth() { 9 | if(head == NULL) { 10 | printf("\nError: Linked_List is Empty\n"); 11 | return; 12 | } 13 | int count = 0; // It will simply increase till the index value and help in returning the value at that Index. 14 | NODE *temp = head; 15 | int index; // Given Index 16 | int num_nodes = 0; // To Find the Number of Nodes 17 | printf("Enter the Index to Find the data value at that NODE\n"); 18 | scanf("%d", &index); 19 | while(temp != NULL) { 20 | num_nodes++; // Finding the Number of Nodes in the Linked_List 21 | temp = temp->next; 22 | } 23 | temp = head; // Reintialize temp to head as it is at last postion Now 24 | if(index > num_nodes || index <= 0) { // Boundary Condition if Entered index is greater then Total number of nodes in Linked_List 25 | printf("\nError: Node at this Index doesn't Exits\n"); // or if 0 or less it will print Error Message 26 | return; 27 | } 28 | while(temp != NULL) { 29 | count++; 30 | if(index == count) { // If count = index then it will print the data value at this index 31 | printf("\nThe Data Value at Index %d is |_%d_|\n", index, temp->data); 32 | return; 33 | } 34 | temp = temp->next; 35 | } 36 | } 37 | 38 | void main() { 39 | int a; 40 | while(a!=0) { 41 | printf("\n\n"); 42 | printf("Enter 1 -> Insert_front\n"); 43 | printf("Enter 2 -> Delete_front\n"); 44 | printf("Enter 3 -> Insert_end\n"); 45 | printf("Enter 4 -> Delete_end\n"); 46 | printf("Enter 5 -> Display_LL\n"); 47 | printf("Enter 6 -> To find the value at the given Index\n"); 48 | printf("Enter 0 -> Exit\n"); 49 | scanf("%d", &a); 50 | if(a==0) { 51 | printf("-------------------------Thank You----------------------------\n"); 52 | return; 53 | } 54 | switch(a) { 55 | case 1 : 56 | { 57 | Insert_front(); 58 | Display_LL(); 59 | break; 60 | } 61 | case 2 : 62 | { 63 | Delete_front(); 64 | Display_LL(); 65 | break; 66 | } 67 | case 3 : 68 | { 69 | Insert_end(); 70 | Display_LL(); 71 | break; 72 | } 73 | case 4 : 74 | { 75 | Delete_end(); 76 | Display_LL(); 77 | break; 78 | } 79 | case 5 : 80 | { 81 | Display_LL(); 82 | break; 83 | } 84 | case 6 : 85 | { 86 | GetNth(); 87 | break; 88 | } 89 | default : 90 | { 91 | printf("Wrong Value Detacted\n"); 92 | break; 93 | } 94 | return; 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /Linked_List/3.1_Delete_list.c: -------------------------------------------------------------------------------- 1 | // Write a function DeleteList() that takes a list, deallocates all of its memory and sets its head pointer 2 | // to NULL (the empty list). 3 | 4 | #include 5 | #include 6 | #include "Linked_List.c" 7 | 8 | void DeleteList() { 9 | if(head == NULL) { 10 | printf("\nError: Linked_List already is Empty\n"); 11 | return; 12 | } 13 | NODE *temp = head; // dereference head to get the head on temp NODE 14 | NODE *temp1; // This will point to next NODE of temp therefore we can access next NODE 15 | while(temp != NULL) { // Traverse the Linked List till last NODE 16 | temp1 = temp->next; // As if I free this temp memory then List will Delete without 17 | free(temp); // deallocating the memory of Other NODEs 18 | temp = temp1; 19 | } 20 | printf("\n-------------------------\n"); 21 | printf("\n Linked_List is Deleted \n"); 22 | printf("\n-------------------------\n"); 23 | head = NULL; 24 | return; 25 | } 26 | 27 | void main() { 28 | int a; 29 | while(a!=0) { 30 | printf("\n\n"); 31 | printf("Enter 1 -> Insert_front\n"); 32 | printf("Enter 2 -> Delete_front\n"); 33 | printf("Enter 3 -> Insert_end\n"); 34 | printf("Enter 4 -> Delete_end\n"); 35 | printf("Enter 5 -> Display_LL\n"); 36 | printf("Enter 6 -> To Delete a List\n"); 37 | printf("Enter 0 -> Exit\n"); 38 | scanf("%d", &a); 39 | if(a==0) { 40 | printf("-------------------------Thank You----------------------------\n"); 41 | return; 42 | } 43 | switch(a) { 44 | case 1 : 45 | { 46 | Insert_front(); 47 | Display_LL(); 48 | break; 49 | } 50 | case 2 : 51 | { 52 | Delete_front(); 53 | Display_LL(); 54 | break; 55 | } 56 | case 3 : 57 | { 58 | Insert_end(); 59 | Display_LL(); 60 | break; 61 | } 62 | case 4 : 63 | { 64 | Delete_end(); 65 | Display_LL(); 66 | break; 67 | } 68 | case 5 : 69 | { 70 | Display_LL(); 71 | break; 72 | } 73 | case 6 : 74 | { 75 | DeleteList(); 76 | break; 77 | } 78 | default : 79 | { 80 | printf("Wrong Value Detacted\n"); 81 | break; 82 | } 83 | return; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Linked_List/3.2_InsertNth.c: -------------------------------------------------------------------------------- 1 | // Write a function InsertNth() which can insert a new node at any index within a list. 2 | 3 | #include 4 | #include 5 | #include "Linked_List.c" 6 | 7 | void InsertNth() { 8 | 9 | if(head == NULL) { 10 | printf("\nError: Intially Linked_List is Empty\n\n"); 11 | head = getnode(); // Intial Condition if List is Empty 12 | return; 13 | } 14 | NODE *curr = head; // Will point to Current Element in Linked_List 15 | NODE *prev; // Will point to Previous Element in Linked_List 16 | int count = 0; // It will simply increase till the index value and help in adding the Element at that index 17 | int index; // Given Index 18 | printf("Enter the value of index for ith postion\n"); 19 | scanf("%d", &index); 20 | 21 | // To Find the Number of Nodes 22 | 23 | int num_nodes = 0; 24 | while(curr != NULL) { 25 | num_nodes++; // Finding the Number of Nodes in the Linked_List 26 | curr = curr->next; 27 | } 28 | curr = head; // Reintialize temp to head as it is at last postion Now 29 | 30 | // Boundary Condition if index is last of the Linked_List 31 | 32 | if(index == num_nodes+1) { 33 | Insert_end(); 34 | return; 35 | } 36 | 37 | // Boundary Condition if Index is first of the Linked_List 38 | 39 | else if(index == 1) { 40 | Insert_front(); 41 | return; 42 | } 43 | 44 | // If index is not in Linked_List 45 | 46 | else if(index <=0 || index > num_nodes+1 ) { 47 | printf("\nError: Index not Exist\n"); 48 | return; 49 | } 50 | 51 | // To Insert at ith Postion if index is inbetween the list 52 | 53 | NODE *newnode; 54 | newnode = getnode(); 55 | while(curr != NULL) { 56 | count++; // It will Increase till count = Index 57 | if(count == index) { 58 | prev->next = newnode; // Here next of prev will point to new NODE 59 | prev = newnode; // And making prev as newnode 60 | newnode->next = curr; // and next of newnode will point to current 61 | return; 62 | } 63 | prev = curr; 64 | curr = curr->next; 65 | } 66 | } 67 | 68 | void main() { 69 | int a; 70 | while(a!=0) { 71 | printf("\n\n"); 72 | printf("Enter 1 -> Insert_front\n"); 73 | printf("Enter 2 -> Delete_front\n"); 74 | printf("Enter 3 -> Insert_end\n"); 75 | printf("Enter 4 -> Delete_end\n"); 76 | printf("Enter 5 -> Display_LL\n"); 77 | printf("Enter 6 -> InsertNth\n"); 78 | printf("Enter 0 -> Exit\n"); 79 | scanf("%d", &a); 80 | if(a==0) { 81 | printf("-------------------------Thank You----------------------------\n"); 82 | return; 83 | } 84 | switch(a) { 85 | case 1 : 86 | { 87 | Insert_front(); 88 | Display_LL(); 89 | break; 90 | } 91 | case 2 : 92 | { 93 | Delete_front(); 94 | Display_LL(); 95 | break; 96 | } 97 | case 3 : 98 | { 99 | Insert_end(); 100 | Display_LL(); 101 | break; 102 | } 103 | case 4 : 104 | { 105 | Delete_end(); 106 | Display_LL(); 107 | break; 108 | } 109 | case 5 : 110 | { 111 | Display_LL(); 112 | break; 113 | } 114 | case 6 : 115 | { 116 | InsertNth(); 117 | Display_LL(); 118 | break; 119 | } 120 | default : 121 | { 122 | printf("Wrong Value Detacted\n"); 123 | break; 124 | } 125 | return; 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /Linked_List/4_SortedInsert.c: -------------------------------------------------------------------------------- 1 | //Write a SortedInsert() function which given a list that is sorted in increasing order, and a single 2 | //node, inserts the node into the correct sorted position in the list. 3 | 4 | #include 5 | #include 6 | #include "Linked_List.c" 7 | 8 | void SortedInsert() { 9 | NODE *newnode; 10 | newnode = getnode(); 11 | 12 | //for inserting at boundary points here I check if head data is greater then newnode data then do Insert_front 13 | 14 | if (head == NULL || head->data >= newnode->data) { 15 | newnode->next = head; 16 | head = newnode; 17 | } 18 | // else while Current next data is less then new node data do insert after Current 19 | else { 20 | NODE *curr = head; 21 | while(curr->next != NULL && curr->next->data < newnode->data) { 22 | curr = curr->next; 23 | } 24 | newnode->next = curr->next; 25 | curr->next = newnode; 26 | } 27 | } 28 | 29 | void main() { 30 | int a; 31 | while(a!=0) { 32 | printf("\n\n"); 33 | printf("Enter 1 -> Insert_front\n"); 34 | printf("Enter 2 -> Delete_front\n"); 35 | printf("Enter 3 -> Insert_end\n"); 36 | printf("Enter 4 -> Delete_end\n"); 37 | printf("Enter 5 -> Display_LL\n"); 38 | printf("Enter 6 -> SortedInsert\n"); 39 | printf("Enter 0 -> Exit\n"); 40 | scanf("%d", &a); 41 | if(a==0) { 42 | printf("-------------------------Thank You----------------------------\n"); 43 | return; 44 | } 45 | switch(a) { 46 | case 1 : 47 | { 48 | Insert_front(); 49 | Display_LL(); 50 | break; 51 | } 52 | case 2 : 53 | { 54 | Delete_front(); 55 | Display_LL(); 56 | break; 57 | } 58 | case 3 : 59 | { 60 | Insert_end(); 61 | Display_LL(); 62 | break; 63 | } 64 | case 4 : 65 | { 66 | Delete_end(); 67 | Display_LL(); 68 | break; 69 | } 70 | case 5 : 71 | { 72 | Display_LL(); 73 | break; 74 | } 75 | case 6 : 76 | { 77 | SortedInsert(); 78 | Display_LL(); 79 | break; 80 | } 81 | default : 82 | { 83 | printf("Wrong Value Detacted\n"); 84 | break; 85 | } 86 | return; 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /Linked_List/6_Append.c: -------------------------------------------------------------------------------- 1 | //Write an Append() function that takes two lists, 'a' and 'b', appends 'b' onto the end of 'a', 2 | //and then sets 'b' to NULL. 3 | 4 | 5 | #include 6 | #include 7 | 8 | struct node { 9 | int data; 10 | struct node *next; 11 | }; 12 | typedef struct node NODE; 13 | 14 | NODE *getnode() { 15 | NODE *newnode; 16 | newnode = (NODE*)malloc(sizeof(NODE)); 17 | int ele; 18 | printf("Enter the Element in Linked List\n"); 19 | scanf("%d", &ele); 20 | newnode->data = ele; 21 | newnode->next = NULL; 22 | return newnode; 23 | } 24 | 25 | void Insert_front(NODE **head) { 26 | NODE *newnode = getnode(); 27 | newnode->next = *head; 28 | *head = newnode; 29 | } 30 | 31 | void Insert_end(NODE **head) { 32 | 33 | NODE *newnode; 34 | newnode = getnode(); 35 | NODE *curr = *head; 36 | if(*head == NULL) { 37 | printf("\nError: Intially Linked_List is Empty\n\n"); 38 | *head = newnode; 39 | return; 40 | } 41 | while(curr->next != NULL) { 42 | curr = curr->next; 43 | } 44 | curr->next = newnode; 45 | return; 46 | } 47 | 48 | void Delete_front(NODE **head) { 49 | if(*head == NULL) { 50 | return; 51 | } 52 | NODE *temp = *head; 53 | *head = temp->next; 54 | free(temp); 55 | } 56 | 57 | void Delete_end(NODE **head) { 58 | if(*head == NULL) { 59 | return; 60 | } 61 | NODE *curr = *head; 62 | NODE *prev; 63 | while(curr->next != NULL) { 64 | prev = curr; 65 | curr = curr->next; 66 | } 67 | if(curr == *head) { 68 | *head = NULL; 69 | } 70 | else { 71 | prev->next = NULL; 72 | } 73 | free(curr); 74 | } 75 | 76 | void Display_LL(NODE **head) { 77 | if(*head == NULL) { 78 | printf("\nError: Linked_List is Empty\n"); 79 | } 80 | NODE *temp = *head; 81 | while(temp != NULL) { 82 | printf("%d --> ", temp->data); 83 | temp = temp->next; 84 | } 85 | } 86 | 87 | 88 | void Append(NODE **h1, NODE **h2) { 89 | NODE *curr; 90 | if(*h1 == NULL) { // If first list is null directly append second to first 91 | *h1 = *h2; 92 | return; 93 | } 94 | else{ 95 | curr = *h1; // else tranverse till last of list 1 and append the list with next of last 96 | while(curr->next != NULL) { 97 | curr = curr->next; 98 | } // say NULL to h2 and free its memory. 99 | curr->next = *h2; 100 | *h2 = NULL; 101 | free(*h2); 102 | } 103 | } 104 | 105 | void main() { 106 | int a; 107 | int b; 108 | NODE *head1 = NULL; 109 | NODE *head2 = NULL; 110 | while(a!=0) { 111 | printf("\nEnter 1: To create list 1\n"); 112 | printf("\nEnter 2: To create list 2\n"); 113 | scanf("%d", &b); 114 | printf("\n"); 115 | printf("Enter 1 -> Insert_front in 1st list\n"); 116 | printf("Enter 2 -> Delete_front in 1st list\n"); 117 | printf("Enter 3 -> Insert_end in 1st list\n"); 118 | printf("Enter 4 -> Delete_end in 1st list\n"); 119 | printf("Enter 5 -> Display_LL in 1st list\n"); 120 | printf("Enter 6 -> Append()\n"); 121 | printf("Enter 0 -> Exit\n"); 122 | scanf("%d", &a); 123 | switch(a) { 124 | case 1 : 125 | { 126 | if(b==1) { 127 | Insert_front(&head1); 128 | Display_LL(&head1); 129 | break; 130 | } 131 | else if(b==2) { 132 | Insert_front(&head2); 133 | Display_LL(&head2); 134 | break; 135 | } 136 | } 137 | case 2 : 138 | { 139 | if(b==1) { 140 | Delete_front(&head1); 141 | Display_LL(&head1); 142 | break; 143 | } 144 | else if(b==2) { 145 | Delete_front(&head2); 146 | Display_LL(&head2); 147 | break; 148 | } 149 | } 150 | case 3 : 151 | { 152 | if(b==1) { 153 | Insert_end(&head1); 154 | Display_LL(&head1); 155 | break; 156 | } 157 | else if(b==2) { 158 | Insert_end(&head2); 159 | Display_LL(&head2); 160 | break; 161 | } 162 | } 163 | case 4 : 164 | { 165 | if(b==1) { 166 | Delete_end(&head1); 167 | Display_LL(&head1); 168 | break; 169 | } 170 | else if(b==2) { 171 | Delete_end(&head2); 172 | Display_LL(&head2); 173 | break; 174 | } 175 | } 176 | case 5 : 177 | { 178 | printf("\nList 1 :\n"); 179 | Display_LL(&head1); 180 | printf("\nList 2 :\n"); 181 | Display_LL(&head2); 182 | break; 183 | } 184 | case 6 : 185 | { 186 | Append(&head1, &head2); 187 | printf("\nList 1 after appending:\n"); 188 | Display_LL(&head1); 189 | printf("\nList 2 after appending:\n"); 190 | Display_LL(&head2); 191 | return; 192 | } 193 | default : 194 | { 195 | printf("Wrong Value Detacted\n"); 196 | break; 197 | } 198 | return; 199 | } 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /Linked_List/7_FrontBackSplit.c: -------------------------------------------------------------------------------- 1 | // Given a list, split it into two sublists — one for the front half, and one for the back half. If 2 | // the number of elements is odd, the extra element should go in the front list 3 | 4 | #include 5 | #include 6 | #include "Linked_List.c" 7 | 8 | void FrontBackSplit() { 9 | NODE *curr = head; // Will point to Current Element in Linked_List 10 | 11 | // To Find the Number of Nodes 12 | int num_nodes = 0; 13 | while(curr != NULL) { 14 | num_nodes++; // Finding the Number of Nodes in the Linked_List 15 | curr = curr->next; 16 | } 17 | curr = head; // Reintialize temp to head as it is at last postion Now 18 | 19 | 20 | NODE *front; // Help to print the Front list by pointing to last Node of front list 21 | NODE *back; // Help to print the Back list by pointing to First Node of back list 22 | int i; 23 | 24 | // If there are only 1 or 0 element in list then front will point to head and back to NULL 25 | // as there will 0 element in back in this condition. 26 | 27 | if (num_nodes < 2) { 28 | front = head; 29 | back = NULL; 30 | } 31 | 32 | // Here I set the front and back value 33 | 34 | else { 35 | int split = (num_nodes-1)/2; 36 | for(i = 0; inext; 38 | } 39 | front = curr; // Front will point to curr after it reaches split 40 | back = curr->next; // Back will point to next element of front 41 | front->next = NULL; // Breaking the List between front and back 42 | } 43 | 44 | // Displaying Front List 45 | 46 | 47 | NODE *temp1 = head; // help to Display front list. 48 | if(temp1 == NULL) { 49 | printf("\nFront and Back Lists are Empty\n"); // If this is Null then both list are Empty 50 | return; 51 | } 52 | printf("\nFront List: \n"); 53 | while(temp1 != NULL) { 54 | printf("%d --> ", temp1->data); 55 | temp1 = temp1->next; 56 | } 57 | 58 | 59 | printf("\n----------------\n"); 60 | 61 | // Displaying Back list 62 | 63 | 64 | NODE *temp2 = back; 65 | if(temp2 == NULL) { 66 | printf("\nBack List is Empty\n"); 67 | return; 68 | } 69 | printf("\nBack List: \n"); 70 | while(temp2 != NULL) { 71 | printf("%d --> ", temp2->data); 72 | temp2 = temp2->next; 73 | } 74 | printf("\n"); 75 | return; 76 | } 77 | 78 | 79 | void main() { 80 | int a; 81 | while(a!=0) { 82 | printf("\n\n"); 83 | printf("Enter 1 -> Insert_front\n"); 84 | printf("Enter 2 -> Delete_front\n"); 85 | printf("Enter 3 -> Insert_end\n"); 86 | printf("Enter 4 -> Delete_end\n"); 87 | printf("Enter 5 -> Display_LL\n"); 88 | printf("Enter 6 -> FrontBackSplit()\n"); 89 | printf("Enter 0 -> Exit\n"); 90 | scanf("%d", &a); 91 | if(a==0) { 92 | printf("-------------------------Thank You----------------------------\n"); 93 | return; 94 | } 95 | switch(a) { 96 | case 1 : 97 | { 98 | Insert_front(); 99 | Display_LL(); 100 | break; 101 | } 102 | case 2 : 103 | { 104 | Delete_front(); 105 | Display_LL(); 106 | break; 107 | } 108 | case 3 : 109 | { 110 | Insert_end(); 111 | Display_LL(); 112 | break; 113 | } 114 | case 4 : 115 | { 116 | Delete_end(); 117 | Display_LL(); 118 | break; 119 | } 120 | case 5 : 121 | { 122 | Display_LL(); 123 | break; 124 | } 125 | case 6 : 126 | { 127 | FrontBackSplit(); 128 | return; 129 | } 130 | default : 131 | { 132 | printf("Wrong Value Detacted\n"); 133 | break; 134 | } 135 | return; 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /Linked_List/7_print_lots.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node { 5 | int data; 6 | struct node *next; 7 | }; 8 | typedef struct node NODE; 9 | 10 | NODE *getnode() { 11 | NODE *newnode; 12 | newnode = (NODE*)malloc(sizeof(NODE)); 13 | int ele; 14 | printf("Enter the Element in Linked List\n"); 15 | scanf("%d", &ele); 16 | newnode->data = ele; 17 | newnode->next = NULL; 18 | return newnode; 19 | } 20 | 21 | void Insert_front(NODE **head) { 22 | NODE *newnode = getnode(); 23 | newnode->next = *head; 24 | *head = newnode; 25 | } 26 | 27 | void Insert_end(NODE **head) { 28 | 29 | NODE *newnode; 30 | newnode = getnode(); 31 | NODE *curr = *head; 32 | if(*head == NULL) { 33 | printf("\nError: Intially Linked_List is Empty\n\n"); 34 | *head = newnode; 35 | return; 36 | } 37 | while(curr->next != NULL) { 38 | curr = curr->next; 39 | } 40 | curr->next = newnode; 41 | return; 42 | } 43 | 44 | void Delete_front(NODE **head) { 45 | if(*head == NULL) { 46 | return; 47 | } 48 | NODE *temp = *head; 49 | *head = temp->next; 50 | free(temp); 51 | } 52 | 53 | void Delete_end(NODE **head) { 54 | if(*head == NULL) { 55 | return; 56 | } 57 | NODE *curr = *head; 58 | NODE *prev; 59 | while(curr->next != NULL) { 60 | prev = curr; 61 | curr = curr->next; 62 | } 63 | if(curr == *head) { 64 | *head = NULL; 65 | } 66 | else { 67 | prev->next = NULL; 68 | } 69 | free(curr); 70 | } 71 | 72 | void Display_LL(NODE **head) { 73 | if(*head == NULL) { 74 | printf("\nError: Linked_List is Empty\n"); 75 | } 76 | NODE *temp = *head; 77 | while(temp != NULL) { 78 | printf("%d --> ", temp->data); 79 | temp = temp->next; 80 | } 81 | } 82 | 83 | // Here if P data is count then print l->data and update P and L to next 84 | // Otherwise only Update L to next 85 | void print_lots(NODE **h1, NODE **h2) { 86 | NODE *L = *h1; 87 | NODE *P = *h2; 88 | int count = 1; 89 | while(L != NULL && P != NULL) { 90 | if(P->data == count) { 91 | printf("%d ", L->data); 92 | P = P->next; 93 | } 94 | L = L->next; 95 | count++; 96 | } 97 | } 98 | 99 | void main() { 100 | int a; 101 | int b; 102 | NODE *head1 = NULL; 103 | NODE *head2 = NULL; 104 | while(a!=0) { 105 | printf("\nEnter 1: To create list 1\n"); 106 | printf("\nEnter 2: To create list 2\n"); 107 | scanf("%d", &b); 108 | printf("\n"); 109 | printf("Enter 1 -> Insert_front in 1st list\n"); 110 | printf("Enter 2 -> Delete_front in 1st list\n"); 111 | printf("Enter 3 -> Insert_end in 1st list\n"); 112 | printf("Enter 4 -> Delete_end in 1st list\n"); 113 | printf("Enter 5 -> Display_LL in 1st list\n"); 114 | printf("Enter 6 -> Print_lots()\n"); 115 | printf("Enter 0 -> Exit\n"); 116 | scanf("%d", &a); 117 | if(a==0) { 118 | printf("-------------------------Thank You----------------------------\n"); 119 | return; 120 | } 121 | switch(a) { 122 | case 1 : 123 | { 124 | if(b==1) { 125 | Insert_front(&head1); 126 | Display_LL(&head1); 127 | break; 128 | } 129 | else if(b==2) { 130 | Insert_front(&head2); 131 | Display_LL(&head2); 132 | break; 133 | } 134 | } 135 | case 2 : 136 | { 137 | if(b==1) { 138 | Delete_front(&head1); 139 | Display_LL(&head1); 140 | break; 141 | } 142 | else if(b==2) { 143 | Delete_front(&head2); 144 | Display_LL(&head2); 145 | break; 146 | } 147 | } 148 | case 3 : 149 | { 150 | if(b==1) { 151 | Insert_end(&head1); 152 | Display_LL(&head1); 153 | break; 154 | } 155 | else if(b==2) { 156 | Insert_end(&head2); 157 | Display_LL(&head2); 158 | break; 159 | } 160 | } 161 | case 4 : 162 | { 163 | if(b==1) { 164 | Delete_end(&head1); 165 | Display_LL(&head1); 166 | break; 167 | } 168 | else if(b==2) { 169 | Delete_end(&head2); 170 | Display_LL(&head2); 171 | break; 172 | } 173 | } 174 | case 5 : 175 | { 176 | printf("\nList 1 :\n"); 177 | Display_LL(&head1); 178 | printf("\nList 2 :\n"); 179 | Display_LL(&head2); 180 | break; 181 | } 182 | case 6 : 183 | { 184 | print_lots(&head1, &head2); 185 | break; 186 | } 187 | default : 188 | { 189 | printf("Wrong Value Detacted\n"); 190 | break; 191 | } 192 | return; 193 | } 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /Linked_List/8_RemoveDuplicates.c: -------------------------------------------------------------------------------- 1 | // Write a RemoveDuplicates() function which takes a list sorted in increasing order and 2 | // deletes any duplicate nodes from the list. Ideally, the list should only be traversed once. 3 | 4 | #include 5 | #include 6 | #include "Linked_List.c" 7 | 8 | void RemoveDuplicates() { 9 | // curr Will point to Current Element in Linked_List 10 | // Temp Node used to delete the duplicate data node 11 | // Intial Condition if List is Empty 12 | NODE *curr = head; 13 | NODE *temp; 14 | if(head == NULL) { 15 | return; 16 | } 17 | // Check if data in next of current node is equal to data in current node 18 | // say temp to next to next node of current 19 | // free next to current node 20 | // link current with temp 21 | 22 | while(curr != NULL){ 23 | if(curr->data == curr->next->data) { 24 | temp = curr->next->next; 25 | free(curr->next); 26 | curr->next = temp; 27 | } 28 | curr = curr->next; 29 | } 30 | return; 31 | } 32 | 33 | void main() { 34 | int a; 35 | while(a!=0) { 36 | printf("\n\n"); 37 | printf("Enter 1 -> Insert_front\n"); 38 | printf("Enter 2 -> Delete_front\n"); 39 | printf("Enter 3 -> Insert_end\n"); 40 | printf("Enter 4 -> Delete_end\n"); 41 | printf("Enter 5 -> Display_LL\n"); 42 | printf("Enter 6 -> RemoveDuplicates\n"); 43 | printf("Enter 0 -> Exit\n"); 44 | scanf("%d", &a); 45 | if(a==0) { 46 | printf("-------------------------Thank You----------------------------\n"); 47 | return; 48 | } 49 | switch(a) { 50 | case 1 : 51 | { 52 | Insert_front(); 53 | Display_LL(); 54 | break; 55 | } 56 | case 2 : 57 | { 58 | Delete_front(); 59 | Display_LL(); 60 | break; 61 | } 62 | case 3 : 63 | { 64 | Insert_end(); 65 | Display_LL(); 66 | break; 67 | } 68 | case 4 : 69 | { 70 | Delete_end(); 71 | Display_LL(); 72 | break; 73 | } 74 | case 5 : 75 | { 76 | Display_LL(); 77 | break; 78 | } 79 | case 6 : 80 | { 81 | RemoveDuplicates(); 82 | printf("\nAfter Removing all duplicates\n"); 83 | Display_LL(); 84 | break; 85 | } 86 | default : 87 | { 88 | printf("Wrong Value Detacted\n"); 89 | break; 90 | } 91 | return; 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /Linked_List/Implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class Node{ 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int d){ 11 | data = d; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | void insert_front(Node* &head, int val){ 17 | if(head == NULL){ 18 | head = new Node(val); 19 | return; 20 | } 21 | 22 | Node* temp = new Node(val); 23 | temp->next = head; 24 | head = temp; 25 | } 26 | 27 | void insert_back(Node* &head, int val){ 28 | if(head == NULL){ 29 | head = new Node(val); 30 | return; 31 | } 32 | Node* temp = head; 33 | while(temp->next){ 34 | temp = temp->next; 35 | } 36 | Node* newnode = new Node(val); 37 | temp->next = newnode; 38 | } 39 | 40 | int length_list(Node* head){ 41 | int count = 0; 42 | while(head){ 43 | count++; 44 | head = head->next; 45 | } 46 | return count; 47 | } 48 | 49 | void insertInMiddle(Node* &head, int val, int p){ 50 | if(head == NULL or p == 0){ 51 | insert_front(head, val); 52 | return; 53 | } 54 | else if(p > length_list(head)){ 55 | insert_back(head, val); 56 | return; 57 | } 58 | else { 59 | int cnt = 0; 60 | Node* temp = head; 61 | while(cnt<=p-1){ 62 | temp = temp->next; 63 | cnt++; 64 | } 65 | 66 | Node* newnode = new Node(val); 67 | newnode->next = temp->next; 68 | temp->next = newnode; 69 | return; 70 | } 71 | 72 | } 73 | 74 | void display(Node* head){ 75 | if(head == NULL){ 76 | cout << "Empty list" << endl; 77 | return; 78 | } 79 | Node* temp = head; 80 | while(temp){ 81 | cout << temp->data<< " -->"; 82 | temp = temp->next; 83 | } 84 | cout << endl; 85 | } 86 | 87 | void delete_head(Node* &head){ 88 | if(head == NULL){ 89 | cout << "Empty List" << endl; 90 | return; 91 | } 92 | 93 | Node* temp = head->next; 94 | delete head; 95 | head = temp; 96 | } 97 | 98 | Node* delete_tail(Node* &head){ 99 | if(head == NULL){ 100 | cout << "Empty List" << endl; 101 | return NULL; 102 | } 103 | 104 | if(head->next == NULL){ 105 | delete head; 106 | return NULL; 107 | } 108 | 109 | Node* temp = head; 110 | while(temp->next->next){ 111 | temp = temp->next; 112 | } 113 | delete temp->next; 114 | temp->next = NULL; 115 | return head; 116 | } 117 | 118 | Node* deletemiddle(Node* &head, int p){ 119 | if(p == 0){ 120 | delete_head(head); 121 | return head; 122 | } 123 | else if(p >= length_list(head)){ 124 | head = delete_tail(head); 125 | return head; 126 | } 127 | else{ 128 | int cnt = 0; 129 | Node* prev = NULL; 130 | Node* curr = head; 131 | while(cnt <= p-1) 132 | { 133 | prev = curr; 134 | curr = curr->next; 135 | cnt++; 136 | } 137 | 138 | prev->next = curr->next; 139 | return head; 140 | } 141 | } 142 | 143 | // Linear Search 144 | bool search(Node* head, int key){ 145 | while(head){ 146 | if(head->data == key){ 147 | return true; 148 | } 149 | head = head->next; 150 | } 151 | return false; 152 | } 153 | 154 | bool search_recursively(Node* head, int key){ 155 | if(head == NULL){ 156 | return false; 157 | } 158 | if(head->data == key){ 159 | return true; 160 | } 161 | else{ 162 | return search_recursively(head->next, key); 163 | } 164 | } 165 | 166 | Node* take_input(){ 167 | int d; 168 | cout << "Input number to add in linked list, write -1 to stop" << endl; 169 | cin >> d; 170 | Node* head = NULL; 171 | while(d!=-1){ 172 | insert_front(head, d); 173 | cin >> d; 174 | } 175 | return head; 176 | } 177 | 178 | Node* take_input_fromfile(){ 179 | int d; 180 | Node* head = NULL; 181 | while(cin >> d){ 182 | insert_back(head, d); 183 | } 184 | return head; 185 | } 186 | 187 | // Operator Overloading 188 | ostream& operator<<(ostream &os, Node* head){ 189 | display(head); 190 | return os; 191 | } 192 | 193 | istream& operator>>(istream &is, Node* &head){ 194 | head = take_input_fromfile(); 195 | return is; 196 | } 197 | 198 | void reverse_ll(Node*&head){ 199 | Node* temp = NULL; 200 | Node* curr = head; 201 | Node* nextp; 202 | while(curr){ 203 | nextp = curr->next; 204 | curr->next = temp; 205 | temp = curr; 206 | curr = nextp; 207 | } 208 | head = temp; 209 | } 210 | 211 | Node* reverse_recursive(Node* &head){ 212 | if(head->next == NULL or head == NULL){ 213 | return head; 214 | } 215 | 216 | Node* shead = reverse_recursive(head->next); 217 | head->next->next = head; 218 | head->next = NULL; 219 | return shead; 220 | } 221 | 222 | Node* midpoint(Node* &head){ 223 | if(head == NULL or head->next == NULL){ 224 | return head; 225 | } 226 | Node* slow = head; 227 | Node* fast = head->next; 228 | while(fast and fast->next){ 229 | fast = fast->next->next; 230 | slow = slow->next; 231 | } 232 | return slow; 233 | } 234 | 235 | Node* kthNode_fromend(Node* &head, int k){ 236 | if(head == NULL or head->next == NULL){ 237 | return head; 238 | } 239 | if(k>length_list(head) or k<0){ 240 | return head; 241 | } 242 | Node* slow = head; 243 | Node* fast = head; 244 | for(int i =0; inext; 246 | } 247 | while(fast){ 248 | fast = fast->next; 249 | slow = slow->next; 250 | } 251 | return slow; 252 | } 253 | 254 | Node* merge2sorted(Node* a, Node* b) 255 | { 256 | if(a==NULL){ 257 | return b; 258 | } 259 | if(b == NULL){ 260 | return a; 261 | } 262 | if(a == NULL and b == NULL){ 263 | return NULL; 264 | } 265 | Node* mergelist = NULL; 266 | if(a->data < b->data){ 267 | mergelist = a; 268 | mergelist->next = merge2sorted(a->next, b); 269 | } 270 | else{ 271 | mergelist = b; 272 | mergelist->next = merge2sorted(a, b->next); 273 | } 274 | 275 | return mergelist; 276 | } 277 | 278 | // Merge sort 279 | Node* mergesort(Node* head){ 280 | // base 281 | if(head == NULL or head->next == NULL){ 282 | return head; 283 | } 284 | 285 | // rec base 286 | // 1. break 287 | Node* mid = midpoint(head); 288 | Node* a = head; 289 | Node* b = mid->next; 290 | 291 | mid->next = NULL; 292 | // rec sort 293 | a = mergesort(a); 294 | b = mergesort(b); 295 | 296 | // merge 297 | Node* c = merge2sorted(a,b); 298 | return c; 299 | } 300 | 301 | int main(){ 302 | // Node* head = take_input_fromfile(); 303 | Node* head; 304 | // Node* head2 = NULL; 305 | // insert_back(head2,2); 306 | // insert_back(head2,4); 307 | // insert_back(head2,6); 308 | // insert_back(head2,8); 309 | // insert_back(head2,10); 310 | cin >> head; 311 | cout << head; 312 | head = mergesort(head); 313 | cout << head; 314 | // cout << head2; 315 | // Node* mergelist = merge2sorted(head, head2); 316 | // cout << mergelist; 317 | 318 | // head = reverse_recursive(head); 319 | // reverse_ll(head); 320 | // cout << head; 321 | // Node* mid = midpoint(head); 322 | // cout << mid; 323 | // Node* kthnodefromend = kthNode_fromend(head, 1); 324 | // cout << kthnodefromend; 325 | // display(head); 326 | // delete_head(head); 327 | // display(head); 328 | // head = delete_tail(head); 329 | // head = deletemiddle(head, 2); 330 | // display(head); 331 | // if(search(head, 7)){ 332 | // cout << "Present" << endl; 333 | // } else{ 334 | // cout << "Absent" << endl; 335 | // } 336 | 337 | // if(search_recursively(head, 5)){ 338 | // cout << "Present" << endl; 339 | // } else{ 340 | // cout << "Absent" << endl; 341 | // } 342 | } -------------------------------------------------------------------------------- /Linked_List/Linked_List.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | struct node { 5 | int data; 6 | struct node *next; 7 | }; 8 | typedef struct node NODE; 9 | 10 | NODE *getnode() { 11 | NODE *newnode; 12 | newnode = (NODE*)malloc(sizeof(NODE)); 13 | if(newnode==NULL) 14 | { 15 | printf("\nThere is no more space in the system\n"); 16 | return NULL; 17 | } 18 | int ele; 19 | printf("Enter the Element in Linked List\n"); 20 | scanf("%d", &ele); 21 | newnode->data = ele; 22 | newnode->next = NULL; 23 | return newnode; 24 | } 25 | 26 | NODE *head = NULL; 27 | 28 | void Insert_front() { 29 | NODE *newnode = getnode(); 30 | newnode->next = head; 31 | head = newnode; 32 | } 33 | 34 | void Insert_end() { 35 | 36 | NODE *newnode; 37 | newnode = getnode(); 38 | NODE *curr = head; 39 | if(head == NULL) { 40 | printf("\nError: Intially Linked_List is Empty\n\n"); 41 | head = newnode; 42 | return; 43 | } 44 | while(curr->next != NULL) { 45 | curr = curr->next; 46 | } 47 | curr->next = newnode; 48 | return; 49 | } 50 | 51 | void Delete_front() { 52 | if(head == NULL) { 53 | return; 54 | } 55 | NODE *temp = head; 56 | head = head->next; 57 | free(temp); 58 | } 59 | 60 | void Delete_end() { 61 | if(head == NULL) { 62 | return; 63 | } 64 | NODE *curr = head; 65 | NODE *prev; 66 | while(curr->next != NULL) { 67 | prev = curr; 68 | curr = curr->next; 69 | } 70 | if(curr == head) { 71 | head = NULL; 72 | } 73 | else { 74 | prev->next = NULL; 75 | } 76 | free(curr); 77 | } 78 | 79 | void Display_LL() { 80 | if(head == NULL) { 81 | printf("\nError: Linked_List is Empty\n"); 82 | } 83 | NODE *temp = head; 84 | while(temp != NULL) { 85 | printf("%d --> ", temp->data); 86 | temp = temp->next; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /Linked_List/cll.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class Node{ 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int d){ 11 | data = d; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | void insert_front(Node* &head, int val){ 17 | 18 | Node* n = new Node(val); 19 | Node* temp = head; 20 | n->next = head; 21 | if(temp!= NULL){ 22 | while(temp->next != head){ 23 | temp = temp->next; 24 | } 25 | temp->next = n; 26 | } 27 | else{ 28 | n->next = n; 29 | } 30 | head = n; 31 | } 32 | 33 | void display(Node* head){ 34 | Node* temp = head; 35 | while(temp->next != head){ 36 | cout << temp->data<< " -->"; 37 | temp = temp->next; 38 | } 39 | cout << temp->data << endl; 40 | } 41 | 42 | Node* getnode(Node* head, int data){ 43 | Node* temp = head; 44 | while(temp->next!= head){ 45 | if(temp->data == data){ 46 | return temp; 47 | } 48 | temp = temp->next; 49 | } 50 | if(temp->data == data) return temp; 51 | return NULL; 52 | } 53 | 54 | void deleteNode(Node* &head, int data){ 55 | Node* del = getnode(head, data); 56 | if(del == NULL){ 57 | return; 58 | } 59 | 60 | if(head == del) { 61 | head = head->next; 62 | } 63 | 64 | Node* temp = head; 65 | while(temp->next!= del){ 66 | temp = temp->next; 67 | } 68 | 69 | temp->next = del->next; 70 | 71 | delete del; 72 | } 73 | 74 | int main(){ 75 | Node* head = NULL; 76 | insert_front(head,2); 77 | insert_front(head,3); 78 | insert_front(head,4); 79 | insert_front(head,8); 80 | display(head); 81 | deleteNode(head, 3); 82 | display(head); 83 | deleteNode(head, 4); 84 | display(head); 85 | } -------------------------------------------------------------------------------- /Linked_List/dll.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class Node{ 6 | public: 7 | int data; 8 | Node* next; 9 | Node* prev; 10 | 11 | Node(int d){ 12 | data = d; 13 | next = NULL; 14 | prev = NULL; 15 | } 16 | }; 17 | 18 | void insert_front(Node* &head, int val){ 19 | if(head == NULL){ 20 | head = new Node(val); 21 | return; 22 | } 23 | 24 | Node* temp = new Node(val); 25 | temp->next = head; 26 | if(head != NULL) head->prev = temp; 27 | head = temp; 28 | } 29 | 30 | void insert_back(Node* &head, int val){ 31 | if(head == NULL){ 32 | head = new Node(val); 33 | return; 34 | } 35 | Node* temp = head; 36 | while(temp->next){ 37 | temp = temp->next; 38 | } 39 | Node* newnode = new Node(val); 40 | temp->next = newnode; 41 | newnode->prev = temp; 42 | } 43 | 44 | int length_list(Node* head){ 45 | int count = 0; 46 | while(head){ 47 | count++; 48 | head = head->next; 49 | } 50 | return count; 51 | } 52 | 53 | void insertInMiddle(Node* &head, int val, int p){ 54 | if(head == NULL or p == 0){ 55 | insert_front(head, val); 56 | return; 57 | } 58 | else if(p >= length_list(head)){ 59 | insert_back(head, val); 60 | return; 61 | } 62 | else { 63 | int cnt = 0; 64 | Node* temp = head; 65 | while(cntnext; 67 | cnt++; 68 | } 69 | 70 | Node* newnode = new Node(val); 71 | newnode->next = temp->next; 72 | temp->next->prev = newnode; 73 | temp->next = newnode; 74 | newnode->prev = temp; 75 | return; 76 | } 77 | 78 | } 79 | 80 | void display(Node* head){ 81 | if(head == NULL){ 82 | cout << "Empty list" << endl; 83 | return; 84 | } 85 | Node* temp = head; 86 | while(temp){ 87 | cout << temp->data<< " -->"; 88 | temp = temp->next; 89 | } 90 | cout << endl; 91 | } 92 | 93 | void display_rev(Node* head){ 94 | if(head == NULL){ 95 | cout << "Empty list" << endl; 96 | return; 97 | } 98 | Node* temp = head; 99 | while(temp->next){ 100 | temp = temp->next; 101 | } 102 | while(temp){ 103 | cout << temp->data<< " -->"; 104 | temp = temp->prev; 105 | } 106 | cout << endl; 107 | } 108 | 109 | 110 | Node* delete_head(Node* &head){ 111 | if(head == NULL){ 112 | cout << "Empty List" << endl; 113 | return NULL; 114 | } 115 | 116 | if(head->next == NULL){ 117 | delete head; 118 | return NULL; 119 | } 120 | 121 | Node* temp = head->next; 122 | temp->prev = NULL; 123 | delete head; 124 | head = temp; 125 | return head; 126 | } 127 | 128 | Node* delete_tail(Node* &head){ 129 | if(head == NULL){ 130 | cout << "Empty List" << endl; 131 | return NULL; 132 | } 133 | 134 | if(head->next == NULL){ 135 | delete head; 136 | return NULL; 137 | } 138 | 139 | Node* temp = head; 140 | while(temp->next->next){ 141 | temp = temp->next; 142 | } 143 | temp->next->prev = NULL; 144 | delete temp->next; 145 | temp->next = NULL; 146 | return head; 147 | } 148 | 149 | Node* deletemiddle(Node* &head, int p){ 150 | if(p == 0){ 151 | delete_head(head); 152 | return head; 153 | } 154 | else if(p >= length_list(head)){ 155 | head = delete_tail(head); 156 | return head; 157 | } 158 | else{ 159 | int cnt = 0; 160 | Node* prev = NULL; 161 | Node* curr = head; 162 | while(cnt <= p-1) 163 | { 164 | prev = curr; 165 | curr = curr->next; 166 | cnt++; 167 | } 168 | 169 | prev->next = curr->next; 170 | if(curr->next) curr->next->prev = prev; 171 | return head; 172 | } 173 | } 174 | 175 | int main(){ 176 | Node* head = NULL; 177 | insert_front(head,2); 178 | insert_back(head,4); 179 | insert_back(head,6); 180 | insert_front(head,8); 181 | insert_back(head,10); 182 | insertInMiddle(head, 5,4); 183 | display(head); 184 | // head = delete_head(head); 185 | // head = delete_tail(head); 186 | head = deletemiddle(head, 4); 187 | display(head); 188 | display_rev(head); 189 | 190 | } -------------------------------------------------------------------------------- /Linked_List/input.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 1 3 | 2 4 | 4 5 | 15 6 | 8 7 | 14 -------------------------------------------------------------------------------- /Linked_List/input2.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 5 3 | 9 4 | 11 5 | 16 6 | 17 -------------------------------------------------------------------------------- /Queue/dequeue.c: -------------------------------------------------------------------------------- 1 | /* 2 | Here is the code of "Dequeue" that means deletion of elements from the 3 | queue and before deletion we have to check whether the queue is empty or not. 4 | 5 | Also deletion is done through "front" pointer. 6 | 7 | */ 8 | 9 | void dequeue(){ 10 | int element; 11 | if(isEmpty()){ 12 | printf("Queue is Empty!"); 13 | } 14 | else{ 15 | element = queue[front]; 16 | front++; 17 | return element; 18 | } 19 | } 20 | 21 | int isEmpty(){ // check condition for empty queue. 22 | if(rear < front){ 23 | return 1; 24 | } 25 | else{ 26 | return 0; 27 | } 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /Queue/dispqueue.c: -------------------------------------------------------------------------------- 1 | /* 2 | Let's display th elements of a Queue. 3 | 4 | Now, you already know that we have to check for the condition if the queue has elements or 5 | it is already empty. 6 | 7 | and the "isEmpty()" function, we have describe before. 8 | 9 | */ 10 | 11 | 12 | void display(){ 13 | int element; 14 | int i; 15 | if(isEmpty()){ 16 | printf("Queue is Empty!"); 17 | } 18 | 19 | else{ 20 | for(i=front; i<= rear; i++){ 21 | printf("%d",queue[i]); 22 | } 23 | } 24 | } 25 | 26 | /** 27 | We can also print the data in the reverse order i.e. from rear to front direction. 28 | Just reverse the loop. 29 | 30 | */ -------------------------------------------------------------------------------- /Queue/enqueue.c: -------------------------------------------------------------------------------- 1 | /* 2 | Queue is a type of Data Structure which follows the rule "FIFO" i.e. (First in First Out). 3 | Ex: A queue of people standing to buy a movie ticket. The first person enters to buy and will leave the 4 | queue first. 5 | 6 | Here insertion and deletion is done through two pointers called rear and front. 7 | "Rear" is used for insertion. 8 | "Front" is used for deletion. 9 | 10 | Also once the first element gets inserted the front pointer will be fixed and 11 | more insertions will be done through rear. 12 | 13 | 14 | */ 15 | 16 | 17 | #include 18 | #include 19 | 20 | #define max 100 21 | int queue[max]; 22 | 23 | int front = -1; // Initializing the pointers to -1 24 | int rear = -1; 25 | 26 | void enqueue( int element){ 27 | if(isFull()){ 28 | printf("Queue is Full!"); 29 | } 30 | else{ 31 | rear++; 32 | queue[rear] = element; 33 | } 34 | } 35 | 36 | int isFull(){ 37 | if(rear >= max-1){ 38 | return 1; 39 | } 40 | elase{ 41 | return 0; 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Algorithms 3 |
4 |
5 |

6 | 7 |

8 |
9 | Data_Structure_n_Algorithms! 10 |
11 |

12 | 13 | ------ 14 | 15 | * Programming Problems Based On Data_Structure and Algorithms consisting of basic to intermediate level problems 16 | 17 | * PRs are welcome. To begin developing, follow the structure: 18 | 19 | > Data_Structure_or_Algorithm_name/file_name.extension 20 | e.g 21 | > Linked_List/Cycle_Detection.c 22 | 23 | ------ 24 | 25 | # Contribution Guidelines! 26 | 27 | Please ensure your `pull request` follow to the below guidelines: 28 | - Contributions are always welcome. Language doesn't matter. Just make sure you're implementing an algorithm.. 29 | - Please be sure that program is compiling without an Error or warning before doing Pull Request.. 30 | - Please Include Comments in the code about your changes you done there.. 31 | - Feel free to suggest any improvements to be done here in Issues.. 32 | 33 | Appreciate your contributions and Thank You in Advance 34 | 35 | 36 | > Hope you will enjoy working on this repo, it will be great fun for beginners of Data Structure! 37 | 38 | ---- 39 |

40 | 41 | Data_Structure_n_Algorithms! is licensed under the MIT License 42 | 43 |

44 |

45 | Copyright (c) 2018 Archit 46 |

47 | -------------------------------------------------------------------------------- /Stacks/Implementing_two_stack_in_one_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define size 5 5 | int top1 = -1; 6 | int top2 = size; 7 | 8 | // Here if top1 and top2 are together then stack is Full otherwise 9 | // For stack 1 enter element from front 10 | // For stack 2 enter element from back 11 | 12 | int push(int *A,int ele,int stack) { 13 | if(abs(top1-top2)!=1) { 14 | if(stack==1) { 15 | top1++; 16 | A[top1]=ele; 17 | } 18 | else if(stack==2) 19 | { 20 | top2--; 21 | A[top2]=ele; 22 | } 23 | else { 24 | return 0; 25 | } 26 | } 27 | else { 28 | printf("\nArray is Full\n"); 29 | return -1; 30 | } 31 | } 32 | 33 | // Here if top is not Null then pop the element 34 | // from front for stack 1 35 | // from end for stack 2 36 | 37 | int pop(int *A,int stack,int ni) { 38 | if(stack==1) { 39 | if(top1==-1) { 40 | printf("Error: Underflow\n"); 41 | } 42 | else 43 | { 44 | return(A[top1--]); 45 | } 46 | } 47 | else if(stack==2) { 48 | if(top2==ni) 49 | printf("Error: Underflow\n"); 50 | else { 51 | return(A[top2++]); 52 | } 53 | } 54 | } 55 | 56 | // Here if stack 1 is not empty print Element in stack 57 | // similar for stack 2 58 | 59 | void display(int *A,int stack) { 60 | int i; 61 | if(stack==1) { 62 | if(top1==-1) { 63 | return; 64 | } 65 | else { 66 | printf("| %d |\n",A[top1--]); 67 | printf("|___|\n"); 68 | display(A,1); 69 | } 70 | } 71 | if(stack==2) 72 | { 73 | if(top2==5) { 74 | return; 75 | } 76 | else { 77 | printf("| %d |\n",A[top2++]); 78 | printf("|___|\n"); 79 | display(A,2); 80 | } 81 | } 82 | } 83 | 84 | 85 | 86 | int main() 87 | { 88 | int n,ele,stack,sd,ch; 89 | int *A=(int*)malloc(size*sizeof(int)); 90 | do 91 | { 92 | printf("Enter stack 1 / 2 : \n"); 93 | scanf("%d",&stack); 94 | printf("Enter element (For quit pushing Elements = -1): \n"); 95 | scanf("%d",&ele); 96 | if(ele==-1) { 97 | break; 98 | } 99 | int ans=push(A,ele,stack); 100 | printf("Want to pop :1 / else: 0: \n"); 101 | scanf("%d",&ch); 102 | if(ch==1) { 103 | printf("\nFrom which stack You want to pop: \n"); 104 | scanf("%d",&stack); 105 | pop(A,stack,5); 106 | } 107 | if(ans==-1) { 108 | break; 109 | } 110 | } 111 | while(ele!=-1); 112 | printf("\nstack 1: \n"); 113 | display(A,1); 114 | printf("\nstack 2: \n"); 115 | display(A,2); 116 | } 117 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /media/Data_Structure_n_Algorithms.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gargarchit/Data_Structure_n_Algorithms/2dcd5cd844103fc0badaa4a07fbfd789717d54b2/media/Data_Structure_n_Algorithms.png --------------------------------------------------------------------------------