├── 6. Data Structures and Algorithms (DSA) ├── 01_helloWorld.c ├── 02_factorialUsingRecursion.c ├── 03_fibonacciSeriesUsingRecursion.c ├── 04_primeNumber.c ├── 05_palindromeNumber.c ├── 17_binaryTreeImplementation.c ├── 06_linearSearch.c ├── 08_bubbleSort.c ├── 10_insertionSort.c ├── 07_binarySearch.c ├── 09_selectionSort.c ├── 16_doublyLinkedListImplementation.c ├── 12_quickSort.c ├── 15_linkedListImplementation.c ├── 23_graphRepresentation.c ├── 13_stackImplementation.c ├── 21_hashTableImplementation.c ├── 20_binarySearchTreeImplementations.c ├── 24_graphTraversalDFS.c ├── 18_depthFirstSearch.c ├── 14_queueImplementation.c ├── 11_mergeSort.c ├── 25_topologicalSorting.c ├── 22_heapImplementation.c └── 19_breadthFirstSearch.c ├── .github ├── ISSUE_TEMPLATE │ ├── custom.md │ ├── feature_request.md │ ├── bug_report.md │ └── security_report.md └── PULL_REQUEST_TEMPLATE │ └── PULL_REQUEST_TEMPLATE.md ├── 1. Tutorial ├── 10_forLoop.c ├── 09_whileLoop.c ├── 13a_string.c ├── 07b_shortHandIf.c ├── 06_boolean.c ├── 15_memoryAddress.c ├── 08_switchCase.c ├── 12b_multiDimensionalArray.c ├── 03_comments.c ├── 05_constants.c ├── 12a_singleDimensionalArray.c ├── 07a_ifelse.c ├── 14_userInput.c ├── 02_escapeSequence.c ├── 11_jumpStatements.c ├── 01_helloworld.c ├── 16a_pointer.c ├── 04b_typeConversion.c ├── 16b_pointerWithArray.c ├── 13b_stringFunctions.c └── 04a_datatypes.c ├── 2. Functions ├── 01_sayHello.c ├── 02_functionWithParameters.c ├── 03_functionDeclaration.c ├── 04_recursionExample.c └── 05_mathFunctions.c ├── .gitignore ├── 3. File Handling ├── 01_createFile.c ├── 03_readFile.c ├── 02a_writeToFile.c └── 02b_writeToFileInAppendMode.c ├── 4. Structures └── 01_structures.c ├── LICENSE ├── 5. Enums └── 01_enums.c ├── SECURITY.md ├── CONTRIBUTING.md ├── README.md └── CODE_OF_CONDUCT.md /6. Data Structures and Algorithms (DSA)/01_helloWorld.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | printf("Hello, World!\n"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/custom.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Custom issue template 3 | about: Describe this issue template's purpose here. 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | 11 | -------------------------------------------------------------------------------- /1. Tutorial/10_forLoop.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // For loop to print numbers from 1 to 5 5 | for (int count = 1; count <= 5; count++) { 6 | printf("%d\n", count); 7 | } 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /1. Tutorial/09_whileLoop.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int count = 1; 5 | 6 | // While loop to print numbers from 1 to 5 7 | while (count <= 5) { 8 | printf("%d\n", count); 9 | count++; 10 | } 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /2. Functions/01_sayHello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to print "Hello, World!" 4 | void sayHello() { 5 | printf("Hello, World!\n"); 6 | } 7 | 8 | // Main function 9 | int main() { 10 | // Call the sayHello function 11 | sayHello(); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /1. Tutorial/13a_string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Static input for the name 5 | char name[] = "John"; 6 | 7 | // Display a personalized greeting 8 | printf("Hello, %s! Welcome to the world of \"C programming\".\n", name); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /1. Tutorial/07b_shortHandIf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num = 10; 5 | 6 | // Shorthand if-else statement 7 | // Syntax: condition ? expression_if_true : expression_if_false 8 | num > 0 ? printf("%d is positive.\n", num) : printf("%d is not positive.\n", num); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /1. Tutorial/06_boolean.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // Boolean variables with bool type 6 | bool isRaining = true; // true - 1 7 | bool isSunny = false; // false - 0 8 | 9 | printf("Is it raining: %d\n", isRaining); 10 | 11 | printf("Is it sunny: %d", isSunny); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # .gitignore for C programming 2 | 3 | # Compiled Object files 4 | *.o 5 | *.ko 6 | *.obj 7 | *.elf 8 | 9 | # Executables 10 | *.out 11 | *.app 12 | *.exe 13 | 14 | # Temporary files 15 | *.swp 16 | *~ 17 | 18 | # Visual Studio Code 19 | .vscode/ 20 | 21 | # Sublime Text 22 | *.tmlanguage.cache 23 | *.tmPreferences.cache 24 | *.stTheme.cache 25 | 26 | # Text Files 27 | *.txt -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/02_factorialUsingRecursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int factorial(int n) { 4 | if (n == 0 || n == 1) { 5 | return 1; 6 | } else { 7 | return n * factorial(n - 1); 8 | } 9 | } 10 | 11 | int main() { 12 | int number; 13 | printf("Enter a number: "); 14 | scanf("%d", &number); 15 | 16 | printf("Factorial of %d = %d\n", number, factorial(number)); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /1. Tutorial/15_memoryAddress.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Declare variables 5 | int integerValue = 42; 6 | float floatValue = 3.14; 7 | char charValue = 'A'; 8 | 9 | // Print memory addresses 10 | printf("Memory address of integerValue: %p\n", (void*)&integerValue); 11 | printf("Memory address of floatValue: %p\n", (void*)&floatValue); 12 | printf("Memory address of charValue: %p\n", (void*)&charValue); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /1. Tutorial/08_switchCase.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Static input value 5 | int choice = 2; 6 | 7 | switch (choice) { 8 | case 1: 9 | printf("You selected Option 1.\n"); 10 | break; 11 | case 2: 12 | printf("You selected Option 2.\n"); 13 | break; 14 | case 3: 15 | printf("You selected Option 3.\n"); 16 | break; 17 | default: 18 | printf("Invalid choice.\n"); 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /1. Tutorial/12b_multiDimensionalArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Declaration and initialization of a 2D integer array 5 | int matrix[3][3] = { 6 | {1, 2, 3}, 7 | {4, 5, 6}, 8 | {7, 8, 9} 9 | }; 10 | 11 | // Displaying the elements of the 2D array 12 | printf("Matrix elements:\n"); 13 | for (int i = 0; i < 3; i++) { 14 | for (int j = 0; j < 3; j++) { 15 | printf("%d ", matrix[i][j]); 16 | } 17 | printf("\n"); 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /1. Tutorial/03_comments.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // This is a single-line comment 5 | printf("Hello, World!"); // This comment is at the end of the line 6 | 7 | /* 8 | This is a multi-line comment. 9 | It can span multiple lines and is often used for more detailed explanations. 10 | */ 11 | 12 | // The following code is commented out and won't be executed 13 | /* 14 | printf("\nThis line is commented out and won't be executed."); 15 | */ 16 | 17 | return 0; // End of the program 18 | } 19 | -------------------------------------------------------------------------------- /1. Tutorial/05_constants.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Constants for conversion 5 | const double MILES_TO_KILOMETERS = 1.60934; 6 | const double POUNDS_TO_KILOGRAMS = 0.453592; 7 | 8 | // Static input values 9 | double miles = 50.0; 10 | double pounds = 150.0; 11 | 12 | // Convert and display the values 13 | double kilometers = miles * MILES_TO_KILOMETERS; 14 | double kilograms = pounds * POUNDS_TO_KILOGRAMS; 15 | 16 | printf("Distance in kilometers: %lf\n", kilometers); 17 | printf("Weight in kilograms: %lf\n", kilograms); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /2. Functions/02_functionWithParameters.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to calculate the sum of two numbers 4 | int add(int a, int b) { 5 | return a + b; 6 | } 7 | 8 | int main() { 9 | // Input: Two numbers 10 | int num1, num2; 11 | 12 | // Get user input 13 | printf("Enter the first number: "); 14 | scanf("%d", &num1); 15 | 16 | printf("Enter the second number: "); 17 | scanf("%d", &num2); 18 | 19 | // Call the add function and display the result 20 | int result = add(num1, num2); 21 | printf("Sum: %d + %d = %d\n", num1, num2, result); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /3. File Handling/01_createFile.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // File pointer 5 | FILE *file; 6 | 7 | // File path 8 | const char *filePath = "newfile.txt"; 9 | 10 | // Open file for writing (create file) 11 | file = fopen(filePath, "w"); 12 | 13 | // Check if the file was created successfully 14 | if (file == NULL) { 15 | printf("Unable to create the file.\n"); 16 | return 1; 17 | } 18 | 19 | // Close the file after creating 20 | fclose(file); 21 | 22 | printf("File '%s' created successfully.\n", filePath); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/03_fibonacciSeriesUsingRecursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void fibonacci(int n) { 4 | int first = 0, second = 1, next; 5 | 6 | printf("Fibonacci Series up to %d terms:\n", n); 7 | 8 | for (int i = 0; i < n; i++) { 9 | printf("%d, ", first); 10 | next = first + second; 11 | first = second; 12 | second = next; 13 | } 14 | 15 | printf("\n"); 16 | } 17 | 18 | int main() { 19 | int terms; 20 | printf("Enter the number of terms: "); 21 | scanf("%d", &terms); 22 | 23 | fibonacci(terms); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /1. Tutorial/12a_singleDimensionalArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Declaration and initialization of an integer array 5 | int numbers[] = {1, 2, 3, 4, 5}; 6 | 7 | // Displaying array elements using a for loop 8 | printf("Array elements:\n"); 9 | for (int i = 0; i < 5; i++) { 10 | printf("%d ", numbers[i]); 11 | } 12 | printf("\n"); 13 | 14 | // Summing up the array elements 15 | int sum = 0; 16 | for (int i = 0; i < 5; i++) { 17 | sum += numbers[i]; 18 | } 19 | 20 | printf("Sum of array elements: %d\n", sum); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /2. Functions/03_functionDeclaration.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function declaration (prototype) 4 | int add(int a, int b); 5 | 6 | int main() { 7 | // Input: Two numbers 8 | int num1, num2; 9 | 10 | // Get user input 11 | printf("Enter the first number: "); 12 | scanf("%d", &num1); 13 | 14 | printf("Enter the second number: "); 15 | scanf("%d", &num2); 16 | 17 | // Call the add function and display the result 18 | int result = add(num1, num2); 19 | printf("Sum: %d + %d = %d\n", num1, num2, result); 20 | 21 | return 0; 22 | } 23 | 24 | // Function definition 25 | int add(int a, int b) { 26 | return a + b; 27 | } 28 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/04_primeNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool isPrime(int n) { 5 | if (n <= 1) { 6 | return false; 7 | } 8 | 9 | for (int i = 2; i * i <= n; i++) { 10 | if (n % i == 0) { 11 | return false; 12 | } 13 | } 14 | 15 | return true; 16 | } 17 | 18 | int main() { 19 | int number; 20 | printf("Enter a number: "); 21 | scanf("%d", &number); 22 | 23 | if (isPrime(number)) { 24 | printf("%d is a prime number.\n", number); 25 | } else { 26 | printf("%d is not a prime number.\n", number); 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1. Tutorial/07a_ifelse.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num = 10; 5 | 6 | // If statement 7 | if (num > 0) { 8 | printf("%d is a positive number.\n", num); 9 | } 10 | 11 | // If-else statement 12 | if (num % 2 == 0) { 13 | printf("%d is an even number.\n", num); 14 | } else { 15 | printf("%d is an odd number.\n", num); 16 | } 17 | 18 | // Else-if ladder 19 | if (num > 0) { 20 | printf("%d is greater than 0.\n", num); 21 | } else if (num < 0) { 22 | printf("%d is less than 0.\n", num); 23 | } else { 24 | printf("%d is equal to 0.\n", num); 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /1. Tutorial/14_userInput.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Integer input 5 | int integerInput; 6 | printf("Enter an integer: "); 7 | scanf("%d", &integerInput); 8 | 9 | // Float input 10 | float floatInput; 11 | printf("Enter a floating-point number: "); 12 | scanf("%f", &floatInput); 13 | 14 | // String input 15 | char stringInput[100]; 16 | printf("Enter a string: "); 17 | scanf("%s", stringInput); 18 | 19 | // Displaying the inputs 20 | printf("\nYou entered:\n"); 21 | printf("Integer: %d\n", integerInput); 22 | printf("Float: %f\n", floatInput); 23 | printf("String: %s\n", stringInput); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/05_palindromeNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | bool isPalindrome(const char *str) { 6 | int length = strlen(str); 7 | for (int i = 0; i < length / 2; i++) { 8 | if (str[i] != str[length - 1 - i]) { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | 15 | int main() { 16 | char input[100]; 17 | printf("Enter a string: "); 18 | scanf("%s", input); 19 | 20 | if (isPalindrome(input)) { 21 | printf("%s is a palindrome.\n", input); 22 | } else { 23 | printf("%s is not a palindrome.\n", input); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /1. Tutorial/02_escapeSequence.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // The \n escape sequence represents a new line. 5 | printf("This is an example of a new line: \n"); 6 | 7 | // The \t escape sequence represents a horizontal tab. 8 | printf("This is an example of a tab: \t TAB\n"); 9 | 10 | // The \\ escape sequence represents a backslash. 11 | printf("This is an example of a backslash: \\ \n"); 12 | 13 | // The \" escape sequence represents a double quote. 14 | printf("This is an example of a double quote: \" \n"); 15 | 16 | // The %% escape sequence represents a percent sign. 17 | printf("This is an example of a percent sign: %% \n"); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /1. Tutorial/11_jumpStatements.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Example of break in a loop 5 | printf("Example of break in a loop:\n"); 6 | for (int i = 1; i <= 5; i++) { 7 | if (i == 3) { 8 | printf("Breaking the loop at i = 3.\n"); 9 | break; 10 | } 11 | printf("%d\n", i); 12 | } 13 | 14 | // Example of continue in a loop 15 | printf("\nExample of continue in a loop:\n"); 16 | for (int j = 1; j <= 5; j++) { 17 | if (j == 2 || j == 4) { 18 | printf("Skipping iteration at j = %d using continue.\n", j); 19 | continue; 20 | } 21 | printf("%d\n", j); 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /3. File Handling/03_readFile.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // File pointer 5 | FILE *file; 6 | 7 | // File path 8 | const char *filePath = "newfile.txt"; 9 | 10 | // Open file for reading 11 | file = fopen(filePath, "r"); 12 | 13 | // Check if the file opened successfully for reading 14 | if (file == NULL) { 15 | printf("Unable to open the file for reading.\n"); 16 | return 1; 17 | } 18 | 19 | // Read and display data from the file 20 | printf("Contents of the file:\n"); 21 | char ch; 22 | while ((ch = fgetc(file)) != EOF) { 23 | putchar(ch); 24 | } 25 | 26 | // Close the file after reading 27 | fclose(file); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1. Tutorial/01_helloworld.c: -------------------------------------------------------------------------------- 1 | // The following line includes the standard input/output library, 2 | // allowing the program to use functions like printf for output. 3 | #include 4 | 5 | // The main function is the entry point of every C program. 6 | // It returns an integer value (int). 7 | int main() { 8 | 9 | // The printf function is used to print text to the console. 10 | // In this case, it prints the string "Hello, World!". 11 | printf("Hello, World!"); 12 | 13 | // The return statement indicates the end of the main function 14 | // and returns the value 0 to the operating system, signifying 15 | // successful execution. Conventionally, a return value of 0 16 | // indicates success. 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /3. File Handling/02a_writeToFile.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // File pointer 5 | FILE *file; 6 | 7 | // File path 8 | const char *filePath = "newfile.txt"; 9 | 10 | // Open file for writing 11 | file = fopen(filePath, "w"); 12 | 13 | // Check if the file opened successfully 14 | if (file == NULL) { 15 | printf("Unable to open the file for writing.\n"); 16 | return 1; 17 | } 18 | 19 | // Data to write to the file 20 | const char *data = "Hello, File!\nThis is a simple file write example."; 21 | 22 | // Write data to the file 23 | fprintf(file, "%s", data); 24 | 25 | // Close the file after writing 26 | fclose(file); 27 | 28 | printf("Data written to the file successfully.\n"); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/17_binaryTreeImplementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct TreeNode { 5 | int data; 6 | struct TreeNode* left; 7 | struct TreeNode* right; 8 | }; 9 | 10 | struct TreeNode* createNode(int newData) { 11 | struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode)); 12 | newNode->data = newData; 13 | newNode->left = newNode->right = NULL; 14 | return newNode; 15 | } 16 | 17 | int main() { 18 | struct TreeNode* root = createNode(1); 19 | root->left = createNode(2); 20 | root->right = createNode(3); 21 | root->left->left = createNode(4); 22 | root->left->right = createNode(5); 23 | 24 | // Additional operations on the binary tree can be added here 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1. Tutorial/16a_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Declare a variable 5 | int number = 42; 6 | 7 | // Declare a pointer and initialize it with the address of the variable 8 | int *pointer = &number; 9 | 10 | // Access the value using the pointer 11 | printf("Value of the variable: %d\n", number); 12 | printf("Value accessed through the pointer: %d\n", *pointer); 13 | 14 | // Modify the value using the pointer 15 | *pointer = 99; 16 | 17 | // Display the modified value 18 | printf("Modified value of the variable: %d\n", number); 19 | 20 | // Display the memory address 21 | printf("Memory address of the variable: %p\n", (void*)&number); 22 | printf("Memory address stored in the pointer: %p\n", (void*)pointer); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /1. Tutorial/04b_typeConversion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Implicit Type Conversion 5 | int integerVar = 10; 6 | double doubleVar = integerVar; // Implicit conversion from int to double 7 | printf("Implicit Conversion - Integer to Double: %lf\n", doubleVar); 8 | 9 | // Explicit Type Conversion (Casting) 10 | double anotherDoubleVar = 3.14; 11 | int intFromDouble = (int)anotherDoubleVar; // Explicit conversion from double to int 12 | printf("Explicit Conversion - Double to Integer: %d\n", intFromDouble); 13 | 14 | // Mixed-Type Expressions 15 | int num1 = 5; 16 | double num2 = 2.5; 17 | double result = num1 + num2; // Mixed-type expression, int implicitly converted to double 18 | printf("Mixed-Type Expression: %lf\n", result); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/06_linearSearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int linearSearch(int arr[], int size, int key) { 4 | for (int i = 0; i < size; i++) { 5 | if (arr[i] == key) { 6 | return i; // Return the index if key is found 7 | } 8 | } 9 | return -1; // Return -1 if key is not found 10 | } 11 | 12 | int main() { 13 | int arr[] = {5, 3, 9, 7, 1}; 14 | int size = sizeof(arr) / sizeof(arr[0]); 15 | int key; 16 | 17 | printf("Enter the element to search: "); 18 | scanf("%d", &key); 19 | 20 | int result = linearSearch(arr, size, key); 21 | 22 | if (result != -1) { 23 | printf("%d found at index %d.\n", key, result); 24 | } else { 25 | printf("%d not found in the array.\n", key); 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /3. File Handling/02b_writeToFileInAppendMode.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // File pointer 5 | FILE *file; 6 | 7 | // File path 8 | const char *filePath = "newfile.txt"; 9 | 10 | // Open file for writing in append mode 11 | file = fopen(filePath, "a"); 12 | 13 | // Check if the file opened successfully 14 | if (file == NULL) { 15 | printf("Unable to open the file for writing in append mode.\n"); 16 | return 1; 17 | } 18 | 19 | // Data to append to the file 20 | const char *data = "\nThis data is appended to the file."; 21 | 22 | // Write data to the file 23 | fprintf(file, "%s", data); 24 | 25 | // Close the file after writing 26 | fclose(file); 27 | 28 | printf("Data appended to the file successfully.\n"); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /4. Structures/01_structures.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Define a structure named 'Person' 4 | struct Person { 5 | char name[50]; 6 | int age; 7 | float height; 8 | }; 9 | 10 | int main() { 11 | // Declare a variable of type 'struct Person' 12 | struct Person person1; 13 | 14 | // Input: Get information about a person 15 | printf("Enter name: "); 16 | scanf("%s", person1.name); 17 | 18 | printf("Enter age: "); 19 | scanf("%d", &person1.age); 20 | 21 | printf("Enter height (in meters): "); 22 | scanf("%f", &person1.height); 23 | 24 | // Display the information using the struct 25 | printf("\nPerson Information:\n"); 26 | printf("Name: %s\n", person1.name); 27 | printf("Age: %d\n", person1.age); 28 | printf("Height: %.2f meters\n", person1.height); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /2. Functions/04_recursionExample.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to print numbers from 1 to n using recursion 4 | void printNumbers(int n) { 5 | if (n > 0) { 6 | // Recursive call to print numbers from 1 to n 7 | printNumbers(n - 1); 8 | printf("%d ", n); 9 | } 10 | } 11 | 12 | int main() { 13 | // Input: The limit of numbers to print 14 | int limit; 15 | 16 | // Get user input 17 | printf("Enter the limit of numbers to print: "); 18 | scanf("%d", &limit); 19 | 20 | // Check if the limit is positive 21 | if (limit <= 0) { 22 | printf("Please enter a positive limit.\n"); 23 | } else { 24 | // Call the printNumbers function to print numbers from 1 to the given limit 25 | printf("Numbers from 1 to %d: ", limit); 26 | printNumbers(limit); 27 | printf("\n"); 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /1. Tutorial/16b_pointerWithArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Declare an array 5 | int numbers[] = {1, 2, 3, 4, 5}; 6 | 7 | // Declare a pointer and initialize it with the address of the first element of the array 8 | int *pointer = numbers; 9 | 10 | // Access and print array elements using pointer notation 11 | printf("Array elements using pointer notation:\n"); 12 | for (int i = 0; i < 5; i++) { 13 | printf("%d ", *(pointer + i)); 14 | } 15 | printf("\n"); 16 | 17 | // Modify array elements using pointer notation 18 | for (int i = 0; i < 5; i++) { 19 | *(pointer + i) *= 2; 20 | } 21 | 22 | // Display the modified array 23 | printf("Modified array elements using pointer notation:\n"); 24 | for (int i = 0; i < 5; i++) { 25 | printf("%d ", numbers[i]); 26 | } 27 | printf("\n"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/08_bubbleSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void bubbleSort(int arr[], int size) { 4 | for (int i = 0; i < size - 1; i++) { 5 | for (int j = 0; j < size - i - 1; j++) { 6 | if (arr[j] > arr[j + 1]) { 7 | // Swap elements if they are in the wrong order 8 | int temp = arr[j]; 9 | arr[j] = arr[j + 1]; 10 | arr[j + 1] = temp; 11 | } 12 | } 13 | } 14 | } 15 | 16 | int main() { 17 | int arr[] = {64, 25, 12, 22, 11}; 18 | int size = sizeof(arr) / sizeof(arr[0]); 19 | 20 | printf("Original array: "); 21 | for (int i = 0; i < size; i++) { 22 | printf("%d ", arr[i]); 23 | } 24 | 25 | bubbleSort(arr, size); 26 | 27 | printf("\nSorted array: "); 28 | for (int i = 0; i < size; i++) { 29 | printf("%d ", arr[i]); 30 | } 31 | 32 | printf("\n"); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/10_insertionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void insertionSort(int arr[], int size) { 4 | for (int i = 1; i < size; i++) { 5 | int key = arr[i]; 6 | int j = i - 1; 7 | 8 | // Move elements of arr[0..i-1] that are greater than key to one position ahead of their current position 9 | while (j >= 0 && arr[j] > key) { 10 | arr[j + 1] = arr[j]; 11 | j--; 12 | } 13 | 14 | arr[j + 1] = key; 15 | } 16 | } 17 | 18 | int main() { 19 | int arr[] = {12, 11, 13, 5, 6}; 20 | int size = sizeof(arr) / sizeof(arr[0]); 21 | 22 | printf("Original array: "); 23 | for (int i = 0; i < size; i++) { 24 | printf("%d ", arr[i]); 25 | } 26 | 27 | insertionSort(arr, size); 28 | 29 | printf("\nSorted array: "); 30 | for (int i = 0; i < size; i++) { 31 | printf("%d ", arr[i]); 32 | } 33 | 34 | printf("\n"); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/07_binarySearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int binarySearch(int arr[], int low, int high, int key) { 4 | while (low <= high) { 5 | int mid = (low + high) / 2; 6 | 7 | if (arr[mid] == key) { 8 | return mid; // Return the index if key is found 9 | } else if (arr[mid] < key) { 10 | low = mid + 1; 11 | } else { 12 | high = mid - 1; 13 | } 14 | } 15 | 16 | return -1; // Return -1 if key is not found 17 | } 18 | 19 | int main() { 20 | int arr[] = {1, 3, 5, 7, 9}; 21 | int size = sizeof(arr) / sizeof(arr[0]); 22 | int key; 23 | 24 | printf("Enter the element to search: "); 25 | scanf("%d", &key); 26 | 27 | int result = binarySearch(arr, 0, size - 1, key); 28 | 29 | if (result != -1) { 30 | printf("%d found at index %d.\n", key, result); 31 | } else { 32 | printf("%d not found in the array.\n", key); 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/09_selectionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void selectionSort(int arr[], int size) { 4 | for (int i = 0; i < size - 1; i++) { 5 | int minIndex = i; 6 | for (int j = i + 1; j < size; j++) { 7 | if (arr[j] < arr[minIndex]) { 8 | minIndex = j; 9 | } 10 | } 11 | // Swap the found minimum element with the first element 12 | int temp = arr[minIndex]; 13 | arr[minIndex] = arr[i]; 14 | arr[i] = temp; 15 | } 16 | } 17 | 18 | int main() { 19 | int arr[] = {64, 25, 12, 22, 11}; 20 | int size = sizeof(arr) / sizeof(arr[0]); 21 | 22 | printf("Original array: "); 23 | for (int i = 0; i < size; i++) { 24 | printf("%d ", arr[i]); 25 | } 26 | 27 | selectionSort(arr, size); 28 | 29 | printf("\nSorted array: "); 30 | for (int i = 0; i < size; i++) { 31 | printf("%d ", arr[i]); 32 | } 33 | 34 | printf("\n"); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Dev Verma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/16_doublyLinkedListImplementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | struct Node* prev; 8 | }; 9 | 10 | void insertAtEnd(struct Node** head, int newData) { 11 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 12 | newNode->data = newData; 13 | newNode->next = NULL; 14 | 15 | if (*head == NULL) { 16 | newNode->prev = NULL; 17 | *head = newNode; 18 | return; 19 | } 20 | 21 | struct Node* last = *head; 22 | while (last->next != NULL) { 23 | last = last->next; 24 | } 25 | 26 | last->next = newNode; 27 | newNode->prev = last; 28 | } 29 | 30 | void printList(struct Node* node) { 31 | while (node != NULL) { 32 | printf("%d -> ", node->data); 33 | node = node->next; 34 | } 35 | printf("NULL\n"); 36 | } 37 | 38 | int main() { 39 | struct Node* head = NULL; 40 | 41 | insertAtEnd(&head, 1); 42 | insertAtEnd(&head, 2); 43 | insertAtEnd(&head, 3); 44 | 45 | printf("Doubly Linked List: "); 46 | printList(head); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/security_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Security Vulnerability Report 3 | about: Report a security vulnerability in the c-programming repository 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | ### Vulnerability Details 11 | 12 | **Description:** 13 | A concise description of the security vulnerability. 14 | 15 | **Steps to Reproduce:** 16 | 1. Detail the steps to reproduce the vulnerability. 17 | 2. Be specific and provide clear instructions. 18 | 19 | **Impact:** 20 | Explain the potential impact of the vulnerability. 21 | 22 | ### Additional Information 23 | 24 | **Environment (if applicable):** 25 | - Operating System: 26 | - Compiler Version: 27 | - Other relevant details. 28 | 29 | **Attachments (if applicable):** 30 | Attach any files, screenshots, or additional documentation related to the vulnerability. 31 | 32 | ### Contact Information 33 | 34 | **Your Name:** 35 | Provide your name or pseudonym. 36 | 37 | **Your Email:** 38 | Provide a contact email for further communication. 39 | 40 | ### Responsible Disclosure 41 | 42 | I confirm that I will follow responsible disclosure principles and not publicly disclose or exploit the vulnerability before it has been addressed by the maintainers. 43 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/12_quickSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int* a, int* b) { 4 | int t = *a; 5 | *a = *b; 6 | *b = t; 7 | } 8 | 9 | int partition(int arr[], int low, int high) { 10 | int pivot = arr[high]; 11 | int i = (low - 1); 12 | 13 | for (int j = low; j <= high - 1; j++) { 14 | if (arr[j] < pivot) { 15 | i++; 16 | swap(&arr[i], &arr[j]); 17 | } 18 | } 19 | swap(&arr[i + 1], &arr[high]); 20 | return (i + 1); 21 | } 22 | 23 | void quickSort(int arr[], int low, int high) { 24 | if (low < high) { 25 | int pi = partition(arr, low, high); 26 | 27 | quickSort(arr, low, pi - 1); 28 | quickSort(arr, pi + 1, high); 29 | } 30 | } 31 | 32 | int main() { 33 | int arr[] = {10, 7, 8, 9, 1, 5}; 34 | int size = sizeof(arr) / sizeof(arr[0]); 35 | 36 | printf("Original array: "); 37 | for (int i = 0; i < size; i++) { 38 | printf("%d ", arr[i]); 39 | } 40 | 41 | quickSort(arr, 0, size - 1); 42 | 43 | printf("\nSorted array: "); 44 | for (int i = 0; i < size; i++) { 45 | printf("%d ", arr[i]); 46 | } 47 | 48 | printf("\n"); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /1. Tutorial/13b_stringFunctions.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // Declare and initialize strings 6 | char str1[] = "Hello"; 7 | char str2[] = "World"; 8 | 9 | // String Length (strlen) 10 | printf("Length of str1: %zu\n", strlen(str1)); 11 | 12 | // String Concatenation (strcat) 13 | strcat(str1, " "); 14 | strcat(str1, str2); 15 | printf("Concatenated String: %s\n", str1); 16 | 17 | // String Copy (strcpy) 18 | char copy[20]; 19 | strcpy(copy, str1); 20 | printf("Copied String: %s\n", copy); 21 | 22 | // String Comparison (strcmp) 23 | if (strcmp(str1, copy) == 0) { 24 | printf("The two strings are equal.\n"); 25 | } else { 26 | printf("The two strings are not equal.\n"); 27 | } 28 | 29 | // Substring (strstr) 30 | char sentence[] = "This is a sample sentence."; 31 | char word[] = "sample"; 32 | char* substring = strstr(sentence, word); //Pointer variable - We will learn about it in upcoming 33 | 34 | if (substring != NULL) { 35 | printf("'%s' found in the sentence.\n", word); 36 | } else { 37 | printf("'%s' not found in the sentence.\n", word); 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/15_linkedListImplementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Node structure for a linked list 5 | struct Node { 6 | int data; 7 | struct Node* next; 8 | }; 9 | 10 | // Function to insert a new node at the end of the linked list 11 | void append(struct Node** head, int newData) { 12 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 13 | struct Node* last = *head; 14 | 15 | newNode->data = newData; 16 | newNode->next = NULL; 17 | 18 | if (*head == NULL) { 19 | *head = newNode; 20 | return; 21 | } 22 | 23 | while (last->next != NULL) { 24 | last = last->next; 25 | } 26 | 27 | last->next = newNode; 28 | } 29 | 30 | // Function to print the elements of the linked list 31 | void printList(struct Node* node) { 32 | while (node != NULL) { 33 | printf("%d -> ", node->data); 34 | node = node->next; 35 | } 36 | printf("NULL\n"); 37 | } 38 | 39 | int main() { 40 | struct Node* head = NULL; 41 | 42 | // Append elements to the linked list 43 | append(&head, 1); 44 | append(&head, 2); 45 | append(&head, 3); 46 | append(&head, 4); 47 | 48 | // Print the linked list 49 | printf("Linked List: "); 50 | printList(head); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/23_graphRepresentation.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_VERTICES 10 4 | 5 | struct Graph { 6 | int vertices; 7 | int matrix[MAX_VERTICES][MAX_VERTICES]; 8 | }; 9 | 10 | void initGraph(struct Graph* g, int vertices) { 11 | g->vertices = vertices; 12 | for (int i = 0; i < MAX_VERTICES; i++) { 13 | for (int j = 0; j < MAX_VERTICES; j++) { 14 | g->matrix[i][j] = 0; 15 | } 16 | } 17 | } 18 | 19 | void addEdge(struct Graph* g, int start, int end) { 20 | if (start >= 0 && start < g->vertices && end >= 0 && end < g->vertices) { 21 | g->matrix[start][end] = 1; 22 | g->matrix[end][start] = 1; // Assuming undirected graph 23 | } else { 24 | printf("Invalid vertices.\n"); 25 | } 26 | } 27 | 28 | void printGraph(struct Graph* g) { 29 | printf("Graph (Adjacency Matrix):\n"); 30 | for (int i = 0; i < g->vertices; i++) { 31 | for (int j = 0; j < g->vertices; j++) { 32 | printf("%d ", g->matrix[i][j]); 33 | } 34 | printf("\n"); 35 | } 36 | } 37 | 38 | int main() { 39 | struct Graph graph; 40 | initGraph(&graph, 5); 41 | 42 | addEdge(&graph, 0, 1); 43 | addEdge(&graph, 0, 2); 44 | addEdge(&graph, 1, 3); 45 | addEdge(&graph, 3, 4); 46 | 47 | printGraph(&graph); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /2. Functions/05_mathFunctions.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // Input: A number for mathematical calculations 6 | double number; 7 | 8 | // Get user input 9 | printf("Enter a number: "); 10 | scanf("%lf", &number); 11 | 12 | // Calculate and display the absolute value 13 | printf("Absolute Value of %.2lf = %.2lf\n", number, fabs(number)); 14 | 15 | // Calculate and display the arccosine 16 | printf("Arccosine of %.2lf = %.2lf\n", number, acos(number)); 17 | 18 | // Calculate and display the arcsine 19 | printf("Arcsine of %.2lf = %.2lf\n", number, asin(number)); 20 | 21 | // Calculate and display the arctangent 22 | printf("Arctangent of %.2lf = %.2lf\n", number, atan(number)); 23 | 24 | // Calculate and display the cube root 25 | printf("Cube Root of %.2lf = %.2lf\n", number, cbrt(number)); 26 | 27 | // Calculate and display the cosine 28 | printf("Cosine of %.2lf = %.2lf\n", number, cos(number)); 29 | 30 | // Calculate and display the exponential function 31 | printf("e^%.2lf = %.2lf\n", number, exp(number)); 32 | 33 | // Calculate and display the sine 34 | printf("Sine of %.2lf = %.2lf\n", number, sin(number)); 35 | 36 | // Calculate and display the tangent 37 | printf("Tangent of %.2lf = %.2lf\n", number, tan(number)); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/13_stackImplementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_SIZE 100 5 | 6 | struct Stack { 7 | int items[MAX_SIZE]; 8 | int top; 9 | }; 10 | 11 | void initialize(struct Stack* stack) { 12 | stack->top = -1; 13 | } 14 | 15 | int isEmpty(struct Stack* stack) { 16 | return (stack->top == -1); 17 | } 18 | 19 | int isFull(struct Stack* stack) { 20 | return (stack->top == MAX_SIZE - 1); 21 | } 22 | 23 | void push(struct Stack* stack, int item) { 24 | if (isFull(stack)) { 25 | printf("Stack Overflow\n"); 26 | return; 27 | } 28 | stack->items[++stack->top] = item; 29 | } 30 | 31 | int pop(struct Stack* stack) { 32 | if (isEmpty(stack)) { 33 | printf("Stack Underflow\n"); 34 | return -1; 35 | } 36 | return stack->items[stack->top--]; 37 | } 38 | 39 | int peek(struct Stack* stack) { 40 | if (isEmpty(stack)) { 41 | printf("Stack is empty\n"); 42 | return -1; 43 | } 44 | return stack->items[stack->top]; 45 | } 46 | 47 | int main() { 48 | struct Stack stack; 49 | initialize(&stack); 50 | 51 | push(&stack, 1); 52 | push(&stack, 2); 53 | push(&stack, 3); 54 | 55 | printf("Top element: %d\n", peek(&stack)); 56 | printf("Popped element: %d\n", pop(&stack)); 57 | printf("Top element after pop: %d\n", peek(&stack)); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/21_hashTableImplementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define SIZE 10 6 | 7 | struct KeyValue { 8 | char key[50]; 9 | int value; 10 | }; 11 | 12 | struct HashTable { 13 | struct KeyValue* table[SIZE]; 14 | }; 15 | 16 | int hashFunction(char key[]) { 17 | int sum = 0; 18 | for (int i = 0; key[i] != '\0'; i++) { 19 | sum += key[i]; 20 | } 21 | return sum % SIZE; 22 | } 23 | 24 | void insert(struct HashTable* ht, char key[], int value) { 25 | int index = hashFunction(key); 26 | struct KeyValue* newPair = (struct KeyValue*)malloc(sizeof(struct KeyValue)); 27 | strcpy(newPair->key, key); 28 | newPair->value = value; 29 | ht->table[index] = newPair; 30 | } 31 | 32 | int search(struct HashTable* ht, char key[]) { 33 | int index = hashFunction(key); 34 | if (ht->table[index] != NULL && strcmp(ht->table[index]->key, key) == 0) { 35 | return ht->table[index]->value; 36 | } 37 | return -1; // Not found 38 | } 39 | 40 | int main() { 41 | struct HashTable ht; 42 | for (int i = 0; i < SIZE; i++) { 43 | ht.table[i] = NULL; 44 | } 45 | 46 | insert(&ht, "John", 25); 47 | insert(&ht, "Alice", 30); 48 | 49 | printf("Age of John: %d\n", search(&ht, "John")); 50 | printf("Age of Alice: %d\n", search(&ht, "Alice")); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/20_binarySearchTreeImplementations.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct BSTNode { 5 | int data; 6 | struct BSTNode* left; 7 | struct BSTNode* right; 8 | }; 9 | 10 | struct BSTNode* createNode(int newData) { 11 | struct BSTNode* newNode = (struct BSTNode*)malloc(sizeof(struct BSTNode)); 12 | newNode->data = newData; 13 | newNode->left = newNode->right = NULL; 14 | return newNode; 15 | } 16 | 17 | struct BSTNode* insert(struct BSTNode* root, int newData) { 18 | if (root == NULL) { 19 | return createNode(newData); 20 | } 21 | 22 | if (newData < root->data) { 23 | root->left = insert(root->left, newData); 24 | } else if (newData > root->data) { 25 | root->right = insert(root->right, newData); 26 | } 27 | 28 | return root; 29 | } 30 | 31 | void inorderTraversal(struct BSTNode* root) { 32 | if (root != NULL) { 33 | inorderTraversal(root->left); 34 | printf("%d ", root->data); 35 | inorderTraversal(root->right); 36 | } 37 | } 38 | 39 | int main() { 40 | struct BSTNode* root = NULL; 41 | 42 | root = insert(root, 50); 43 | insert(root, 30); 44 | insert(root, 20); 45 | insert(root, 40); 46 | insert(root, 70); 47 | insert(root, 60); 48 | insert(root, 80); 49 | 50 | printf("Inorder traversal of BST: "); 51 | inorderTraversal(root); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /1. Tutorial/04a_datatypes.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Integer data types 5 | int integerVar = 10; 6 | short int shortVar = 32767; 7 | long int longVar = 2147483647; 8 | long long int longLongVar = 9223372036854775807; 9 | 10 | // Unsigned integer data types 11 | unsigned int unsignedVar = 4294967295; 12 | unsigned short int unsignedShortVar = 65535; 13 | unsigned long int unsignedLongVar = 4294967295; 14 | unsigned long long int unsignedLongLongVar = 18446744073709551615; 15 | 16 | // Floating-point data types 17 | float floatVar = 3.14; 18 | double doubleVar = 3.14159265359; 19 | long double longDoubleVar = 3.141592653589793238; 20 | 21 | // Character data type 22 | char charVar = 'A'; 23 | 24 | // Displaying values 25 | printf("Integer: %d\n", integerVar); 26 | printf("Short Integer: %d\n", shortVar); 27 | printf("Long Integer: %ld\n", longVar); 28 | printf("Long Long Integer: %lld\n", longLongVar); 29 | 30 | printf("Unsigned Integer: %u\n", unsignedVar); 31 | printf("Unsigned Short Integer: %u\n", unsignedShortVar); 32 | printf("Unsigned Long Integer: %lu\n", unsignedLongVar); 33 | printf("Unsigned Long Long Integer: %llu\n", unsignedLongLongVar); 34 | 35 | printf("Float: %f\n", floatVar); 36 | printf("Double: %lf\n", doubleVar); 37 | printf("Long Double: %Lf\n", longDoubleVar); 38 | 39 | printf("Character: %c\n", charVar); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /5. Enums/01_enums.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Define an enumeration named 'Day' 4 | enum Day { 5 | SUNDAY, 6 | MONDAY, 7 | TUESDAY, 8 | WEDNESDAY, 9 | THURSDAY, 10 | FRIDAY, 11 | SATURDAY 12 | }; 13 | 14 | int main() { 15 | // Declare a variable of type 'enum Day' 16 | enum Day today; 17 | 18 | // Input: Get the day from the user (assuming user inputs a valid number between 0 and 6) 19 | int dayNumber; 20 | printf("Enter the day number (0-6): "); 21 | scanf("%d", &dayNumber); 22 | 23 | // Assign the user input to the 'today' variable 24 | today = (enum Day)dayNumber; 25 | 26 | // Display the corresponding day 27 | switch (today) { 28 | case SUNDAY: 29 | printf("Today is Sunday.\n"); 30 | break; 31 | case MONDAY: 32 | printf("Today is Monday.\n"); 33 | break; 34 | case TUESDAY: 35 | printf("Today is Tuesday.\n"); 36 | break; 37 | case WEDNESDAY: 38 | printf("Today is Wednesday.\n"); 39 | break; 40 | case THURSDAY: 41 | printf("Today is Thursday.\n"); 42 | break; 43 | case FRIDAY: 44 | printf("Today is Friday.\n"); 45 | break; 46 | case SATURDAY: 47 | printf("Today is Saturday.\n"); 48 | break; 49 | default: 50 | printf("Invalid day number.\n"); 51 | break; 52 | } 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Reporting a Security Vulnerability 4 | 5 | If you discover a security vulnerability in the c-programming repository, please report it to us by sending an email to [dkv204p@gmail.com]. Please do not create public GitHub issues for security vulnerabilities. 6 | 7 | Include the following details in your email: 8 | 9 | - A brief description of the vulnerability. 10 | - Steps to reproduce the vulnerability. 11 | - Your contact information for further communication. 12 | 13 | We will acknowledge your email within [24 to 48 hours] and will work on addressing the issue promptly. 14 | 15 | ## Responsible Disclosure 16 | 17 | We kindly request that you follow responsible disclosure principles: 18 | 19 | - **Do not publicly disclose the vulnerability before it has been addressed by the maintainers.** 20 | - **Do not exploit the vulnerability for malicious purposes.** 21 | 22 | We appreciate your help in responsibly disclosing any security vulnerabilities. 23 | 24 | ## Security Measures 25 | 26 | While we strive to maintain a secure repository, you can also take the following measures to enhance security: 27 | 28 | - Keep your dependencies up-to-date. 29 | - Review and test code changes thoroughly before submitting pull requests. 30 | 31 | ## Attribution 32 | 33 | This Security Policy is adapted from the [GitHub Security Policy](https://docs.github.com/en/github/managing-security-vulnerabilities/adding-a-security-policy-to-your-repository). 34 | 35 | Thank you for your commitment to security! 36 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Pull Request 3 | about: Submit a contribution to the c-programming repository 4 | 5 | --- 6 | 7 | ## Pull Request Details 8 | 9 | **Description:** 10 | Provide a concise description of your changes. 11 | 12 | **Issue (if applicable):** 13 | Link to the related issue, if one exists. 14 | 15 | ## Type of Change 16 | 17 | Please check the type of change your PR introduces: 18 | 19 | - [ ] Bug Fix 20 | - [ ] New Feature 21 | - [ ] Enhancement (improvements, optimizations, etc.) 22 | - [ ] Documentation Update 23 | - [ ] Other (please specify) 24 | 25 | ## Checklist 26 | 27 | Before submitting your PR, make sure to complete the following tasks: 28 | 29 | - [ ] Code follows the established coding style and conventions. 30 | - [ ] Tests (if applicable) have been added or updated to cover the changes. 31 | - [ ] Documentation has been updated if necessary. 32 | - [ ] Commit messages follow the [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) format. 33 | 34 | ## Additional Information 35 | 36 | Include any additional information that might be relevant to the reviewers. 37 | 38 | ## Screenshots (if applicable) 39 | 40 | Include screenshots or animated GIFs demonstrating the changes (if applicable). 41 | 42 | ## Reviewer Guidelines 43 | 44 | Provide any specific instructions for the reviewer, if necessary. 45 | 46 | ## Closing Issues 47 | 48 | If this PR resolves any open issues, please mention them here using the keyword "Closes" or "Fixes." 49 | 50 | Closes # 51 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/24_graphTraversalDFS.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_VERTICES 10 4 | 5 | struct Graph { 6 | int vertices; 7 | int matrix[MAX_VERTICES][MAX_VERTICES]; 8 | }; 9 | 10 | void initGraph(struct Graph* g, int vertices) { 11 | g->vertices = vertices; 12 | for (int i = 0; i < MAX_VERTICES; i++) { 13 | for (int j = 0; j < MAX_VERTICES; j++) { 14 | g->matrix[i][j] = 0; 15 | } 16 | } 17 | } 18 | 19 | void addEdge(struct Graph* g, int start, int end) { 20 | if (start >= 0 && start < g->vertices && end >= 0 && end < g->vertices) { 21 | g->matrix[start][end] = 1; 22 | g->matrix[end][start] = 1; // Assuming undirected graph 23 | } else { 24 | printf("Invalid vertices.\n"); 25 | } 26 | } 27 | 28 | void dfs(struct Graph* g, int vertex, int visited[]) { 29 | printf("%d ", vertex); 30 | visited[vertex] = 1; 31 | 32 | for (int i = 0; i < g->vertices; i++) { 33 | if (g->matrix[vertex][i] == 1 && !visited[i]) { 34 | dfs(g, i, visited); 35 | } 36 | } 37 | } 38 | 39 | void dfsTraversal(struct Graph* g, int start) { 40 | int visited[MAX_VERTICES] = {0}; 41 | printf("DFS Traversal starting from vertex %d: ", start); 42 | dfs(g, start, visited); 43 | printf("\n"); 44 | } 45 | 46 | int main() { 47 | struct Graph graph; 48 | initGraph(&graph, 5); 49 | 50 | addEdge(&graph, 0, 1); 51 | addEdge(&graph, 0, 2); 52 | addEdge(&graph, 1, 3); 53 | addEdge(&graph, 3, 4); 54 | 55 | dfsTraversal(&graph, 0); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/18_depthFirstSearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct GraphNode { 5 | int data; 6 | struct GraphNode** neighbors; 7 | int numNeighbors; 8 | int visited; 9 | }; 10 | 11 | struct GraphNode* createNode(int newData) { 12 | struct GraphNode* newNode = (struct GraphNode*)malloc(sizeof(struct GraphNode)); 13 | newNode->data = newData; 14 | newNode->neighbors = NULL; 15 | newNode->numNeighbors = 0; 16 | newNode->visited = 0; 17 | return newNode; 18 | } 19 | 20 | void addNeighbor(struct GraphNode* node, struct GraphNode* neighbor) { 21 | node->neighbors = realloc(node->neighbors, (node->numNeighbors + 1) * sizeof(struct GraphNode*)); 22 | node->neighbors[node->numNeighbors++] = neighbor; 23 | } 24 | 25 | void dfs(struct GraphNode* start) { 26 | if (start == NULL) { 27 | return; 28 | } 29 | 30 | printf("%d ", start->data); 31 | start->visited = 1; 32 | 33 | for (int i = 0; i < start->numNeighbors; i++) { 34 | if (!start->neighbors[i]->visited) { 35 | dfs(start->neighbors[i]); 36 | } 37 | } 38 | } 39 | 40 | int main() { 41 | struct GraphNode* node1 = createNode(1); 42 | struct GraphNode* node2 = createNode(2); 43 | struct GraphNode* node3 = createNode(3); 44 | struct GraphNode* node4 = createNode(4); 45 | 46 | addNeighbor(node1, node2); 47 | addNeighbor(node1, node3); 48 | addNeighbor(node2, node4); 49 | addNeighbor(node3, node4); 50 | 51 | printf("DFS traversal starting from node 1: "); 52 | dfs(node1); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/14_queueImplementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_SIZE 100 5 | 6 | struct Queue { 7 | int items[MAX_SIZE]; 8 | int front; 9 | int rear; 10 | }; 11 | 12 | void initialize(struct Queue* queue) { 13 | queue->front = -1; 14 | queue->rear = -1; 15 | } 16 | 17 | int isEmpty(struct Queue* queue) { 18 | return (queue->front == -1 && queue->rear == -1); 19 | } 20 | 21 | int isFull(struct Queue* queue) { 22 | return (queue->rear == MAX_SIZE - 1); 23 | } 24 | 25 | void enqueue(struct Queue* queue, int item) { 26 | if (isFull(queue)) { 27 | printf("Queue is full\n"); 28 | return; 29 | } else if (isEmpty(queue)) { 30 | queue->front = queue->rear = 0; 31 | } else { 32 | queue->rear++; 33 | } 34 | queue->items[queue->rear] = item; 35 | } 36 | 37 | int dequeue(struct Queue* queue) { 38 | int item; 39 | if (isEmpty(queue)) { 40 | printf("Queue is empty\n"); 41 | return -1; 42 | } else if (queue->front == queue->rear) { 43 | item = queue->items[queue->front]; 44 | queue->front = queue->rear = -1; 45 | } else { 46 | item = queue->items[queue->front++]; 47 | } 48 | return item; 49 | } 50 | 51 | int front(struct Queue* queue) { 52 | if (isEmpty(queue)) { 53 | printf("Queue is empty\n"); 54 | return -1; 55 | } 56 | return queue->items[queue->front]; 57 | } 58 | 59 | int main() { 60 | struct Queue queue; 61 | initialize(&queue); 62 | 63 | enqueue(&queue, 1); 64 | enqueue(&queue, 2); 65 | enqueue(&queue, 3); 66 | 67 | printf("Front element: %d\n", front(&queue)); 68 | printf("Dequeued element: %d\n", dequeue(&queue)); 69 | printf("Front element after dequeue: %d\n", front(&queue)); 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/11_mergeSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void merge(int arr[], int left, int mid, int right) { 4 | int n1 = mid - left + 1; 5 | int n2 = right - mid; 6 | 7 | int L[n1], R[n2]; 8 | 9 | // Copy data to temporary arrays L[] and R[] 10 | for (int i = 0; i < n1; i++) 11 | L[i] = arr[left + i]; 12 | for (int j = 0; j < n2; j++) 13 | R[j] = arr[mid + 1 + j]; 14 | 15 | int i = 0, j = 0, k = left; 16 | 17 | while (i < n1 && j < n2) { 18 | if (L[i] <= R[j]) { 19 | arr[k] = L[i]; 20 | i++; 21 | } else { 22 | arr[k] = R[j]; 23 | j++; 24 | } 25 | k++; 26 | } 27 | 28 | // Copy the remaining elements of L[], if there are any 29 | while (i < n1) { 30 | arr[k] = L[i]; 31 | i++; 32 | k++; 33 | } 34 | 35 | // Copy the remaining elements of R[], if there are any 36 | while (j < n2) { 37 | arr[k] = R[j]; 38 | j++; 39 | k++; 40 | } 41 | } 42 | 43 | void mergeSort(int arr[], int left, int right) { 44 | if (left < right) { 45 | // Same as (left+right)/2, but avoids overflow for large left and right 46 | int mid = left + (right - left) / 2; 47 | 48 | // Sort first and second halves 49 | mergeSort(arr, left, mid); 50 | mergeSort(arr, mid + 1, right); 51 | 52 | // Merge the sorted halves 53 | merge(arr, left, mid, right); 54 | } 55 | } 56 | 57 | int main() { 58 | int arr[] = {12, 11, 13, 5, 6, 7}; 59 | int size = sizeof(arr) / sizeof(arr[0]); 60 | 61 | printf("Original array: "); 62 | for (int i = 0; i < size; i++) { 63 | printf("%d ", arr[i]); 64 | } 65 | 66 | mergeSort(arr, 0, size - 1); 67 | 68 | printf("\nSorted array: "); 69 | for (int i = 0; i < size; i++) { 70 | printf("%d ", arr[i]); 71 | } 72 | 73 | printf("\n"); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/25_topologicalSorting.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_VERTICES 10 4 | 5 | struct Graph { 6 | int vertices; 7 | int matrix[MAX_VERTICES][MAX_VERTICES]; 8 | }; 9 | 10 | void initGraph(struct Graph* g, int vertices) { 11 | g->vertices = vertices; 12 | for (int i = 0; i < MAX_VERTICES; i++) { 13 | for (int j = 0; j < MAX_VERTICES; j++) { 14 | g->matrix[i][j] = 0; 15 | } 16 | } 17 | } 18 | 19 | void addEdge(struct Graph* g, int start, int end) { 20 | if (start >= 0 && start < g->vertices && end >= 0 && end < g->vertices) { 21 | g->matrix[start][end] = 1; 22 | // Assuming directed graph for topological sorting 23 | } else { 24 | printf("Invalid vertices.\n"); 25 | } 26 | } 27 | 28 | void dfs(struct Graph* g, int vertex, int visited[], int stack[], int* stackIndex) { 29 | visited[vertex] = 1; 30 | 31 | for (int i = 0; i < g->vertices; i++) { 32 | if (g->matrix[vertex][i] == 1 && !visited[i]) { 33 | dfs(g, i, visited, stack, stackIndex); 34 | } 35 | } 36 | 37 | stack[(*stackIndex)++] = vertex; 38 | } 39 | 40 | void topologicalSort(struct Graph* g) { 41 | int visited[MAX_VERTICES] = {0}; 42 | int stack[MAX_VERTICES]; 43 | int stackIndex = 0; 44 | 45 | for (int i = 0; i < g->vertices; i++) { 46 | if (!visited[i]) { 47 | dfs(g, i, visited, stack, &stackIndex); 48 | } 49 | } 50 | 51 | printf("Topological Sorting: "); 52 | while (stackIndex > 0) { 53 | printf("%d ", stack[--stackIndex]); 54 | } 55 | printf("\n"); 56 | } 57 | 58 | int main() { 59 | struct Graph graph; 60 | initGraph(&graph, 6); 61 | 62 | addEdge(&graph, 5, 2); 63 | addEdge(&graph, 5, 0); 64 | addEdge(&graph, 4, 0); 65 | addEdge(&graph, 4, 1); 66 | addEdge(&graph, 2, 3); 67 | addEdge(&graph, 3, 1); 68 | 69 | topologicalSort(&graph); 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to c-programming 2 | 3 | Thank you for considering contributing to the c-programming repository! Your contributions help improve the learning experience for others interested in C programming. 4 | 5 | Before you start contributing, please take a moment to review the following guidelines. 6 | 7 | ## Code of Conduct 8 | 9 | This project follows a [Code of Conduct](CODE_OF_CONDUCT.md) to ensure a welcoming and inclusive environment for everyone. Please review and abide by this code in all your interactions. 10 | 11 | ## How to Contribute 12 | 13 | 1. **Fork the repository to your GitHub account.** 14 | 15 | 2. **Clone the forked repository to your local machine:** 16 | 17 | git clone https://github.com/dkv204p/c-programming.git 18 | 19 | 4. **Create a new branch for your feature or bug fix:** 20 | 21 | git checkout -b feature-name 22 | 23 | 6. **Make your changes and commit them:** 24 | 25 | git add . 26 | git commit -m "Your descriptive commit message" 27 | 28 | 7. **Push the changes to your forked repository:** 29 | 30 | git push origin feature-name 31 | 32 | 8. **Open a pull request from your forked repository to the original repository.** 33 | 34 | 9. **Provide a clear and concise description of your changes in the pull request.** 35 | 36 | 10. **Be patient! Your contribution will be reviewed, and feedback may be provided.** 37 | 38 | ## Code Style 39 | 40 | Please follow the established coding style and conventions used in the project. If there are specific guidelines, they will be mentioned in the project's documentation. 41 | 42 | ## Report Issues 43 | 44 | If you encounter any issues with the project or have suggestions for improvements, please open an issue on GitHub. 45 | 46 | ## Folder Structure 47 | 48 | - **Tutorial:** Place tutorial programs and documentation here. 49 | - **Functions:** Add function-related programs and explanations. 50 | - **Files:** Store programs related to file handling. 51 | - **Structures:** Include programs and content related to structures. 52 | - **Enums:** Store programs demonstrating the use of enums. 53 | - **Data Structures and Algorithms (DSA):** Place additional examples or miscellaneous programs here (topic wise). 54 | 55 | ## License 56 | 57 | By contributing to this project, you agree that your contributions will be licensed under the [MIT License](LICENSE) associated with this project. 58 | 59 | ## Contact 60 | 61 | If you have further questions or need clarification, feel free to reach out to the maintainers. 62 | 63 | Thank you for your contribution! 64 | 65 | Dev Verma 66 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/22_heapImplementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct MinHeap { 5 | int* heapArray; 6 | int size; 7 | int capacity; 8 | }; 9 | 10 | struct MinHeap* createMinHeap(int capacity) { 11 | struct MinHeap* heap = (struct MinHeap*)malloc(sizeof(struct MinHeap)); 12 | heap->capacity = capacity; 13 | heap->size = 0; 14 | heap->heapArray = (int*)malloc(capacity * sizeof(int)); 15 | return heap; 16 | } 17 | 18 | int parent(int i) { 19 | return (i - 1) / 2; 20 | } 21 | 22 | int leftChild(int i) { 23 | return 2 * i + 1; 24 | } 25 | 26 | int rightChild(int i) { 27 | return 2 * i + 2; 28 | } 29 | 30 | void swap(int* a, int* b) { 31 | int temp = *a; 32 | *a = *b; 33 | *b = temp; 34 | } 35 | 36 | void heapifyUp(struct MinHeap* heap, int index) { 37 | while (index > 0 && heap->heapArray[parent(index)] > heap->heapArray[index]) { 38 | swap(&heap->heapArray[parent(index)], &heap->heapArray[index]); 39 | index = parent(index); 40 | } 41 | } 42 | 43 | void insert(struct MinHeap* heap, int value) { 44 | if (heap->size == heap->capacity) { 45 | printf("Heap is full. Cannot insert.\n"); 46 | return; 47 | } 48 | 49 | heap->heapArray[heap->size] = value; 50 | heap->size++; 51 | heapifyUp(heap, heap->size - 1); 52 | } 53 | 54 | void heapifyDown(struct MinHeap* heap, int index) { 55 | int minIndex = index; 56 | int left = leftChild(index); 57 | int right = rightChild(index); 58 | 59 | if (left < heap->size && heap->heapArray[left] < heap->heapArray[minIndex]) { 60 | minIndex = left; 61 | } 62 | 63 | if (right < heap->size && heap->heapArray[right] < heap->heapArray[minIndex]) { 64 | minIndex = right; 65 | } 66 | 67 | if (index != minIndex) { 68 | swap(&heap->heapArray[index], &heap->heapArray[minIndex]); 69 | heapifyDown(heap, minIndex); 70 | } 71 | } 72 | 73 | int extractMin(struct MinHeap* heap) { 74 | if (heap->size <= 0) { 75 | printf("Heap is empty.\n"); 76 | return -1; 77 | } 78 | 79 | if (heap->size == 1) { 80 | heap->size--; 81 | return heap->heapArray[0]; 82 | } 83 | 84 | int root = heap->heapArray[0]; 85 | heap->heapArray[0] = heap->heapArray[heap->size - 1]; 86 | heap->size--; 87 | heapifyDown(heap, 0); 88 | 89 | return root; 90 | } 91 | 92 | int main() { 93 | struct MinHeap* minHeap = createMinHeap(10); 94 | 95 | insert(minHeap, 3); 96 | insert(minHeap, 2); 97 | insert(minHeap, 1); 98 | 99 | printf("Minimum element: %d\n", extractMin(minHeap)); 100 | 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /6. Data Structures and Algorithms (DSA)/19_breadthFirstSearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Forward declarations 5 | struct GraphNode; 6 | struct Queue; 7 | void enqueue(struct Queue* queue, struct GraphNode* data); 8 | struct GraphNode* dequeue(struct Queue* queue); 9 | 10 | struct GraphNode { 11 | int data; 12 | struct GraphNode** neighbors; 13 | int numNeighbors; 14 | int visited; 15 | }; 16 | 17 | struct QueueNode { 18 | struct GraphNode* data; 19 | struct QueueNode* next; 20 | }; 21 | 22 | struct Queue { 23 | struct QueueNode* front; 24 | struct QueueNode* rear; 25 | }; 26 | 27 | struct GraphNode* createNode(int newData) { 28 | struct GraphNode* newNode = (struct GraphNode*)malloc(sizeof(struct GraphNode)); 29 | newNode->data = newData; 30 | newNode->neighbors = NULL; 31 | newNode->numNeighbors = 0; 32 | newNode->visited = 0; 33 | return newNode; 34 | } 35 | 36 | void addNeighbor(struct GraphNode* node, struct GraphNode* neighbor) { 37 | node->neighbors = realloc(node->neighbors, (node->numNeighbors + 1) * sizeof(struct GraphNode*)); 38 | node->neighbors[node->numNeighbors++] = neighbor; 39 | } 40 | 41 | void bfs(struct GraphNode* start) { 42 | if (start == NULL) { 43 | return; 44 | } 45 | 46 | struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue)); 47 | queue->front = queue->rear = NULL; 48 | 49 | enqueue(queue, start); 50 | start->visited = 1; 51 | 52 | while (queue->front != NULL) { 53 | struct GraphNode* current = dequeue(queue); 54 | printf("%d ", current->data); 55 | 56 | for (int i = 0; i < current->numNeighbors; i++) { 57 | if (!current->neighbors[i]->visited) { 58 | enqueue(queue, current->neighbors[i]); 59 | current->neighbors[i]->visited = 1; 60 | } 61 | } 62 | } 63 | } 64 | 65 | // Function to add a node to the queue 66 | void enqueue(struct Queue* queue, struct GraphNode* data) { 67 | struct QueueNode* newNode = (struct QueueNode*)malloc(sizeof(struct QueueNode)); 68 | newNode->data = data; 69 | newNode->next = NULL; 70 | 71 | if (queue->rear == NULL) { 72 | queue->front = queue->rear = newNode; 73 | } else { 74 | queue->rear->next = newNode; 75 | queue->rear = newNode; 76 | } 77 | } 78 | 79 | // Function to remove a node from the queue 80 | struct GraphNode* dequeue(struct Queue* queue) { 81 | if (queue->front == NULL) { 82 | return NULL; 83 | } 84 | 85 | struct QueueNode* temp = queue->front; 86 | struct GraphNode* data = temp->data; 87 | 88 | queue->front = queue->front->next; 89 | 90 | if (queue->front == NULL) { 91 | queue->rear = NULL; 92 | } 93 | 94 | free(temp); 95 | return data; 96 | } 97 | 98 | int main() { 99 | struct GraphNode* node1 = createNode(1); 100 | struct GraphNode* node2 = createNode(2); 101 | struct GraphNode* node3 = createNode(3); 102 | struct GraphNode* node4 = createNode(4); 103 | 104 | addNeighbor(node1, node2); 105 | addNeighbor(node1, node3); 106 | addNeighbor(node2, node4); 107 | addNeighbor(node3, node4); 108 | 109 | printf("BFS traversal starting from node 1: "); 110 | bfs(node1); 111 | 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C Programming Repository 2 | 3 | Welcome to the "c-programming" repository! This collection serves as a comprehensive resource for learning and mastering the fundamentals of C programming. Whether you're a beginner taking your first steps into coding or an experienced developer looking to sharpen your C skills, you'll find a variety of well-documented programs covering key concepts and practical applications. 4 | 5 | ## Table of Contents 6 | 7 | - [Folders](#folders) 8 | - [Tutorial](#tutorial) 9 | - [Functions](#functions) 10 | - [Files](#files) 11 | - [Structures](#structures) 12 | - [Enums](#enums) 13 | - [Data Structures and Algorithms (DSA)](#DSA) 14 | - [How to Use](#how-to-use) 15 | - [Contributing](#contributing) 16 | - [License](#license) 17 | 18 | ## Folders 19 | 20 | ### Tutorial 21 | 22 | The `Tutorial` folder provides step-by-step guides and explanations to help you understand C programming concepts. Each tutorial is designed to be beginner-friendly, making it an ideal starting point for those new to the language. 23 | 24 | ### Functions 25 | 26 | Explore the `Functions` folder to find examples and demonstrations related to C programming functions. Gain insights into how functions work, their syntax, and best practices for writing efficient code. 27 | 28 | ### Files 29 | 30 | The `Files` folder contains programs and examples related to file handling in C. Learn how to read from and write to files, manipulate file content, and understand file-related concepts. 31 | 32 | ### Structures 33 | 34 | In the `Structures` folder, discover programs that demonstrate the use of structures in C programming. Understand how to define, declare, and work with structures to organize and manage data effectively. 35 | 36 | ### Enums 37 | 38 | Explore the `Enums` folder to learn about enumerations in C. Enumerations provide a way to represent a set of named integer constants, and this section provides examples and explanations to guide you through their usage. 39 | 40 | ### DSA 41 | 42 | The `Data Structures and Algorithms (DSA)` directory organizes C programming examples based on various Data Structures and Algorithms (DSA) concepts. Each subfolder corresponds to a specific DSA topic, making it easier to locate and learn from relevant examples. Use these programs to practice and reinforce your understanding of C programming in the context of different DSA concepts. 43 | 44 | ## How to Use 45 | 46 | 1. **Navigate:** Browse through the categorized folders to find examples related to specific C programming concepts. 47 | 2. **Learn:** Read the code comments for in-depth explanations and insights into the programs. 48 | 3. **Practice:** Engage in hands-on learning with examples and exercises to solidify your understanding of C programming concepts. 49 | 4. **Contribute:** This repository is open for collaboration. Feel free to contribute by opening issues or submitting pull requests. Your feedback and contributions are highly valued! 50 | 51 | ## Contributing 52 | 53 | If you have ideas for improvement, found a bug, or want to add your own C programming examples, follow these steps: 54 | 55 | 1. Fork the repository. 56 | 2. Create a new branch for your feature or bug fix. 57 | 3. Make your changes and commit them. 58 | 4. Push your changes to your fork. 59 | 5. Open a pull request. 60 | 61 | ## License 62 | 63 | This repository is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 64 | 65 | Start coding today! Happy learning! 66 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | dkv204p@gmail.com. 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | --------------------------------------------------------------------------------