├── README.md ├── CP-ALGORITHMS ├── unique_bitwise_2n+2.exe ├── unique_bitwise_2n+1.cpp ├── optimised_EXPonent.cpp ├── minecraftpyramid.c ├── subset_bitwise.cpp ├── bin.cpp ├── primeSieve.cpp ├── primeNoSUBETS.cpp ├── graph_adjacency_list.cpp ├── extendedGCD.cpp ├── Factorize+optimizedFactorize.cpp ├── bigFACTORIAL.cpp ├── linearDiophotineEquation_solution.cpp ├── factorizationBYsieve.cpp ├── unique_bitwise_2n+2.cpp ├── MMI.cpp ├── BFS.cpp ├── graph_connectedComponent.cpp ├── priority_queue_pairs.cpp ├── graph_loopExistence.cpp ├── unique_3N+1.CPP ├── subtree_count.cpp ├── max_DepthOfTree.cpp ├── bigINT_addition.cpp ├── LCA.cpp ├── Segemented_sieve.cpp ├── Finding_Multiple_Missing_Element_In_An_Array.cpp └── bitwise.cpp ├── swapping.c ├── max-number-in-array.c ├── CountDigit.c ├── gcd.c ├── print.c ├── Distance2points.cpp ├── BinaryExponentiation.cpp ├── negative positive.c ├── palindrome.c ├── large-num-in-array.c ├── active selection.c ├── .github └── workflows │ └── lockdown.yml ├── Fibonacci series.c ├── Upper case to lower case.c ├── strrev.c ├── SelectionSort.c ├── count_of_keys.cpp ├── GetAnagramDifference ├── Large_factorials.c ├── doubly linkedlist ├── remove_duplicates_of_length_k.cpp ├── insertion_sort.cpp ├── bubble_sort.cpp ├── Determinant_of_matrix.cpp ├── swap_array.c ├── Numbers_lessthanorequal_to_target.cpp ├── searching_element_in_array.c ├── Maximum subarray sum.cpp ├── boundedBuffer.c ├── longest_sequence_consecutive_zeroes.c ├── BInary_search.c ├── Radix_Sort.c ├── nextPermutations.cpp ├── BinarySearch.cpp ├── bellman-ford.cpp ├── CircularLinkedList.c ├── topological_sort_NISHANT.cpp ├── checksum1.c ├── bipartite_check_nishant.cpp ├── Stack_Infix_to_Postfix.c ├── prims.c ├── Flatten binary tree to linked list.cpp ├── permutations.c ├── Post-Fix_expression_evaluation.c ├── kosaraju's_algo.cpp ├── Valid_Parenthesis.c ├── thread.c ├── ReverseLinkedList (1).c ├── AddingElementsLinkedLIst.c ├── merge_2array.cpp ├── 4sum.cpp ├── Stacks.c ├── bst_Inorder_recursive_traversal ├── matrix_multiplication.c ├── Employee.c ├── fractional-knapsack.c ├── Circular_Queue.c ├── Inversion-pair.c ├── Generic LinkedList.c ├── Zigzag order traversal.cpp ├── MergeSort.c ├── Dining_Philosopher.c ├── quick.c ├── multipleparentheses.c ├── Matrix_Chain_Multiplication_Dynamic_Programming.c ├── Adding two polynomials.c ├── kruska.c ├── bfs.c ├── BST.c ├── binary_tree.cpp ├── DEQueue-using-array.c ├── linkedlist.cpp ├── restaurant_bill.c ├── bst.cpp └── sorting.c /README.md: -------------------------------------------------------------------------------- 1 | # Contributions to this repository are closed for now. 2 | 3 | All new Pull Requests will be closed automatically. 4 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/unique_bitwise_2n+2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iamsidofficial/c-programs-for-college-students/HEAD/CP-ALGORITHMS/unique_bitwise_2n+2.exe -------------------------------------------------------------------------------- /swapping.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a=10, b=20; 5 | printf("Before swap a=%d b=%d",a,b); 6 | a=a+b;//a=30 (10+20) 7 | b=a-b;//b=10 (30-20) 8 | a=a-b;//a=20 (30-10) 9 | printf("\nAfter swap a=%d b=%d",a,b); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /max-number-in-array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 100 4 | 5 | int max(int arr[N]){ 6 | int max = 0; 7 | for(int i=0;i max){ 9 | max = arr[i]; } 10 | } 11 | printf("%d",max); 12 | } 13 | 14 | int main(){ 15 | int arr[N]= {214,2134,3456,54}; 16 | max(arr); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /CountDigit.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n, temp, count=0; 5 | printf("Enter a number: \n"); 6 | scanf("%d", &n); 7 | temp=n; 8 | while(n!=0) { 9 | n/=10; 10 | count++; 11 | } 12 | printf("Number of digits in %d is %d", temp, count); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/unique_bitwise_2n+1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int arr[7]={1,2,1,9,2,5,5}; 6 | int n=sizeof(arr)/sizeof(arr[0]); 7 | int cxor=arr[0]; 8 | for(int i=1;i 2 | int hcf(int n1, int n2); 3 | int main() { 4 | int n1, n2; 5 | printf("Enter two positive integers: "); 6 | scanf("%d %d", &n1, &n2); 7 | printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2)); 8 | return 0; 9 | } 10 | 11 | int hcf(int n1, int n2) { 12 | if (n2 != 0) 13 | return hcf(n2, n1 % n2); 14 | else 15 | return n1; 16 | } 17 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/optimised_EXPonent.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int power(int a,int b){ 6 | int resultant=1; 7 | while(b){ 8 | if(b&1){ 9 | resultant=resultant*a; 10 | } 11 | a*=a; 12 | b=b>>1; 13 | } 14 | return resultant; 15 | } 16 | int main(){ 17 | int a,b; 18 | cin>>a>>b; 19 | cout< 2 | #include 3 | 4 | int main(void) 5 | { 6 | int i; 7 | printf("---------------------------------------------------\n"); 8 | printf("Enter a number from which you want to count:\t"); 9 | scanf("%d", &i); 10 | printf("\n---------------------------------------------------\n"); 11 | 12 | for (i; i <= 100; i++) 13 | { 14 | 15 | printf("%d\n", i); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Distance2points.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Function to calculate distance 5 | float distance(int x1, int y1, int x2, int y2) 6 | { 7 | // Calculating distance 8 | return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0); 9 | } 10 | 11 | // Drivers Code 12 | int main() 13 | { 14 | printf("%f", distance(3, 4, 4, 3)); 15 | return 0; 16 | } 17 | 18 | // This code is contributed by Aditya Kumar (adityakumar129) 19 | -------------------------------------------------------------------------------- /BinaryExponentiation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long power(long long a, long long b, int mod) 5 | { long long result = 1; 6 | while(b) { 7 | if(b%2) result=(result*a)%mod; 8 | a=(a*a)%mod; 9 | b/=2; 10 | } 11 | return result; 12 | } 13 | 14 | int main() 15 | { 16 | int a = 20, b = 2000000; 17 | int mod = 1e9+7; 18 | cout< 2 | #include 3 | 4 | // Number of blocks in a minecraft pyramid 5 | 6 | int main(void){ 7 | int levels = 0; 8 | int calc = 0; 9 | int sum = 0; 10 | 11 | printf("Number of levels: "); 12 | scanf(" %d", &levels); 13 | 14 | for(int n = 1; n <= levels; n++){ 15 | calc = 4*pow(n,2); 16 | printf("\n%d", calc); 17 | sum = sum + calc; 18 | } 19 | 20 | printf("\n\nSum: %d", sum); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /negative positive.c: -------------------------------------------------------------------------------- 1 | /* Description: A program to check whether the input 2 | * integer number is positive or negative. 3 | */ 4 | #include 5 | 6 | void main() 7 | { 8 | int num; 9 | 10 | printf("Enter a number: \n"); 11 | scanf("%d", &num); 12 | if (num > 0) 13 | printf("%d is a positive number \n", num); 14 | else if (num < 0) 15 | printf("%d is a negative number \n", num); 16 | else 17 | printf("0 is neither positive nor negative"); 18 | } 19 | -------------------------------------------------------------------------------- /palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | char str[20]; 5 | int n,i,flag=0; 6 | printf("Enter the string:"); 7 | gets(str); 8 | n=strlen(str); /*calculate string length*/ 9 | /* Loop to check the characters*/ 10 | for(i=0;i 2 | using namespace std; 3 | 4 | void overlaynumber(string s,int number){ 5 | int j=0; 6 | while(number>0){ 7 | if(number&1){ 8 | cout<>1; 12 | } 13 | } 14 | void subset(string s){ 15 | int n=s.size(); 16 | for(int i=0;i<(1< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | mapm; 11 | for(int i=0;i>x; 14 | m[x]++; 15 | } 16 | vector>v; 17 | int i=0; 18 | for(auto it:m){ 19 | v[i].first=it.first; 20 | v[i].second=it.second; 21 | i++; 22 | 23 | } 24 | 25 | } 26 | 27 | 28 | 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /large-num-in-array.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | int main() 3 | { 4 | int num[5],max; 5 | printf("Enter numbers\n"); 6 | for (int i = 0; i < 5; i++) 7 | { 8 | scanf("%d", &num[i]); 9 | } 10 | printf("You entered numbers!\n"); 11 | for (int i = 0; i < 5; i++) 12 | { 13 | printf("%d\n", num[i]); 14 | } 15 | for (int i = 0; i < 5; i++) 16 | { 17 | while (max < num[i]) 18 | { 19 | max = num[i]; 20 | } 21 | } 22 | printf("Max number is %d\n", max); 23 | } 24 | -------------------------------------------------------------------------------- /active selection.c: -------------------------------------------------------------------------------- 1 | #include 2 | void printMaxActivities(int start[], int finish[], int n) 3 | { 4 | int i, j; 5 | 6 | printf ("Selected activities are n"); 7 | 8 | i = 0; 9 | printf("%d ", i); 10 | 11 | for (j = 1; j < n; j++) 12 | { 13 | if (start[j] >= finish[i]) 14 | { 15 | printf ("%d ", j); 16 | i = j; 17 | } 18 | } 19 | } 20 | 21 | int main() 22 | { 23 | int start[] = {1, 3, 0, 5, 8, 5}; 24 | int finish[] = {2, 4, 6, 7, 9, 9}; 25 | int n = sizeof(start)/sizeof(start[0]); 26 | printMaxActivities(start, finish, n); 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /.github/workflows/lockdown.yml: -------------------------------------------------------------------------------- 1 | name: 'Repo Lockdown' 2 | 3 | on: 4 | workflow_dispatch: 5 | issues: 6 | types: opened 7 | pull_request_target: 8 | types: opened 9 | 10 | permissions: 11 | issues: write 12 | pull-requests: write 13 | 14 | jobs: 15 | action: 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: dessant/repo-lockdown@v2 19 | with: 20 | github-token: ${{ github.token }} 21 | pr-comment: > 22 | This repository does not accept pull requests, 23 | see the README for details. 24 | skip-closed-pr-comment: true 25 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/primeSieve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define N 1000000 5 | 6 | void primeSieve(vector&prime){ 7 | prime[0]=prime[1]=0; 8 | for(long long i=2;i<=N;i++){ 9 | if(prime[i]==1){ 10 | for(long long j=i*i;j<=N;j=j+i){ 11 | prime[j]=0; 12 | } 13 | } 14 | } 15 | 16 | } 17 | int main(){ 18 | int n; 19 | cin>>n; 20 | vector prime(N,1); 21 | primeSieve(prime); 22 | for(int i=0;i<=n;i++){ 23 | if(prime[i]==1){ 24 | cout< 2 | void printFibonacci(int n){ 3 | static unsigned long n1=0,n2=1,n3; 4 | if(n>0){ 5 | n3 = n1 + n2; 6 | n1 = n2; 7 | n2 = n3; 8 | printf("%lu ",n3); 9 | printFibonacci(n-1); 10 | } 11 | } 12 | int main(){ 13 | int n; 14 | printf("Enter the number of elements: "); 15 | scanf("%d",&n); 16 | printf("Fibonacci Series: "); 17 | printf("%d %d ",0,1); 18 | printFibonacci(n-2);//n-2 because 2 numbers are already printed 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/primeNoSUBETS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | 8 | vector primes={2,3,5,7,11,13,17,23,29};//10 prime numbers 9 | int m=primes.size(); 10 | int ans=0; 11 | for(int i=1;i<(1<>j)&1){ 15 | //cout< 2 | 3 | #include 4 | 5 | int main(){ 6 | 7 | /* This array can hold a string of upto 25 8 | 9 | * chars, if you are going to enter larger string 10 | 11 | * then increase the array size accordingly 12 | 13 | */ 14 | 15 | char str[25]; 16 | 17 | int i; 18 | 19 | printf("Enter the string: "); 20 | 21 | scanf("%s",str); 22 | 23 | 24 | 25 | for(i=0;i<=strlen(str);i++){ 26 | 27 | if(str[i]>=65&&str[i]<=90) 28 | 29 | str[i]=str[i]+32; 30 | 31 | } 32 | 33 | printf("\nLower Case String is: %s",str); 34 | 35 | return 0; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/graph_adjacency_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N=1e5+10; 5 | vector g[N]; 6 | bool vis[N]; 7 | 8 | void dfs(int vertex){ 9 | cout<>n>>m; 24 | //vertices=n edges=m 25 | for(int i=0;i>a>>b; 28 | g[a].push_back(b); 29 | g[b].push_back(a); 30 | 31 | } 32 | 33 | dfs(1); 34 | 35 | 36 | 37 | 38 | 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /strrev.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // declaring recursive function 5 | char* reverse(char* str); 6 | 7 | void main() 8 | { 9 | int i, j, k; 10 | char str[100]; 11 | char *rev; 12 | printf("Enter the string:\t"); 13 | scanf("%s", str); 14 | printf("The original string is: %s\n", str); 15 | rev = reverse(str); 16 | printf("The reversed string is: %s\n", rev); 17 | getch(); 18 | } 19 | 20 | // defining the function 21 | char* reverse(char *str) 22 | { 23 | static int i = 0; 24 | static char rev[100]; 25 | if(*str) 26 | { 27 | reverse(str+1); 28 | rev[i++] = *str; 29 | } 30 | return rev; 31 | } -------------------------------------------------------------------------------- /CP-ALGORITHMS/extendedGCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int gcd(int a,int b){ 5 | if(b==0){ 6 | return a; 7 | } 8 | 9 | return gcd(b,a%b); 10 | } 11 | 12 | //To solve ax+by=gcd(a,b) 13 | vector extendedGCD(int a,int b){ 14 | if(b==0){ 15 | return {1,0}; 16 | } 17 | vector result=extendedGCD(b,a%b); 18 | 19 | //After recursions 20 | int smallX=result[0]; 21 | int smallY=result[1]; 22 | 23 | int x=smallY; 24 | int y=smallX-(a/b)*smallY; 25 | return {x,y}; 26 | } 27 | 28 | int main(){ 29 | int a,b; 30 | cin>>a>>b; 31 | 32 | vectorv=extendedGCD(a,b); 33 | 34 | cout< 2 | using namespace std; 3 | 4 | 5 | void factorize(int n){ 6 | for(int i=2;i<=n;i++){ 7 | int ct=0; 8 | if(n%i==0){ 9 | while(n%i==0){ 10 | ct++; 11 | n=n/i; 12 | } 13 | } 14 | cout<>n; 38 | optimizedFactorize(n); 39 | 40 | 41 | 42 | 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /SelectionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | 5 | int a[20],i,j,n,temp,s,pos; 6 | printf("Enter n"); 7 | scanf("%d",&n); 8 | printf("Enter elements in the array"); 9 | for(i=0;i 2 | #include 3 | using namespace std; 4 | /* 5 | cpp program to find the count of each key(string) in a given register using hashmap 6 | Note that this program is case-sensitive 7 | Author: Chandana 8 | */ 9 | int main(){ 10 | int n; 11 | string x; 12 | unordered_map um; 13 | cout<<"Enter the size of the register"<>n; 15 | vector v(n); 16 | cout<<"Enter the name of each entity"<>x; 19 | v[i]=x; 20 | um[x]++; 21 | } 22 | for(auto x:um){ 23 | cout<<"Number of entities named as "< 2 | using namespace std; 3 | 4 | 5 | void multiply(vector&a,int no,int &size){ 6 | int carry=0; 7 | for(int i=0;i a(1000,0); 25 | a[0]=1;//1! 26 | int size=1; 27 | for(int i=2;i<=n;i++){ 28 | multiply(a,i,size); 29 | } 30 | 31 | for(int i=size-1;i>=0;i--){ 32 | cout<>n; 40 | bigFactorial(n); 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /CP-ALGORITHMS/linearDiophotineEquation_solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int gcd(int a,int b){ 5 | if(b==0) return a; 6 | return gcd(b,a%b); 7 | } 8 | 9 | vector extendedGCD(int a,int b){ 10 | if(b==0) return {1,0}; 11 | 12 | vectorresult=extendedGCD(b,a%b); 13 | int x=result[1]; 14 | int y=result[0]-(a/b)*result[1]; 15 | return {x,y}; 16 | } 17 | 18 | int main(){ 19 | 20 | int a,b,c; 21 | cin>>a>>b>>c; 22 | int g=gcd(a,b); 23 | int k=c/g; 24 | if(c%g!=0) cout<<"No solution"<result=extendedGCD(a,b); 29 | 30 | int x=result[0]*k; 31 | int y=result[1]*k; 32 | cout< 2 | using namespace std; 3 | #define N 1000000 4 | 5 | vector v(N,1); 6 | vector p(N,0); 7 | 8 | void primeSieve(vector &v,vector &p){ 9 | v[0]=v[1]=0; 10 | for(long long i=2;i 2 | #include 3 | //program to find minimum number of changes needed in string s2 to make it an anagram of string s1, the strings consists of lowercase alphabets only 4 | 5 | int getAnagramDifference(std::string s1, std::string s2){ 6 | 7 | int freq[26]{}; 8 | 9 | for(char c: s1) freq[c - 'a']++; 10 | for(char c: s2) freq[c - 'a']--; 11 | int ans = 0; 12 | for(auto i: freq) i <= 0 ?: ans += i; 13 | 14 | return ans; 15 | 16 | } 17 | 18 | int main() 19 | { 20 | std::string s1, s2; 21 | std::cin >> s1 >> s2; 22 | 23 | std::cout << "Minimum Anagram difference for the given strings is " << getAnagramDifference(s1, s2) << std::endl; 24 | 25 | return 0; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/unique_bitwise_2n+2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int arr[8]={1,2,1,2,3,5,3,9}; 6 | int n=sizeof(arr)/sizeof(arr[0]); 7 | //XOR 8 | int cxor=0; 9 | for(int i=0;i>1; 17 | } 18 | 19 | // Filter out the numbers from the array which have set bit at 'pos' 20 | int setA=0; 21 | int setB=0; 22 | int mask=1<0){ 26 | setA=setA^arr[i]; 27 | } 28 | else{ 29 | setB=setB^arr[i]; 30 | } 31 | } 32 | 33 | cout< 2 | using namespace std; 3 | 4 | int gcd(int a,int b){ 5 | if(b==0){ 6 | return a; 7 | } 8 | 9 | return gcd(b,a%b); 10 | } 11 | 12 | //To solve ax+by=gcd(a,b) 13 | vector extendedGCD(int a,int b){ 14 | if(b==0){ 15 | return {1,0}; 16 | } 17 | vector result=extendedGCD(b,a%b); 18 | 19 | //After recursions 20 | int smallX=result[0]; 21 | int smallY=result[1]; 22 | 23 | int x=smallY; 24 | int y=smallX-(a/b)*smallY; 25 | return {x,y}; 26 | } 27 | 28 | int MMI(int a,int m){ 29 | if(gcd(a,m)!=1){ 30 | return -1; 31 | } 32 | 33 | vectorans=extendedGCD(a,m); 34 | return (ans[0]%m+m)%m;//to make result positive 35 | } 36 | int main(){ 37 | int a,m; 38 | cin>>a>>m; 39 | cout< 2 | #include 3 | 4 | #define MAX 600 5 | 6 | int res[MAX]; 7 | int multi(int a[],int,int); 8 | 9 | 10 | int main(int argc, char const *argv[]) 11 | { 12 | int n,size=1; 13 | res[0]=1; 14 | scanf("%d",&n); 15 | for (int i = 2; i <=n ; ++i) 16 | { 17 | size = multi(res,i,size); 18 | } 19 | for (int i = size-1; i >=0; --i) 20 | { 21 | printf("%d",res[i] ); 22 | } 23 | printf("\n"); 24 | return 0; 25 | } 26 | 27 | 28 | int multi(int a[],int x,int size) 29 | { 30 | int carry=0,product; 31 | for (int i = 0; i 2 | using namespace std; 3 | struct Node { 4 | int data; 5 | struct Node *prev; 6 | struct Node *next; 7 | }; 8 | struct Node* head = NULL; 9 | void insert(int newdata) { 10 | struct Node* newnode = (struct Node*) malloc(sizeof(struct Node)); 11 | newnode->data = newdata; 12 | newnode->prev = NULL; 13 | newnode->next = head; 14 | if(head != NULL) 15 | head->prev = newnode ; 16 | head = newnode; 17 | } 18 | void display() { 19 | struct Node* ptr; 20 | ptr = head; 21 | while(ptr != NULL) { 22 | cout<< ptr->data <<" "; 23 | ptr = ptr->next; 24 | } 25 | } 26 | int main() { 27 | insert(3); 28 | insert(1); 29 | insert(7); 30 | insert(2); 31 | insert(9); 32 | cout<<"The doubly linked list is: "; 33 | display(); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /remove_duplicates_of_length_k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | string s; 8 | int k; 9 | cout<<"Enter the string and the value of k"; 10 | cin>>s>>k; 11 | 12 | vector>v; 13 | 14 | for(char c:s) 15 | { 16 | if(v.empty() || v.back().first !=c) 17 | { 18 | v.push_back(make_pair(c,1)); 19 | 20 | } 21 | else 22 | { 23 | v.back().second++; 24 | if(v.back().second == k) 25 | v.pop_back(); 26 | } 27 | } 28 | 29 | string result = ""; 30 | for(auto res: v) 31 | { 32 | result.append(res.second,res.first); 33 | } 34 | 35 | cout< 8 | using namespace std; 9 | 10 | void insertionshort(int a[],int n){ 11 | 12 | for (int i = 1; i < n; i++) 13 | { 14 | int j=i-1; 15 | int x=a[i]; 16 | while (j>=0 && a[j]>x) 17 | { 18 | a[j+1]=a[j]; 19 | j--; 20 | } 21 | a[j+1]=x; 22 | 23 | 24 | } 25 | 26 | } 27 | 28 | int main(){ 29 | 30 | cout<<"enter the number of element:"; 31 | int n; 32 | cin>>n; 33 | int a[n]; 34 | cout<>a[i]; 38 | } 39 | 40 | insertionshort(a , n); 41 | cout< 2 | using namespace std; 3 | 4 | void swap(int *xp, int *yp) 5 | { 6 | int temp = *xp; 7 | *xp = *yp; 8 | *yp = temp; 9 | } 10 | 11 | void bubbleSort(int arr[], int n) 12 | { 13 | int i, j; 14 | for (i = 0; i < n-1; i++) 15 | 16 | for (j = 0; j < n-i-1; j++) 17 | if (arr[j] > arr[j+1]) 18 | swap(&arr[j], &arr[j+1]); 19 | } 20 | 21 | void printArray(int arr[], int size) 22 | { 23 | int i; 24 | for (i=0; i < size; i++) 25 | printf("%d ", arr[i]); 26 | printf("\n"); 27 | } 28 | 29 | int main() 30 | { 31 | int arr[500],n,i; 32 | 33 | cout<<"Enter the numbber of elements: "; 34 | cin>>n; 35 | 36 | cout<<"Enter the numbers:\n"; 37 | for(i=0;i>arr[i]; 39 | 40 | bubbleSort(arr, n); 41 | cout<<"Sorted array: \n"; 42 | printArray(arr, n); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /CP-ALGORITHMS/BFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N=1e5+10; 4 | vectorg[N]; 5 | int visited[N]; 6 | int level[N]; 7 | 8 | void bfs(int source){ 9 | queueq; 10 | q.push(source); 11 | visited[source]=1; 12 | 13 | while(!q.empty()){ 14 | 15 | int current_v=q.front(); 16 | cout<>n; 36 | for(int i=0;i>a>>b; 39 | g[a].push_back(b); 40 | g[b].push_back(a); 41 | } 42 | bfs(1); 43 | for(int i=1;i<=n;i++){ 44 | cout< 2 | using namespace std; 3 | const int N=1e5+10; 4 | vectorg[N]; 5 | int visited[N]={0}; 6 | vector>cc; 7 | vectorcurrent_cc; 8 | void dfs(int vertex){ 9 | current_cc.push_back(vertex); 10 | //cout<>m>>n; 20 | for(int i=0;i>a>>b; 23 | g[a].push_back(b); 24 | g[b].push_back(a); 25 | } 26 | int ct=0; 27 | for(int i=1;i<=m;i++){ 28 | if(visited[i]) continue; 29 | current_cc.clear(); 30 | dfs(i); 31 | cc.push_back(current_cc); 32 | ct++; 33 | } 34 | for(auto c1:cc){ 35 | for(auto c2:c1){ 36 | cout< 2 | using namespace std; 3 | int mod(int x,int y){ 4 | if(x-y<0){ 5 | return y-x; 6 | } 7 | return x-y; 8 | } 9 | vector findClosestElements(vector arr, int k, int x) { 10 | vectorans; 11 | priority_queue,vector>, greater>>q; 12 | int n=arr.size(); 13 | for(int i=0;iv){ 29 | for(int i=0;iarr={1,2,3,4,5}; 37 | print(findClosestElements(arr,4,3)); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Determinant_of_matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int det(int**a,int n){ 4 | 5 | if(n==1) return a[0][0]; 6 | int sum=0; 7 | for(int c=0;c>n; 25 | int**a; a=new int*[n]; 26 | for(int i=0;i>a[i][j]; 30 | 31 | cout<<"\nDeterminant is "< 2 | 3 | void swap(int arr1[], int arr[], int len); 4 | 5 | int main(int argc, const char * argv[]) { 6 | 7 | int arr1[6] = {1, 2, 3, 4, 5, 6}; 8 | int arr2[6] = {99, 88, 77, 66, 55, 44}; 9 | 10 | swap(arr1, arr2, 6); 11 | 12 | return 0; 13 | } 14 | 15 | void swap(int arr1[], int arr2[], int len) 16 | { 17 | int tmp; // create a tmp integer to stpre a value. 18 | 19 | for(int i = 0; i < len; i++) 20 | { 21 | tmp = arr1[i]; 22 | arr1[i] = arr2[i]; 23 | arr2[i] = tmp; 24 | } 25 | 26 | printf("First Array after the swap:\n"); 27 | //print arr1 28 | for(int i = 0; i < len; i++) 29 | { 30 | printf("%d ", arr1[i]); 31 | } 32 | 33 | printf("\n\nSecond Array after the swap:\n"); 34 | //print arr2 35 | for(int i = 0; i < len; i++) 36 | { 37 | printf("%d ", arr2[i]); 38 | } 39 | printf("\n"); 40 | } 41 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/graph_loopExistence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N=1e5+10; 4 | vectorg[N]; 5 | int visited[N]={0}; 6 | vector>cc; 7 | vectorcurrent_cc; 8 | int dfs(int vertex,int par){ 9 | current_cc.push_back(vertex); 10 | //cout<>m>>n; 25 | for(int i=0;i>a>>b; 28 | g[a].push_back(b); 29 | g[b].push_back(a); 30 | } 31 | 32 | for(int i=1;i<=m;i++){ 33 | if(visited[i]) continue; 34 | int a=0; 35 | a=dfs(i,0); 36 | if(a==1) { 37 | cout<<"YES"< 2 | using namespace std; 3 | 4 | void updateArr(vector &v,int x){ 5 | for(int i=0;i<32;i++){ 6 | int ithbit=x&(1<sumArr){ 14 | int v=0; 15 | int p=1; 16 | for(int i=0;i<32;i++){ 17 | v+=sumArr[i]*(1<arr){ 23 | vector sumArr(32,0); 24 | 25 | for(int i=0;iv){ 36 | for(int i=0;iv={1,1,1,2,2,2,3,3,3,9}; 44 | cout< 2 | #include 3 | using namespace std; 4 | /* 5 | cpp program to find the count of numbers in an array of numbers less than or equal to a given target (implemented using binary search algorithm) 6 | Author: Chandana 7 | */ 8 | int main(){ 9 | int n,k,x; 10 | cout<<"Enter the size of the array"<>n; 12 | vector v(n); 13 | cout<<"Enter the elements of the array"<>x; 16 | v[i]=x; 17 | } 18 | cout<<"Enter your target"<>k; 20 | sort(v.begin(),v.end()); 21 | int l=0,h=n-1,ans=-1,mid; 22 | while(l<=h){ 23 | mid=h-(h-l)/2; 24 | if(k>=v[mid]){ 25 | l=mid+1; 26 | ans=mid; 27 | } 28 | else{ 29 | h=mid-1; 30 | } 31 | } 32 | cout<<"Number of numbers less than or equal to your target "< 2 | void main() 3 | { 4 | 5 | int array[20]; 6 | int i, low, mid, high, key, size; 7 | 8 | printf("Enter the size of an array\n"); 9 | scanf("%d", &size); 10 | 11 | printf("Enter the array elements\n"); 12 | for (i = 0; i < size; i++) 13 | { 14 | scanf("%d", &array[i]); 15 | } 16 | 17 | printf("Enter the key\n"); 18 | scanf("%d", &key); 19 | 20 | /* search begins */ 21 | 22 | low = 0; 23 | high = (size - 1); 24 | 25 | while (low <= high) 26 | { 27 | mid = (low + high) / 2; 28 | 29 | if (key == array[mid]) 30 | { 31 | printf("SUCCESSFUL SEARCH\n"); 32 | return; 33 | } 34 | 35 | if (key < array[mid]) 36 | high = mid - 1; 37 | 38 | else 39 | low = mid + 1; 40 | 41 | } 42 | 43 | printf("UNSUCCESSFUL SEARCH\n"); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/subtree_count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N=1e5+10; 5 | vectorg[N]; 6 | int sum[N]; 7 | int depth[N],height[N]; 8 | int even_count[N]; 9 | void dfs(int vertex,int par){ 10 | /* 11 | Taking action on child before entering the child node 12 | */ 13 | if(vertex%2==0) even_count[vertex]++; 14 | sum[vertex]+=vertex; 15 | //cout<>n; 39 | for(int i=0;i>a>>b; 42 | g[a].push_back(b); 43 | g[b].push_back(a); 44 | } 45 | dfs(1,0); 46 | for(int i=1;i<=n;i++){ 47 | cout< 2 | #include 3 | 4 | int mutex=1,full=0,empty=3,x=0; 5 | 6 | int main() 7 | { 8 | int n; 9 | void producer(); 10 | void consumer(); 11 | int wait(int); 12 | int signal(int); 13 | printf("\n1.Producer\n2.Consumer\n3.Exit"); 14 | while(1) 15 | { 16 | printf("\nEnter your choice:"); 17 | scanf("%d",&n); 18 | switch(n) 19 | { 20 | case 1: if((mutex==1)&&(empty!=0)) 21 | producer(); 22 | else 23 | printf("Buffer is full!!"); 24 | break; 25 | case 2: if((mutex==1)&&(full!=0)) 26 | consumer(); 27 | else 28 | printf("Buffer is empty!!"); 29 | break; 30 | case 3: 31 | exit(0); 32 | break; 33 | } 34 | } 35 | return 0; 36 | } 37 | 38 | int wait(int s) 39 | { 40 | return (--s); 41 | } 42 | 43 | int signal(int s) 44 | { 45 | return(++s); 46 | } 47 | 48 | void producer() 49 | { 50 | mutex=wait(mutex); 51 | full=signal(full); 52 | empty=wait(empty); 53 | x++; 54 | printf("\nProducer produces the item %d",x); 55 | mutex=signal(mutex); 56 | } 57 | 58 | void consumer() 59 | { 60 | mutex=wait(mutex); 61 | full=wait(full); 62 | empty=signal(empty); 63 | printf("\nConsumer consumes item %d",x); 64 | x--; 65 | mutex=signal(mutex); 66 | } -------------------------------------------------------------------------------- /longest_sequence_consecutive_zeroes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int getLongestSequence(long long int num){ 5 | int binary[1001],binInd=0; 6 | while(num>0){ 7 | binary[binInd++]=num%2; 8 | num/=2; 9 | } 10 | binary[binInd++]=1; 11 | int maxLen=-1001,currLen=0; 12 | for(int index=0;indexmaxLen){ 18 | maxLen=currLen; 19 | } 20 | currLen=0; 21 | } 22 | } 23 | return maxLen; 24 | } 25 | 26 | int main() 27 | { 28 | int N; 29 | scanf("%d",&N); 30 | long long int arr[N],maxLen=-1001; 31 | for(int index=0;indexmaxLen){ 35 | maxLen=currLen; 36 | } 37 | } 38 | for(int index=N-1;index>=0;index--){ 39 | if(getLongestSequence(arr[index])==maxLen){ 40 | printf("%lld ",arr[index]); 41 | } 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /BInary_search.c: -------------------------------------------------------------------------------- 1 | // C program to implement recursive Binary Search 2 | #include 3 | 4 | // A recursive binary search function. It returns 5 | // location of x in given array arr[l..r] is present, 6 | // otherwise -1 7 | int binarySearch(int arr[], int l, int r, int x) 8 | { 9 | if (r >= l) { 10 | int mid = l + (r - l) / 2; 11 | 12 | // If the element is present at the middle 13 | // itself 14 | if (arr[mid] == x) 15 | return mid; 16 | 17 | // If element is smaller than mid, then 18 | // it can only be present in left subarray 19 | if (arr[mid] > x) 20 | return binarySearch(arr, l, mid - 1, x); 21 | 22 | // Else the element can only be present 23 | // in right subarray 24 | return binarySearch(arr, mid + 1, r, x); 25 | } 26 | 27 | // We reach here when element is not 28 | // present in array 29 | return -1; 30 | } 31 | 32 | int main(void) 33 | { 34 | int arr[] = { 2, 3, 4, 10, 40 }; 35 | int n = sizeof(arr) / sizeof(arr[0]); 36 | int x = 10; 37 | int result = binarySearch(arr, 0, n - 1, x); 38 | (result == -1) 39 | ? printf("Element is not present in array") 40 | : printf("Element is present at index %d", result); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Radix_Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void countsort(int a[],int n,int i) 3 | { 4 | int count[10]={0},b[30],j; 5 | for(j=0;j=0;j--) 14 | { 15 | b[--count[(a[j]/i)%10]]=a[j]; 16 | } 17 | for(j=0;j0;i=i*10) 37 | { 38 | countsort(a,n,i); 39 | } 40 | } 41 | int main() 42 | { 43 | int a[30],count[10]={0},b[30],n,i; 44 | printf("\nEnter number of elements of array:\n"); 45 | scanf("%d",&n); 46 | printf("\nEnter elements of array:\n"); 47 | for(i=0;i 6 | using namespace std; 7 | 8 | class Solution { 9 | public: 10 | void swap(int i,int j,vector& nums){ 11 | int temp=nums[i]; 12 | nums[i]=nums[j]; 13 | nums[j]=temp; 14 | } 15 | void reverse(int i,int j,vector& nums){ 16 | while(i& nums) { 22 | int index1=-1,index2; 23 | int n=nums.size(); 24 | for(int i=n-2;i>=0;i--){ 25 | if(nums[i+1]>nums[i]){ 26 | index1=i; 27 | break; 28 | } 29 | } 30 | if(index1==-1){ 31 | reverse(0,n-1,nums); 32 | return; 33 | } 34 | for(int i=n-1;i>index1;i--){ 35 | if(nums[index1] 2 | #include 3 | using namespace std; 4 | int main () 5 | { 6 | int arr[100], st, mid, end, i, num, tgt; 7 | 8 | cout << " Define the size of the array: " << endl; 9 | cin >> num; 10 | 11 | cout << " Enter the values in sorted array either ascending or descending order: " << endl; 12 | for (i = 0; i < num; i++) 13 | { 14 | cout << " arr [" << i << "] = "; 15 | cin >> arr[i]; 16 | } 17 | 18 | st = 0; 19 | end = num - 1; 20 | 21 | cout << " Define a value to be searched from sorted array: " << endl; 22 | cin >> tgt; 23 | 24 | while ( st <= end) 25 | { 26 | mid = ( st + end ) / 2; 27 | if (arr[mid] == tgt) 28 | { 29 | cout << " Element is found at index " << (mid + 1); 30 | exit (0); 31 | } 32 | else if ( tgt > arr[mid]) 33 | { 34 | st = mid + 1; 35 | } 36 | 37 | else if ( tgt < arr[mid]) 38 | { 39 | end = mid - 1; 40 | } 41 | } 42 | cout << " Number is not found. " << endl; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /bellman-ford.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct node { 4 | int u; 5 | int v; 6 | int wt; 7 | node(int first, int second, int weight) { 8 | u = first; 9 | v = second; 10 | wt = weight; 11 | } 12 | }; 13 | 14 | int main(){ 15 | int N=6,m=7; 16 | vector edges; 17 | edges.push_back(node(0,1,5)); 18 | edges.push_back(node(1,2,-2)); 19 | edges.push_back(node(1,5,-3)); 20 | edges.push_back(node(2,4,3)); 21 | edges.push_back(node(3,2,6)); 22 | edges.push_back(node(3,4,-2)); 23 | edges.push_back(node(5,3,1)); 24 | int src=0; 25 | int inf = 10000000; 26 | vector dist(N, inf); 27 | dist[src] = 0; 28 | for(int i = 1;i<=N-1;i++) { 29 | for(auto it: edges) { 30 | if(dist[it.u] + it.wt < dist[it.v]) { 31 | dist[it.v] = dist[it.u] + it.wt; 32 | } 33 | } 34 | } 35 | 36 | int fl = 0; 37 | for(auto it: edges) { 38 | if(dist[it.u] + it.wt < dist[it.v]) { 39 | cout << -1; 40 | fl = 1; 41 | break; 42 | } 43 | } 44 | 45 | if(!fl) { 46 | for(int i = 0;i 2 | #include 3 | 4 | struct node { 5 | int info; 6 | struct node* next; 7 | }; 8 | 9 | 10 | struct node* last = NULL; 11 | 12 | void insertAtFront() 13 | { 14 | 15 | int data; 16 | 17 | 18 | struct node* temp; 19 | temp = (struct node*)malloc(sizeof(struct node)); 20 | 21 | printf("\nEnter data to be " 22 | "inserted: \n"); 23 | scanf("%d", &data); 24 | 25 | if (last == NULL) { 26 | temp->info = data; 27 | temp->next = temp; 28 | last = temp; 29 | } 30 | 31 | 32 | else { 33 | temp->info = data; 34 | temp->next = last->next; 35 | 36 | last->next = temp; 37 | } 38 | } 39 | 40 | void viewList() 41 | { 42 | 43 | if (last == NULL) 44 | printf("\nList is empty\n"); 45 | 46 | 47 | else { 48 | struct node* temp; 49 | temp = last->next; 50 | 51 | do { 52 | printf("\nData = %d", temp->info); 53 | temp = temp->next; 54 | } while (temp != last->next); 55 | } 56 | } 57 | 58 | int main() 59 | { 60 | // Function Call 61 | insertAtFront(); 62 | insertAtFront(); 63 | insertAtFront(); 64 | 65 | 66 | viewList(); 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /topological_sort_NISHANT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector topo(int N, vector adj[]) { 7 | queue q; 8 | vector indegree(N, 0); 9 | for(int i = 0;i topo; 21 | while(!q.empty()) { 22 | int node = q.front(); 23 | q.pop(); 24 | topo.push_back(node); 25 | for(auto it : adj[node]) { 26 | indegree[it]--; 27 | if(indegree[it] == 0) { 28 | q.push(it); 29 | } 30 | } 31 | } 32 | return topo; 33 | } 34 | }; 35 | 36 | 37 | 38 | int main() 39 | { 40 | 41 | vector adj[6]; 42 | adj[5].push_back(2); 43 | adj[5].push_back(0); 44 | adj[4].push_back(0); 45 | adj[4].push_back(1); 46 | adj[3].push_back(1); 47 | adj[2].push_back(3); 48 | 49 | Solution obj; 50 | vector v=obj.topo(6, adj); 51 | for(auto it:v) 52 | cout< 3 | 4 | #include 5 | 6 | #include 7 | 8 | int sender(int b[10],int k) 9 | 10 | { 11 | 12 | int checksum,sum=0,i; 13 | 14 | printf("\n***SENDER*\n"); 15 | 16 | for(i=0;i 2 | using namespace std; 3 | 4 | const int N=1e5+10; 5 | vectorg[N]; 6 | int sum[N]; 7 | int depth[N],height[N]; 8 | int even_count[N]; 9 | void dfs(int vertex,int par){ 10 | /* 11 | Taking action on child before entering the child node 12 | */ 13 | 14 | for(int child:g[vertex]){ 15 | 16 | if(child==par) continue; 17 | 18 | depth[child]=depth[vertex]+1; 19 | 20 | dfs(child,vertex); 21 | 22 | /* 23 | Take action on child after exiting the child node 24 | */ 25 | 26 | 27 | } 28 | /* 29 | Taking sction on vertex before exiting the vertex node 30 | */ 31 | } 32 | 33 | int main(){ 34 | int n; 35 | cin>>n; 36 | for(int i=0;i>a>>b; 39 | g[a].push_back(b); 40 | g[b].push_back(a); 41 | 42 | } 43 | dfs(1,0); 44 | int max_depth=-1; 45 | int max_depthNode; 46 | for(int i=1;i<=n;i++){ 47 | if(max_depth 2 | using namespace std; 3 | 4 | bool bipartiteDfs(int node, vector adj[], int color[]) { 5 | for(auto it : adj[node]) { 6 | if(color[it] == -1) { 7 | color[it] = 1 - color[node]; 8 | if(!bipartiteDfs(it, adj, color)) { 9 | return false; 10 | } 11 | } else if(color[it] == color[node]) return false; 12 | } 13 | return true; 14 | } 15 | bool checkBipartite(vector adj[], int n) { 16 | int color[n]; 17 | memset(color, -1, sizeof color); 18 | for(int i = 0;i adj[],int u,int v) 29 | { 30 | adj[u].push_back(v); 31 | adj[v].push_back(u); 32 | } 33 | int main() { 34 | 35 | vector adj[6]; 36 | addedge(adj,0,1); 37 | addedge(adj,1,2); 38 | addedge(adj,1,4); 39 | addedge(adj,1,5); 40 | addedge(adj,2,3); 41 | addedge(adj,3,4); 42 | addedge(adj,3,5); 43 | 44 | if(checkBipartite(adj, 6)) { 45 | cout << "It is a Bipartite Graph"; 46 | } else { 47 | cout << "It is not a Bipartite Graph"; 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Stack_Infix_to_Postfix.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Sumit Suman on 29-09-2021. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | char stack[10]; 10 | int top = -1; 11 | 12 | void push(char x) 13 | { 14 | stack[++top] = x; 15 | } 16 | 17 | char pop() 18 | { 19 | if(top == -1) 20 | return -1; 21 | else 22 | return stack[top--]; 23 | } 24 | 25 | int priority(char x) 26 | { 27 | if(x == '(') 28 | return 0; 29 | if(x == '+' || x == '-') 30 | return 1; 31 | if(x == '*' || x == '/') 32 | return 2; 33 | return 0; 34 | } 35 | 36 | int main() 37 | { 38 | char expression[100]; 39 | char *e, x; 40 | printf("Enter the expression : "); 41 | scanf("%s",expression); 42 | printf("\nPostfix: "); 43 | e = expression; 44 | 45 | while(*e != '\0') 46 | { 47 | if(isalnum(*e)) 48 | printf("%c ",*e); 49 | else if(*e == '(') 50 | push(*e); 51 | else if(*e == ')') 52 | { 53 | while((x = pop()) != '(') 54 | printf("%c ", x); 55 | } 56 | else 57 | { 58 | while(priority(stack[top]) >= priority(*e)) 59 | printf("%c ",pop()); 60 | push(*e); 61 | } 62 | e++; 63 | } 64 | 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /prims.c: -------------------------------------------------------------------------------- 1 | //Prim's algorithm for minimum spanning tree 2 | #include 3 | #include 4 | #include 5 | #define V 5 6 | 7 | int minKey(int key[], bool mstSet[]) 8 | { 9 | int min = INT_MAX, min_index; 10 | 11 | for (int v = 0; v < V; v++) 12 | if (mstSet[v] == false && key[v] < min) 13 | min = key[v], min_index = v; 14 | 15 | return min_index; 16 | } 17 | 18 | int printMST(int parent[], int graph[V][V]) 19 | { 20 | printf("Edge \tWeight\n"); 21 | for (int i = 1; i < V; i++) 22 | printf("%d - %d \t%d \n", parent[i], i, 23 | graph[i][parent[i]]); 24 | } 25 | 26 | void primMST(int graph[V][V]) 27 | { 28 | int parent[V]; 29 | int key[V]; 30 | bool mstSet[V]; 31 | 32 | for (int i = 0; i < V; i++) 33 | key[i] = INT_MAX, mstSet[i] = false; 34 | 35 | key[0] = 0; 36 | parent[0] = -1; 37 | 38 | for (int count = 0; count < V - 1; count++) { 39 | 40 | int u = minKey(key, mstSet); 41 | mstSet[u] = true; 42 | 43 | for (int v = 0; v < V; v++) 44 | 45 | if (graph[u][v] && mstSet[v] == false 46 | && graph[u][v] < key[v]) 47 | parent[v] = u, key[v] = graph[u][v]; 48 | } 49 | 50 | printMST(parent, graph); 51 | } 52 | 53 | int main() 54 | { 55 | 56 | int graph[V][V] = { { 0, 2, 0, 6, 0 }, 57 | { 2, 0, 3, 8, 5 }, 58 | { 0, 3, 0, 0, 7 }, 59 | { 6, 8, 0, 0, 9 }, 60 | { 0, 5, 7, 9, 0 } }; 61 | 62 | primMST(graph); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/bigINT_addition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int charToint(char s){ 6 | return s-'0'; 7 | } 8 | 9 | char digitTochar(int n){ 10 | return '0'+n; 11 | } 12 | string addNumbers(string n1,string n2){ 13 | //Make sure that second number is larger 14 | if(n1.length()>n2.length()){ 15 | swap(n1,n2); 16 | } 17 | //result array 18 | string result=""; 19 | 20 | //reverse the numbers 21 | reverse(n1.begin(),n2.end()); 22 | reverse(n2.begin(),n2.end());\ 23 | 24 | int carry=0; 25 | 26 | //Add digits upto n1.length() 27 | for(int i=0;i>s1>>s2; 57 | 58 | string result=addNumbers(s1,s2); 59 | cout< 2 | using namespace std; 3 | 4 | const int N=1e5+10; 5 | vectorg[N]; 6 | int depth[N]; 7 | int parent[N]; 8 | void dfs(int vertex,int par=-1){ 9 | /* 10 | Taking action on child before entering the child node 11 | */ 12 | parent[vertex]=par; 13 | for(int child:g[vertex]){ 14 | 15 | if(child==par) continue; 16 | 17 | depth[child]=depth[vertex]+1; 18 | 19 | dfs(child,vertex); 20 | 21 | /* 22 | Take action on child after exiting the child node 23 | */ 24 | 25 | 26 | } 27 | /* 28 | Taking sction on vertex before exiting the vertex node 29 | */ 30 | } 31 | 32 | vector path(int v){ 33 | vectorans; 34 | while(v!=-1){ 35 | ans.push_back(v); 36 | v=parent[v]; 37 | 38 | } 39 | reverse(ans.begin(),ans.end()); 40 | return ans; 41 | } 42 | 43 | int main(){ 44 | int n; 45 | cin>>n; 46 | for(int i=0;i>a>>b; 49 | g[a].push_back(b); 50 | g[b].push_back(a); 51 | } 52 | dfs(1); 53 | int a,b; 54 | cin>>a>>b; 55 | vectora1=path(a); 56 | vectora2=path(b); 57 | int min_len=min(a1.size(),a2.size()); 58 | int lca=-1; 59 | for(int i=0;i 5 | 6 | using namespace std; 7 | 8 | struct node { 9 | int data; 10 | struct node * left, * right; 11 | }; 12 | 13 | class Solve { 14 | node * prev = NULL; 15 | public: 16 | void flatten(node * root) { 17 | if (root == NULL) return; 18 | 19 | flatten(root -> right); 20 | flatten(root -> left); 21 | 22 | root -> right = prev; 23 | root -> left = NULL; 24 | prev = root; 25 | } 26 | 27 | }; 28 | 29 | struct node * newNode(int data) { 30 | struct node * node = (struct node * ) malloc(sizeof(struct node)); 31 | node -> data = data; 32 | node -> left = NULL; 33 | node -> right = NULL; 34 | 35 | return (node); 36 | } 37 | 38 | int main() { 39 | 40 | struct node * root = newNode(10); 41 | root -> left = newNode(20); 42 | root -> left -> left = newNode(30); 43 | root -> left -> right = newNode(40); 44 | root -> right = newNode(50); 45 | root -> right -> right = newNode(60); 46 | root -> right -> right -> left = newNode(70); 47 | 48 | Solve obj; 49 | 50 | obj.flatten(root); 51 | while(root->right!=NULL) 52 | { 53 | cout<data<<"->"; 54 | root=root->right; 55 | } 56 | cout<data; 57 | return 0; 58 | } 59 | 60 | //Output: 61 | //10->20->30->40->50->60->70 62 | 63 | //Time Complexity: O(N) 64 | 65 | //Space Complexity: O(N) 66 | -------------------------------------------------------------------------------- /permutations.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int count = 0; 4 | //function to print the array 5 | void printarray(int arr[], int size) 6 | { 7 | count++; 8 | int i,j; 9 | FILE *fptr; 10 | fptr = fopen("input.txt", "a"); 11 | for(i=0; i 2 | int stack[20]; 3 | int top = -1; 4 | 5 | void push(int x) 6 | { 7 | stack[++top] = x; 8 | } 9 | 10 | int pop() 11 | { 12 | return stack[top--]; 13 | } 14 | 15 | int main() 16 | { 17 | char exp[20]; 18 | char *e; 19 | int n1,n2,n3,num; 20 | printf("Enter the post-fix expression :"); 21 | scanf("%s",exp); 22 | e = exp; 23 | while(*e != '\0') 24 | { 25 | if(isdigit(*e)) 26 | { 27 | num = *e - 48; 28 | push(num); 29 | } 30 | else 31 | { 32 | n1 = pop(); 33 | n2 = pop(); 34 | switch(*e) 35 | { 36 | case '+': 37 | { 38 | n3 = n1 + n2; 39 | break; 40 | } 41 | case '-': 42 | { 43 | n3 = n2 - n1; 44 | break; 45 | } 46 | case '*': 47 | { 48 | n3 = n1 * n2; 49 | break; 50 | } 51 | case '/': 52 | { 53 | n3 = n2 / n1; 54 | break; 55 | } 56 | } 57 | push(n3); 58 | } 59 | e++; 60 | } 61 | printf("\nThe result of expression %s = %d\n\n",exp,pop()); 62 | return 0; 63 | } 64 | /*Output: 65 | Enter the expression :235*+ 66 | 67 | The result of expression 235*+ = 17 68 | */ 69 | -------------------------------------------------------------------------------- /kosaraju's_algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void dfs(int node, stack &st, vector &vis, vector adj[]) { 4 | vis[node] = 1; 5 | for(auto it: adj[node]) { 6 | if(!vis[it]) { 7 | dfs(it, st, vis, adj); 8 | } 9 | } 10 | 11 | st.push(node); 12 | } 13 | void revDfs(int node, vector &vis, vector transpose[]) { 14 | cout << node << " "; 15 | vis[node] = 1; 16 | for(auto it: transpose[node]) { 17 | if(!vis[it]) { 18 | revDfs(it, vis, transpose); 19 | } 20 | } 21 | } 22 | int main() { 23 | int n=6, m=7; 24 | vector adj[n+1]; 25 | adj[1].push_back(3); 26 | adj[2].push_back(1); 27 | adj[3].push_back(2); 28 | adj[3].push_back(5); 29 | adj[4].push_back(6); 30 | adj[5].push_back(4); 31 | adj[6].push_back(5); 32 | 33 | stack st; 34 | vector vis(n+1, 0); 35 | for(int i = 1;i<=n;i++) { 36 | if(!vis[i]) { 37 | dfs(i, st, vis, adj); 38 | } 39 | } 40 | 41 | vector transpose[n+1]; 42 | 43 | for(int i = 1;i<=n;i++) { 44 | vis[i] = 0; 45 | for(auto it: adj[i]) { 46 | transpose[it].push_back(i); 47 | } 48 | } 49 | 50 | 51 | 52 | while(!st.empty()) { 53 | int node = st.top(); 54 | st.pop(); 55 | if(!vis[node]) { 56 | cout << "SCC: "; 57 | revDfs(node, vis, transpose); 58 | cout << endl; 59 | } 60 | } 61 | 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Valid_Parenthesis.c: -------------------------------------------------------------------------------- 1 | #include 2 | int stack[50],top,x,i,n; 3 | char par[30]; 4 | void push(int ); 5 | void pop(); 6 | int c; 7 | c=0; 8 | int main() 9 | { 10 | top=-1; 11 | printf("Enter the paranthesized expression:"); 12 | scanf("%s",&par); 13 | n=sizeof(par); 14 | for (int i=0;i=n-1){ 61 | printf("Stack is overflowed\n"); 62 | } 63 | else{ 64 | stack[++top]=x; 65 | } 66 | } 67 | void pop() 68 | { 69 | if(top<=-1){ 70 | printf("stack is underflown\n"); 71 | } 72 | else{ 73 | top--; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /thread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int n; 5 | void *fun1(void *args) 6 | { 7 | pthread_t call_id=(pthread_t)args; 8 | printf("\ncaller id :%lu",call_id); 9 | pthread_t sid; 10 | printf("\n Thread id :%lu",sid); 11 | int i,f=1; 12 | for(i=1;i<=n;i++) 13 | f=f*i; 14 | printf("\n Factorial of %d is %d",n,f); 15 | } 16 | void *func2(void *args) 17 | { 18 | int i,f=1,sum=0,j; 19 | pthread_t call_id=(pthread_t)args; 20 | printf("\ncaller id :%lu",call_id); 21 | pthread_t sid; 22 | printf("\n Thread id :%lu",sid); 23 | for(i=1;i<=n;i++) 24 | { 25 | for(j=1;j<=i;j++) 26 | { 27 | f=f*j; 28 | } 29 | sum=sum+f; 30 | f=1; 31 | } 32 | printf("\n Total Number of factorial of %d is %d\n",n,sum); 33 | } 34 | void main() 35 | { 36 | printf("\n Enter the value of n : "); 37 | scanf("%d",&n); 38 | printf("\n Strating of thread 1"); 39 | pthread_t t_id,sid; 40 | sid=pthread_self(); 41 | pthread_create(&t_id,NULL,fun1,(void*)&sid); 42 | pthread_join(t_id,NULL); 43 | printf("\n Ending of thread 1"); 44 | printf("\n Starting thread 2"); 45 | pthread_t w_id,si; 46 | si=pthread_self(); 47 | pthread_create(&w_id,NULL,func2,(void*)&si); 48 | pthread_join(w_id,NULL); 49 | printf("Ending of thread 2\n"); 50 | 51 | } -------------------------------------------------------------------------------- /ReverseLinkedList (1).c: -------------------------------------------------------------------------------- 1 | // Iterative C program to reverse a linked list // 2 | #include 3 | #include 4 | 5 | /* Link list node */ 6 | struct Node { 7 | int data; 8 | struct Node* next; 9 | }; 10 | 11 | /* Function to reverse the linked list */ 12 | static void reverse(struct Node** head_ref) 13 | { 14 | struct Node* prev = NULL; 15 | struct Node* current = *head_ref; 16 | struct Node* next = NULL; 17 | while (current != NULL) { 18 | // Store next 19 | next = current->next; 20 | 21 | // Reverse current node's pointer 22 | current->next = prev; 23 | 24 | // Move pointers one position ahead. 25 | prev = current; 26 | current = next; 27 | } 28 | *head_ref = prev; 29 | } 30 | 31 | /* Function to push a node */ 32 | void push(struct Node** head_ref, int new_data) 33 | { 34 | struct Node* new_node 35 | = (struct Node*)malloc(sizeof(struct Node)); 36 | new_node->data = new_data; 37 | new_node->next = (*head_ref); 38 | (*head_ref) = new_node; 39 | } 40 | 41 | /* Function to print linked list */ 42 | void printList(struct Node* head) 43 | { 44 | struct Node* temp = head; 45 | while (temp != NULL) { 46 | printf("%d ", temp->data); 47 | temp = temp->next; 48 | } 49 | } 50 | 51 | /* Driver code*/ 52 | int main() 53 | { 54 | struct Node* head = NULL; 55 | 56 | push(&head, 20); 57 | push(&head, 4); 58 | push(&head, 15); 59 | push(&head, 85); 60 | 61 | printf("Given linked list\n"); 62 | printList(head); 63 | reverse(&head); 64 | printf("\nReversed Linked list \n"); 65 | printList(head); 66 | getchar(); 67 | } 68 | -------------------------------------------------------------------------------- /AddingElementsLinkedLIst.c: -------------------------------------------------------------------------------- 1 | //Q. adding the element to the head node of a given linked list 2 | //eg : before deletion : 2 5 8 9 7 3 | //eg : after deletion : 10 2 5 8 9 7 4 | 5 | 6 | 7 | #include 8 | #include 9 | 10 | struct Node { 11 | int data; 12 | struct Node * next; 13 | }; 14 | 15 | struct Node * addingElements(struct Node * head, int ele){ 16 | struct Node * ptr = (struct Node *)malloc(sizeof(struct Node)); 17 | struct Node * p = head; 18 | ptr->next = p; 19 | ptr->data = ele; 20 | head = ptr; 21 | return(head); 22 | } 23 | 24 | struct Node * deletionElements(struct Node * head){ 25 | head = head->next; 26 | return(head); 27 | } 28 | 29 | void llTraversal(struct Node * head){ 30 | while(head != NULL){ 31 | printf("%d\n", head->data); 32 | head = head->next; 33 | } 34 | } 35 | 36 | int main(){ 37 | struct Node * head = (struct Node *)malloc(sizeof(struct Node)); 38 | struct Node * second = (struct Node *)malloc(sizeof(struct Node)); 39 | struct Node * third = (struct Node *)malloc(sizeof(struct Node)); 40 | 41 | int ele; 42 | 43 | head->data = 10; 44 | head->next = second; 45 | 46 | second->data = 20; 47 | second->next = third; 48 | 49 | third->data = 30; 50 | third->next = NULL; 51 | 52 | printf("Enter the element : "); 53 | scanf("%d", &ele); 54 | 55 | printf("Linked list before insertion : \n"); 56 | llTraversal(head); 57 | 58 | printf("linked list after insertion is : \n"); 59 | head = addingElements(head, ele); 60 | llTraversal(head); 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /merge_2array.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | cpp program for merging two arraay 4 | Author: Anil Kumar 5 | Date modified:22-10-2021 6 | */ 7 | 8 | 9 | #include 10 | using namespace std; 11 | 12 | void marge(int a[], int b[], int n1, int n2) 13 | { 14 | 15 | int m[n1 + n2]; 16 | 17 | int i = 0, j = 0, k = 0; 18 | 19 | while (i < n1 && j < n2) 20 | { 21 | if (a[i] < b[j]) 22 | { 23 | m[k] = a[i]; 24 | i++; 25 | k++; 26 | } 27 | else 28 | { 29 | 30 | m[k] = b[j]; 31 | j++; 32 | k++; 33 | } 34 | } 35 | 36 | if (i == n1) 37 | { 38 | 39 | do 40 | { 41 | m[k] = b[j]; 42 | j++; 43 | k++; 44 | 45 | } while (j < n2); 46 | } 47 | if (j == n2) 48 | { 49 | 50 | do 51 | { 52 | m[k] = a[i]; 53 | i++; 54 | k++; 55 | 56 | } while (i < n1); 57 | } 58 | 59 | for (int i = 0; i < n1+n2; i++) 60 | { 61 | cout<> n1; 72 | int a1[n1]; 73 | cout << endl 74 | << "enter the elements:"; 75 | for (int i = 0; i < n1; i++) 76 | { 77 | cin >> a1[i]; 78 | } 79 | 80 | cout << endl 81 | << "enter the element in 2 array:"; 82 | int n2; 83 | 84 | cin >> n2; 85 | int a2[n2]; 86 | cout << "enter the elements:"; 87 | for (int i = 0; i < n2; i++) 88 | { 89 | cin >> a2[i]; 90 | } 91 | 92 | marge(a1, a2, n1, n2); 93 | } 94 | -------------------------------------------------------------------------------- /4sum.cpp: -------------------------------------------------------------------------------- 1 | // Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: 2 | 3 | // 0 <= a, b, c, d < n 4 | // a, b, c, and d are distinct. 5 | // nums[a] + nums[b] + nums[c] + nums[d] == target 6 | // You may return the answer in any order. 7 | 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | class Solution { 14 | public: 15 | vector> fourSum(vector& nums, int target) { 16 | vector> ans; 17 | if(nums.size()<4) return ans; 18 | sort(nums.begin(),nums.end()); 19 | int n=nums.size(); 20 | vector temp(4); 21 | for(int i=0;inums[left]+nums[right]) left++; 28 | else if(req 2 | #include 3 | 4 | struct Stack{ 5 | int top; 6 | int capacity; 7 | int* array; 8 | }; 9 | 10 | struct stack* createstack(int capacity){ 11 | struct Stack* stack = malloc(sizeof(struct Stack)); 12 | stack->top = -1; 13 | stack->capacity = capacity; 14 | stack->array = malloc(capacity*sizeof(int)); 15 | return stack; 16 | } 17 | 18 | int isFull(struct Stack* stack){ 19 | return stack->capacity-1 == stack->top; 20 | } 21 | 22 | int isEmpty(struct Stack* stack){ 23 | return stack->top == -1; 24 | } 25 | 26 | int peek(struct Stack* stack){ 27 | return stack->array[stack->top]; 28 | } 29 | 30 | void push(struct Stack* stack, int value){ 31 | (!isFull(stack)) ? stack->array[++(stack->top)] = value : printf("Stack is full\n"); 32 | } 33 | 34 | void pop(struct Stack* stack){ 35 | (!isEmpty(stack)) ? stack->array[stack->top--] = 0 : printf("Stack is already Empty\n"); 36 | } 37 | 38 | void display(struct Stack* stack){ 39 | if(stack->top == -1) printf("list is empty"); 40 | else for(int i=0;itop+1;i++) printf("%d ",stack->array[i]); 41 | printf("\n"); 42 | } 43 | 44 | int main() 45 | { 46 | int capacity; 47 | printf("Enter the capacity: "); 48 | scanf("%d",&capacity); 49 | struct Stack* stack = createstack(capacity); 50 | 51 | display(stack); 52 | isEmpty(stack) ? printf("The list is Empty\n"): printf("The stack is not empty\n"); 53 | push(stack,15); 54 | printf("Value pushed in the stack\n"); 55 | push(stack,63); 56 | push(stack,85); 57 | push(stack,85); 58 | push(stack,85); 59 | push(stack,85); 60 | pop(stack); 61 | printf("The last value is popped of the stack\n"); 62 | display(stack); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /bst_Inorder_recursive_traversal: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct tn{ 4 | int data; 5 | struct tn *left; 6 | struct tn *right; 7 | struct tn *parent; 8 | }; 9 | struct tn *root = NULL; 10 | struct tn *create(int n) 11 | { 12 | struct tn *new_node=(struct tn*)malloc(sizeof(struct tn)); 13 | if (new_node==NULL) 14 | { 15 | printf("MEMORY CANNOT BE ALLOCATED"); 16 | } 17 | new_node->data=n; 18 | new_node->left=NULL; 19 | new_node->right=NULL; 20 | return new_node; 21 | } 22 | void insert(int a) 23 | { 24 | struct tn*node=create(a); 25 | if(node!=NULL){ 26 | if(root==NULL){ 27 | node->parent=NULL; 28 | root=node; 29 | printf("%d data is inserted in the bst\n",a); 30 | } 31 | else{ 32 | struct tn*temp=root; 33 | struct tn*prev=NULL; 34 | while(temp!=NULL){ 35 | prev=temp; 36 | if (adata){ 37 | temp=temp->left; 38 | 39 | } 40 | else{ 41 | temp=temp->right; 42 | } 43 | } 44 | node->parent=prev; 45 | if(prev->data>a){ 46 | prev->left = node; 47 | } 48 | else{ 49 | prev->right =node; 50 | } 51 | printf("Node having data %d was inserted\n",a); 52 | } 53 | } 54 | } 55 | void display(struct tn* node) 56 | { 57 | if (node == NULL) 58 | return; 59 | 60 | display(node->left); 61 | 62 | printf("%d ", node->data); 63 | 64 | display(node->right); 65 | } 66 | void main(){ 67 | insert(7); 68 | insert(5); 69 | insert(4); 70 | insert(2); 71 | insert(8); 72 | insert(1); 73 | insert(6); 74 | display(root); 75 | } 76 | -------------------------------------------------------------------------------- /matrix_multiplication.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int m, n, m2, n2, sum = 0; 6 | printf("No of coloum of first matrix must be equal to the no. of rows in second matrix\n "); 7 | printf("Enter the number of rows and coloum of your first matrix\n"); 8 | scanf("%d %d", &m, &n); 9 | printf("Enter the number of rows and coloum of your first matrix\n"); 10 | scanf("%d %d", &m2, &n2); 11 | if (n == m2) 12 | { 13 | int a[m][n], b[m2][n2], result[m][n2]; 14 | printf("Enter your First matrix\n"); 15 | 16 | for (int i = 0; i < m; i++) 17 | { 18 | for (int j = 0; j < n; j++) 19 | { 20 | scanf("%d", &a[i][j]); 21 | } 22 | } 23 | 24 | printf("Enter your second matrix\n"); 25 | for (int i = 0; i < m2; i++) 26 | { 27 | for (int j = 0; j < n2; j++) 28 | { 29 | scanf("%d", &b[i][j]); 30 | } 31 | } 32 | 33 | printf("Resultant matrix is\n"); 34 | for (int i = 0; i < m; i++) 35 | { 36 | for (int j = 0; j < n2; j++) 37 | { 38 | for (int k = 0; k < n; k++) 39 | { 40 | sum += a[i][k] * b[k][j]; 41 | } 42 | result[i][j] = sum; 43 | sum = 0; 44 | } 45 | } 46 | 47 | for (int i = 0; i < m; i++) 48 | { 49 | for (int j = 0; j < n2; j++) 50 | { 51 | 52 | printf("%d \t", result[i][j]); 53 | } 54 | printf("\n"); 55 | } 56 | } 57 | else 58 | { 59 | printf("Multiplication of this matrix is not possible"); 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Employee.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | struct employee 6 | { 7 | char name[100],acc_num[100]; 8 | int salary; 9 | }name,acc_num,salary; 10 | void main() 11 | { 12 | struct employee *emp; 13 | int i,n; 14 | int max_sal=0,temp=0; 15 | 16 | clock_t start, end; 17 | double cpu_time_used; 18 | start = clock(); 19 | printf("The program execution begins at: %ld ms",start); 20 | 21 | printf("\n\n Enter the number of employees: "); 22 | scanf("%d",&n); 23 | emp =calloc(n,(sizeof(*emp))); 24 | 25 | printf("Enter the informations : \n"); 26 | for(i=0;i 4 | 5 | void knapsack(int n, float weight[], float profit[], float capacity) 6 | { 7 | float x[20], tp = 0; 8 | int i, j, u=capacity; 9 | 10 | for (i = 0; i < n; i++) 11 | x[i] = 0.0; 12 | 13 | for (i = 0; i < n; i++) 14 | { 15 | if (weight[i] > u) 16 | break; 17 | else 18 | { 19 | x[i] = 1.0; 20 | tp = tp + profit[i]; 21 | u = u - weight[i]; 22 | } 23 | } 24 | 25 | if (i < n) 26 | x[i] = u / weight[i]; 27 | 28 | tp = tp + (x[i] * profit[i]); 29 | 30 | printf("\nMaximum profit is:- %f", tp); 31 | } 32 | 33 | int main() 34 | { 35 | float weight[20], profit[20], capacity, ratio[20], temp; 36 | int num, i, j; 37 | 38 | printf("\nEnter the no. of objects : "); 39 | scanf("%d", &num); 40 | 41 | printf("\nEnter the weights and profits of each object :\n"); 42 | for (i = 0; i < num; i++) 43 | scanf("%f %f", &weight[i], &profit[i]); 44 | 45 | printf("\nEnter the capacity of knapsack : "); 46 | scanf("%f", &capacity); 47 | 48 | for (i = 0; i < num; i++) 49 | ratio[i] = profit[i] / weight[i]; 50 | 51 | for (i = 0; i < num; i++) 52 | { 53 | for (j = i + 1; j < num; j++) 54 | { 55 | if (ratio[i] < ratio[j]) 56 | { 57 | temp = ratio[j]; 58 | ratio[j] = ratio[i]; 59 | ratio[i] = temp; 60 | 61 | temp = weight[j]; 62 | weight[j] = weight[i]; 63 | weight[i] = temp; 64 | 65 | temp = profit[j]; 66 | profit[j] = profit[i]; 67 | profit[i] = temp; 68 | } 69 | } 70 | } 71 | 72 | knapsack(num, weight, profit, capacity); 73 | return(0); 74 | } 75 | -------------------------------------------------------------------------------- /Circular_Queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define size 6 4 | int queue[size]; 5 | int front = -1; 6 | int rear = -1; 7 | void enqueue(int element) 8 | { 9 | if (front == -1 && rear == -1) 10 | { 11 | front = 0; 12 | rear = 0; 13 | queue[rear] = element; 14 | } 15 | else if ((rear + 1) % size == front) 16 | { 17 | printf("Queue is Full\n"); 18 | } 19 | else 20 | { 21 | rear = (rear + 1) % size; 22 | queue[rear] = element; 23 | } 24 | } 25 | 26 | void dequeue() 27 | { 28 | if ((front == -1) && (rear == -1)) 29 | { 30 | printf("\nQueue is empty"); 31 | } 32 | else 33 | { 34 | printf("\nThe dequeued element is %d", queue[front]); 35 | front = (front + 1) %size; 36 | } 37 | } 38 | void display() 39 | { 40 | int i = front; 41 | if (front == -1 && rear == -1) 42 | { 43 | printf("\n Queue is empty"); 44 | } 45 | else 46 | { 47 | printf("\nElements in a Queue are :"); 48 | while (i <= rear) 49 | { 50 | printf("%d,", queue[i]); 51 | i = (i + 1) % size; 52 | } 53 | } 54 | } 55 | int main() 56 | { 57 | int choice, x; 58 | while (1) 59 | { 60 | printf("1. Push\n2. Pop\n3. Display\n4. Exit\n"); 61 | printf("\nEnter your choice: "); 62 | scanf("%d", &choice); 63 | switch (choice) 64 | { 65 | case 1: 66 | printf("Enter the element which is to be inserted"); 67 | scanf("%d", &x); 68 | enqueue(x); 69 | break; 70 | case 2: 71 | dequeue(); 72 | break; 73 | case 3: 74 | display(); 75 | break; 76 | case 4: 77 | exit(0); 78 | default: 79 | printf("\n Wrong choice"); 80 | } 81 | } 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/Segemented_sieve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define N 100000 6 | 7 | int sieveArr[N+1] = {0}; 8 | vector primes; 9 | 10 | //Sieve of Eratosthenes (O(NLogLogN)) 11 | void sieve(){ 12 | for(long long i=2; i<=N; i++){ 13 | //mark non primes as 1 14 | if(sieveArr[i]==0){ 15 | primes.push_back(i); 16 | //marking all multiples of i (prime) as non-prime 17 | for(long long j= i*i; j<=N; j+=i){ 18 | sieveArr[j] = 1; //non-prime 19 | } 20 | } 21 | } 22 | } 23 | 24 | // Segmented Sieve 25 | 26 | int main(){ 27 | 28 | //Precompute 29 | sieve(); 30 | int t; 31 | cin>>t; 32 | 33 | while(t--){ 34 | int n,m; 35 | cin>>m >> n; 36 | 37 | vector segment(n-m+1,0); 38 | 39 | 40 | //iterate over the primes, mark multiples of 41 | // prime in segment array as non-prime (1) 42 | 43 | for(auto p : primes){ 44 | 45 | //stop the loop if prime is larger than root n 46 | if( p*p > n){ 47 | break; 48 | } 49 | 50 | int start = (m/p) * p; 51 | 52 | // don't start from 0, instead 2 * prime 53 | if(p>=m and p<=n){ 54 | start = 2 * p; 55 | } 56 | 57 | for(int j = start; j<=n; j = j + p){ 58 | if(j < m){ 59 | continue; 60 | } 61 | //non-prime 62 | segment[j - m] = 1; 63 | } 64 | } 65 | //Loop over the number m ... n and print the primes 66 | for(int i=m; i<=n; i++){ 67 | if(segment[i-m]==0 and i!=1){ 68 | cout< 4 | #include 5 | 6 | int Sort(int arr[], int temp[], int left, int right); 7 | int merge(int arr[], int temp[], int left, int mid, int right); 8 | 9 | int mergeSort(int arr[], int array_size) 10 | { 11 | int* temp = (int*)malloc(sizeof(int) * array_size); 12 | return Sort(arr, temp, 0, array_size - 1); 13 | } 14 | 15 | int Sort(int arr[], int temp[], int left, int right) 16 | { 17 | int mid, inv_count = 0; 18 | if (right > left) 19 | { 20 | mid = (right + left) / 2; 21 | inv_count += Sort(arr, temp, left, mid); 22 | inv_count += Sort(arr, temp, mid + 1, right); 23 | inv_count += merge(arr, temp, left, mid + 1, right); 24 | } 25 | return inv_count; 26 | } 27 | 28 | int merge(int arr[], int temp[], int left, int mid, int right) 29 | { 30 | int i, j, k; 31 | int inv_count = 0; 32 | 33 | i = left; // i is index for left subarray 34 | j = mid; // j is index for right subarray 35 | k = left; // k is index for resultant merged subarray 36 | while ((i <= mid - 1) && (j <= right)) 37 | { 38 | if (arr[i] <= arr[j]) 39 | { 40 | temp[k++] = arr[i++]; 41 | } 42 | else 43 | { 44 | temp[k++] = arr[j++]; 45 | inv_count = inv_count + (mid - i); 46 | } 47 | } 48 | 49 | while (i <= mid - 1) 50 | temp[k++] = arr[i++]; 51 | 52 | while (j <= right) 53 | temp[k++] = arr[j++]; 54 | 55 | //Copy merged elements to original array 56 | for (i = left; i <= right; i++) 57 | arr[i] = temp[i]; 58 | 59 | return inv_count; 60 | } 61 | 62 | int main() 63 | { 64 | int size,i; 65 | printf("Enter size of array:"); 66 | scanf("%d",&size); 67 | int arr[size]; 68 | printf("\nEnter elements of the array:"); 69 | for(i=0;i 3 | #include 4 | 5 | struct Node 6 | { 7 | void *data; 8 | 9 | struct Node *next; 10 | }; 11 | 12 | 13 | void push(struct Node** head_ref, void *new_data, size_t data_size) 14 | { 15 | // Allocate memory for node 16 | struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); 17 | 18 | new_node->data = malloc(data_size); 19 | new_node->next = (*head_ref); 20 | 21 | // Copy contents of new_data to newly allocated memory. 22 | // Assumption: char takes 1 byte. 23 | int i; 24 | for (i=0; idata + i) = *(char *)(new_data + i); 26 | 27 | // Change head pointer as new node is added at the beginning 28 | (*head_ref) = new_node; 29 | } 30 | 31 | 32 | void printList(struct Node *node, void (*fptr)(void *)) 33 | { 34 | while (node != NULL) 35 | { 36 | (*fptr)(node->data); 37 | node = node->next; 38 | } 39 | } 40 | 41 | // Function to print an integer 42 | void printInt(void *n) 43 | { 44 | printf(" %d", *(int *)n); 45 | } 46 | 47 | // Function to print a float 48 | void printFloat(void *f) 49 | { 50 | printf(" %f", *(float *)f); 51 | } 52 | 53 | /* Driver program to test above function */ 54 | int main() 55 | { 56 | struct Node *start = NULL; 57 | 58 | // Create and print an int linked list 59 | unsigned int_size = sizeof(int); 60 | int arr[] = {10, 20, 30, 40, 50}, i; 61 | for (i=4; i>=0; i--) 62 | push(&start, &arr[i], int_size); 63 | printf("Created integer linked list is \n"); 64 | printList(start, printInt); 65 | 66 | // Create and print a float linked list 67 | unsigned float_size = sizeof(float); 68 | start = NULL; 69 | float arr2[] = {10.1, 20.2, 30.3, 40.4, 50.5}; 70 | for (i=4; i>=0; i--) 71 | push(&start, &arr2[i], float_size); 72 | printf("\n\nCreated float linked list is \n"); 73 | printList(start, printFloat); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/Finding_Multiple_Missing_Element_In_An_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Array { 5 | int A[10]; 6 | int size; 7 | }; 8 | 9 | // The input 'arr' needs to be a SORTED array. Time Complexity is O(n), while loop is negligible 10 | // Using the difference between the element and the index 11 | // Example : [6,7,9,10,13,14] 12 | // Diff: A[0] - 0 is 6 13 | // Iterate until the Diff is not 6. A[2] - 2 is 7, so go to while loop. 14 | // Inside while loop, print out 'i + diff', which is 8 (aka the missing value). 15 | // Then increment the diff value. Diff is now 7. In the next iteration, the while loop condition fails. 16 | // Iterate until the Diff is not 7. A[4] - 4 is 9, so go to while loop. 17 | // Inside while loop, print out 'i + diff', which is 11 (aka the missing value). 18 | // Then increment the diff value. Diff is now 8. While loop condition fails, so go to next iteration. 19 | // Inside while loop, print out 'i + diff', which is 12 (aka the missing value). 20 | // Then increment the diff value. Diff is now 9. In the next iteration, the while loop condition fails. 21 | // And so on... 22 | void FindMultipleMissingElements(struct Array arr) { 23 | int diff = arr.A[0] - 0; 24 | for (int i = 0; i < arr.size; i++) { 25 | if (arr.A[i] - i != diff) { 26 | while (diff < arr.A[i] - i) { 27 | cout << i + diff << endl; 28 | diff++; 29 | } 30 | } 31 | } 32 | } 33 | int main() { 34 | struct Array arr = {{3,4,5,6,7,9,10,13,14,15}, 10}; 35 | 36 | // arr.A = {8,10,11,12,13,14,15,16,17,18}; 37 | // This initializer list is illegal syntax for arrays inside a struct. 38 | // Makes sense because initializer list can only be used during initialzation 39 | // Either assign the values when you create the struct object, like how I did it above 40 | // Or assign it individually like this: arr.A[6] = 29; 41 | 42 | FindMultipleMissingElements(arr); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Zigzag order traversal.cpp: -------------------------------------------------------------------------------- 1 | // C++ implementation of a O(n) time method for 2 | // Zigzag order traversal 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | // Binary Tree node 8 | struct Node { 9 | int data; 10 | struct Node *left, *right; 11 | }; 12 | 13 | // function to print the zigzag traversal 14 | void zizagtraversal(struct Node* root) 15 | { 16 | // if null then return 17 | if (!root) 18 | return; 19 | 20 | // declare two stacks 21 | stack currentlevel; 22 | stack nextlevel; 23 | 24 | // push the root 25 | currentlevel.push(root); 26 | 27 | // check if stack is empty 28 | bool lefttoright = true; 29 | while (!currentlevel.empty()) { 30 | 31 | // pop out of stack 32 | struct Node* temp = currentlevel.top(); 33 | currentlevel.pop(); 34 | 35 | // if not null 36 | if (temp) { 37 | 38 | // print the data in it 39 | cout << temp->data << " "; 40 | 41 | // store data according to current 42 | // order. 43 | if (lefttoright) { 44 | if (temp->left) 45 | nextlevel.push(temp->left); 46 | if (temp->right) 47 | nextlevel.push(temp->right); 48 | } 49 | else { 50 | if (temp->right) 51 | nextlevel.push(temp->right); 52 | if (temp->left) 53 | nextlevel.push(temp->left); 54 | } 55 | } 56 | 57 | if (currentlevel.empty()) { 58 | lefttoright = !lefttoright; 59 | swap(currentlevel, nextlevel); 60 | } 61 | } 62 | } 63 | 64 | // A utility function to create a new node 65 | struct Node* newNode(int data) 66 | { 67 | struct Node* node = new struct Node; 68 | node->data = data; 69 | node->left = node->right = NULL; 70 | return (node); 71 | } 72 | 73 | // driver program to test the above function 74 | int main() 75 | { 76 | // create tree 77 | struct Node* root = newNode(1); 78 | root->left = newNode(2); 79 | root->right = newNode(3); 80 | root->left->left = newNode(7); 81 | root->left->right = newNode(6); 82 | root->right->left = newNode(5); 83 | root->right->right = newNode(4); 84 | cout << "ZigZag Order traversal of binary tree is \n"; 85 | 86 | zizagtraversal(root); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /CP-ALGORITHMS/bitwise.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int isodd(int x){ 4 | if(x&1){ 5 | return 1; 6 | } 7 | else{ 8 | return 0; 9 | } 10 | } 11 | 12 | int getithbit(int x,int i){ 13 | /* 14 | 000000101 15 | & 000000100 16 | 000000100 > 0 =>set bit 17 | 000000100 ==0 =>unset bit 18 | */ 19 | int mask=(1<0000111 33 | */ 34 | int mask=1<clear the ith bit first 55 | ||0000100=>if want to set this bit 56 | ||0000000=>if unset the bit 57 | 58 | */ 59 | clearbit(x,i); 60 | int mask=v<~0 or -1 70 | 000001100000 71 | */ 72 | int mask=(~0)<111000000 83 | |000000011(2^i-1) 84 | 111000011 85 | no. & mask=>000000011 86 | */ 87 | int a=(~0)<0){ 97 | 98 | ct+=(n&1); 99 | n=n>>1; 100 | } 101 | return ct; 102 | } 103 | 104 | int convertTobinary(int n){ 105 | int ans=0; 106 | int p=1; 107 | while(n>0){ 108 | ans+=(n&1)*p; 109 | n=n>>1; 110 | p=p*10; 111 | } 112 | return ans; 113 | } 114 | int main(){ 115 | 116 | cout< 3 | #include 4 | 5 | // Merges two subarrays of arr[]. 6 | // First subarray is arr[l..m] 7 | // Second subarray is arr[m+1..r] 8 | void merge(int arr[], int l, int m, int r) 9 | { 10 | int i, j, k; 11 | int n1 = m - l + 1; 12 | int n2 = r - m; 13 | 14 | /* create temp arrays */ 15 | int L[n1], R[n2]; 16 | 17 | /* Copy data to temp arrays L[] and R[] */ 18 | for (i = 0; i < n1; i++) 19 | L[i] = arr[l + i]; 20 | for (j = 0; j < n2; j++) 21 | R[j] = arr[m + 1 + j]; 22 | 23 | /* Merge the temp arrays back into arr[l..r]*/ 24 | i = 0; // Initial index of first subarray 25 | j = 0; // Initial index of second subarray 26 | k = l; // Initial index of merged subarray 27 | while (i < n1 && j < n2) { 28 | if (L[i] <= R[j]) { 29 | arr[k] = L[i]; 30 | i++; 31 | } 32 | else { 33 | arr[k] = R[j]; 34 | j++; 35 | } 36 | k++; 37 | } 38 | 39 | /* Copy the remaining elements of L[], if there 40 | are any */ 41 | while (i < n1) { 42 | arr[k] = L[i]; 43 | i++; 44 | k++; 45 | } 46 | 47 | /* Copy the remaining elements of R[], if there 48 | are any */ 49 | while (j < n2) { 50 | arr[k] = R[j]; 51 | j++; 52 | k++; 53 | } 54 | } 55 | 56 | /* l is for left index and r is right index of the 57 | sub-array of arr to be sorted */ 58 | void mergeSort(int arr[], int l, int r) 59 | { 60 | if (l < r) { 61 | // Same as (l+r)/2, but avoids overflow for 62 | // large l and h 63 | int m = l + (r - l) / 2; 64 | 65 | // Sort first and second halves 66 | mergeSort(arr, l, m); 67 | mergeSort(arr, m + 1, r); 68 | 69 | merge(arr, l, m, r); 70 | } 71 | } 72 | 73 | /* UTILITY FUNCTIONS */ 74 | /* Function to print an array */ 75 | void printArray(int A[], int size) 76 | { 77 | int i; 78 | for (i = 0; i < size; i++) 79 | printf("%d ", A[i]); 80 | printf("\n"); 81 | } 82 | 83 | /* Driver code */ 84 | int main() 85 | { 86 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 87 | int arr_size = sizeof(arr) / sizeof(arr[0]); 88 | 89 | printf("Given array is \n"); 90 | printArray(arr, arr_size); 91 | 92 | mergeSort(arr, 0, arr_size - 1); 93 | 94 | printf("\nSorted array is \n"); 95 | printArray(arr, arr_size); 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /Dining_Philosopher.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Sumit on 26/09/22. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #define N 5 12 | #define THINKING 2 13 | #define HUNGRY 1 14 | #define EATING 0 15 | #define LEFT (phnum + 4) % N 16 | #define RIGHT (phnum + 1) % N 17 | int state[N]; 18 | int phil[N] = { 0, 1, 2, 3, 4 }; 19 | sem_t mutex; 20 | sem_t S[N]; 21 | 22 | void sleep(int i); 23 | 24 | void test(int phnum) 25 | { 26 | if (state[phnum] == HUNGRY && state[LEFT] != EATING&& state[RIGHT] != EATING) 27 | { 28 | // state that eating 29 | state[phnum] = EATING; 30 | sleep(2); 31 | printf("Philosopher %d takes fork %d and %d\n", phnum + 1, LEFT + 1, phnum + 1); 32 | printf("Philosopher %d is Eating\n", phnum + 1); 33 | sem_post(&S[phnum]); 34 | } 35 | } 36 | 37 | void sleep(int i) { 38 | 39 | } 40 | 41 | // take up chopsticks 42 | void take_fork(int phnum) 43 | { 44 | sem_wait(&mutex); 45 | // state that hungry 46 | state[phnum] = HUNGRY; 47 | printf("Philosopher %d is Hungry\n", phnum + 1); 48 | // eat if neighbours are not eating 49 | test(phnum); 50 | sem_post(&mutex); 51 | // if unable to eat wait to be signalled 52 | sem_wait(&S[phnum]); 53 | sleep(1); 54 | } 55 | // put down chopsticks 56 | void put_fork(int phnum) 57 | { 58 | sem_wait(&mutex); 59 | // state that thinking 60 | state[phnum] = THINKING; 61 | printf("Philosopher %d putting fork %d and %d down\n",phnum + 1, LEFT + 1, phnum + 1); 62 | printf("Philosopher %d is thinking\n", phnum + 1); 63 | test(LEFT); 64 | test(RIGHT); 65 | sem_post(&mutex); 66 | } 67 | void* philosopher(void* num) 68 | { 69 | while (1) { 70 | int* i = num; 71 | sleep(1); 72 | take_fork(*i); 73 | sleep(0); 74 | put_fork(*i); 75 | } 76 | } 77 | int main() 78 | { 79 | int i; 80 | pthread_t thread_id[N]; 81 | // initialize the semaphores 82 | sem_init(&mutex, 0, 1); 83 | for (i = 0; i < N; i++) 84 | sem_init(&S[i], 0, 0); 85 | for (i = 0; i < N; i++) { 86 | // create philosopher processes 87 | pthread_create(&thread_id[i], NULL,philosopher, &phil[i]); 88 | printf("Philosopher %d is thinking\n", i + 1); 89 | } 90 | for (i = 0; i < N; i++) 91 | pthread_join(thread_id[i], NULL); 92 | } -------------------------------------------------------------------------------- /quick.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int divide(int *a,int start,int end) 5 | { 6 | int i=start+1,j,t; 7 | int div=a[start]; 8 | for(j=start+1;j<=end;j++) 9 | { 10 | if(a[j]=10000000) 108 | printf("\t%ld",a[i][j]); 109 | else 110 | printf("\t%ld\t",a[i][j]); 111 | } 112 | printf("\n"); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /multipleparentheses.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct stack 5 | { 6 | int size; 7 | int top; 8 | char *arr; 9 | }; 10 | 11 | int isEmpty(struct stack *ptr) 12 | { 13 | if (ptr->top == -1) 14 | { 15 | return 1; 16 | } 17 | else 18 | { 19 | return 0; 20 | } 21 | } 22 | 23 | int isFull(struct stack *ptr) 24 | { 25 | if (ptr->top == ptr->size - 1) 26 | { 27 | return 1; 28 | } 29 | else 30 | { 31 | return 0; 32 | } 33 | } 34 | 35 | void push(struct stack* ptr, char val){ 36 | if(isFull(ptr)){ 37 | printf("Stack Overflow! Cannot push %d to the stack\n", val); 38 | } 39 | else{ 40 | ptr->top++; 41 | ptr->arr[ptr->top] = val; 42 | } 43 | } 44 | 45 | char pop(struct stack* ptr){ 46 | if(isEmpty(ptr)){ 47 | printf("Stack Underflow! Cannot pop from the stack\n"); 48 | return -1; 49 | } 50 | else{ 51 | char val = ptr->arr[ptr->top]; 52 | ptr->top--; 53 | return val; 54 | } 55 | } 56 | 57 | char stackTop(struct stack* sp){ 58 | return sp->arr[sp->top]; 59 | } 60 | 61 | int match(char a, char b){ 62 | if(a=='{' && b=='}'){ 63 | return 1; 64 | } 65 | if(a=='(' && b==')'){ 66 | return 1; 67 | } 68 | if(a=='[' && b==']'){ 69 | return 1; 70 | } 71 | return 0; 72 | } 73 | 74 | int parenthesisMatch(char * exp){ 75 | // Create and initialize the stack 76 | struct stack* sp; 77 | sp->size = 100; 78 | sp->top = -1; 79 | sp->arr = (char *)malloc(sp->size * sizeof(char)); 80 | char popped_ch; 81 | 82 | for (int i = 0; exp[i]!='\0'; i++) 83 | { 84 | if(exp[i]=='(' || exp[i]=='{' || exp[i]=='['){ 85 | push(sp, exp[i]); 86 | } 87 | else if(exp[i]==')'|| exp[i]=='}' || exp[i]==']'){ 88 | if(isEmpty(sp)){ 89 | return 0; 90 | } 91 | popped_ch = pop(sp); 92 | if(!match(popped_ch, exp[i])){ 93 | return 0; 94 | } 95 | } 96 | } 97 | 98 | if(isEmpty(sp)){ 99 | return 1; 100 | } 101 | else{ 102 | return 0; 103 | } 104 | 105 | } 106 | 107 | int main() 108 | { 109 | char * exp = "[4-6]((8){(9-8)})"; 110 | 111 | if(parenthesisMatch(exp)){ 112 | printf("The parenthesis is balanced"); 113 | } 114 | else{ 115 | printf("The parenthesis is not balanced"); 116 | } 117 | return 0; 118 | } 119 | -------------------------------------------------------------------------------- /Matrix_Chain_Multiplication_Dynamic_Programming.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int **t,**p; 6 | int n; 7 | int mcmt(int n,int arr[]) 8 | { 9 | //memset(t,0,sizeof(t)); 10 | for (int d=1;dj) printf("x\t"); 27 | else printf("%d\t",t[i][j]); 28 | printf("\n"); 29 | } 30 | printf("Parenthesizing Matrix:\n"); 31 | for(int i=1;ij) printf("x\t"); 34 | else printf("%d\t",p[i][j]); 35 | printf("\n"); 36 | } 37 | return t[1][n]; 38 | } 39 | 40 | void printParenthesis(int i, int j) 41 | { 42 | if (i>=j){ 43 | printf(" A%d ",i); 44 | } 45 | else{ 46 | printf("("); 47 | printParenthesis(i,p[i][j]); 48 | printParenthesis(p[i][j]+1,j); 49 | printf(")"); 50 | } 51 | } 52 | 53 | int main() 54 | { 55 | printf("Enter no. of matrices: "); 56 | scanf("%d",&n); 57 | int *arr=malloc((n+1)*sizeof(int)); 58 | printf("Enter Dimensions: \n"); 59 | for (int i=0;i 75 | 76 | Enter no. of matrices: 4 77 | Enter Dimensions: 78 | 13 5 89 3 34 79 | Tabulation: 80 | 0 5785 1530 2856 81 | x 0 1335 1845 82 | x x 0 9078 83 | x x x 0 84 | Parenthesizing Matrix: 85 | 0 1 1 3 86 | x 0 2 3 87 | x x 0 3 88 | x x x 0 89 | Optimal Multiplication Xost: 2856 90 | Parenthesized: 91 | (( A1 ( A2 A3 )) A4 ) 92 | Process returned 0 (0x0) execution time : 24.225 s 93 | Press any key to continue. 94 | */ 95 | -------------------------------------------------------------------------------- /Adding two polynomials.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | struct poly 7 | { 8 | float coef; 9 | int exp; 10 | }; 11 | 12 | 13 | int main() 14 | { 15 | struct poly *p1, *p2,*p3; 16 | 17 | int i; 18 | int deg1,deg2; 19 | int k=0,l=0; 20 | 21 | printf("Enter the highest degree of polynomial 1:"); //stores the degree of the polynomial 22 | scanf("%d",°1); 23 | p1 = (struct poly *)malloc(deg1 * 2 * sizeof(struct poly)); 24 | 25 | for(i=0;i<=deg1;i++) //taking input of first polynomial 26 | { 27 | 28 | printf("\nEnter the coefficient of x^%d :",i); 29 | scanf("%f",&(p1+i)->coef); 30 | 31 | 32 | (p1+k)->exp = i; 33 | k++; 34 | } 35 | k = 0; 36 | 37 | printf("\nEnter the highest degree of polynomial 2:"); //taking input of second polynomial 38 | scanf("%d",°2); 39 | p2 = (struct poly *)malloc(deg2 * 2 * sizeof(struct poly)); 40 | 41 | for(i=0;i<=deg2;i++) 42 | { 43 | printf("\nEnter the coefficient of x^%d :",i); 44 | scanf("%f",&(p2+i)->coef); 45 | 46 | (p2+k)->exp = i; 47 | k++; 48 | } 49 | 50 | printf("\nThe first polynomial is 1 = %.1f ",(p1)->coef); //to print the first polynomial 51 | 52 | for(i=1;i<=deg1;i++) 53 | { 54 | 55 | printf("+ %.1fx^%d",(p1+i)->coef,(p1+i)->exp); 56 | } 57 | 58 | printf("\n"); 59 | 60 | printf("\nThe second polynomial is = %.1f ",(p2)->coef); //to print the second polynomial 61 | 62 | for(i=1;i<=deg2;i++) 63 | { 64 | 65 | printf("+ %.1fx^%d",(p2+i)->coef,(p2+i)->exp); 66 | } 67 | 68 | printf("\n"); 69 | 70 | p3 = (struct poly *)malloc(deg1 * deg2 * 2 * sizeof(struct poly)) ; //allocating memory for the third polynomial to store the sum 71 | 72 | 73 | if(deg2>deg1) //to add two polynomials 74 | { 75 | for(i=0;i<=deg1;i++) 76 | { 77 | (p3+l)->coef = (p2+i)->coef + (p1+i)->coef; 78 | (p3+l)->exp = (p2+i)->exp; 79 | l++; 80 | } 81 | 82 | for(i=deg1+1;i<=deg2;i++) 83 | { 84 | (p3+l)->coef = (p2+i)->coef; 85 | (p3+l)->exp = (p2+i)->exp; 86 | l++; 87 | } 88 | 89 | } 90 | else 91 | { 92 | for(i=0;i<=deg2;i++) 93 | { 94 | (p3+l)->coef= (p2+i)->coef + (p1+i)->coef; 95 | (p3+l)->exp= (p2+i)->exp; 96 | l++; 97 | } 98 | 99 | for(i=deg2+1;i<=deg1;i++) 100 | { 101 | (p3+l)->coef = (p1+i)->coef; 102 | (p3+l)->exp = (p1+i)->exp; 103 | l++; 104 | } 105 | } 106 | 107 | printf("\nThe sum of two polynomials is = %.1f",(p3)->coef); //to print the sum of two polynomials 108 | for(i=1;icoef,(p3+i)->exp); 111 | } 112 | 113 | printf("\n"); 114 | 115 | free(p1); 116 | free(p2); 117 | free(p3); 118 | return 0; 119 | 120 | } 121 | -------------------------------------------------------------------------------- /kruska.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 30 4 | FILE *f,*o; 5 | typedef struct edge { 6 | int u, v, w; 7 | } edge; 8 | 9 | typedef struct edge_list { 10 | edge data[MAX]; 11 | int n; 12 | } edge_list; 13 | 14 | edge_list elist; 15 | 16 | int Graph[MAX][MAX], n; 17 | edge_list spanlist; 18 | 19 | void kruskalAlgo(); 20 | int find(int belongs[], int vertexno); 21 | void applyUnion(int belongs[], int c1, int c2); // initialising parent. 22 | void sort(); 23 | void print(); 24 | 25 | // Applying Krushkal Algo 26 | void kruskalAlgo() { 27 | int belongs[MAX], i, j, cno1, cno2; 28 | elist.n = 0; 29 | 30 | for (i = 1; i < n; i++) 31 | for (j = 0; j < i; j++) { 32 | if (Graph[i][j] != 0) { 33 | elist.data[elist.n].u = i; 34 | elist.data[elist.n].v = j; 35 | elist.data[elist.n].w = Graph[i][j]; 36 | elist.n++; 37 | } 38 | } 39 | 40 | sort(); 41 | 42 | for (i = 0; i < n; i++) 43 | belongs[i] = i; 44 | 45 | spanlist.n = 0; 46 | 47 | for (i = 0; i < elist.n; i++) { 48 | cno1 = find(belongs, elist.data[i].u); //cno1, cno2 parents 49 | cno2 = find(belongs, elist.data[i].v); 50 | 51 | if (cno1 != cno2) { // ie not cycle 52 | spanlist.data[spanlist.n] = elist.data[i]; 53 | spanlist.n = spanlist.n + 1; 54 | applyUnion(belongs, cno1, cno2); //apply union 55 | } 56 | } 57 | } 58 | 59 | int find(int belongs[], int vertexno) { // find orignal parent 60 | return (belongs[vertexno]); 61 | } 62 | 63 | void applyUnion(int belongs[], int c1, int c2) { //called only when its not a cycle 64 | int i; 65 | 66 | for (i = 0; i < n; i++) 67 | if (belongs[i] == c2) 68 | belongs[i] = c1; 69 | } 70 | 71 | // Sorting algo 72 | void sort() { 73 | int i, j; 74 | edge temp; 75 | 76 | for (i = 1; i < elist.n; i++) 77 | for (j = 0; j < elist.n - 1; j++) 78 | if (elist.data[j].w > elist.data[j + 1].w) { 79 | temp = elist.data[j]; 80 | elist.data[j] = elist.data[j + 1]; 81 | elist.data[j + 1] = temp; 82 | } 83 | } 84 | 85 | // Printing the result 86 | void print() { 87 | int i, cost = 0; 88 | 89 | char vert[7]={'A','B','C','D','E','F','G'}; // setting names of the vertices 90 | 91 | o=fopen("output.txt","w"); 92 | for (i = 0; i < spanlist.n; i++) { 93 | fprintf(o,"\n%c - %c : %d", vert[spanlist.data[i].u], vert[spanlist.data[i].v], spanlist.data[i].w); 94 | 95 | cost = cost + spanlist.data[i].w; 96 | } 97 | 98 | 99 | fprintf(o,"\nSpanning tree cost: %d", cost); 100 | fclose(o); 101 | } 102 | 103 | int main() { 104 | int i, j, total_cost,p[30][30]; 105 | 106 | printf("Kruskal's algorithm in C\n"); 107 | 108 | printf("Enter the no. of vertices:\n"); 109 | scanf("%d", &n); 110 | f=fopen("input.txt","w"); 111 | 112 | printf("\nEnter the cost adjacency matrix:\n"); 113 | for (i = 0; i < n; i++) 114 | { 115 | for (j = 0; j < n; j++) 116 | { 117 | scanf("%d", &p[i][j]); 118 | 119 | fprintf(f,"\n%d",p[i][j]); 120 | } 121 | } 122 | fclose(f); 123 | f=fopen("input.txt","r"); 124 | for(i=0;i 4 | #include 5 | #define SIZE 40 6 | 7 | struct queue { 8 | int items[SIZE]; 9 | int front; 10 | int rear; 11 | }; 12 | 13 | struct queue* createQueue(); 14 | void enqueue(struct queue* q, int); 15 | int dequeue(struct queue* q); 16 | void display(struct queue* q); 17 | int isEmpty(struct queue* q); 18 | void printQueue(struct queue* q); 19 | 20 | struct node { 21 | int vertex; 22 | struct node* next; 23 | }; 24 | 25 | struct node* createNode(int); 26 | 27 | struct Graph { 28 | int numVertices; 29 | struct node** adjLists; 30 | int* visited; 31 | }; 32 | 33 | // BFS algorithm 34 | void bfs(struct Graph* graph, int startVertex) { 35 | struct queue* q = createQueue(); 36 | 37 | graph->visited[startVertex] = 1; 38 | enqueue(q, startVertex); 39 | 40 | while (!isEmpty(q)) { 41 | printQueue(q); 42 | int currentVertex = dequeue(q); 43 | printf("Visited %d\n", currentVertex); 44 | 45 | struct node* temp = graph->adjLists[currentVertex]; 46 | 47 | while (temp) { 48 | int adjVertex = temp->vertex; 49 | 50 | if (graph->visited[adjVertex] == 0) { 51 | graph->visited[adjVertex] = 1; 52 | enqueue(q, adjVertex); 53 | } 54 | temp = temp->next; 55 | } 56 | } 57 | } 58 | 59 | // Creating a node 60 | struct node* createNode(int v) { 61 | struct node* newNode = malloc(sizeof(struct node)); 62 | newNode->vertex = v; 63 | newNode->next = NULL; 64 | return newNode; 65 | } 66 | 67 | // Creating a graph 68 | struct Graph* createGraph(int vertices) { 69 | struct Graph* graph = malloc(sizeof(struct Graph)); 70 | graph->numVertices = vertices; 71 | 72 | graph->adjLists = malloc(vertices * sizeof(struct node*)); 73 | graph->visited = malloc(vertices * sizeof(int)); 74 | 75 | int i; 76 | for (i = 0; i < vertices; i++) { 77 | graph->adjLists[i] = NULL; 78 | graph->visited[i] = 0; 79 | } 80 | 81 | return graph; 82 | } 83 | 84 | // Add edge 85 | void addEdge(struct Graph* graph, int src, int dest) { 86 | // Add edge from src to dest 87 | struct node* newNode = createNode(dest); 88 | newNode->next = graph->adjLists[src]; 89 | graph->adjLists[src] = newNode; 90 | 91 | // Add edge from dest to src 92 | newNode = createNode(src); 93 | newNode->next = graph->adjLists[dest]; 94 | graph->adjLists[dest] = newNode; 95 | } 96 | 97 | // Create a queue 98 | struct queue* createQueue() { 99 | struct queue* q = malloc(sizeof(struct queue)); 100 | q->front = -1; 101 | q->rear = -1; 102 | return q; 103 | } 104 | 105 | // Check if the queue is empty 106 | int isEmpty(struct queue* q) { 107 | if (q->rear == -1) 108 | return 1; 109 | else 110 | return 0; 111 | } 112 | 113 | // Adding elements into queue 114 | void enqueue(struct queue* q, int value) { 115 | if (q->rear == SIZE - 1) 116 | printf("\nQueue is Full!!"); 117 | else { 118 | if (q->front == -1) 119 | q->front = 0; 120 | q->rear++; 121 | q->items[q->rear] = value; 122 | } 123 | } 124 | 125 | // Removing elements from queue 126 | int dequeue(struct queue* q) { 127 | int item; 128 | if (isEmpty(q)) { 129 | printf("Queue is empty"); 130 | item = -1; 131 | } else { 132 | item = q->items[q->front]; 133 | q->front++; 134 | if (q->front > q->rear) { 135 | printf("Resetting queue "); 136 | q->front = q->rear = -1; 137 | } 138 | } 139 | return item; 140 | } 141 | 142 | // Print the queue 143 | void printQueue(struct queue* q) { 144 | int i = q->front; 145 | 146 | if (isEmpty(q)) { 147 | printf("Queue is empty"); 148 | } else { 149 | printf("\nQueue contains \n"); 150 | for (i = q->front; i < q->rear + 1; i++) { 151 | printf("%d ", q->items[i]); 152 | } 153 | } 154 | } 155 | 156 | int main() { 157 | struct Graph* graph = createGraph(6); 158 | addEdge(graph, 0, 1); 159 | addEdge(graph, 0, 2); 160 | addEdge(graph, 1, 2); 161 | addEdge(graph, 1, 4); 162 | addEdge(graph, 1, 3); 163 | addEdge(graph, 2, 4); 164 | addEdge(graph, 3, 4); 165 | 166 | bfs(graph, 0); 167 | 168 | return 0; 169 | } -------------------------------------------------------------------------------- /BST.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Sumit Suman on 03-11-2021. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | struct Node{ 9 | int data; 10 | struct Node* left; 11 | struct Node* right; 12 | int key; 13 | }; 14 | 15 | struct Node* new_node(int val){ 16 | struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); 17 | temp->data = val; 18 | temp->left = temp->right = NULL; 19 | return temp; 20 | } 21 | 22 | struct Node *insert(struct Node *Node, int data) { 23 | if (Node == NULL) return new_node(data); 24 | 25 | if (data < Node->data) 26 | Node->left = insert(Node->left, data); 27 | else 28 | Node->right = insert(Node->right, data); 29 | 30 | return Node; 31 | } 32 | 33 | void inorder(struct Node* root){ 34 | if (root != NULL) { 35 | inorder(root->left); 36 | printf("%d -> ", root->data); 37 | inorder(root->right); 38 | } 39 | } 40 | 41 | void preorder(struct Node* root){ 42 | if(root != NULL){ 43 | printf("%d -> ", root->data); 44 | preorder(root->left); 45 | preorder(root->right); 46 | } 47 | } 48 | 49 | void postorder(struct Node* root){ 50 | if(root != NULL){ 51 | postorder(root->left); 52 | postorder(root->right); 53 | printf("%d -> ", root->data); 54 | } 55 | } 56 | 57 | // Find the inorder successor 58 | struct Node *minValueNode(struct Node *node) { 59 | struct Node *current = node; 60 | // Find the leftmost leaf 61 | while (current && current->left != NULL) 62 | current = current->left; 63 | return current; 64 | } 65 | 66 | // Deleting a node 67 | struct Node *deleteNode(struct Node *root, int key) { 68 | // Return if the tree is empty 69 | if (root == NULL) return root; 70 | // Find the node to be deleted 71 | if (key < root->key) 72 | root->left = deleteNode(root->left, key); 73 | else if (key > root->key) 74 | root->right = deleteNode(root->right, key); 75 | else { 76 | // If the node is with only one child or no child 77 | if (root->left == NULL) { 78 | struct Node *temp = root->right; 79 | free(root); 80 | return temp; 81 | } else if (root->right == NULL) { 82 | struct Node *temp = root->left; 83 | free(root); 84 | return temp; 85 | } 86 | // If the node has two children 87 | struct Node *temp = minValueNode(root->right); 88 | // Place the inorder successor in position of the node to be deleted 89 | root->key = temp->key; 90 | // Delete the inorder successor 91 | root->right = deleteNode(root->right, temp->key); 92 | } 93 | return root; 94 | } 95 | 96 | int count(struct Node* root){ 97 | if(root == NULL){ 98 | return 0; 99 | } 100 | return 1 + count(root->left) + count(root->right); 101 | } 102 | 103 | struct Node *min(struct Node *root){ 104 | if(root == NULL){ 105 | return 0; 106 | } 107 | else if(root->right == NULL) 108 | return root; 109 | else 110 | return min(root->right); 111 | } 112 | 113 | 114 | int main() { 115 | struct Node *root = NULL; 116 | root = insert(root, 5);//Root of tree will be 5 117 | insert(root, 1); 118 | insert(root, 3); 119 | insert(root, 4); 120 | insert(root, 2); 121 | insert(root, 7); 122 | 123 | printf("Inorder traversal: "); 124 | inorder(root); 125 | printf("\n"); 126 | printf("Preorder traversal: "); 127 | preorder(root); 128 | printf("\n"); 129 | printf("Postorder traversal: "); 130 | postorder(root); 131 | printf("\n"); 132 | printf("Total no. of nodes: %d",count(root)); 133 | printf("\n"); 134 | root = min(root); 135 | printf("Minimum value node:%d",root->data); 136 | 137 | 138 | 139 | return 0; 140 | } 141 | 142 | 143 | #include 144 | int main() 145 | { 146 | int N,i; 147 | scanf("%d",&N); 148 | int count = 0; 149 | for(i = 1; i <=N; ++i) 150 | { 151 | if(N % i == 0) 152 | { 153 | count += 1; 154 | } 155 | } 156 | printf("%d",count); 157 | 158 | for(i = 1; i <=N; ++i) { 159 | if (N % i == 0) { 160 | printf("%d ", i); 161 | } 162 | 163 | } 164 | return 0; 165 | } 166 | 167 | 168 | -------------------------------------------------------------------------------- /binary_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node *left; 8 | struct Node *right; 9 | Node(int val) 10 | { 11 | data = val; 12 | left = NULL; 13 | right = NULL; 14 | } 15 | }; 16 | 17 | void preOrder(struct Node *root) 18 | { 19 | if (root == NULL) 20 | { 21 | return; 22 | } 23 | cout << root->data << " "; 24 | preOrder(root->left); 25 | preOrder(root->right); 26 | } 27 | 28 | void inOrder(struct Node *root) 29 | { 30 | if (root == NULL) 31 | { 32 | return; 33 | } 34 | inOrder(root->left); 35 | cout << root->data << " "; 36 | inOrder(root->right); 37 | } 38 | 39 | void postOrder(struct Node *root) 40 | { 41 | 42 | if (root == NULL) 43 | { 44 | return; 45 | } 46 | 47 | postOrder(root->left); 48 | postOrder(root->right); 49 | cout << root->data << " "; 50 | } 51 | 52 | string tree2str(Node* root) { 53 | string ans = to_string(root->data); 54 | if(root == NULL) return ans; 55 | 56 | if (root->left) //left side check 57 | ans += "(" + tree2str(root->left) + ")"; 58 | 59 | if (root->right) { //right side check 60 | if (!root->left) ans += "()"; //left side not present, but right side present 61 | ans += "(" + tree2str(root->right) + ")"; 62 | 63 | } 64 | cout< q; 75 | q.push(root); 76 | q.push(NULL); 77 | while (!q.empty()) 78 | { 79 | Node* node = q.front(); 80 | q.pop(); 81 | if(node != NULL){ 82 | cout<data<<" "; 83 | if(node->left != NULL) 84 | q.push(node->left); 85 | if(node->right != NULL) 86 | q.push(node->right); 87 | }else if(!q.empty()) 88 | q.push(NULL); 89 | 90 | } 91 | 92 | } 93 | 94 | int sumAtKthLevel(Node* root, int k){ 95 | if(root == NULL){ 96 | return -1; 97 | } 98 | queue q; 99 | q.push(root); 100 | q.push(NULL); 101 | int level = 0; 102 | int sum =0; 103 | while (!q.empty()) 104 | { 105 | Node* node = q.front(); 106 | q.pop(); 107 | if(node != NULL){ 108 | { 109 | if(level == k){ 110 | sum+= node->data; 111 | } 112 | } 113 | if(node->left != NULL) 114 | q.push(node->left); 115 | if(node->right != NULL) 116 | q.push(node->right); 117 | }else if(!q.empty()){ 118 | q.push(NULL); 119 | level++; 120 | } 121 | 122 | } 123 | return sum; 124 | } 125 | 126 | int countNodes(Node* root){ 127 | if(root == NULL){ 128 | return 0; 129 | } 130 | return countNodes(root->left) + countNodes(root->right) + 1; 131 | 132 | } 133 | 134 | int sumOfAllNode(Node* root){ 135 | if(root== NULL){ 136 | return 0; 137 | } 138 | return sumOfAllNode(root->left) + sumOfAllNode(root->right) + root->data; 139 | } 140 | 141 | int calHeight(Node* root){ 142 | if(root == NULL){ 143 | return 0; 144 | } 145 | int lheight = calHeight(root->left); 146 | int rheight = calHeight(root->right); 147 | return max(lheight,rheight) + 1; 148 | } 149 | // Diamter is the number of nodes in the longest path between any 2 leaves 150 | int calDiameter(Node* root){ 151 | if(root == NULL){ 152 | return 0; 153 | } 154 | int lheight = calHeight(root->left); 155 | int rheight = calHeight(root->right); 156 | int currDiameter = lheight + rheight + 1; 157 | int ldiameter = calDiameter(root->left); 158 | int rdiameter = calDiameter(root->right); 159 | return max(currDiameter, max(ldiameter,rdiameter)); 160 | } 161 | 162 | int main() 163 | { 164 | 165 | struct Node *root = new Node(1); 166 | root->left = new Node(2); 167 | root->right = new Node(3); 168 | root->left->left = new Node(4); 169 | root->left->right = new Node(5); 170 | root->left->left->left = new Node(6); 171 | root->left->right->right = new Node(7); 172 | root->left->right->right->right = new Node(8); 173 | root->left->left->right=new Node(10); 174 | //preOrder(root); 175 | // cout << endl; 176 | // inOrder(root); 177 | // cout << endl; 178 | // postOrder(root); 179 | // cout< 2 | #include 3 | #define MAX 7 4 | 5 | int deque_arr[MAX]; 6 | int front=-1; 7 | int rear=-1; 8 | 9 | void insert_frontEnd(int item); 10 | void insert_rearEnd(int item); 11 | int delete_frontEnd(); 12 | int delete_rearEnd(); 13 | void display(); 14 | int isEmpty(); 15 | int isFull(); 16 | 17 | int main() 18 | { 19 | int choice,item; 20 | while(1) 21 | { 22 | printf("\n\n1.Insert at the front end\n"); 23 | printf("2.Insert at the rear end\n"); 24 | printf("3.Delete from front end\n"); 25 | printf("4.Delete from rear end\n"); 26 | printf("5.Display\n"); 27 | printf("6.Quit\n"); 28 | printf("\nEnter your choice : "); 29 | scanf("%d",&choice); 30 | 31 | switch(choice) 32 | { 33 | case 1: 34 | printf("\nInput the element for adding in queue : "); 35 | scanf("%d",&item); 36 | insert_frontEnd(item); 37 | break; 38 | case 2: 39 | printf("\nInput the element for adding in queue : "); 40 | scanf("%d",&item); 41 | insert_rearEnd(item); 42 | break; 43 | case 3: 44 | printf("\nElement deleted from front end is : %d\n",delete_frontEnd()); 45 | break; 46 | case 4: 47 | printf("\nElement deleted from rear end is : %d\n",delete_rearEnd()); 48 | break; 49 | case 5: 50 | display(); 51 | break; 52 | case 6: 53 | exit(1); 54 | default: 55 | printf("\nWrong choice\n"); 56 | }/*End of switch*/ 57 | printf("\nfront = %d, rear =%d\n", front , rear); 58 | display(); 59 | }/*End of while*/ 60 | }/*End of main()*/ 61 | 62 | void insert_frontEnd(int item) 63 | { 64 | if( isFull() ) 65 | { 66 | printf("\nQueue Overflow\n"); 67 | return; 68 | } 69 | if( front==-1 )/*If queue is initially empty*/ 70 | { 71 | front=0; 72 | rear=0; 73 | } 74 | else if(front==0) 75 | front=MAX-1; 76 | else 77 | front=front-1; 78 | deque_arr[front]=item ; 79 | }/*End of insert_frontEnd()*/ 80 | 81 | void insert_rearEnd(int item) 82 | { 83 | if( isFull() ) 84 | { 85 | printf("\nQueue Overflow\n"); 86 | return; 87 | } 88 | if(front==-1) /*if queue is initially empty*/ 89 | { 90 | front=0; 91 | rear=0; 92 | } 93 | else if(rear==MAX-1) /*rear is at last position of queue */ 94 | rear=0; 95 | else 96 | rear=rear+1; 97 | deque_arr[rear]=item ; 98 | }/*End of insert_rearEnd()*/ 99 | 100 | int delete_frontEnd() 101 | { 102 | int item; 103 | if( isEmpty() ) 104 | { 105 | printf("\nQueue Underflow\n"); 106 | exit(1); 107 | } 108 | item=deque_arr[front]; 109 | if(front==rear) /*Queue has only one element */ 110 | { 111 | front=-1; 112 | rear=-1; 113 | } 114 | else 115 | if(front==MAX-1) 116 | front=0; 117 | else 118 | front=front+1; 119 | return item; 120 | }/*End of delete_frontEnd()*/ 121 | 122 | int delete_rearEnd() 123 | { 124 | int item; 125 | if( isEmpty() ) 126 | { 127 | printf("\nQueue Underflow\n"); 128 | exit(1); 129 | } 130 | item=deque_arr[rear]; 131 | 132 | if(front==rear) /*queue has only one element*/ 133 | { 134 | front=-1; 135 | rear=-1; 136 | } 137 | else if(rear==0) 138 | rear=MAX-1; 139 | else 140 | rear=rear-1; 141 | return item; 142 | }/*End of delete_rearEnd() */ 143 | 144 | int isFull() 145 | { 146 | if ( (front==0 && rear==MAX-1) || (front==rear+1) ) 147 | return 1; 148 | else 149 | return 0; 150 | }/*End of isFull()*/ 151 | 152 | int isEmpty() 153 | { 154 | if( front == -1) 155 | return 1; 156 | else 157 | return 0; 158 | }/*End of isEmpty()*/ 159 | 160 | void display() 161 | { 162 | int i; 163 | if( isEmpty() ) 164 | { 165 | printf("\nQueue is empty\n"); 166 | return; 167 | } 168 | printf("\nQueue elements :\n"); 169 | i=front; 170 | if( front<=rear ) 171 | { 172 | while(i<=rear) 173 | printf("%d ",deque_arr[i++]); 174 | } 175 | else 176 | { 177 | while(i<=MAX-1) 178 | printf("%d ",deque_arr[i++]); 179 | i=0; 180 | while(i<=rear) 181 | printf("%d ",deque_arr[i++]); 182 | } 183 | printf("\n"); 184 | }/*End of display() */ 185 | -------------------------------------------------------------------------------- /linkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node 5 | { 6 | public: 7 | int data; 8 | node *next; 9 | node(int value) 10 | { 11 | data = value; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | int length(node *head) 17 | { 18 | int l = 0; 19 | node *temp = head; 20 | while (temp != NULL) 21 | { 22 | l++; 23 | temp = temp->next; 24 | } 25 | return l; 26 | } 27 | 28 | void insertAtTail(node *&head, int val) 29 | { 30 | node *n = new node(val); 31 | if (head == NULL) 32 | { 33 | head = n; 34 | return; 35 | } 36 | node *temp = head; 37 | while (temp->next != NULL) 38 | { 39 | temp = temp->next; 40 | } 41 | temp->next = n; 42 | }; 43 | void display(node *head) 44 | { 45 | node *temp = head; 46 | while (temp != NULL) 47 | { 48 | cout << temp->data << "->"; 49 | temp = temp->next; 50 | } 51 | cout << "NULL" << endl; 52 | }; 53 | void insertAthead(node *&head, int val) 54 | { 55 | node *n = new node(val); 56 | n->next = head; 57 | head = n; 58 | }; 59 | bool search(node *head, int key) 60 | { 61 | node *temp = head; 62 | while (temp != NULL) 63 | { 64 | if (temp->data == key) 65 | { 66 | return true; 67 | } 68 | temp = temp->next; 69 | } 70 | return false; 71 | }; 72 | void deletion(node *&head, int val) 73 | { 74 | node *temp = head; 75 | while (temp->next->data != val) 76 | { 77 | temp = temp->next; 78 | } 79 | node *todelete = temp->next; 80 | temp->next = temp->next->next; 81 | delete todelete; 82 | }; 83 | 84 | node *reverse(node *&head) 85 | { 86 | node *prevptr = NULL; 87 | node *currptr = head; 88 | node *nextptr; 89 | while (currptr != NULL) 90 | { 91 | nextptr = currptr->next; 92 | currptr->next = prevptr; 93 | 94 | prevptr = currptr; 95 | currptr = nextptr; 96 | } 97 | return prevptr; 98 | }; 99 | node *reversek(node *&head, int k) 100 | { 101 | node *prevptr = NULL; 102 | node *currptr = head; 103 | node *nextptr; 104 | int count = 0; 105 | while (currptr != NULL && count < k) 106 | { 107 | nextptr = currptr->next; 108 | currptr->next = prevptr; 109 | prevptr = currptr; 110 | currptr = nextptr; 111 | count++; 112 | } 113 | 114 | if (nextptr != NULL) 115 | { 116 | head->next = reversek(nextptr, k); 117 | } 118 | 119 | return prevptr; 120 | } 121 | 122 | void makecycle(node *&head, int pos) 123 | { 124 | node *temp = head; 125 | node *startnode; 126 | 127 | int count = 1; 128 | while (temp->next != NULL) 129 | { 130 | if (count == pos) 131 | { 132 | startnode = temp; 133 | }; 134 | temp = temp->next; 135 | count++; 136 | } 137 | 138 | temp->next = startnode; 139 | } 140 | 141 | //Floyd's Algorithm or hare and tortoise algorithm 142 | // If both the pointers meet at a point then the given link list has a cycle 143 | bool detectcycle(node *&head) 144 | { 145 | node *slow = head; // moves by one block at a time 146 | node *fast = head; // moves by two blocks at a time 147 | 148 | while (fast != NULL && fast->next != NULL) 149 | { 150 | slow = slow->next; 151 | fast = fast->next->next; 152 | 153 | if (fast == slow) 154 | { 155 | return true; 156 | } 157 | } 158 | return false; 159 | } 160 | 161 | void removeCycle(node *&head) 162 | { 163 | node *slow = head; 164 | node *fast = head; 165 | 166 | do 167 | { 168 | slow = slow->next; 169 | fast = fast->next->next; 170 | } while (slow != fast); 171 | 172 | fast = head; 173 | 174 | while (slow->next != fast->next) 175 | { 176 | slow = slow->next; 177 | fast = fast->next; 178 | } 179 | 180 | slow->next = NULL; 181 | } 182 | 183 | 184 | 185 | node *kappend(node *&head, int k) 186 | { 187 | node *newtail; 188 | node *newhead; 189 | node *tail = head; 190 | 191 | int l = length(head); 192 | k = k % l; 193 | int count = 1; 194 | 195 | while (tail->next != NULL) 196 | { 197 | if (count == k) 198 | { 199 | newtail = tail; 200 | } 201 | 202 | if (count == k + 1) 203 | { 204 | newhead = tail; 205 | } 206 | tail = tail->next; 207 | count++; 208 | } 209 | 210 | newtail->next = NULL; 211 | tail->next = head; 212 | 213 | return newhead; 214 | } 215 | 216 | 217 | void intersect(node* &head1, node* &head2, int pos){ 218 | node* temp1 = head1; 219 | pos--; 220 | while (pos--) 221 | { 222 | temp1 = temp1->next; 223 | } 224 | 225 | node* temp2 = head2; 226 | while (temp2->next != NULL) 227 | { 228 | temp2 = temp2->next; 229 | } 230 | temp2->next = temp1; 231 | 232 | 233 | } 234 | 235 | int intersectionpt(node* &head1, node* &head2){ 236 | int l1 = length(head1); 237 | int l2 = length(head2); 238 | int d = 0; 239 | node* ptr1; 240 | node* ptr2; 241 | if(l1>l2){ 242 | d = l1 - l2; 243 | ptr1 = head1; 244 | ptr2 = head2; 245 | }else{ 246 | d = l2- l1; 247 | ptr1 = head2; 248 | ptr2 = head1; 249 | } 250 | while (d) 251 | { 252 | ptr1 = ptr1->next; 253 | if (ptr1 == NULL) 254 | { 255 | return -1; 256 | } 257 | 258 | d--; 259 | } 260 | while (ptr1 != NULL && ptr2 != NULL) 261 | { 262 | if (ptr1 == ptr2) 263 | { 264 | return ptr1->data; 265 | } 266 | ptr1 = ptr1->next; 267 | ptr2 = ptr2->next; 268 | 269 | } 270 | 271 | return -1; 272 | 273 | } 274 | 275 | int main() 276 | { 277 | ios_base::sync_with_stdio(false); 278 | cin.tie(NULL); 279 | node *head = NULL; 280 | insertAtTail(head, 1); 281 | insertAtTail(head, 5); 282 | insertAthead(head, 10); 283 | insertAtTail(head, 8); 284 | insertAtTail(head, 32); 285 | // insertAtTail(head, 6); 286 | // insertAtTail(head, 465); 287 | // insertAtTail(head, 564); 288 | //makecycle(head, 3); 289 | 290 | //cout << detectcycle(head) << endl; 291 | //removeCycle(head); 292 | //cout << detectcycle(head) << endl; 293 | display(head); 294 | // node *newhead = reversek(head, 2); 295 | // display(newhead); 296 | 297 | // deletion(head, 1); 298 | 299 | // cout << length(head); 300 | 301 | node *newhead = kappend(head, 2); 302 | display(newhead); 303 | return 0; 304 | } -------------------------------------------------------------------------------- /restaurant_bill.c: -------------------------------------------------------------------------------- 1 | // restaurant bill system by Aayush TC 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | struct items 8 | { 9 | char item[20]; 10 | float price; 11 | int qty; 12 | }; 13 | 14 | struct order 15 | { 16 | char costomer[50]; // name of the costomer 17 | char date[50]; //will fetch date form __DATE__ 18 | int numOfItems; 19 | struct items itm[50]; 20 | }; 21 | 22 | // function to generate bills 23 | int generateBillHeader(char name[50], char date[30]) 24 | { 25 | 26 | printf("\n\n"); 27 | printf("\t ADV. Restaurant"); 28 | printf("\n\t --------------------"); 29 | printf("\nDate:%s", date); 30 | printf("\nInvoice to: %s", name); 31 | printf("\n"); 32 | printf("---------------------------------------------\n"); 33 | printf("Items\t\t"); 34 | printf("Qty\t\t"); 35 | printf("Total\t\t"); 36 | printf("\n---------------------------------------------"); 37 | printf("\n\n"); 38 | } 39 | 40 | int generateBillBody(char item[30], int qty, float price) 41 | { 42 | printf("%s\t\t", item); 43 | printf("%d\t\t", qty); 44 | printf("%.2f\t\t", qty * price); 45 | printf("\n"); 46 | } 47 | 48 | int generateBillFooter(float total) 49 | { 50 | printf("\n"); 51 | float dis = 0.1 * total; 52 | float net_total = total - dis; 53 | float cgst = 0.09 * net_total; 54 | float grand_total = net_total + 2 * cgst; 55 | 56 | printf("-------------------------------\n"); 57 | printf("Sub Total\t\t\t%.2f", total); 58 | printf("\nDiscount @10%s\t\t\t%.2f", "%", dis); 59 | printf("\n\t\t\t\t-------"); 60 | printf("\n Net Total\t\t\t%.2f", net_total); 61 | printf("\n CGST @9\t\t\t%.2f", cgst); 62 | printf("\n SGST @9\t\t\t%.2f", cgst); 63 | printf("\n-------------------------------------"); 64 | printf("\nGrand Total\t\t\t%.2f", grand_total); 65 | printf("\n---------------------------------------\n"); 66 | } 67 | 68 | int main(int argc, char const *argv[]) 69 | { 70 | float total; 71 | int opt, n; 72 | struct order ord; // particularly for file write 73 | struct order ord1; // particularly for file read 74 | char savebill = 'y'; 75 | char contFlag = 'y'; 76 | char name[50]; 77 | FILE *fp; 78 | 79 | // dashboard 80 | while(contFlag == 'y'){ 81 | float total = 0; 82 | int invoiceFound = 0; 83 | system("CLS"); 84 | printf("\n\nPlease select ypur prefered option: "); 85 | printf("\n\n\t=============ADV. RESTAURANT============="); 86 | printf("\n\n1. Generate Invoice"); 87 | printf("\n2. Show all Invoices"); 88 | printf("\n3. Search Invoice"); 89 | printf("\n4. Exit"); 90 | printf("\n\nPLease select your choice here:\t"); 91 | scanf("%d", &opt); 92 | fgetc(stdin); // to remove mixture error of scanf and fgets... 93 | 94 | switch (opt) 95 | { 96 | case 1: 97 | system("CLS"); 98 | printf("\nPlease enter the name of a costomer:\t"); 99 | fgets(ord.costomer, 50, stdin); 100 | ord.costomer[strlen(ord.costomer) - 1] = 0; // to esc extra \n created by scanf 101 | strcpy(ord.date, __DATE__); 102 | printf("\nPlease enter the number of Items:\t"); 103 | scanf("%d", &n); 104 | ord.numOfItems = n; 105 | for (int i = 0; i < n; i++) 106 | { 107 | fgetc(stdin); 108 | printf("\n\n"); 109 | printf("Please enter the item %d:\t", i + 1); 110 | fgets(ord.itm[i].item, 20, stdin); // items liine jati number halyo teti 111 | ord.itm[i].item[strlen(ord.itm[i].item) - 1] = 0; 112 | printf("Please enter the quantity:\t"); // quantity of particular item 113 | scanf("%d", &ord.itm[i].qty); 114 | printf("Please enter the unit price:\t"); // single price of a particular item 115 | scanf("%f", &ord.itm[i].price); 116 | total += ord.itm[i].qty * ord.itm[i].price; 117 | } 118 | 119 | generateBillHeader(ord.costomer, ord.date); 120 | 121 | for (int i = 0; i < ord.numOfItems; i++) 122 | { 123 | generateBillBody(ord.itm[i].item, ord.itm[i].qty, ord.itm[i].price); 124 | } 125 | 126 | generateBillFooter(total); 127 | printf("\nDO you want to save the invoice [y/n]:\t"); 128 | scanf("%s", &savebill); 129 | if (savebill == 'y') 130 | { 131 | fp = fopen("invoices.txt", "a+"); 132 | fwrite(&ord, sizeof(struct order), 1, fp); 133 | if (fwrite != 0) 134 | { 135 | printf("\nSuccessfully saved"); 136 | } 137 | else 138 | { 139 | printf("\nError saving"); 140 | } 141 | fclose(fp); 142 | } 143 | break; 144 | 145 | case 2: 146 | system("CLS"); 147 | fp = fopen("invoices.txt", "r"); 148 | printf("\n\n ****Your Previous Invoices****\n"); 149 | while (fread(&ord1, sizeof(struct order), 1, fp)) 150 | { 151 | float tot = 0; 152 | generateBillHeader(ord1.costomer, ord1.date); 153 | for (int i = 0; i < ord1.numOfItems; i++) 154 | { 155 | generateBillBody(ord1.itm[i].item, ord1.itm[i].qty, ord1.itm[i].price); 156 | tot += ord1.itm[i].qty * ord1.itm[i].price; 157 | } 158 | generateBillFooter(tot); 159 | } 160 | fclose(fp); 161 | break; 162 | 163 | case 3: 164 | system("CLS"); 165 | printf("\nPlease Enter the Name of the costomer:\t"); 166 | // fgetc(stdin); 167 | fgets(name, 50, stdin); 168 | name[strlen(name) - 1] = 0; 169 | fp = fopen("invoices.txt", "r"); 170 | printf("\n ****Invoices Of %s****\n", name); 171 | while (fread(&ord1, sizeof(struct order), 1, fp)) 172 | { 173 | float tot = 0; 174 | if (strcmp(ord1.costomer, name) == 0) 175 | { 176 | generateBillHeader(ord1.costomer, ord1.date); 177 | for (int i = 0; i < ord1.numOfItems; i++) 178 | { 179 | generateBillBody(ord1.itm[i].item, ord1.itm[i].qty, ord1.itm[i].price); 180 | tot += ord1.itm[i].qty * ord1.itm[i].price; 181 | } 182 | generateBillFooter(tot); 183 | invoiceFound = 1; 184 | } 185 | } 186 | if (!invoiceFound) 187 | { 188 | printf("\nSorry the invoice for %s doesn't exists", name); 189 | } 190 | fclose(fp); 191 | break; 192 | 193 | case 4: 194 | system("CLS"); 195 | system("exit"); 196 | break; 197 | default: 198 | printf("\nError"); 199 | break; 200 | } 201 | printf("\nDo you want to perform another operation [y/n]:\t"); 202 | scanf("%s", &contFlag); 203 | } 204 | 205 | printf("\n\n"); 206 | return 0; 207 | } -------------------------------------------------------------------------------- /bst.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | struct node 6 | { 7 | int key; 8 | struct node *left; 9 | struct node *right; 10 | struct node *parent; 11 | }; 12 | 13 | struct node *newnode(int item) 14 | { 15 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 16 | temp->key = item; 17 | temp->left = temp->right = temp->parent = NULL; 18 | return temp; 19 | } 20 | struct node *insert(struct node *node, int key) 21 | { 22 | if (node == NULL) 23 | return newnode(key); 24 | else 25 | { 26 | struct node *temp; 27 | if (key < node->key) 28 | { 29 | temp = insert(node->left, key); 30 | node->left = temp; 31 | temp->parent = node; 32 | } 33 | 34 | else 35 | { 36 | temp = insert(node->right, key); 37 | node->right = temp; 38 | temp->parent = node; 39 | } 40 | return node; 41 | } 42 | } 43 | struct node *search(struct node *node, int key) 44 | { 45 | struct node *current = node; 46 | if (current == NULL || current->key == key) 47 | return current; 48 | else if (key < current->key) 49 | return search(current->left, key); 50 | else 51 | return search(current->right, key); 52 | } 53 | struct node *find_min(struct node *node) 54 | { 55 | struct node *current = node; 56 | while (current->left != NULL) 57 | { 58 | current = current->left; 59 | } 60 | return current; 61 | } 62 | struct node *find_max(struct node *node) 63 | { 64 | struct node *current = node; 65 | while (current->right != NULL) 66 | current = current->right; 67 | return current; 68 | } 69 | void inorder(struct node *node) 70 | { 71 | if (node != NULL) 72 | { 73 | inorder(node->left); 74 | cout << node->key << " "; 75 | inorder(node->right); 76 | } 77 | else 78 | return; 79 | } 80 | struct node *successor(struct node *node, int key) 81 | { 82 | struct node *item = search(node, key); 83 | if (item->right != NULL) 84 | { 85 | return find_min(item->right); 86 | } 87 | else 88 | { 89 | struct node *p = item->parent; 90 | while (p != NULL && item == p->right) 91 | { 92 | item = p; 93 | p = p->parent; 94 | } 95 | return p; 96 | } 97 | } 98 | struct node *predecessor(struct node *node, int key) 99 | { 100 | struct node *item = search(node, key); 101 | if (item->left != NULL) 102 | return find_max(item->left); 103 | else 104 | { 105 | struct node *p = item->parent; 106 | while (p != NULL && item == p->left) 107 | { 108 | item = p; 109 | p = p->parent; 110 | } 111 | return p; 112 | } 113 | } 114 | struct node *del(struct node *node, int key) 115 | { 116 | struct node *ptr = NULL; 117 | ptr = search(node, key); 118 | if(ptr==NULL) 119 | return node; 120 | if (ptr->left == NULL && ptr->right == NULL) 121 | { 122 | if (ptr->parent != NULL) 123 | { 124 | if (ptr->parent->left == ptr) 125 | { 126 | ptr->parent->left = NULL; 127 | ptr = NULL; 128 | } 129 | else 130 | { 131 | ptr->parent->right = NULL; 132 | ptr = NULL; 133 | } 134 | } 135 | else 136 | { 137 | node = NULL; 138 | } 139 | free(ptr); 140 | return node; 141 | } 142 | else if (ptr->left == NULL && ptr->right != NULL) 143 | { 144 | if (ptr->parent != NULL) 145 | { 146 | if (ptr->parent->key > key) 147 | { 148 | ptr->parent->left = ptr->right; 149 | ptr->right->parent = ptr->parent; 150 | } 151 | else 152 | { 153 | ptr->parent->right = ptr->right; 154 | ptr->right->parent = ptr->parent; 155 | } 156 | } 157 | else 158 | { 159 | node=ptr->right; 160 | ptr->right->parent=NULL; 161 | } 162 | free(ptr); 163 | return node; 164 | } 165 | else if(ptr->left!=NULL && ptr->right==NULL) 166 | { 167 | if(ptr->parent!=NULL) 168 | { 169 | if(ptr->parent->key>key) 170 | { 171 | ptr->parent->left=ptr->left; 172 | ptr->left->parent=ptr->parent; 173 | } 174 | else 175 | 176 | 177 | { 178 | ptr->parent->right=ptr->left; 179 | ptr->left->parent=ptr->parent; 180 | } 181 | } 182 | else 183 | { 184 | node=ptr->left; 185 | ptr->left->parent=NULL; 186 | } 187 | free(ptr); 188 | return node; 189 | } 190 | else 191 | { 192 | struct node *temp = successor(ptr, key); 193 | int temp_data = temp->key; 194 | del(node, temp->key); 195 | ptr->key = temp_data; 196 | free(temp); 197 | return node; 198 | } 199 | } 200 | int main() 201 | { 202 | struct node *root = NULL,*temp=NULL; 203 | int choice,key; 204 | cout<<"1.Insertion"<>choice; 209 | switch (choice) 210 | { 211 | case 1: 212 | cout<<"Enter the key to be inserted: "; 213 | cin>>key; 214 | root=insert(root,key); 215 | break; 216 | case 2: 217 | cout<<"Enter the key to be deleted: "; 218 | cin>>key; 219 | root=del(root,key); 220 | break; 221 | case 3: 222 | inorder(root); 223 | cout<>key; 228 | temp=predecessor(root,key); 229 | cout<<"Predecessor: "<key<>key; 234 | temp=successor(root,key); 235 | cout<<"Successor: "<key<>key; 240 | temp=search(root,key); 241 | cout<<"Searched Element: "<key<key<key< 2 | #include 3 | #include 4 | clock_t start,end; 5 | double cpu_time_used; 6 | 7 | 8 | void bubblesort(int *a,int n) 9 | { 10 | int i, j; 11 | int swapped,temp; 12 | printf("\nThe array before sorting: "); 13 | for(int i=0;i a[j+1]) 21 | { 22 | temp=a[j]; 23 | a[j]=a[j+1]; 24 | a[j+1]=temp; 25 | swapped = 1; 26 | } 27 | } 28 | 29 | if (swapped == 0) 30 | break; 31 | } 32 | 33 | } 34 | 35 | 36 | void cocktailsort(int *a,int n) 37 | { 38 | int swapped = 1; 39 | int start = 0; 40 | int end = n-1; 41 | printf("\nThe array before sorting: "); 42 | for(int i=0;i a[i + 1]) 50 | { 51 | int temp=a[i]; 52 | a[i]=a[i+1]; 53 | a[i+1]=temp; 54 | swapped = 1; 55 | } 56 | } 57 | if (!swapped) 58 | break; 59 | swapped = 0; 60 | --end; 61 | 62 | for (int i = end - 1; i >= start; --i) 63 | { 64 | if (a[i] > a[i + 1]) 65 | { 66 | int temp=a[i]; 67 | a[i]=a[i+1]; 68 | a[i+1]=temp; 69 | swapped = 1; 70 | } 71 | } 72 | 73 | ++start; 74 | } 75 | 76 | } 77 | 78 | 79 | int partition (int *a, int low, int high) 80 | { 81 | int pivot = a[high]; 82 | int i = (low - 1); 83 | 84 | for (int j = low; j <= high - 1; j++) 85 | { 86 | 87 | if (a[j] < pivot) 88 | { 89 | i++; 90 | int temp=a[i]; 91 | a[i]=a[j]; 92 | a[j]=temp; 93 | } 94 | } 95 | int temp=a[i+1]; 96 | a[i+1]=a[high]; 97 | a[high]=temp; 98 | return (i + 1); 99 | } 100 | 101 | void quicksort(int *a,int low,int high) 102 | { 103 | if (low < high) 104 | { 105 | int pi = partition(a, low, high); 106 | printf("\nThe value of the first element is %d and the last element is %d\n",low,high); 107 | quicksort(a, low, pi - 1); 108 | quicksort(a, pi + 1, high); 109 | } 110 | 111 | } 112 | 113 | 114 | 115 | void insertionsort(int *a,int n) 116 | { 117 | int i, key, j; 118 | printf("\nThe array before sorting: "); 119 | for(int i=0;i= 0 && a[j] > key) 126 | { 127 | a[j + 1] = a[j]; 128 | j = j - 1; 129 | } 130 | a[j + 1] = key; 131 | } 132 | } 133 | 134 | void selectionsort(int *a,int n) 135 | { 136 | int i, j, m; 137 | printf("\nThe array before sorting: "); 138 | for(int i=0;i= 0; i--) 157 | heapify(a, n, i); 158 | for (int i = n - 1; i > 0; i--) 159 | { 160 | int temp=a[0]; 161 | a[0]=a[i]; 162 | a[i]=temp; 163 | heapify(a, i, 0); 164 | } 165 | } 166 | 167 | 168 | void heapify(int a[], int n, int i) 169 | { 170 | int largest = i; 171 | int l = 2 * i + 1; 172 | int r = 2 * i + 2; 173 | 174 | // If left child is larger than root 175 | if (l < n && a[l] > a[largest]) 176 | largest = l; 177 | 178 | // If right child is larger than largest so far 179 | if (r < n && a[r] > a[largest]) 180 | largest = r; 181 | 182 | // If largest is not root 183 | if (largest != i) 184 | { 185 | int temp=a[i]; 186 | a[i]=a[largest]; 187 | a[largest]=temp; 188 | heapify(a, n, largest); 189 | } 190 | } 191 | 192 | 193 | int main() 194 | { 195 | int *a,n,ch,i; 196 | 197 | while(1) 198 | { 199 | 200 | printf("\n"); 201 | printf("\n---OPERATIONS!!--\n"); 202 | printf("\n1.Modified Bubble Sort.\n2.Cocktail Shaker Sort.\n3.Quick Sort.\n4.Insertion Sort.\n5.Selection Sort.\n6.Heap Sort.\n7.Exit.\n"); 203 | printf("\n"); 204 | printf("\nEnter your choice "); 205 | scanf("%d",&ch); 206 | switch(ch) 207 | { 208 | case 1:printf("\nEnter size of the array "); 209 | scanf("%d",&n); 210 | a=(int*)malloc(n*sizeof(int)); 211 | for(i=0;i