├── .vscode └── tasks.json ├── Algorithms ├── Dynamic programming Implementation │ ├── CoinChange.c │ ├── DP.md │ └── FibonacciSeries.c ├── Greedy Implementation │ ├── FractionalKnapsack.c │ └── Greedy Implementation.md └── Random Numbers Search.c ├── Data Structures ├── Array Data Structure.c ├── Binary tree Data Structure.c ├── Graph Data Structure.c ├── Hashing data structure.c ├── Heap Data Structure.c ├── Linked List Data structure.c ├── Queue Data structure.c ├── Set Data Structure.c ├── Stack Data structure.c └── Tree Data structure.c ├── Examples (Algorithms) ├── Simple bug game - 01.c ├── Simple bug game - 02.c ├── The Word Search Game │ ├── WSG - Hash.c │ ├── WSG - linear search.c │ ├── WSG.md │ └── database.txt └── Tic Tac Toe game.c ├── LICENSE └── README.md /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: gcc.exe build active file", 6 | "command": "C:\\Program Files\\CodeBlocks\\MinGW\\bin\\gcc.exe", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /Algorithms/Dynamic programming Implementation/CoinChange.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int minCoins(int coins[], int n, int amount) { 5 | int memo[amount+1]; 6 | memo[0] = 0; 7 | for (int i = 1; i <= amount; i++) { 8 | memo[i] = INT_MAX; 9 | for (int j = 0; j < n; j++) { 10 | if (coins[j] <= i) { 11 | int sub_res = memo[i-coins[j]]; 12 | if (sub_res != INT_MAX && sub_res + 1 < memo[i]) { 13 | memo[i] = sub_res + 1; 14 | } 15 | } 16 | } 17 | } 18 | return memo[amount]; 19 | } 20 | 21 | int main() { 22 | int coins[] = {1, 5, 10, 25}; 23 | int n = sizeof(coins)/sizeof(coins[0]); 24 | int amount = 43; 25 | printf("Minimum number of coins required to make %d cents is %d\n", amount, minCoins(coins, n, amount)); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Algorithms/Dynamic programming Implementation/DP.md: -------------------------------------------------------------------------------- 1 | # Dynamic programming Implementation 2 | 3 | Dynamic programming is a powerful algorithmic technique used to solve problems by breaking them down into smaller subproblems and reusing solutions to those subproblems. This technique is especially useful for problems with overlapping subproblems, where the same subproblem is solved multiple times. Here is an implementation of dynamic programming using C, with examples. 4 | 5 | ## Example 1: Fibonacci Series 6 | 7 | The Fibonacci sequence is a classic example of dynamic programming. The sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones. Here is an implementation of the Fibonacci sequence using dynamic programming: 8 | 9 | ``` 10 | #include 11 | 12 | int fibonacci(int n) { 13 | int memo[n+1]; 14 | memo[0] = 0; 15 | memo[1] = 1; 16 | for (int i = 2; i <= n; i++) { 17 | memo[i] = memo[i-1] + memo[i-2]; 18 | } 19 | return memo[n]; 20 | } 21 | 22 | int main() { 23 | int n = 10; 24 | printf("The %dth number in the Fibonacci sequence is %d", n, fibonacci(n)); 25 | return 0; 26 | } 27 | ``` 28 | 29 | In this implementation, we create an array called memo that stores the results of previous calculations. We start by initializing memo[0] and memo[1] to the first two numbers in the sequence. Then we loop through the remaining numbers and calculate each one by adding the two preceding numbers together. Finally, we return the nth number in the sequence. 30 | 31 | ## Example 2: Coin Change 32 | 33 | The coin change problem is a classic example of dynamic programming. The problem is to find the minimum number of coins needed to make a given amount of change, using a given set of coin denominations. Here is an implementation of the coin change problem using dynamic programming: 34 | 35 | ``` 36 | #include 37 | #include 38 | 39 | int minCoins(int coins[], int n, int amount) { 40 | int memo[amount+1]; 41 | memo[0] = 0; 42 | for (int i = 1; i <= amount; i++) { 43 | memo[i] = INT_MAX; 44 | for (int j = 0; j < n; j++) { 45 | if (coins[j] <= i) { 46 | int sub_res = memo[i-coins[j]]; 47 | if (sub_res != INT_MAX && sub_res + 1 < memo[i]) { 48 | memo[i] = sub_res + 1; 49 | } 50 | } 51 | } 52 | } 53 | return memo[amount]; 54 | } 55 | 56 | int main() { 57 | int coins[] = {1, 5, 10, 25}; 58 | int n = sizeof(coins)/sizeof(coins[0]); 59 | int amount = 43; 60 | printf("Minimum number of coins required to make %d cents is %d\n", amount, minCoins(coins, n, amount)); 61 | return 0; 62 | } 63 | 64 | ``` 65 | 66 | In this implementation, we create an array called memo that stores the minimum number of coins needed to make each amount of change. We start by initializing memo[0] to 0, since no coins are needed to make 0 cents. Then we loop through the remaining amounts and calculate the minimum number of coins needed for each one by iterating through the coin denominations and checking if each coin can be used to make the current amount of change. If so, we subtract the coin value from the current amount and look up the minimum number of coins needed to make the remaining amount in memo. We update memo[i] if the new minimum is smaller than the current value. Finally, we return memo[amount], which contains the minimum number of coins needed to make the given amount of change. -------------------------------------------------------------------------------- /Algorithms/Dynamic programming Implementation/FibonacciSeries.c: -------------------------------------------------------------------------------- 1 | /* The Fibonacci sequence is a classic example of dynamic programming. The sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones. */ 2 | 3 | #include 4 | 5 | int fibonacci(int n) { 6 | int memo[n+1]; 7 | memo[0] = 0; 8 | memo[1] = 1; 9 | for (int i = 2; i <= n; i++) { 10 | memo[i] = memo[i-1] + memo[i-2]; 11 | } 12 | return memo[n]; 13 | } 14 | 15 | int main() { 16 | int n; 17 | printf("Enter the number: "); 18 | scanf("%d", &n); 19 | printf("The %dth number in the Fibonacci sequence is %d", n, fibonacci(n)); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Algorithms/Greedy Implementation/FractionalKnapsack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct { 5 | int weight; 6 | int value; 7 | double density; 8 | } Item; 9 | 10 | int cmp(const void* a, const void* b) { 11 | Item* ia = (Item*)a; 12 | Item* ib = (Item*)b; 13 | return (int)(ib->density - ia->density); 14 | } 15 | 16 | double fractionalKnapsack(int capacity, Item items[], int n) { 17 | double result = 0.0; 18 | qsort(items, n, sizeof(Item), cmp); 19 | for (int i = 0; i < n; i++) { 20 | if (items[i].weight <= capacity) { 21 | result += items[i].value; 22 | capacity -= items[i].weight; 23 | } 24 | else { 25 | result += items[i].density * capacity; 26 | break; 27 | } 28 | } 29 | return result; 30 | } 31 | 32 | int main() { 33 | int capacity = 50; 34 | Item items[] = {{10, 60, 0.6}, {20, 100, 0.5}, {30, 120, 0.4}}; 35 | int n = sizeof(items)/sizeof(items[0]); 36 | printf("Maximum value that can be obtained with a capacity of %d is %.2f\n", capacity, fractionalKnapsack(capacity, items, n)); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Algorithms/Greedy Implementation/Greedy Implementation.md: -------------------------------------------------------------------------------- 1 | # Greedy Implementation 2 | 3 | Greedy algorithms are a class of algorithms that make locally optimal choices at each step, with the hope of finding a global optimum. Here is an implementation of greedy algorithms using C, with examples. 4 | 5 | ## Example 1: Fractional Knapsack 6 | 7 | The fractional knapsack problem is a classic example of a greedy algorithm. The problem is to fill a knapsack with a given capacity with items of different weights and values, such that the total value of the items in the knapsack is maximized. Here is an implementation of the fractional knapsack problem using a greedy algorithm: 8 | 9 | ``` 10 | #include 11 | #include 12 | 13 | typedef struct { 14 | int weight; 15 | int value; 16 | double density; 17 | } Item; 18 | 19 | int cmp(const void* a, const void* b) { 20 | Item* ia = (Item*)a; 21 | Item* ib = (Item*)b; 22 | return (int)(ib->density - ia->density); 23 | } 24 | 25 | double fractionalKnapsack(int capacity, Item items[], int n) { 26 | double result = 0.0; 27 | qsort(items, n, sizeof(Item), cmp); 28 | for (int i = 0; i < n; i++) { 29 | if (items[i].weight <= capacity) { 30 | result += items[i].value; 31 | capacity -= items[i].weight; 32 | } 33 | else { 34 | result += items[i].density * capacity; 35 | break; 36 | } 37 | } 38 | return result; 39 | } 40 | 41 | int main() { 42 | int capacity = 50; 43 | Item items[] = {{10, 60, 0.6}, {20, 100, 0.5}, {30, 120, 0.4}}; 44 | int n = sizeof(items)/sizeof(items[0]); 45 | printf("Maximum value that can be obtained with a capacity of %d is %.2f\n", capacity, fractionalKnapsack(capacity, items, n)); 46 | return 0; 47 | } 48 | 49 | ``` 50 | 51 | In this implementation, we first calculate the density of each item by dividing its value by its weight. Then we sort the items in descending order of density, so that we choose the most valuable items first. We loop through the sorted items and add them to the knapsack until it is full. If an item cannot fit completely into the knapsack, we add a fraction of it proportional to the remaining capacity. Finally, we return the total value of the items in the knapsack. 52 | 53 | ## Example 2: Activity Selection 54 | 55 | The activity selection problem is another classic example of a greedy algorithm. The problem is to select the maximum number of activities that can be performed by a single person or machine, assuming that a person can only work on a single activity at a time. Here is an implementation of the activity selection problem using a greedy algorithm: 56 | 57 | ``` 58 | #include 59 | #include 60 | 61 | typedef struct { 62 | int start; 63 | int finish; 64 | } Activity; 65 | 66 | int cmp(const void* a, const void* b) { 67 | Activity* ia = (Activity*)a; 68 | Activity* ib = (Activity*)b; 69 | return (ia->finish - ib->finish); 70 | } 71 | 72 | int activitySelection(Activity activities[], int n) { 73 | int result = 1; 74 | qsort(activities, n, sizeof(Activity), cmp); 75 | int last = 0; 76 | for (int i = 1; i < n; i++) { 77 | if (activities[i].start >= activities[last].finish) { 78 | result++; 79 | last = i; 80 | } 81 | } 82 | return result; 83 | } 84 | 85 | int main() { 86 | Activity activities[] = {{1, 2}, {3, 4}, {0, 6}, {5, 7}, {8, 9}, {5, 9}}; 87 | int n = sizeof(activities)/sizeof(activities[0]); 88 | printf("Maximum number of activities that can be performed is %d\n", activitySelection(activities, n)); 89 | return 0; 90 | } 91 | 92 | ``` 93 | 94 | In this implementation, we first sort the activities in ascending order of finish time, so that we choose the activities that finish first. We loop through the sorted activities and add them to the list of selected activities if they do not overlap with the last selected activity. Finally, we return the number of selected activities. 95 | -------------------------------------------------------------------------------- /Algorithms/Random Numbers Search.c: -------------------------------------------------------------------------------- 1 | // Find the given number in the Array. 2 | // The array contains random numbers between 1 to 2000000 3 | 4 | #include "stdio.h" 5 | #include "stdlib.h" 6 | #include "time.h" 7 | 8 | int main(void) 9 | { 10 | 11 | // Allocates memory for the array using malloc function 12 | int *array = malloc (sizeof(int)*2000000); 13 | for(int i=0;i<2000000;i++) 14 | { 15 | array[i]=rand(); 16 | } 17 | // Enter the value to search 18 | int value,found=0; 19 | printf("Enter the value to search : "); 20 | scanf("%i",&value); 21 | 22 | clock_t start, end; 23 | double cpu_time_used; 24 | 25 | start = clock(); 26 | 27 | while(value!=0) 28 | { 29 | for(int i=0;i<2000000;i++) 30 | { 31 | // checked array content entered value 32 | if(array[i]==value) 33 | { 34 | found=1; 35 | break; 36 | } 37 | } 38 | if(found==1) 39 | { 40 | printf("Searched data found! \n"); 41 | break; 42 | }else 43 | { 44 | printf("Searched data not found! \n"); 45 | } 46 | } 47 | end = clock(); 48 | cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; 49 | printf("%fcpu_time_used : ",cpu_time_used); 50 | // free memory 51 | free(array); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Data Structures/Array Data Structure.c: -------------------------------------------------------------------------------- 1 | /* Array Data Structure implementation 2 | Date created: Saturday; February 01, 2023 */ 3 | 4 | #include 5 | #include 6 | 7 | #define MAX_SIZE 100 8 | 9 | int array[MAX_SIZE]; 10 | int n = 0; 11 | 12 | void add(int value) { 13 | if (n < MAX_SIZE) { 14 | array[n++] = value; 15 | printf("Value added successfully!\n"); 16 | } else { 17 | printf("Array is full. Cannot add more values.\n"); 18 | } 19 | } 20 | 21 | void update(int index, int value) { 22 | if (index >= 0 && index < n) { 23 | array[index] = value; 24 | printf("Value updated successfully!\n"); 25 | } else { 26 | printf("Invalid index. Cannot update value.\n"); 27 | } 28 | } 29 | 30 | void delete(int index) { 31 | if (index >= 0 && index < n) { 32 | for (int i = index; i < n - 1; i++) { 33 | array[i] = array[i + 1]; 34 | } 35 | n--; 36 | printf("Value deleted successfully!\n"); 37 | } else { 38 | printf("Invalid index. Cannot delete value.\n"); 39 | } 40 | } 41 | 42 | int main() { 43 | int decision, value, index; 44 | while (1) { 45 | printf("1. Add\n"); 46 | printf("2. Update\n"); 47 | printf("3. Delete\n"); 48 | printf("4. Exit\n"); 49 | printf("Enter your decision: "); 50 | scanf("%d", &decision); 51 | switch (decision) { 52 | case 1: 53 | printf("Enter the value to be added: "); 54 | scanf("%d", &value); 55 | add(value); 56 | break; 57 | case 2: 58 | printf("Enter the index of the value to be updated: "); 59 | scanf("%d", &index); 60 | printf("Enter the new value: "); 61 | scanf("%d", &value); 62 | update(index, value); 63 | break; 64 | case 3: 65 | printf("Enter the index of the value to be deleted: "); 66 | scanf("%d", &index); 67 | delete(index); 68 | break; 69 | case 4: 70 | for (int i = 0; i < n; i++) { 71 | printf("%d ", array[i]); 72 | } 73 | printf("\n"); 74 | exit(0); 75 | default: 76 | printf("Invalid decision. Please enter a valid option.\n"); 77 | break; 78 | } 79 | } 80 | return 0; 81 | } 82 | 83 | 84 | // This implementation uses a fixed-size array of size MAX_SIZE to store the values, and the n variable to keep track of the number of elements currently stored in the array. The add function adds a new value to the end of the array, the update function updates the value at a specific index, and the delete function removes the value at a specific index. 85 | 86 | // In this code, the user is prompted to make a decision on what operation to perform (add, update, delete, or exit), and the appropriate function is called based on the decision. The program will keep prompting the user for a decision until they choose to exit the program. -------------------------------------------------------------------------------- /Data Structures/Binary tree Data Structure.c: -------------------------------------------------------------------------------- 1 | /* Binary tree Data structure implementation 2 | Date created: Saturday; February 28, 2023 */ 3 | 4 | #include 5 | #include 6 | 7 | struct node { 8 | int data; 9 | struct node *left; 10 | struct node *right; 11 | }; 12 | 13 | struct node* create_node(int data) { 14 | struct node* new_node = (struct node*)malloc(sizeof(struct node)); 15 | new_node->data = data; 16 | new_node->left = NULL; 17 | new_node->right = NULL; 18 | return new_node; 19 | } 20 | 21 | struct node* insert_node(struct node* root, int data) { 22 | if (root == NULL) { 23 | return create_node(data); 24 | } 25 | if (data < root->data) { 26 | root->left = insert_node(root->left, data); 27 | } else if (data > root->data) { 28 | root->right = insert_node(root->right, data); 29 | } 30 | return root; 31 | } 32 | 33 | struct node* delete_node(struct node* root, int data) { 34 | if (root == NULL) { 35 | return root; 36 | } 37 | if (data < root->data) { 38 | root->left = delete_node(root->left, data); 39 | } else if (data > root->data) { 40 | root->right = delete_node(root->right, data); 41 | } else { 42 | if (root->left == NULL) { 43 | struct node* temp = root->right; 44 | free(root); 45 | return temp; 46 | } else if (root->right == NULL) { 47 | struct node* temp = root->left; 48 | free(root); 49 | return temp; 50 | } 51 | struct node* temp = root->right; 52 | while (temp->left != NULL) { 53 | temp = temp->left; 54 | } 55 | root->data = temp->data; 56 | root->right = delete_node(root->right, temp->data); 57 | } 58 | return root; 59 | } 60 | 61 | struct node* edit_node(struct node* root, int old_data, int new_data) { 62 | if (root == NULL) { 63 | return root; 64 | } 65 | if (old_data < root->data) { 66 | root->left = edit_node(root->left, old_data, new_data); 67 | } else if (old_data > root->data) { 68 | root->right = edit_node(root->right, old_data, new_data); 69 | } else { 70 | root->data = new_data; 71 | } 72 | return root; 73 | } 74 | 75 | void print_inorder(struct node* root) { 76 | if (root != NULL) { 77 | print_inorder(root->left); 78 | printf("%d ", root->data); 79 | print_inorder(root->right); 80 | } 81 | } 82 | 83 | int main() { 84 | struct node* root = NULL; 85 | 86 | root = insert_node(root, 50); 87 | root = insert_node(root, 30); 88 | root = insert_node(root, 20); 89 | root = insert_node(root, 40); 90 | root = insert_node(root, 70); 91 | root = insert_node(root, 60); 92 | root = insert_node(root, 80); 93 | 94 | printf("Inorder traversal of the binary tree: "); 95 | print_inorder(root); 96 | printf("\n"); 97 | 98 | int old_data, new_data; 99 | printf("Enter the data value of the node to edit: "); 100 | scanf("%d", &old_data); 101 | printf("Enter the new data value for the node: "); 102 | scanf("%d", &new_data); 103 | 104 | root = edit_node(root, old_data, new_data); 105 | 106 | printf("Inorder traversal of the binary tree after editing a node: "); 107 | print_inorder(root); 108 | printf("\n"); 109 | 110 | int data_to_delete; 111 | printf("Enter the data value of the"); 112 | root = delete_node(root, data_to_delete); 113 | 114 | printf("Inorder traversal of the binary tree after deleting a node: "); 115 | print_inorder(root); 116 | printf("\n"); 117 | 118 | return 0; 119 | } 120 | 121 | 122 | // In this modified version, after the binary tree is constructed and printed, the user is prompted to enter the data value of the node they want to edit. After entering the old and new data values, the `edit_node` function is called to modify the node with the old data value to have the new data value. Then, the binary tree is printed again to show the modification. 123 | 124 | // Next, the user is prompted to enter the data value of the node they want to delete. After entering the value, the `delete_node` function is called to remove the node with that data value from the binary tree. Finally, the binary tree is printed again to show the deletion. 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /Data Structures/Graph Data Structure.c: -------------------------------------------------------------------------------- 1 | /* Graph Data Structure implementation 2 | Date created: Monday; June 19, 2023. 3 | 4 | In this code, the user is prompted to make a decision on what operation to perform (add, update, delete, print or exit), and the appropriate function is called based on the decision. The program will keep prompting the user for a decision until they choose to exit the program.*/ 5 | 6 | #include 7 | #include 8 | 9 | #define MAX_VERTICES 100 10 | 11 | struct Node { 12 | int vertex; 13 | struct Node* next; 14 | }; 15 | 16 | struct Graph { 17 | struct Node* adjList[MAX_VERTICES]; 18 | }; 19 | 20 | struct Graph* createGraph() { 21 | struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph)); 22 | int i; 23 | for (i = 0; i < MAX_VERTICES; i++) 24 | graph->adjList[i] = NULL; 25 | return graph; 26 | } 27 | 28 | struct Node* createNode(int v) { 29 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 30 | newNode->vertex = v; 31 | newNode->next = NULL; 32 | return newNode; 33 | } 34 | 35 | void addEdge(struct Graph* graph, int src, int dest) { 36 | struct Node* newNode = createNode(dest); 37 | newNode->next = graph->adjList[src]; 38 | graph->adjList[src] = newNode; 39 | 40 | newNode = createNode(src); 41 | newNode->next = graph->adjList[dest]; 42 | graph->adjList[dest] = newNode; 43 | } 44 | 45 | void printGraph(struct Graph* graph) { 46 | int v; 47 | for (v = 0; v < MAX_VERTICES; v++) { 48 | struct Node* temp = graph->adjList[v]; 49 | printf("Adjacency list of vertex %d\n", v); 50 | while (temp) { 51 | printf("%d -> ", temp->vertex); 52 | temp = temp->next; 53 | } 54 | printf("NULL\n"); 55 | } 56 | } 57 | 58 | void updateEdge(struct Graph* graph, int src, int dest) { 59 | struct Node* temp = graph->adjList[src]; 60 | while (temp) { 61 | if (temp->vertex == dest) { 62 | printf("Enter new value for the edge between %d and %d: ", src, dest); 63 | int newDest; 64 | scanf("%d", &newDest); 65 | temp->vertex = newDest; 66 | printf("Edge updated successfully!\n"); 67 | return; 68 | } 69 | temp = temp->next; 70 | } 71 | printf("Edge between %d and %d does not exist.\n", src, dest); 72 | } 73 | 74 | void deleteEdge(struct Graph* graph, int src, int dest) { 75 | struct Node* temp = graph->adjList[src]; 76 | struct Node* prev = NULL; 77 | 78 | while (temp) { 79 | if (temp->vertex == dest) { 80 | if (prev == NULL) 81 | graph->adjList[src] = temp->next; 82 | else 83 | prev->next = temp->next; 84 | 85 | free(temp); 86 | printf("Edge deleted successfully!\n"); 87 | return; 88 | } 89 | prev = temp; 90 | temp = temp->next; 91 | } 92 | printf("Edge between %d and %d does not exist.\n", src, dest); 93 | } 94 | 95 | int main() { 96 | struct Graph* graph = createGraph(); 97 | int src, dest; 98 | char decision; 99 | 100 | do { 101 | printf("\nGraph Operations:\n"); 102 | printf("1. Add an edge\n"); 103 | printf("2. Update an edge\n"); 104 | printf("3. Delete an edge\n"); 105 | printf("4. Print the graph\n"); 106 | printf("5. Exit\n"); 107 | printf("Enter your decision: "); 108 | scanf(" %c", &decision); 109 | 110 | switch (decision) { 111 | case '1': 112 | printf("Enter source and destination vertices: "); 113 | scanf("%d %d", &src, &dest); 114 | addEdge(graph, src, dest); 115 | break; 116 | case '2': 117 | printf("Enter source and destination vertices: "); 118 | scanf("%d %d", &src, &dest); 119 | updateEdge(graph, src, dest); 120 | break; 121 | case '3': 122 | printf("Enter source and destination vertices: "); 123 | scanf("%d %d", &src, &dest); 124 | deleteEdge(graph, src, dest); 125 | break; 126 | case '4': 127 | printf("Graph:\n"); 128 | printGraph(graph); 129 | break; 130 | case '5': 131 | printf("Exiting...\n"); 132 | break; 133 | default: 134 | printf("Invalid decision. Please try again.\n"); 135 | } 136 | } while (decision != '5'); 137 | 138 | return 0; 139 | } 140 | -------------------------------------------------------------------------------- /Data Structures/Hashing data structure.c: -------------------------------------------------------------------------------- 1 | /* Hashing Data Structure implementation 2 | Date created: Saturday; February 01, 2023. 3 | 4 | In this code, the user is prompted to make a decision on what operation to perform (add, update, delete, or exit), and the appropriate function is called based on the decision. The program will keep prompting the user for a decision until they choose to exit the program.*/ 5 | 6 | #include 7 | #include 8 | 9 | #define MAX_SIZE 100 10 | #define HASH_TABLE_SIZE 10 11 | 12 | struct Node { 13 | int key; 14 | int value; 15 | struct Node *next; 16 | }; 17 | 18 | struct Node *hash_table[HASH_TABLE_SIZE]; 19 | 20 | int hash(int key) { 21 | return key % HASH_TABLE_SIZE; 22 | } 23 | 24 | void add(int key, int value) { 25 | int index = hash(key); 26 | struct Node *new_node = (struct Node *) malloc(sizeof(struct Node)); 27 | new_node->key = key; 28 | new_node->value = value; 29 | new_node->next = hash_table[index]; 30 | hash_table[index] = new_node; 31 | } 32 | 33 | void update(int key, int value) { 34 | int index = hash(key); 35 | struct Node *temp = hash_table[index]; 36 | while (temp != NULL) { 37 | if (temp->key == key) { 38 | temp->value = value; 39 | break; 40 | } 41 | temp = temp->next; 42 | } 43 | } 44 | 45 | void delete(int key) { 46 | int index = hash(key); 47 | struct Node *temp = hash_table[index]; 48 | if (temp != NULL && temp->key == key) { 49 | hash_table[index] = temp->next; 50 | free(temp); 51 | return; 52 | } 53 | while (temp != NULL && temp->next != NULL) { 54 | if (temp->next->key == key) { 55 | struct Node *to_be_deleted = temp->next; 56 | temp->next = to_be_deleted->next; 57 | free(to_be_deleted); 58 | break; 59 | } 60 | temp = temp->next; 61 | } 62 | } 63 | 64 | int main() { 65 | int decision, key, value; 66 | while (1) { 67 | printf("1. Add\n"); 68 | printf("2. Update\n"); 69 | printf("3. Delete\n"); 70 | printf("4. Exit\n"); 71 | printf("Enter your decision: "); 72 | scanf("%d", &decision); 73 | switch (decision) { 74 | case 1: 75 | printf("Enter the key: "); 76 | scanf("%d", &key); 77 | printf("Enter the value: "); 78 | scanf("%d", &value); 79 | add(key, value); 80 | break; 81 | case 2: 82 | printf("Enter the key: "); 83 | scanf("%d", &key); 84 | printf("Enter the new value: "); 85 | scanf("%d", &value); 86 | update(key, value); 87 | break; 88 | case 3: 89 | printf("Enter the key: "); 90 | scanf("%d", &key); 91 | delete(key); 92 | break; 93 | case 4: 94 | exit(0); 95 | default: 96 | printf("Invalid decision. Please enter a valid option.\n"); 97 | break; 98 | } 99 | } 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /Data Structures/Heap Data Structure.c: -------------------------------------------------------------------------------- 1 | /* 2 | Heap Data Structure implementation 3 | Date created: Monday; June 19, 2023 4 | 5 | In this code, you can perform operations on a heap data structure, including inserting elements, deleting elements, and printing the heap. The program will keep prompting the user for a decision until they choose to exit.*/ 6 | 7 | #include 8 | #include 9 | 10 | #define MAX_HEAP_SIZE 100 11 | 12 | struct Heap { 13 | int arr[MAX_HEAP_SIZE]; 14 | int size; 15 | }; 16 | 17 | struct Heap* createHeap() { 18 | struct Heap* heap = (struct Heap*)malloc(sizeof(struct Heap)); 19 | heap->size = 0; 20 | return heap; 21 | } 22 | 23 | void swap(int* a, int* b) { 24 | int temp = *a; 25 | *a = *b; 26 | *b = temp; 27 | } 28 | 29 | void insert(struct Heap* heap, int value) { 30 | if (heap->size == MAX_HEAP_SIZE) { 31 | printf("Heap is full. Cannot insert any more elements.\n"); 32 | return; 33 | } 34 | 35 | heap->size++; 36 | int i = heap->size - 1; 37 | heap->arr[i] = value; 38 | 39 | while (i != 0 && heap->arr[i] < heap->arr[(i - 1) / 2]) { 40 | swap(&heap->arr[i], &heap->arr[(i - 1) / 2]); 41 | i = (i - 1) / 2; 42 | } 43 | 44 | printf("Element inserted successfully!\n"); 45 | } 46 | 47 | void heapify(struct Heap* heap, int index) { 48 | int smallest = index; 49 | int left = 2 * index + 1; 50 | int right = 2 * index + 2; 51 | 52 | if (left < heap->size && heap->arr[left] < heap->arr[smallest]) 53 | smallest = left; 54 | 55 | if (right < heap->size && heap->arr[right] < heap->arr[smallest]) 56 | smallest = right; 57 | 58 | if (smallest != index) { 59 | swap(&heap->arr[index], &heap->arr[smallest]); 60 | heapify(heap, smallest); 61 | } 62 | } 63 | 64 | void deleteElement(struct Heap* heap, int value) { 65 | int i; 66 | for (i = 0; i < heap->size; i++) { 67 | if (heap->arr[i] == value) 68 | break; 69 | } 70 | 71 | if (i == heap->size) { 72 | printf("Element %d not found in the heap.\n", value); 73 | return; 74 | } 75 | 76 | heap->arr[i] = heap->arr[heap->size - 1]; 77 | heap->size--; 78 | 79 | heapify(heap, i); 80 | 81 | printf("Element deleted successfully!\n"); 82 | } 83 | 84 | void printHeap(struct Heap* heap) { 85 | if (heap->size == 0) { 86 | printf("Heap is empty.\n"); 87 | return; 88 | } 89 | 90 | int i; 91 | printf("Heap elements: "); 92 | for (i = 0; i < heap->size; i++) { 93 | printf("%d ", heap->arr[i]); 94 | } 95 | printf("\n"); 96 | } 97 | 98 | int main() { 99 | struct Heap* heap = createHeap(); 100 | int value; 101 | char decision; 102 | 103 | do { 104 | printf("\nHeap Operations:\n"); 105 | printf("1. Insert an element\n"); 106 | printf("2. Delete an element\n"); 107 | printf("3. Print the heap\n"); 108 | printf("4. Exit\n"); 109 | printf("Enter your decision: "); 110 | scanf(" %c", &decision); 111 | 112 | switch (decision) { 113 | case '1': 114 | printf("Enter the element to be inserted: "); 115 | scanf("%d", &value); 116 | insert(heap, value); 117 | break; 118 | case '2': 119 | printf("Enter the element to be deleted: "); 120 | scanf("%d", &value); 121 | deleteElement(heap, value); 122 | break; 123 | case '3': 124 | printHeap(heap); 125 | break; 126 | case '4': 127 | printf("Exiting...\n"); 128 | break; 129 | default: 130 | printf("Invalid decision. Please try again.\n"); 131 | } 132 | } while (decision != '4'); 133 | 134 | return 0; 135 | } 136 | -------------------------------------------------------------------------------- /Data Structures/Linked List Data structure.c: -------------------------------------------------------------------------------- 1 | /* A simple linked list (dynamic) implementation 2 | Date created: Friday; April 01, 2022 3 | 4 | 5 | Content 6 | 7 | 01. Header files 8 | 9 | 1.1 stdio.h 10 | 11 | 1.2 stdlib.h 12 | 13 | 02. Data structure 14 | 15 | 03. Functions 16 | 17 | 3.1 insertbegin() - Function to insert a node at the beginning of a linked list 18 | 19 | 3.2 insertend() - Function to Add a node at the end of the Linked list 20 | 21 | 3.3 searchnode() - Function to search for a node in the Linked list 22 | 23 | 3.4 updatenode() - Function to update a node in the Linked list 24 | 25 | 3.5 deletenode() - Function to delete nodes in the Linked list 26 | 27 | 3.6 countList() - Function counts number of nodes of linked list. 28 | 29 | 3.7 printList() - Function prints contents of linked list starting from head. 30 | 31 | 3.8 sortList() - Function to Sort the given linked list Ascending. 32 | 33 | swap() - Function to swap data of two nodes (using when sorting). 34 | 35 | 3.9 deleteList() - Function to delete the linked list. 36 | 37 | 04. main 38 | 39 | 40 | */ 41 | 42 | // 01. Header files 43 | #include 44 | #include // this pre-processor directive is required for malloc 45 | 46 | // 02. Data structure 47 | // create the node user-defined data structure 48 | struct Node 49 | { 50 | int data; 51 | struct Node *link; 52 | }; 53 | 54 | // 03. Functions 55 | 56 | // Function to insert a node at the beginning of a linked list 57 | void insertbegin(struct Node **header, int new_data) 58 | { 59 | struct Node *new_node = (struct Node*)malloc(sizeof(struct Node)); 60 | new_node->data = new_data; //(*new_node).data 61 | new_node->link = *header; //(*new_node).link 62 | *header = new_node; 63 | } 64 | 65 | // Function to Add a node at the end of the list 66 | void insertend(struct Node** header, int new_data) 67 | { 68 | /* 1. allocate node */ 69 | struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); 70 | 71 | struct Node *last = *header; /* used in step 5*/ 72 | 73 | /* 2. put in the data */ 74 | new_node->data = new_data; 75 | 76 | /* 3. This new node is going to be the last node, so make link of 77 | it as NULL*/ 78 | new_node->link = NULL; 79 | 80 | /* 4. If the Linked List is empty, then make the new node as head */ 81 | if (*header == NULL) 82 | { 83 | *header = new_node; 84 | return; 85 | } 86 | 87 | /* 5. Else traverse till the last node */ 88 | while (last->link != NULL) 89 | last = last->link; 90 | 91 | /* 6. Change the link of last node */ 92 | last->link = new_node; 93 | return; 94 | } 95 | 96 | // This function to search for a node 97 | void searchnode(struct Node** header, int value) 98 | { 99 | // Store head node 100 | struct Node *temp = *header, *prev; 101 | 102 | while(temp != NULL) 103 | { 104 | if(temp->data == value) 105 | { 106 | return printf(" Searched data found."); 107 | } 108 | else{temp = temp->link;} 109 | } 110 | return printf(" Searched data Not found."); 111 | } 112 | 113 | // This function to update a node 114 | void updatenode(struct Node** header, int value,int new_data) 115 | { 116 | // Store head node 117 | struct Node *temp = *header, *prev; 118 | 119 | while(temp != NULL) 120 | { 121 | if(temp->data == value) 122 | { 123 | temp->data = new_data; 124 | return ; 125 | } 126 | else{temp = temp->link;} 127 | } 128 | return printf("\n Existing value entered incorrect."); 129 | } 130 | 131 | // Function to delete nodes 132 | void deletenode(struct Node** header, int value) 133 | { 134 | // Store head node 135 | struct Node *temp = *header, *prev; 136 | 137 | // If head node itself holds the value to be deleted 138 | if (temp != NULL && temp->data == value) { 139 | *header = temp->link; // Changed head 140 | free(temp); // free old head 141 | return; 142 | } 143 | 144 | // Search for the value to be deleted, keep track of the 145 | // previous node as we need to change 'prev->link' 146 | while (temp != NULL && temp->data != value) { 147 | prev = temp; 148 | temp = temp->link; 149 | } 150 | 151 | // If value was not present in linked list 152 | if (temp == NULL) 153 | return printf("\n Value entered incorrect."); 154 | 155 | // Unlink the node from linked list 156 | prev->link = temp->link; 157 | 158 | free(temp); // Free memory 159 | } 160 | 161 | // This function counts number of nodes of linked list. 162 | void countList(struct Node *node){ 163 | int count = 0 ; 164 | while (node != NULL) 165 | { 166 | count++; 167 | node = node->link; 168 | } 169 | 170 | printf("\n Total no. of nodes : %d",count); 171 | 172 | } 173 | 174 | // This function prints contents of linked list starting from head. 175 | void printList(struct Node *node) 176 | { 177 | while (node != NULL) 178 | { 179 | printf(" %d ", node->data); 180 | node = node->link; 181 | } 182 | } 183 | 184 | // Function to Sort the given linked list Ascending. 185 | void sortList(struct Node *start) 186 | { 187 | int swapped; 188 | struct Node *temp; 189 | struct Node *prev = NULL; 190 | 191 | /* Checking for empty list */ 192 | if (start == NULL) 193 | return; 194 | 195 | do 196 | { 197 | swapped = 0; 198 | temp = start; 199 | 200 | while (temp->link != prev) 201 | { 202 | if (temp->data > temp->link->data) 203 | { 204 | swap(temp, temp->link); 205 | swapped = 1; 206 | } 207 | temp = temp->link; 208 | } 209 | prev = temp; 210 | } 211 | while (swapped); 212 | } 213 | 214 | // Function to swap data of two nodes a and b*/ 215 | void swap(struct Node *a, struct Node *b) 216 | { 217 | int temp = a->data; 218 | a->data = b->data; 219 | b->data = temp; 220 | } 221 | 222 | // Iterative function to delete a linked list 223 | void deleteList(struct Node** head) 224 | { 225 | struct Node* prev = *head; 226 | 227 | while (*head) 228 | { 229 | *head = (*head)->link; 230 | 231 | free(prev); 232 | prev = *head; 233 | } 234 | } 235 | 236 | // 04. main 237 | int main() 238 | { 239 | // Start with the empty list (create the head pointer of the linked list) 240 | struct Node* head = NULL; 241 | 242 | // Allocation variables for choices 243 | int choice; 244 | int i = 0; 245 | 246 | do 247 | { 248 | printf("\n\n------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"); 249 | printf("\n| Add a Node in the begining - 1 | Add a Node in the end - 2 | Update a Node - 3 | Delete a Node - 4 | Search for a Node - 5 | Print the List - 6 | Sort the list - 7 | Count Nodes - 8 | Remove the list - 9 | Exit program - 10 |"); 250 | printf("\n------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"); 251 | printf( "\nEnter the choice :"); 252 | scanf("%d", &choice); 253 | 254 | switch (choice) 255 | { 256 | case 1: 257 | i = 1; 258 | break; 259 | case 2: 260 | i = 2; 261 | break; 262 | case 3: 263 | i = 3; 264 | break; 265 | case 4: 266 | i = 4; 267 | break; 268 | case 5: 269 | i = 5; 270 | break; 271 | case 6: 272 | i = 6; 273 | break; 274 | case 7: 275 | i = 7; 276 | break; 277 | case 8: 278 | i = 8; 279 | break; 280 | case 9: 281 | i = 9; 282 | break; 283 | } 284 | 285 | // Inserting a node at the beginning. 286 | if(i == 1) 287 | { 288 | int addF; 289 | printf( "\n Enter node Data :"); 290 | scanf("%d", &addF); 291 | insertbegin(&head,addF); 292 | } 293 | 294 | // Inserting a node at the end 295 | if(i == 2) 296 | { 297 | int addE; 298 | printf( "\n Enter node Data :"); 299 | scanf("%d", &addE); 300 | insertend(&head,addE); 301 | } 302 | 303 | // Updating a selected node's value 304 | if(i == 3) 305 | { 306 | int exVal,newVal; 307 | printf( "\n Enter the Existing value :"); 308 | scanf("%d", &exVal); 309 | printf( "\n Enter the New value :"); 310 | scanf("%d", &newVal); 311 | updatenode(&head,exVal,newVal); 312 | } 313 | 314 | // deleting a node from the linked list 315 | if(i == 4) 316 | { 317 | int value; 318 | printf( "\n Enter value to delete :"); 319 | scanf("%d", &value); 320 | deletenode(&head,value); 321 | } 322 | 323 | // Search for a node. 324 | if(i == 5) 325 | { 326 | int value; 327 | printf( "\n Enter the value to search :"); 328 | scanf("%d", &value); 329 | searchnode(&head,value); 330 | } 331 | 332 | // Printing Linked list 333 | if(i == 6) 334 | { 335 | printf("\n Created Linked list :"); 336 | printList(head); 337 | } 338 | 339 | // Sort the Linked list. 340 | if(i == 7) 341 | { 342 | sortList(head); 343 | } 344 | 345 | // counting nodes in the linked list 346 | if(i == 8) 347 | { 348 | countList(head); 349 | } 350 | 351 | // Remove the Linked list 352 | if(i == 9) 353 | { 354 | deleteList(&head); 355 | if (head == NULL) 356 | { 357 | printf("\n List deleted successfully."); 358 | } 359 | } 360 | i=0; 361 | }while(choice !=10 ); 362 | // End the whole program 363 | return 0; 364 | } 365 | 366 | -------------------------------------------------------------------------------- /Data Structures/Queue Data structure.c: -------------------------------------------------------------------------------- 1 | /* Queue Data structure implementation 2 | Date created: Saturday; February 01, 2023 */ 3 | 4 | 5 | #include 6 | #include 7 | 8 | #define MAX_SIZE 100 9 | 10 | typedef struct queue { 11 | int items[MAX_SIZE]; 12 | int front, rear; 13 | } Queue; 14 | 15 | void initialize(Queue *queue) { 16 | queue->front = 0; 17 | queue->rear = -1; 18 | } 19 | 20 | int isEmpty(Queue *queue) { 21 | return queue->rear == -1; 22 | } 23 | 24 | int isFull(Queue *queue) { 25 | return queue->rear == MAX_SIZE - 1; 26 | } 27 | 28 | void enqueue(Queue *queue, int item) { 29 | if (isFull(queue)) { 30 | printf("Queue is full.\n"); 31 | return; 32 | } 33 | queue->items[++queue->rear] = item; 34 | printf("%d enqueued to queue\n", item); 35 | } 36 | 37 | int dequeue(Queue *queue) { 38 | if (isEmpty(queue)) { 39 | printf("Queue is empty.\n"); 40 | return -1; 41 | } 42 | int item = queue->items[queue->front++]; 43 | if (queue->front > queue->rear) { 44 | initialize(queue); 45 | } 46 | return item; 47 | } 48 | 49 | int main() { 50 | Queue queue; 51 | initialize(&queue); 52 | 53 | enqueue(&queue, 1); 54 | enqueue(&queue, 2); 55 | enqueue(&queue, 3); 56 | 57 | printf("%d dequeued from queue\n", dequeue(&queue)); 58 | printf("%d dequeued from queue\n", dequeue(&queue)); 59 | printf("%d dequeued from queue\n", dequeue(&queue)); 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Data Structures/Set Data Structure.c: -------------------------------------------------------------------------------- 1 | /* Set Data structure implementation 2 | Date created: Thursday, June 22, 2023 */ 3 | 4 | #include 5 | #include 6 | 7 | struct set_node { 8 | int value; 9 | struct set_node *next; 10 | }; 11 | 12 | struct set { 13 | struct set_node *head; 14 | }; 15 | 16 | void set_init(struct set *set) { 17 | set->head = NULL; 18 | } 19 | 20 | void set_insert(struct set *set, int value) { 21 | struct set_node *new_node = malloc(sizeof(struct set_node)); 22 | new_node->value = value; 23 | new_node->next = set->head; 24 | set->head = new_node; 25 | } 26 | 27 | void set_update(struct set *set, int value) { 28 | struct set_node *node = set->head; 29 | while (node != NULL) { 30 | if (node->value == value) { 31 | printf("Enter the new value: "); 32 | scanf("%d", &value); 33 | node->value = value; 34 | break; 35 | } 36 | node = node->next; 37 | } 38 | 39 | if (node == NULL) { 40 | printf("The value to update does not exist in the set.\n"); 41 | } 42 | } 43 | 44 | 45 | void set_delete(struct set *set, int value) { 46 | struct set_node *prev = NULL; 47 | struct set_node *node = set->head; 48 | while (node != NULL) { 49 | if (node->value == value) { 50 | if (prev == NULL) { 51 | set->head = node->next; 52 | } else { 53 | prev->next = node->next; 54 | } 55 | free(node); 56 | break; 57 | } 58 | prev = node; 59 | node = node->next; 60 | } 61 | } 62 | 63 | void print_set(struct set *set) { 64 | struct set_node *node = set->head; 65 | printf("The set is: "); 66 | while (node != NULL) { 67 | printf("%d ", node->value); 68 | node = node->next; 69 | } 70 | printf("\n"); 71 | } 72 | 73 | int main() { 74 | struct set set; 75 | set_init(&set); 76 | 77 | int choice; 78 | while (1) { 79 | printf("1. Insert\n2. Update\n3. Delete\n4. Print\n5. Exit\n"); 80 | printf("Enter your choice: "); 81 | scanf("%d", &choice); 82 | 83 | switch (choice) { 84 | case 1: { 85 | int value; 86 | printf("Enter the value to insert: "); 87 | scanf("%d", &value); 88 | set_insert(&set, value); 89 | break; 90 | } 91 | case 2: { 92 | int value; 93 | printf("Enter the value to update: "); 94 | scanf("%d", &value); 95 | set_update(&set, value); 96 | break; 97 | } 98 | case 3: { 99 | int value; 100 | printf("Enter the value to delete: "); 101 | scanf("%d", &value); 102 | set_delete(&set, value); 103 | break; 104 | } 105 | case 4: { 106 | print_set(&set); 107 | break; 108 | } 109 | case 5: { 110 | printf("Exiting...\n"); 111 | exit(0); 112 | } 113 | default: { 114 | printf("Invalid choice.\n"); 115 | break; 116 | } 117 | } 118 | } 119 | 120 | return 0; 121 | } 122 | -------------------------------------------------------------------------------- /Data Structures/Stack Data structure.c: -------------------------------------------------------------------------------- 1 | /* A simple Stack (dynamic) implementation 2 | Date created: Friday; April 01, 2022 3 | 4 | 5 | Content 6 | 7 | 01. Header files 8 | 9 | 1.1 stdio.h 10 | 11 | 02. Data structure 12 | 13 | 03. Functions 14 | 15 | 3.1 push() - Function to insert a data into the Stack 16 | 17 | 3.2 pop() - Function to throw a data from the Stack 18 | 19 | 3.3 display() - Function to print data values in the Stack 20 | 21 | 04. main 22 | 23 | */ 24 | 25 | // 01. Header files 26 | #include 27 | 28 | // 02. Data structure 29 | int stack[10],choice,n,top,x,i; 30 | 31 | // 03. Functions 32 | void push() 33 | { 34 | if(top>=n-1) 35 | { 36 | printf("\n\tSTACK is full"); 37 | 38 | } 39 | else 40 | { 41 | printf(" Enter a value to be pushed:"); 42 | scanf("%d",&x); 43 | top++; 44 | stack[top]=x; 45 | } 46 | } 47 | 48 | void pop() 49 | { 50 | if(top<=-1) 51 | { 52 | printf("\n\t No elements to pop"); 53 | } 54 | else 55 | { 56 | printf("\n\t The popped elements is %d",stack[top]); 57 | top--; 58 | } 59 | } 60 | 61 | void display() 62 | { 63 | if(top>=0) 64 | { 65 | printf("\n The elements in STACK are, \n"); 66 | for(i=top; i>=0; i--) 67 | printf("\n%d",stack[i]); 68 | printf("\n Press Next Choice"); 69 | } 70 | else 71 | { 72 | printf("\n The STACK is empty"); 73 | } 74 | 75 | } 76 | 77 | // 04. main 78 | int main() 79 | { 80 | 81 | top=-1; 82 | printf("\n Enter the size of STACK (maximum 10):"); 83 | scanf("%d",&n); 84 | printf("\n\t STACK OPERATIONS "); 85 | printf("\n\t--------------------------------"); 86 | printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT"); 87 | do 88 | { 89 | printf("\n Enter the Choice:"); 90 | scanf("%d",&choice); 91 | switch(choice) 92 | { 93 | case 1: 94 | { 95 | push(); 96 | break; 97 | } 98 | case 2: 99 | { 100 | pop(); 101 | break; 102 | } 103 | case 3: 104 | { 105 | display(); 106 | break; 107 | } 108 | case 4: 109 | { 110 | printf("\n\t EXIT "); 111 | break; 112 | } 113 | default: 114 | { 115 | printf ("\n\t Please Enter a Valid Choice"); 116 | } 117 | 118 | } 119 | } 120 | while(choice!=4); 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /Data Structures/Tree Data structure.c: -------------------------------------------------------------------------------- 1 | /* A simple Tree (dynamic) implementation 2 | Date created: Friday; April 01, 2022 3 | 4 | 5 | Content 6 | 7 | 01. Header files 8 | 9 | 1.1 stdio.h 10 | 11 | 1.2 stdlib.h 12 | 13 | 02. Data structure 14 | 15 | 03. Functions 16 | 17 | 04. main 18 | 19 | 20 | */ 21 | 22 | // 01. Header files 23 | #include 24 | #include 25 | 26 | // 02. Data structure 27 | struct node { 28 | int data; 29 | struct node *left, *right; 30 | }; 31 | 32 | // A utility function to create a new BST node 33 | struct node* newNode(int item) 34 | { 35 | struct node* temp 36 | = (struct node*)malloc(sizeof(struct node)); 37 | temp->data = item; 38 | temp->left = temp->right = NULL; 39 | return temp; 40 | } 41 | 42 | // 03. Functions 43 | // A utility function to do in-order traversal of BST 44 | void inorder(struct node* root) 45 | { 46 | if (root != NULL) { 47 | inorder(root->left); 48 | printf("%d -> ", root->data); 49 | inorder(root->right); 50 | } 51 | } 52 | 53 | // A utility function to do pre-order traversal of BST 54 | void preorder(struct node* root) 55 | { 56 | if (root != NULL) { 57 | printf("%d -> ", root->data); 58 | inorder(root->left); 59 | inorder(root->right); 60 | } 61 | } 62 | 63 | // A utility function to do post-order traversal of BST 64 | void postorder(struct node* root) 65 | { 66 | if (root != NULL) { 67 | inorder(root->left); 68 | inorder(root->right); 69 | printf("%d -> ", root->data); 70 | } 71 | } 72 | 73 | /* A utility function to insert a new node with given data in all BST */ 74 | struct node* insert(struct node* node, int data) 75 | { 76 | /* If the tree is empty, return a new node */ 77 | if (node == NULL) 78 | return newNode(data); 79 | 80 | /* Otherwise, recur down the tree */ 81 | if (data < node->data) 82 | node->left = insert(node->left, data); 83 | else if (data > node->data) 84 | node->right = insert(node->right, data); 85 | 86 | /* return the (unchanged) node pointer */ 87 | return node; 88 | } 89 | 90 | // 04. main 91 | int main() 92 | { 93 | struct node* root = NULL; 94 | root = insert(root, 0); 95 | insert(root, 9); 96 | insert(root, -8); 97 | insert(root, 14); 98 | insert(root, 2); 99 | insert(root, -11); 100 | 101 | // print in-oder traversal of the BST 102 | printf(" In-oder traversal of the BST\n"); 103 | printf(" Start -> "); 104 | inorder(root); 105 | printf(" End\n"); 106 | // print pre-oder traversal of the BST 107 | printf("\n Pre-oder traversal of the BST\n"); 108 | printf(" Start -> "); 109 | preorder(root); 110 | printf(" End\n"); 111 | // print post-oder traversal of the BST 112 | printf("\n Post-oder traversal of the BST\n"); 113 | printf(" Start -> "); 114 | postorder(root); 115 | printf(" End"); 116 | return 0; 117 | } 118 | -------------------------------------------------------------------------------- /Examples (Algorithms)/Simple bug game - 01.c: -------------------------------------------------------------------------------- 1 | // Simple bug game 2 | // The bug can turn only right and go forward 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | char world[8][8] = {{'x',' ',' ',' ',' ',' ',' ','x'}, 9 | {'x',' ','x','x','x','x',' ','x'}, 10 | {'x',' ','x','x','x','x',' ','x'}, 11 | {'x',' ',' ',' ',' ','x',' ','x'}, 12 | {' ',' ',' ',' ','x',' ',' ',' '}, 13 | {' ','x','x',' ','x','x','x',' '}, 14 | {' ',' ',' ',' ',' ',' ',' ',' '}, 15 | {'x','x','x',' ','x','x','x','x'}}; 16 | int x_b,y_b,x_f,y_f,rot_st = 1 ; //bug positions, rotation and food position 17 | int conf_b,conf_f,next,win =0; 18 | char bug = 2 ; 19 | char food = 4; 20 | 21 | void main_menu(); 22 | void display_bord(); 23 | void sense(); 24 | void turn(); 25 | void step(); 26 | void start(); 27 | void won(); 28 | void go(); 29 | 30 | int main() 31 | { 32 | main_menu(); 33 | start(); 34 | win = 0; 35 | display_bord(); 36 | go(); 37 | printf("Game Over.\n"); 38 | printf("Bug ate food\n\n"); 39 | 40 | return 0; 41 | } 42 | 43 | void main_menu(){ 44 | system("cls"); 45 | printf(" X - %c \n",26); 46 | printf(" 0 1 2 3 4 5 6 7\n"); 47 | printf(" _____ _____ _____ _____ _____ _____ _____ _____ \n"); 48 | printf(" y 0 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[0][0],world[0][1],world[0][2],world[0][3],world[0][4],world[0][5],world[0][6],world[0][7]); 49 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 50 | printf(" | 1 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[1][0],world[1][1],world[1][2],world[1][3],world[1][4],world[1][5],world[1][6],world[1][7]); 51 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 52 | printf(" %C 2 | %C | %C | %C | %C | %C | %C | %C | %C |\n",25,world[2][0],world[2][1],world[2][2],world[2][3],world[2][4],world[2][5],world[2][6],world[2][7]); 53 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 54 | printf(" 3 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[3][0],world[3][1],world[3][2],world[3][3],world[3][4],world[3][5],world[3][6],world[3][7]); 55 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 56 | printf(" 4 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[4][0],world[4][1],world[4][2],world[4][3],world[4][4],world[4][5],world[4][6],world[4][7]); 57 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 58 | printf(" 5 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[5][0],world[5][1],world[5][2],world[5][3],world[5][4],world[5][5],world[5][6],world[5][7]); 59 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 60 | printf(" 6 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[6][0],world[6][1],world[6][2],world[6][3],world[6][4],world[6][5],world[6][6],world[6][7]); 61 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 62 | printf(" 7 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[7][0],world[7][1],world[7][2],world[7][3],world[7][4],world[7][5],world[7][6],world[7][7]); 63 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n\n"); 64 | 65 | printf("Bug - %c || Food - %c\n\n",bug,food); 66 | } 67 | 68 | void display_bord(){ 69 | system("cls"); 70 | printf(" _____ _____ _____ _____ _____ _____ _____ _____ \n"); 71 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[0][0],world[0][1],world[0][2],world[0][3],world[0][4],world[0][5],world[0][6],world[0][7]); 72 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 73 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[1][0],world[1][1],world[1][2],world[1][3],world[1][4],world[1][5],world[1][6],world[1][7]); 74 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 75 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[2][0],world[2][1],world[2][2],world[2][3],world[2][4],world[2][5],world[2][6],world[2][7]); 76 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 77 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[3][0],world[3][1],world[3][2],world[3][3],world[3][4],world[3][5],world[3][6],world[3][7]); 78 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 79 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[4][0],world[4][1],world[4][2],world[4][3],world[4][4],world[4][5],world[4][6],world[4][7]); 80 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 81 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[5][0],world[5][1],world[5][2],world[5][3],world[5][4],world[5][5],world[5][6],world[5][7]); 82 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 83 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[6][0],world[6][1],world[6][2],world[6][3],world[6][4],world[6][5],world[6][6],world[6][7]); 84 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 85 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[7][0],world[7][1],world[7][2],world[7][3],world[7][4],world[7][5],world[7][6],world[7][7]); 86 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n\n"); 87 | } 88 | 89 | void sense(){ 90 | next = 0; 91 | if(rot_st ==1){ 92 | if(world[y_b-1][x_b] != 'x' && y_b > 0){ 93 | next = 1; 94 | } 95 | }else if(rot_st == 2){ 96 | if(world[y_b][x_b+1] != 'x' && x_b < 7){ 97 | next = 1; 98 | } 99 | }else if(rot_st == 3){ 100 | if(world[y_b+1][x_b] != 'x' && y_b < 7 ){ 101 | next = 1; 102 | } 103 | }else if(rot_st == 4){ 104 | if(world[y_b][x_b-1] != 'x' && x_b > 0){ 105 | next = 1; 106 | } 107 | } 108 | } 109 | 110 | void turn(){ 111 | if(rot_st ==1 ){ 112 | rot_st = 2; 113 | }else if(rot_st == 2 ){ 114 | rot_st = 3; 115 | }else if(rot_st == 3){ 116 | rot_st = 4; 117 | }else if(rot_st == 4){ 118 | rot_st = 1; 119 | } 120 | } 121 | 122 | void step(){ 123 | if(rot_st ==1){ 124 | world[y_b-1][x_b] = bug; 125 | world[y_b][x_b] = ' '; 126 | y_b = y_b - 1; 127 | }else if(rot_st == 2){ 128 | world[y_b][x_b+1] = bug; 129 | world[y_b][x_b] = ' '; 130 | x_b = x_b + 1; 131 | }else if(rot_st == 3){ 132 | world[y_b+1][x_b] = bug; 133 | world[y_b][x_b] = ' '; 134 | y_b = y_b + 1; 135 | }else if(rot_st == 4){ 136 | world[y_b][x_b-1] = bug; 137 | world[y_b][x_b] = ' '; 138 | x_b = x_b - 1; 139 | } 140 | } 141 | 142 | void start(){ 143 | conf_b = 0; 144 | while (conf_b != 1){ 145 | printf("Enter bug position (x): "); 146 | scanf("%d", &x_b); 147 | printf("Enter bug position (y): "); 148 | scanf("%d", &y_b); 149 | 150 | 151 | if(world[y_b][x_b] != 'x' && x_b < 8 && y_b < 8){ 152 | world[y_b][x_b] = bug; 153 | conf_b = 1; 154 | }else { 155 | printf("Invalid bug Positions.\n"); 156 | 157 | } 158 | printf("\n"); 159 | printf("-------------------------------\n"); 160 | } 161 | conf_f = 0; 162 | while(conf_f != 1){ 163 | printf("Enter food position (x): "); 164 | scanf("%d", &x_f); 165 | printf("Enter food position (y): "); 166 | scanf("%d", &y_f); 167 | 168 | 169 | if(world[y_f][x_f] != 'x' && x_f < 8 && y_f < 8){ 170 | world[y_f][x_f] = food; 171 | conf_f = 1; 172 | }else { 173 | printf("Invalid food Positions.\n"); 174 | } 175 | printf("\n"); 176 | printf("-------------------------------\n"); 177 | } 178 | 179 | 180 | } 181 | 182 | void won(){ 183 | if(x_b == x_f && y_b == y_f){ 184 | win = 1; 185 | } 186 | } 187 | 188 | void go(){ 189 | int t; 190 | while(win != 1){ 191 | sense(); 192 | t = 0; 193 | while(next != 1){ 194 | t++; 195 | if(t == 1){ 196 | turn(); 197 | }else if(t == 2){ 198 | turn(); 199 | turn(); 200 | }else if(t == 3){ 201 | turn(); 202 | turn(); 203 | turn(); 204 | } 205 | sense(); 206 | } 207 | step(); 208 | display_bord(); 209 | Sleep(600); 210 | won(); 211 | } 212 | } 213 | 214 | -------------------------------------------------------------------------------- /Examples (Algorithms)/Simple bug game - 02.c: -------------------------------------------------------------------------------- 1 | // Simple bug game 2 | // The bug can turn only right and go forward 3 | // At this time, we give the direction of Bug faces 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | char world[8][8] = {{'x',' ',' ',' ',' ',' ',' ','x'}, 11 | {'x',' ','x',' ','x','x',' ','x'}, 12 | {'x',' ','x',' ','x','x',' ','x'}, 13 | {'x',' ',' ',' ',' ','x',' ','x'}, 14 | {' ',' ',' ',' ',' ',' ',' ','x'}, 15 | {' ','x','x',' ','x','x',' ',' '}, 16 | {' ',' ',' ',' ','x',' ',' ',' '}, 17 | {'x','x','x',' ','x','x','x','x'}}; 18 | int x_b,y_b,x_f,y_f,rot_st ; //bug positions, rotation and food position 19 | int conf_b,conf_f,conf_r,next,win =0; //confirming variables 20 | char bug = 2 ; 21 | char food = 4; 22 | 23 | void main_menu(); 24 | void display_bord(); 25 | void sense(); 26 | void turn(); 27 | void step(); 28 | void start(); 29 | void won(); 30 | void go(); 31 | 32 | int main() 33 | { 34 | main_menu(); 35 | start(); 36 | win = 0; 37 | display_bord(); 38 | go(); 39 | printf("Game Over.\n"); 40 | printf("Bug ate food\n\n"); 41 | 42 | return 0; 43 | } 44 | 45 | void main_menu(){ 46 | system("cls"); 47 | printf(" X - %c \n",26); 48 | printf(" 0 1 2 3 4 5 6 7\n"); 49 | printf(" _____ _____ _____ _____ _____ _____ _____ _____ \n"); 50 | printf(" y 0 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[0][0],world[0][1],world[0][2],world[0][3],world[0][4],world[0][5],world[0][6],world[0][7]); 51 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 52 | printf(" | 1 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[1][0],world[1][1],world[1][2],world[1][3],world[1][4],world[1][5],world[1][6],world[1][7]); 53 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 54 | printf(" %C 2 | %C | %C | %C | %C | %C | %C | %C | %C |\n",25,world[2][0],world[2][1],world[2][2],world[2][3],world[2][4],world[2][5],world[2][6],world[2][7]); 55 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 56 | printf(" 3 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[3][0],world[3][1],world[3][2],world[3][3],world[3][4],world[3][5],world[3][6],world[3][7]); 57 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 58 | printf(" 4 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[4][0],world[4][1],world[4][2],world[4][3],world[4][4],world[4][5],world[4][6],world[4][7]); 59 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 60 | printf(" 5 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[5][0],world[5][1],world[5][2],world[5][3],world[5][4],world[5][5],world[5][6],world[5][7]); 61 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 62 | printf(" 6 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[6][0],world[6][1],world[6][2],world[6][3],world[6][4],world[6][5],world[6][6],world[6][7]); 63 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n"); 64 | printf(" 7 | %C | %C | %C | %C | %C | %C | %C | %C |\n",world[7][0],world[7][1],world[7][2],world[7][3],world[7][4],world[7][5],world[7][6],world[7][7]); 65 | printf(" |_____|_____|_____|_____|_____|_____|_____|_____|\n\n"); 66 | 67 | printf("Bug - %c || Food - %c\n",bug,food); 68 | printf("Rotation Stats : \n"); 69 | printf("Up - press 1 || Right - press 2 || Down - press 3 || Left - press 4\n\n"); 70 | 71 | } 72 | 73 | void display_bord(){ 74 | system("cls"); 75 | printf(" _____ _____ _____ _____ _____ _____ _____ _____ \n"); 76 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[0][0],world[0][1],world[0][2],world[0][3],world[0][4],world[0][5],world[0][6],world[0][7]); 77 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 78 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[1][0],world[1][1],world[1][2],world[1][3],world[1][4],world[1][5],world[1][6],world[1][7]); 79 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 80 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[2][0],world[2][1],world[2][2],world[2][3],world[2][4],world[2][5],world[2][6],world[2][7]); 81 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 82 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[3][0],world[3][1],world[3][2],world[3][3],world[3][4],world[3][5],world[3][6],world[3][7]); 83 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 84 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[4][0],world[4][1],world[4][2],world[4][3],world[4][4],world[4][5],world[4][6],world[4][7]); 85 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 86 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[5][0],world[5][1],world[5][2],world[5][3],world[5][4],world[5][5],world[5][6],world[5][7]); 87 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 88 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[6][0],world[6][1],world[6][2],world[6][3],world[6][4],world[6][5],world[6][6],world[6][7]); 89 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n"); 90 | printf("| %C | %C | %C | %C | %C | %C | %C | %C |\n",world[7][0],world[7][1],world[7][2],world[7][3],world[7][4],world[7][5],world[7][6],world[7][7]); 91 | printf("|_____|_____|_____|_____|_____|_____|_____|_____|\n\n"); 92 | } 93 | 94 | void sense(){ 95 | next = 0; 96 | if(rot_st ==1){ 97 | if(world[y_b-1][x_b] != 'x' && y_b > 0){ 98 | next = 1; 99 | } 100 | }else if(rot_st == 2){ 101 | if(world[y_b][x_b+1] != 'x' && x_b < 7){ 102 | next = 1; 103 | } 104 | }else if(rot_st == 3){ 105 | if(world[y_b+1][x_b] != 'x' && y_b < 7 ){ 106 | next = 1; 107 | } 108 | }else if(rot_st == 4){ 109 | if(world[y_b][x_b-1] != 'x' && x_b > 0){ 110 | next = 1; 111 | } 112 | } 113 | } 114 | 115 | void turn(){ 116 | if(rot_st ==1 ){ 117 | rot_st = 2; 118 | }else if(rot_st == 2 ){ 119 | rot_st = 3; 120 | }else if(rot_st == 3){ 121 | rot_st = 4; 122 | }else if(rot_st == 4){ 123 | rot_st = 1; 124 | } 125 | } 126 | 127 | void step(){ 128 | if(rot_st ==1){ 129 | world[y_b-1][x_b] = bug; 130 | world[y_b][x_b] = ' '; 131 | y_b = y_b - 1; 132 | }else if(rot_st == 2){ 133 | world[y_b][x_b+1] = bug; 134 | world[y_b][x_b] = ' '; 135 | x_b = x_b + 1; 136 | }else if(rot_st == 3){ 137 | world[y_b+1][x_b] = bug; 138 | world[y_b][x_b] = ' '; 139 | y_b = y_b + 1; 140 | }else if(rot_st == 4){ 141 | world[y_b][x_b-1] = bug; 142 | world[y_b][x_b] = ' '; 143 | x_b = x_b - 1; 144 | } 145 | } 146 | 147 | void start(){ 148 | conf_b = 0; 149 | while (conf_b != 1){ 150 | printf("Enter bug position (x): "); 151 | scanf("%d", &x_b); 152 | printf("Enter bug position (y): "); 153 | scanf("%d", &y_b); 154 | 155 | 156 | if(world[y_b][x_b] != 'x' && x_b < 8 && y_b < 8){ 157 | world[y_b][x_b] = bug; 158 | conf_b = 1; 159 | }else { 160 | printf("Invalid bug Positions.\n"); 161 | 162 | } 163 | printf("\n"); 164 | printf("-------------------------------\n"); 165 | } 166 | conf_f = 0; 167 | while(conf_f != 1){ 168 | printf("Enter food position (x): "); 169 | scanf("%d", &x_f); 170 | printf("Enter food position (y): "); 171 | scanf("%d", &y_f); 172 | 173 | 174 | if(world[y_f][x_f] != 'x' && x_f < 8 && y_f < 8){ 175 | world[y_f][x_f] = food; 176 | conf_f = 1; 177 | }else { 178 | printf("Invalid food Positions.\n"); 179 | } 180 | printf("\n"); 181 | printf("-------------------------------\n"); 182 | } 183 | while(conf_r != 1){ 184 | printf("Enter Rotation state : "); 185 | scanf("%d", &rot_st); 186 | 187 | if(rot_st < 5 && rot_st > 0){ 188 | conf_r =1; 189 | }else { 190 | printf("Invalid rotation state.\n"); 191 | } 192 | } 193 | } 194 | 195 | void won(){ 196 | if(x_b == x_f && y_b == y_f){ 197 | win = 1; 198 | } 199 | } 200 | 201 | void go(){ 202 | int t; 203 | while(win != 1){ 204 | sense(); 205 | t = 0; 206 | while(next != 1){ 207 | t++; 208 | if(t == 1){ 209 | turn(); 210 | }else if(t == 2){ 211 | turn(); 212 | turn(); 213 | }else if(t == 3){ 214 | turn(); 215 | turn(); 216 | turn(); 217 | } 218 | sense(); 219 | } 220 | step(); 221 | display_bord(); 222 | Sleep(600); 223 | won(); 224 | } 225 | } 226 | 227 | -------------------------------------------------------------------------------- /Examples (Algorithms)/The Word Search Game/WSG - Hash.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define MAX_WORD_LENGTH 100 // the maximum length of a word in the hash table is 100 characters 6 | #define HASH_TABLE_SIZE 1000003 // the size of the hash table is 1000003 7 | 8 | typedef struct Node { 9 | char word[MAX_WORD_LENGTH]; // 'word' used to store the word that is stored in the Node struct 10 | struct Node* next; // 'next' used to point to the next Node struct in the linked list 11 | } Node; 12 | 13 | Node* hashTable[HASH_TABLE_SIZE]; // hashTable array is used to store the linked lists of words that hash to the same index in the hash table. 14 | 15 | unsigned long hash(const char* str) { 16 | unsigned long hash = 5381; 17 | int c; 18 | 19 | while ((c = *str++)) { 20 | hash = ((hash << 5) + hash) + c; // djb2 hash function 21 | } 22 | 23 | return hash % HASH_TABLE_SIZE; 24 | } 25 | 26 | void insertWord(const char* word) { 27 | unsigned long index = hash(word); // called to calculate the hash of the word 28 | 29 | Node* newNode = (Node*)malloc(sizeof(Node)); 30 | strcpy(newNode->word, word); 31 | newNode->next = NULL; 32 | 33 | if (hashTable[index] == NULL) { 34 | hashTable[index] = newNode; 35 | } else { 36 | Node* current = hashTable[index]; 37 | while (current->next != NULL) { 38 | current = current->next; 39 | } 40 | current->next = newNode; 41 | } 42 | } 43 | 44 | int checkWordInDatabase(const char* word) { 45 | unsigned long index = hash(word); // calculate the hash 46 | 47 | if (hashTable[index] == NULL) { 48 | return 0; // Word not found in the database 49 | } 50 | 51 | Node* current = hashTable[index]; 52 | while (current != NULL) { 53 | if (strcmp(current->word, word) == 0) { 54 | return 1; // Word found in the database 55 | } 56 | current = current->next; 57 | } 58 | 59 | return 0; // Word not found in the database 60 | } 61 | 62 | void freeHashTable() { 63 | for (int i = 0; i < HASH_TABLE_SIZE; i++) { 64 | Node* current = hashTable[i]; 65 | while (current != NULL) { 66 | Node* temp = current; 67 | current = current->next; 68 | free(temp); 69 | } 70 | } 71 | } 72 | 73 | void initializeHashTable() { 74 | for (int i = 0; i < HASH_TABLE_SIZE; i++) { 75 | hashTable[i] = NULL; 76 | } 77 | } 78 | 79 | int main() { 80 | initializeHashTable(); 81 | 82 | FILE* file = fopen("database.txt", "r"); 83 | if (file == NULL) { 84 | printf("Unable to open the database file.\n"); 85 | return 1; 86 | } 87 | 88 | char line[MAX_WORD_LENGTH * 2]; // Twice the max word length to account for commas 89 | while (fgets(line, sizeof(line), file) != NULL) { 90 | char* token = strtok(line, ","); 91 | while (token != NULL) { 92 | insertWord(token); 93 | token = strtok(NULL, ","); 94 | } 95 | } 96 | 97 | fclose(file); 98 | 99 | char word[MAX_WORD_LENGTH]; 100 | printf("Enter a word to check in the database: "); 101 | scanf("%s", word); 102 | 103 | // time para 104 | clock_t start, end; 105 | double execution_time; 106 | start = clock(); // time starts 107 | 108 | if (checkWordInDatabase(word)) { 109 | printf("The word '%s' is in the database.\n", word); 110 | } else { 111 | printf("The word '%s' is not in the database.\n", word); 112 | } 113 | 114 | end = clock(); // time ends 115 | /* Get the time taken by program to execute in seconds */ 116 | double duration = ((double)end - start)/CLOCKS_PER_SEC; 117 | printf("Time taken to execute in seconds : %f \n", duration); // print time 118 | 119 | // Calculate the time in miliseconds 120 | double duration_ms = duration * 1000; 121 | printf("Time taken: %f miliseconds\n", duration_ms); 122 | 123 | // Calculate the time in nanoseconds 124 | double duration_ns = duration * 1000000000; 125 | printf("Time taken: %f nanoseconds\n", duration_ns); 126 | 127 | freeHashTable(); 128 | 129 | // Prompt the user to press a key before exiting 130 | getchar(); 131 | 132 | return 0; 133 | } -------------------------------------------------------------------------------- /Examples (Algorithms)/The Word Search Game/WSG - linear search.c: -------------------------------------------------------------------------------- 1 | // The algorithm I provided uses a simple linear search approach to check whether a word is present in the text file acting as a database. 2 | // It reads each line of the file and tokenizes it based on commas, then compares each token (word) with the target word. 3 | // In terms of data structures, the algorithm does not use any specific data structure beyond basic C arrays and strings. 4 | // The char line[MAX_WORD_LENGTH * 2] array is used to store a line read from the file, and the char* token pointer is used to hold each word token extracted using strtok(). 5 | // The algorithm does not utilize any advanced data structures like hash tables or binary search trees, which could provide faster search times for larger databases. 6 | // Since the algorithm performs a linear search, the time complexity is O(n), where n is the total number of words in the text file. 7 | 8 | #include 9 | #include 10 | #include 11 | # include 12 | #define MAX_WORD_LENGTH 100 13 | 14 | int checkWordInDatabase(const char* word, const char* databaseFile) { 15 | FILE* file = fopen(databaseFile, "r"); 16 | if (file == NULL) { 17 | printf("Unable to open the database file.\n"); 18 | return 0; // Word not found (error accessing the database file) 19 | } 20 | 21 | char line[MAX_WORD_LENGTH * 2]; // Twice the max word length to account for commas 22 | while (fgets(line, sizeof(line), file) != NULL) { 23 | char* token = strtok(line, ","); 24 | while (token != NULL) { 25 | if (strcmp(token, word) == 0) { 26 | fclose(file); 27 | return 1; // Word found in the database 28 | } 29 | token = strtok(NULL, ","); 30 | } 31 | } 32 | 33 | fclose(file); 34 | return 0; // Word not found in the database 35 | } 36 | 37 | int main() { 38 | char word[MAX_WORD_LENGTH]; 39 | 40 | // time para 41 | clock_t start, end; 42 | double execution_time; 43 | 44 | printf("Enter a word to check in the database: "); 45 | scanf("%s", word); 46 | 47 | start = clock(); // time starts 48 | 49 | if (checkWordInDatabase(word, "database.txt")) { 50 | printf("The word '%s' is in the database.\n", word); 51 | } else { 52 | printf("The word '%s' is not in the database.\n", word); 53 | } 54 | 55 | end = clock(); // time ends 56 | /* Get the time taken by program to execute in seconds */ 57 | double duration = ((double)end - start)/CLOCKS_PER_SEC; 58 | printf("Time taken to execute in seconds : %f", duration); // print time 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Examples (Algorithms)/The Word Search Game/WSG.md: -------------------------------------------------------------------------------- 1 | # The Word Search Game 2 | 3 | A word search engine is a website or application that allows users to find words in a grid of letters. The words can be hidden in any direction, including horizontally, vertically, diagonally, and backwards. Word search engines typically have a large database of words, and they allow users to search for words by letter, length, or theme. 4 | 5 | ## How I implement this? 6 | 7 | I've crafted two fascinating algorithms, each employing distinct data structures, to power an engaging Word Search game. My noble pursuit revolves around unearthing the ultimate algorithm that can swiftly locate a word amidst a vast database of words. To achieve this goal, I've harnessed the prowess of both the Linear Search and Hash algorithms. While it's possible that superior algorithms exist, let's delve into the remarkable speed of my implementations and explore the wonders they unfold. 8 | 9 | For this experiment, I used a database that includes more than 500,000 words. The database of words is available in the `database.txt` file. 10 | 11 | ## Speed of the Linear Search Algorithm 12 | 13 | The Linear Search algorithm is a brute-force algorithm that sequentially searches for a target value in a list. It's a simple algorithm that's easy to implement and understand. However, it's not the most efficient algorithm for searching large databases. The Linear Search algorithm has a time complexity of O(n), which means that the time it takes to search for a word in a database is proportional to the size of the database. 14 | 15 | Example code: [Linear Search Algorithm](./WSG%20-%20linear%20search.c) 16 | 17 | ## Speed of the Hash Algorithm 18 | 19 | The Hash algorithm is a more efficient algorithm for searching large databases. It uses a hash function to map a word to a unique number. The hash function is a mathematical function that takes a word as input and returns a number as output. 20 | 21 | Example code: [Hash Algorithm](./WSG%20-%20Hash.c) -------------------------------------------------------------------------------- /Examples (Algorithms)/Tic Tac Toe game.c: -------------------------------------------------------------------------------- 1 | // Tic Tac Toe game in C 2 | 3 | #include 4 | #include 5 | 6 | char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; 7 | 8 | int checkwin(); 9 | void board(); 10 | 11 | int main() 12 | { 13 | int player = 1, i, choice; 14 | 15 | char mark; 16 | do 17 | { 18 | board(); 19 | player = (player % 2) ? 1 : 2; 20 | 21 | printf("Player %d, enter a number: ", player); 22 | scanf("%d", &choice); 23 | 24 | mark = (player == 1) ? 'X' : 'O'; 25 | 26 | if (choice == 1 && square[1] == '1') 27 | square[1] = mark; 28 | 29 | else if (choice == 2 && square[2] == '2') 30 | square[2] = mark; 31 | 32 | else if (choice == 3 && square[3] == '3') 33 | square[3] = mark; 34 | 35 | else if (choice == 4 && square[4] == '4') 36 | square[4] = mark; 37 | 38 | else if (choice == 5 && square[5] == '5') 39 | square[5] = mark; 40 | 41 | else if (choice == 6 && square[6] == '6') 42 | square[6] = mark; 43 | 44 | else if (choice == 7 && square[7] == '7') 45 | square[7] = mark; 46 | 47 | else if (choice == 8 && square[8] == '8') 48 | square[8] = mark; 49 | 50 | else if (choice == 9 && square[9] == '9') 51 | square[9] = mark; 52 | 53 | else 54 | { 55 | printf("Invalid move "); 56 | 57 | player--; 58 | getch(); 59 | } 60 | i = checkwin(); 61 | 62 | player++; 63 | }while (i == - 1); 64 | 65 | board(); 66 | 67 | if (i == 1) 68 | printf("==>\aPlayer %d win ", --player); 69 | else 70 | printf("==>\aGame draw"); 71 | 72 | getch(); 73 | 74 | return 0; 75 | } 76 | 77 | /********************************************* 78 | 79 | FUNCTION TO RETURN GAME STATUS 80 | 1 FOR GAME IS OVER WITH RESULT 81 | -1 FOR GAME IS IN PROGRESS 82 | O GAME IS OVER AND NO RESULT 83 | 84 | **********************************************/ 85 | 86 | int checkwin() 87 | { 88 | if (square[1] == square[2] && square[2] == square[3]) 89 | return 1; 90 | 91 | else if (square[4] == square[5] && square[5] == square[6]) 92 | return 1; 93 | 94 | else if (square[7] == square[8] && square[8] == square[9]) 95 | return 1; 96 | 97 | else if (square[1] == square[4] && square[4] == square[7]) 98 | return 1; 99 | 100 | else if (square[2] == square[5] && square[5] == square[8]) 101 | return 1; 102 | 103 | else if (square[3] == square[6] && square[6] == square[9]) 104 | return 1; 105 | 106 | else if (square[1] == square[5] && square[5] == square[9]) 107 | return 1; 108 | 109 | else if (square[3] == square[5] && square[5] == square[7]) 110 | return 1; 111 | 112 | else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && 113 | square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] 114 | != '7' && square[8] != '8' && square[9] != '9') 115 | 116 | return 0; 117 | else 118 | return - 1; 119 | } 120 | 121 | 122 | /******************************************************************* 123 | FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK 124 | ********************************************************************/ 125 | 126 | 127 | void board() 128 | { 129 | system("cls"); 130 | printf("\n\n\tTic Tac Toe\n\n"); 131 | 132 | printf("Player 1 (X) - Player 2 (O)\n\n\n"); 133 | 134 | 135 | printf(" | | \n"); 136 | printf(" %c | %c | %c \n", square[1], square[2], square[3]); 137 | 138 | printf("_____|_____|_____\n"); 139 | printf(" | | \n"); 140 | 141 | printf(" %c | %c | %c \n", square[4], square[5], square[6]); 142 | 143 | printf("_____|_____|_____\n"); 144 | printf(" | | \n"); 145 | 146 | printf(" %c | %c | %c \n", square[7], square[8], square[9]); 147 | 148 | printf(" | | \n\n"); 149 | } 150 | 151 | /******************************************************************* 152 | END OF PROJECT 153 | ********************************************************************/ 154 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-structure-using-C 2 | 3 | The data structure means the way of arrangement of data. There are several data structures in the world. However, this repository shows some of the data structures build using the C language. 4 | 5 | ![Static Badge](https://img.shields.io/badge/Repo-Data_structure_using_C-orange) ![GitHub](https://img.shields.io/github/license/manulthanura/Data-structure-using-C) ![GitHub Repo stars](https://img.shields.io/github/stars/manulthanura/Data-structure-using-C?color=yellow) ![GitHub top language](https://img.shields.io/github/languages/top/manulthanura/Data-structure-using-C) ![GitHub last commit](https://img.shields.io/github/last-commit/manulthanura/Data-structure-using-C) 6 | 7 | ## Data Structures 8 | - [Linked List](./Data%20Structures/Linked%20List%20Data%20structure.c) 9 | - [Stack](./Data%20Structures/Stack%20Data%20structure.c) 10 | - [Queue](./Data%20Structures/Queue%20Data%20structure.c) 11 | - [Array](./Data%20Structures/Array%20Data%20Structure.c) 12 | - [Tree](./Data%20Structures/Tree%20Data%20structure.c) 13 | - [Binary Search Tree](./Data%20Structures/Binary%20tree%20Data%20Structure.c) 14 | - [Graph](./Data%20Structures/Graph%20Data%20Structure.c) 15 | - [Hashing](./Data%20Structures/Hashing%20data%20structure.c) 16 | - [Heap](./Data%20Structures/Heap%20Data%20Structure.c) 17 | - [Set](./Data%20Structures/Set%20Data%20Structure.c) 18 | 19 | 39 | 40 | ## Algorithms 41 | - [Searching](./Algorithms/Random%20Numbers%20Search.c) 42 | - [Dynamic Programming](./Algorithms/Dynamic%20programming%20Implementation/DP.md) 43 | - [Greedy Algorithms](./Algorithms/Greedy%20Implementation/Greedy%20Implementation.md) 44 | 45 | ## Fun Facts 46 | - [Word Search](./Examples%20(Algorithms)/The%20Word%20Search%20Game/WSG.md) 47 | - [Simple Bug Game 01](./Examples%20(Algorithms)/Simple%20bug%20game%20-%2001.c) 48 | - [Simple Bug Game 02](./Examples%20(Algorithms)/Simple%20bug%20game%20-%2002.c) 49 | - [Tic Tac Toe](./Examples%20(Algorithms)/Tic%20Tac%20Toe%20game.c) 50 | 51 | ## Contributing 52 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. 53 | 54 | ## License 55 | This repository is licensed under [Creative Commons Zero v1.0 Universal](https://github.com/manulthanura/Data-structure-using-C/blob/main/LICENSE) License. 56 | 57 | ![Static Badge](https://img.shields.io/badge/License-Creative_Commons_Zero_v1.0-blue) 58 | 59 | ## Support 60 | 61 | **Love This? Give my repo a star :star:** So that you can find it easily next time you need it! :heart: 62 | 63 | By [manulthanura](https://github.com/manulthanura) © 2023 64 | --------------------------------------------------------------------------------