├── .gitignore ├── .vscode └── settings.json ├── README.md ├── boilerplate ├── data-structure-1 │ ├── main.c │ └── modules │ │ ├── implementation-1 │ │ ├── v1 │ │ │ ├── _header.h │ │ │ └── def.c │ │ └── v2 │ │ │ ├── _header.h │ │ │ └── def.c │ │ └── implementation-2 │ │ ├── v1 │ │ ├── _header.h │ │ └── def.c │ │ └── v2 │ │ ├── _header.h │ │ └── def.c └── data-structure-2 │ ├── main.c │ └── modules │ ├── implementation-1 │ ├── _header.h │ ├── def-1.c │ └── def-2.c │ └── implementation-2 │ ├── _header.h │ ├── def-1.c │ └── def-2.c └── examples ├── binary-search-tree ├── main.c └── modules │ └── double-pointer │ ├── v1 │ ├── _header.h │ └── def.c │ └── v2 │ ├── _header.h │ └── def.c ├── breadth-first-search ├── main.c └── modules │ └── iterative │ ├── util │ └── queue │ │ ├── _header.h │ │ └── def.c │ ├── v1 │ ├── _header.h │ └── def.c │ ├── v2 │ ├── _header.h │ └── def.c │ └── v3 │ ├── _header.h │ └── def.c ├── depth-first-search ├── main.c └── modules │ ├── iterative │ ├── util │ │ └── stack │ │ │ ├── _header.h │ │ │ └── def.c │ ├── v1 │ │ ├── _header.h │ │ └── def.c │ ├── v2 │ │ ├── _header.h │ │ └── def.c │ └── v3 │ │ ├── _header.h │ │ └── def.c │ └── recursive │ ├── v1 │ ├── _header.h │ └── def.c │ ├── v2 │ ├── _header.h │ └── def.c │ └── v3 │ ├── _header.h │ └── def.c ├── dictionary └── open-hashing │ ├── main.c │ └── modules │ └── linked-list │ ├── v1 │ ├── _header.h │ └── def.c │ └── v2 │ ├── _header.h │ └── def.c ├── graph-implementation ├── main.c ├── main_boilerplate.c └── modules │ ├── _header.h │ ├── def_boilerplate.c │ └── v1 │ └── def.c ├── path-algorithms ├── main.c └── modules │ ├── v1 │ ├── _header.h │ └── def.c │ ├── v2 │ ├── _header.h │ └── def.c │ └── v3 │ ├── _header.h │ └── def.c ├── priority-queue ├── main.c └── modules │ └── heap │ ├── v1 │ ├── _header.h │ └── def.c │ └── v2 │ ├── _header.h │ └── def.c ├── queues ├── main.c └── modules │ ├── array │ ├── v1 │ │ ├── _header.h │ │ └── def.c │ ├── v2 │ │ ├── _header.h │ │ └── def.c │ └── v3 │ │ ├── _header.h │ │ └── def.c │ ├── cursor-based │ ├── v1 │ │ ├── _header.h │ │ └── def.c │ └── v2 │ │ ├── _header.h │ │ └── def.c │ └── linked-list │ ├── v1 │ ├── _header.h │ └── def.c │ ├── v2 │ ├── _header.h │ └── def.c │ └── v3 │ ├── _header.h │ └── def.c ├── spanning-tree ├── main.c └── modules │ ├── util │ └── minHeap │ │ ├── _header.h │ │ └── def.c │ ├── v1 │ ├── _header.h │ └── def.c │ ├── v2 │ ├── _header.h │ └── def.c │ └── v3 │ ├── _header.h │ └── def.c └── stacks ├── main.c └── modules ├── array ├── v1 │ ├── _header.h │ └── def.c ├── v2 │ ├── _header.h │ └── def.c └── v3 │ ├── _header.h │ └── def.c ├── cursor-based ├── v1 │ ├── _header.h │ └── def.c └── v2 │ ├── _header.h │ └── def.c └── linked-list ├── v1 ├── _header.h └── def.c └── v2 ├── _header.h └── def.c /.gitignore: -------------------------------------------------------------------------------- 1 | **/*.exe -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "_header.h": "c" 4 | } 5 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | This repository provides a framework that you can use to deliberately practice implementing data structures. 3 | 4 | # Quickstart 5 | ## Instructions (File Structure 1 - DSA Definition and Implementation) 6 | 1. Copy-paste one of the provided directories under `boilerplate` 7 | 2. Change the name of the directory into the name of the data structure you wish to practice implementing (e.g. `data-structure-1` to `binary-search-tree`) 8 | 3. Under the `_header.h` file in the `implementation-[number]` directory, place the structure definition and operations 9 | 4. In the adjacent `def.c` file, implement the logic of the operations defined in `_header.h` 10 | 5. In `main.c`, include the `def.c` file you just filled up and call the operations for testing 11 | 6. Should you wish to practice the same implementation again, create another folder under the same `implementation` directory and call it `v-[number]`, where `number` is your nth attempt to implement the data structure with the aforementioned implementation. 12 | 7. Redo steps 3-5. If the output for this new attempt isn't the same as that of the previous attempts, then refactor the new attempt until the output is the same (no need to rewrite tests!!) 13 | 14 | ## Instructions (File Structure 2 - DSA Implementation Only) 15 | 1. Copy-paste one of the provided directories under `boilerplate` 16 | 2. Change the name of the directory into the name of the data structure you wish to practice implementing (e.g. `data-structure-1` to `binary-search-tree`) 17 | 3. Under the `_header.h` file in the `implementation-[number]` directory, place the structure definition and operations 18 | 4. In the adjacent `def-[number].c` file, implement the logic of the operations defined in `_header.h` 19 | 5. In `main.c`, include the `def-[number].c` file you just filled up and call the operations for testing 20 | 6. Should you wish to practice the same implementation again, create `def-[number].c`file, where `number` is your nth attempt to implement the data structure with the aforementioned implementation, in the same directory as the previous one 21 | 7. Redo steps 3-5. If the output for this new attempt isn't the same as that of the previous attempts, then refactor the new attempt until the output is the same (no need to rewrite tests!!) 22 | 23 | ## Example 24 | Say you want to practice implementing the `adt-list` data structure via the following implementations: 25 | 26 | 1. Linked List 27 | 2. Array 28 | 3. Cursor-based 29 | 30 | Basing it off the boilerplate provided under `boilerplate/data-structure-1`, the resulting directory you can use for practice is as follows: 31 | 32 | ```c 33 | | adt-list/ 34 | |-- main.c 35 | |-- modules/ 36 | |---- linked-list-implementation/ // Implementation 1 37 | |------ v1/ // Practice version 1 38 | |-------- _header.h 39 | |-------- def.c 40 | |------ v2/ // Practice version 2 41 | |-------- _header.h 42 | |-------- def.c 43 | |---- array-implementation/ // Implementation 2 44 | |------ v1/ // Practice version 1 45 | |-------- _header.h 46 | |-------- def.c 47 | |---- cursor-based-implementation/ // Implementation 3 48 | |------ v1/ // Practice version 1 49 | |-------- _header.h 50 | |-------- def.c 51 | ``` 52 | 53 | # Philosophy 54 | ## Introduction 55 | Deliberate practice is a learning technique wherein one focuses on improving a specific aspect of the skill they're trying to learn. They go into the practice session knowing what they need to improve and how to improve it. This repo provides a framework that allows one to perform deliberate practice in the context of data structures and algorithms. 56 | 57 | The go-to method of learning how to implement data structures and algorithms is to...implement data structures and algorithms. However, when practicing this, there are a lot more steps than meets the eye. For example, when trying to implement a linked list, one normally creates a C file and starts coding right away. However, besides just writing the definition of a linked list and implementing the different operations, the student has to also do other things such as: 58 | 59 | - Importing the necessary libraries 60 | - Writing out tests to verify the correctness of the implementation 61 | 62 | This additional overhead presents an additional challenge to the student which, although serves as practice for how to set up the C file from the ground up, takes away time and concentration the main point of the practice session - **implementing data structures**. 63 | 64 | When a student is finished with this practice file and wishes to re-implement the data structure again later, he/she must create a new file and recreate everything. 65 | 66 | ## Purpose 67 | The main goal of this repository is to allow students to focus more on implementing different data structures and algorithms. As such, the file organization proposed in this repository is optimized for such. 68 | 69 | 70 | # File Structure 71 | There are two file structures proposed in this repo, both of which can be found in the `boilerplate/` directory. The common elements present are as follows: 72 | 73 | - `data-structure-[number]/` - The directory, labelled according to the name of the data structure you wish to practice, containing all the code for that data structure 74 | - `main.c` - The main file where your code is to be executed. Here is where you define your tests for each implementation you have to verify that it works. 75 | - `modules/` - The directory containing all the implementation code 76 | - `implementation-[number]/` - The directory, labelled according to the name of the data structure implementation you want to practice, containing the code for all your implementations 77 | - `_header.h` - The file containing the structure definition and operations 78 | - `def.c` - The file containing the implementation of the operations defined in `_header.h` 79 | 80 | ## File Structure 1 81 | ```c 82 | | data-structure-1/ 83 | |-- main.c 84 | |-- modules/ 85 | |---- implementation-1/ 86 | |------ v1/ 87 | |-------- _header.h 88 | |-------- def.c 89 | |------ v2/ 90 | |-------- _header.h 91 | |-------- def.c 92 | |---- implementation-2/ 93 | |------ v1/ 94 | |-------- _header.h 95 | |-------- def.c 96 | |---- implementation-3/ 97 | |------ v1/ 98 | |-------- _header.h 99 | |-------- def.c 100 | ``` 101 | 102 | Under the `implementation-[number]` folder, you can see a number of folders following the format `v-[number]`. These refer to the "versions", which are created when you want to practice the aforementioned implementation. Doing so allows you to practice everything about implementing the data structure, from structure definition to operation implementation. However, if you only want to focus on implementing the logic behind the implementation, you can use file structure 2 (see below). 103 | 104 | 105 | ## File Structure 2 106 | ```c 107 | | data-structure-2/ 108 | |-- main.c 109 | |-- modules/ 110 | |---- implementation-1/ 111 | |------ _header.h 112 | |------ def-1.c 113 | |------ def-2.c 114 | |---- implementation-2/ 115 | |------ _header.h 116 | |------ def-1.c 117 | |------ def-2.c 118 | ``` 119 | 120 | Under the `implementation-[number]` folder, you can see a single `_header.h` file along with multiple other c files with the format `def-[number].c`. In this file structure, you can focus more on implementing the operations behind each data structure instead of having to redefine the structure in each version (unlike file structure 1 above). 121 | 122 | ## Conclusion 123 | 124 | As was hopefully demonstrated, the proposed file structures allow you to focus on actually practice implementing data structures, instead of using up time on unnecessary overhead. As you can see, having the `main.c` file contain only the tests, instead of code for each implementation, allows you to test new implementation by simply replacing the import statement (i.e. the tests are implementation-agnostic). By drilling down the focus of your practice sessions to implementing data structures and algorithms, not only will your implementations be more optimized, but hopefully your grades as well 😉 Happy coding! 125 | 126 | ## Other Study Techniques 127 | 128 | 1. [Pomodoro Technique](https://todoist.com/productivity-methods/pomodoro-technique) 129 | 2. [Spaced Repetition](https://e-student.org/spaced-repetition/) 130 | 3. [Interleaving](https://psychology.ucsd.edu/undergraduate-program/undergraduate-resources/academic-writing-resources/effective-studying/other-learning-techniques.html#:~:text=Interleaved%20practice%20%E2%80%93%20when%20you%20are,B%20on%20the%20next%2C%20you) 131 | 132 | ## TODOS 133 | 1. Check if version number is necessary in `#ifndef` 134 | 2. Flesh out study techniques here w/ concrete examples 135 | 3. Documentation (expected output of each test, code explanation) -------------------------------------------------------------------------------- /boilerplate/data-structure-1/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/implementation-1/v2/_header.h" 2 | 3 | // * TESTS HERE 4 | int main() { 5 | DataStructureName DS; 6 | 7 | initializeStruct(); 8 | addToStruct(); 9 | deleteFromStruct(); 10 | updateStruct(); 11 | displayStruct(); 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /boilerplate/data-structure-1/modules/implementation-1/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DATASTRUCTURE_IMPLEMENTATIONNAME_V1 2 | #define DATASTRUCTURE_IMPLEMENTATIONNAME_V1 3 | 4 | // * MACROS HERE 5 | #define MAX 10 6 | 7 | // * TYPE DEFINITIONS HERE 8 | typedef struct { 9 | int someIntegerData[MAX]; 10 | char someCharData[MAX]; 11 | } DataStructureName; 12 | 13 | // * OPERATIONS HERE 14 | void initializeStruct(); 15 | void addToStruct(); 16 | void deleteFromStruct(); 17 | void updateStruct(); 18 | void displayStruct(); 19 | 20 | #endif -------------------------------------------------------------------------------- /boilerplate/data-structure-1/modules/implementation-1/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | 3 | // * FUNCTION DEFINITIONS HERE 4 | void initializeStruct() { 5 | // Implement function here 6 | } 7 | 8 | void addToStruct() { 9 | // Implement function here 10 | } 11 | 12 | void deleteFromStruct() { 13 | // Implement function here 14 | } 15 | 16 | void updateStruct() { 17 | // Implement function here 18 | } 19 | 20 | void displayStruct() { 21 | // Implement function here 22 | } -------------------------------------------------------------------------------- /boilerplate/data-structure-1/modules/implementation-1/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DATASTRUCTURE_IMPLEMENTATIONNAME_V2 2 | #define DATASTRUCTURE_IMPLEMENTATIONNAME_V2 3 | 4 | // * MACROS HERE 5 | #define MAX 10 6 | 7 | // * TYPE DEFINITIONS HERE 8 | typedef struct { 9 | int someIntegerData[MAX]; 10 | char someCharData[MAX]; 11 | } DataStructureName; 12 | 13 | // * OPERATIONS HERE 14 | void initializeStruct(); 15 | void addToStruct(); 16 | void deleteFromStruct(); 17 | void updateStruct(); 18 | void displayStruct(); 19 | 20 | #endif -------------------------------------------------------------------------------- /boilerplate/data-structure-1/modules/implementation-1/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | 3 | // * FUNCTION DEFINITIONS HERE 4 | void initializeStruct() { 5 | // Implement function here 6 | } 7 | 8 | void addToStruct() { 9 | // Implement function here 10 | } 11 | 12 | void deleteFromStruct() { 13 | // Implement function here 14 | } 15 | 16 | void updateStruct() { 17 | // Implement function here 18 | } 19 | 20 | void displayStruct() { 21 | // Implement function here 22 | } -------------------------------------------------------------------------------- /boilerplate/data-structure-1/modules/implementation-2/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DATASTRUCTURE_IMPLEMENTATIONNAME_V1 2 | #define DATASTRUCTURE_IMPLEMENTATIONNAME_V1 3 | 4 | // * MACROS HERE 5 | #define MAX 10 6 | 7 | // * TYPE DEFINITIONS HERE 8 | typedef struct { 9 | int someIntegerData[MAX]; 10 | char someCharData[MAX]; 11 | } DataStructureName; 12 | 13 | // * OPERATIONS HERE 14 | void initializeStruct(); 15 | void addToStruct(); 16 | void deleteFromStruct(); 17 | void updateStruct(); 18 | void displayStruct(); 19 | 20 | #endif -------------------------------------------------------------------------------- /boilerplate/data-structure-1/modules/implementation-2/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | 3 | // * FUNCTION DEFINITIONS HERE 4 | void initializeStruct() { 5 | // Implement function here 6 | } 7 | 8 | void addToStruct() { 9 | // Implement function here 10 | } 11 | 12 | void deleteFromStruct() { 13 | // Implement function here 14 | } 15 | 16 | void updateStruct() { 17 | // Implement function here 18 | } 19 | 20 | void displayStruct() { 21 | // Implement function here 22 | } -------------------------------------------------------------------------------- /boilerplate/data-structure-1/modules/implementation-2/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DATASTRUCTURE_IMPLEMENTATIONNAME_V2 2 | #define DATASTRUCTURE_IMPLEMENTATIONNAME_V2 3 | 4 | // * MACROS HERE 5 | #define MAX 10 6 | 7 | // * TYPE DEFINITIONS HERE 8 | typedef struct { 9 | int someIntegerData[MAX]; 10 | char someCharData[MAX]; 11 | } DataStructureName; 12 | 13 | // * OPERATIONS HERE 14 | void initializeStruct(); 15 | void addToStruct(); 16 | void deleteFromStruct(); 17 | void updateStruct(); 18 | void displayStruct(); 19 | 20 | #endif -------------------------------------------------------------------------------- /boilerplate/data-structure-1/modules/implementation-2/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | 3 | // * FUNCTION DEFINITIONS HERE 4 | void initializeStruct() { 5 | // Implement function here 6 | } 7 | 8 | void addToStruct() { 9 | // Implement function here 10 | } 11 | 12 | void deleteFromStruct() { 13 | // Implement function here 14 | } 15 | 16 | void updateStruct() { 17 | // Implement function here 18 | } 19 | 20 | void displayStruct() { 21 | // Implement function here 22 | } -------------------------------------------------------------------------------- /boilerplate/data-structure-2/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/implementation-1/_header.h" 2 | 3 | // * TESTS HERE 4 | int main() { 5 | DataStructureName DS; 6 | 7 | initializeStruct(); 8 | addToStruct(); 9 | deleteFromStruct(); 10 | updateStruct(); 11 | displayStruct(); 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /boilerplate/data-structure-2/modules/implementation-1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DATASTRUCTURE_IMPLEMENTATIONNAME 2 | #define DATASTRUCTURE_IMPLEMENTATIONNAME 3 | 4 | // * MACROS HERE 5 | #define MAX 10 6 | 7 | // * TYPE DEFINITIONS HERE 8 | typedef struct { 9 | int someIntegerData[MAX]; 10 | char someCharData[MAX]; 11 | } DataStructureName; 12 | 13 | // * OPERATIONS HERE 14 | void initializeStruct(); 15 | void addToStruct(); 16 | void deleteFromStruct(); 17 | void updateStruct(); 18 | void displayStruct(); 19 | 20 | #endif -------------------------------------------------------------------------------- /boilerplate/data-structure-2/modules/implementation-1/def-1.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | 3 | // * FUNCTION DEFINITIONS HERE 4 | void initializeStruct() { 5 | // Implement function here 6 | } 7 | 8 | void addToStruct() { 9 | // Implement function here 10 | } 11 | 12 | void deleteFromStruct() { 13 | // Implement function here 14 | } 15 | 16 | void updateStruct() { 17 | // Implement function here 18 | } 19 | 20 | void displayStruct() { 21 | // Implement function here 22 | } -------------------------------------------------------------------------------- /boilerplate/data-structure-2/modules/implementation-1/def-2.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | 3 | // * FUNCTION DEFINITIONS HERE 4 | void initializeStruct() { 5 | // Implement function here 6 | } 7 | 8 | void addToStruct() { 9 | // Implement function here 10 | } 11 | 12 | void deleteFromStruct() { 13 | // Implement function here 14 | } 15 | 16 | void updateStruct() { 17 | // Implement function here 18 | } 19 | 20 | void displayStruct() { 21 | // Implement function here 22 | } -------------------------------------------------------------------------------- /boilerplate/data-structure-2/modules/implementation-2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DATASTRUCTURE_IMPLEMENTATIONNAME 2 | #define DATASTRUCTURE_IMPLEMENTATIONNAME 3 | 4 | // * MACROS HERE 5 | #define MAX 10 6 | 7 | // * TYPE DEFINITIONS HERE 8 | typedef struct { 9 | int someIntegerData[MAX]; 10 | char someCharData[MAX]; 11 | } DataStructureName; 12 | 13 | // * OPERATIONS HERE 14 | void initializeStruct(); 15 | void addToStruct(); 16 | void deleteFromStruct(); 17 | void updateStruct(); 18 | void displayStruct(); 19 | 20 | #endif -------------------------------------------------------------------------------- /boilerplate/data-structure-2/modules/implementation-2/def-1.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | 3 | // * FUNCTION DEFINITIONS HERE 4 | void initializeStruct() { 5 | // Implement function here 6 | } 7 | 8 | void addToStruct() { 9 | // Implement function here 10 | } 11 | 12 | void deleteFromStruct() { 13 | // Implement function here 14 | } 15 | 16 | void updateStruct() { 17 | // Implement function here 18 | } 19 | 20 | void displayStruct() { 21 | // Implement function here 22 | } -------------------------------------------------------------------------------- /boilerplate/data-structure-2/modules/implementation-2/def-2.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | 3 | // * FUNCTION DEFINITIONS HERE 4 | void initializeStruct() { 5 | // Implement function here 6 | } 7 | 8 | void addToStruct() { 9 | // Implement function here 10 | } 11 | 12 | void deleteFromStruct() { 13 | // Implement function here 14 | } 15 | 16 | void updateStruct() { 17 | // Implement function here 18 | } 19 | 20 | void displayStruct() { 21 | // Implement function here 22 | } -------------------------------------------------------------------------------- /examples/binary-search-tree/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/double-pointer/v2/_header.h" 2 | #include 3 | 4 | int main() { 5 | BST B; 6 | initialize(&B); 7 | 8 | insert(&B, 'I'); 9 | insert(&B, 'C'); 10 | insert(&B, 'L'); 11 | insert(&B, 'M'); 12 | insert(&B, 'Q'); 13 | insert(&B, 'B'); 14 | insert(&B, 'U'); 15 | insert(&B, 'C'); 16 | insert(&B, 'J'); 17 | insert(&B, 'F'); 18 | insert(&B, 'D'); 19 | insert(&B, 'G'); 20 | 21 | printf("\n----- PRE-ORDER -----\n"); 22 | preOrderWalk(B); 23 | 24 | printf("\n----- IN-ORDER -----\n"); 25 | inOrderWalk(B); 26 | 27 | printf("\n----- POST-ORDER -----\n"); 28 | postOrderWalk(B); 29 | 30 | delete(&B, 'I'); 31 | delete(&B, 'C'); 32 | delete(&B, 'B'); 33 | delete(&B, 'U'); 34 | 35 | printf("\n----- PRE-ORDER -----\n"); 36 | preOrderWalk(B); 37 | 38 | printf("\nMin: %c\n", min(B)); 39 | printf("Max: %c\n", max(B)); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /examples/binary-search-tree/modules/double-pointer/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef BST_DOUBLEPOINTER_V1 2 | #define BST_DOUBLEPOINTER_V1 3 | #include 4 | 5 | typedef struct node { 6 | char data; 7 | struct node *LC; 8 | struct node *RC; 9 | } Node, *BST; 10 | 11 | void initialize(BST *B); 12 | void insert(BST *B, char elem); 13 | bool isMember(BST B, char elem); 14 | void delete(BST *B, char elem); 15 | char min(BST B); 16 | char max(BST B); 17 | 18 | void preOrderWalk(BST B); 19 | void inOrderWalk(BST B); 20 | void postOrderWalk(BST B); 21 | 22 | #endif -------------------------------------------------------------------------------- /examples/binary-search-tree/modules/double-pointer/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void initialize(BST *B) { 7 | *B = NULL; 8 | } 9 | 10 | void insert(BST *B, char elem) { 11 | BST *trav; 12 | 13 | for (trav = B; *trav != NULL && elem != (*trav)->data;) { 14 | trav = elem > (*trav)->data ? &(*trav)->RC : &(*trav)->LC; 15 | } 16 | 17 | if (*trav == NULL) { 18 | BST node = (BST)malloc(sizeof(Node)); 19 | 20 | if (node != NULL) { 21 | node->data = elem; 22 | node->LC = NULL; 23 | node->RC = NULL; 24 | 25 | *trav = node; 26 | } 27 | } 28 | } 29 | 30 | bool isMember(BST B, char elem) { 31 | BST trav; 32 | 33 | for (trav = B; trav != NULL && elem != trav->data;) { 34 | trav = elem > trav->data ? trav->RC : trav->LC; 35 | } 36 | 37 | return trav != NULL; 38 | } 39 | 40 | void delete(BST *B, char elem) { 41 | BST *trav; 42 | 43 | for (trav = B; *trav != NULL && elem != (*trav)->data;) { 44 | trav = elem > (*trav)->data ? &(*trav)->RC : &(*trav)->LC; 45 | } 46 | 47 | BST temp; 48 | 49 | if (*trav != NULL) { 50 | if ((*trav)->LC == NULL) { 51 | temp = *trav; 52 | *trav = temp->RC; 53 | free(temp); 54 | } else if ((*trav)->RC == NULL) { 55 | temp = *trav; 56 | *trav = temp->LC; 57 | free(temp); 58 | } else { 59 | BST *predecessor; 60 | for (predecessor = &(*trav)->LC; (*predecessor)->RC != NULL; predecessor = &(*predecessor)->RC) {} 61 | 62 | temp = *predecessor; 63 | (*trav)->data = temp->data; 64 | *predecessor = temp->LC; 65 | free(temp); 66 | } 67 | } 68 | } 69 | 70 | char min(BST B) { 71 | BST trav; 72 | 73 | for (trav = B; trav != NULL && trav->LC != NULL; trav = trav->LC) {} 74 | 75 | return B == NULL ? '0' : trav->data; 76 | } 77 | 78 | char max(BST B) { 79 | BST trav; 80 | 81 | for (trav = B; trav != NULL && trav->RC != NULL; trav = trav->RC) {} 82 | 83 | return B == NULL ? '0' : trav->data; 84 | } 85 | 86 | void preOrderWalk(BST B) { 87 | if (B != NULL) { 88 | printf("%c", B->data); 89 | preOrderWalk(B->LC); 90 | preOrderWalk(B->RC); 91 | } 92 | } 93 | 94 | void inOrderWalk(BST B) { 95 | if (B != NULL) { 96 | inOrderWalk(B->LC); 97 | printf("%c", B->data); 98 | inOrderWalk(B->RC); 99 | } 100 | } 101 | 102 | void postOrderWalk(BST B) { 103 | if (B != NULL) { 104 | postOrderWalk(B->LC); 105 | postOrderWalk(B->RC); 106 | printf("%c", B->data); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /examples/binary-search-tree/modules/double-pointer/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef BST_DOUBLEPOINTER_V2 2 | #define BST_DOUBLEPOINTER_V2 3 | #include 4 | 5 | typedef struct node { 6 | char data; 7 | struct node *LC; 8 | struct node *RC; 9 | } Node, *BST; 10 | 11 | void initialize(BST *B); 12 | void insert(BST *B, char elem); 13 | bool isMember(BST B, char elem); 14 | void delete(BST *B, char elem); 15 | char min(BST B); 16 | char max(BST B); 17 | char deleteMin(BST *B); 18 | 19 | void preOrderWalk(BST B); 20 | void inOrderWalk(BST B); 21 | void postOrderWalk(BST B); 22 | 23 | 24 | #endif -------------------------------------------------------------------------------- /examples/binary-search-tree/modules/double-pointer/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void initialize(BST *B) { 7 | *B = NULL; 8 | } 9 | void insert(BST *B, char elem) { 10 | BST *trav = B; 11 | if (*trav == NULL) { 12 | BST temp = (BST)malloc(sizeof(Node)); 13 | 14 | if (temp != NULL) { 15 | temp->data = elem; 16 | temp->LC = *trav; 17 | temp->RC = *trav; 18 | *trav = temp; 19 | } 20 | } else if (elem > (*trav)->data) { 21 | insert(&(*trav)->RC, elem); 22 | } else if (elem < (*trav)->data) { 23 | insert(&(*trav)->LC, elem); 24 | } 25 | } 26 | bool isMember(BST B, char elem) { 27 | if (B == NULL) { 28 | return false; 29 | } else if (elem == B->data) { 30 | return true; 31 | } else if (elem > B->data) { 32 | return isMember(B->RC, elem); 33 | } else { 34 | return isMember(B->LC, elem); 35 | } 36 | } 37 | 38 | void delete(BST *B, char elem) { 39 | BST *trav = B, temp; 40 | 41 | if ((*trav) == NULL) { 42 | return; 43 | } else if (elem == (*trav)->data) { 44 | if ((*trav)->LC == NULL) { 45 | temp = *trav; 46 | *trav = temp->RC; 47 | free(temp); 48 | } else if ((*trav)->RC == NULL) { 49 | temp = *trav; 50 | *trav = temp->LC; 51 | free(temp); 52 | } else { 53 | (*trav)->data = deleteMin(&(*trav)->RC); 54 | } 55 | } else if (elem > (*trav)->data) { 56 | delete(&(*trav)->RC, elem); 57 | } else { 58 | delete(&(*trav)->LC, elem); 59 | } 60 | } 61 | char deleteMin(BST *B) { 62 | BST *trav = B, temp; 63 | int ret; 64 | 65 | if ((*trav)->LC == NULL) { 66 | temp = *trav; 67 | *trav = temp->RC; 68 | ret = temp->data; 69 | free(temp); 70 | return ret; 71 | } else { 72 | return deleteMin(&(*trav)->LC); 73 | } 74 | } 75 | 76 | char min(BST B) { 77 | if (B == NULL) { 78 | return '0'; 79 | } else if (B->LC == NULL) { 80 | return B->data; 81 | } else { 82 | return min(B->LC); 83 | } 84 | } 85 | char max(BST B) { 86 | if (B == NULL) { 87 | return '0'; 88 | } else if (B->RC == NULL) { 89 | return B->data; 90 | } else { 91 | return max(B->RC); 92 | } 93 | } 94 | 95 | void preOrderWalk(BST B) { 96 | if (B != NULL) { 97 | printf("%c", B->data); 98 | preOrderWalk(B->LC); 99 | preOrderWalk(B->RC); 100 | } 101 | } 102 | void inOrderWalk(BST B) { 103 | if (B != NULL) { 104 | inOrderWalk(B->LC); 105 | printf("%c", B->data); 106 | inOrderWalk(B->RC); 107 | } 108 | } 109 | void postOrderWalk(BST B) { 110 | if (B != NULL) { 111 | postOrderWalk(B->LC); 112 | postOrderWalk(B->RC); 113 | printf("%c", B->data); 114 | } 115 | } -------------------------------------------------------------------------------- /examples/breadth-first-search/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/iterative/v3/_header.h" 2 | 3 | int main() { 4 | int m1[MAX][MAX] = { 5 | {0, 1, 0, 1, 1}, 6 | {0, 0, 1, 0, 0}, 7 | {1, 0, 0, 0, 1}, 8 | {0, 0, 1, 0, 1}, 9 | {0, 0, 0, 0, 0} 10 | }; 11 | // 0 1 4 3 2 12 | // 1 2 0 4 3 13 | // 2 0 4 1 3 14 | // 3 2 4 0 1 15 | // 4 INF INF INF 16 | 17 | int m2[MAX][MAX] = { 18 | {0, 1, 1, 0, 0}, 19 | {1, 0, 1, 1, 1}, 20 | {1, 1, 0, 1, 0}, 21 | {0, 1, 1, 0, 1}, 22 | {0, 1, 0, 1, 0} 23 | }; 24 | // 0 1 2 3 4 25 | // 1 0 2 3 4 26 | // 2 0 1 3 4 27 | // 3 1 2 4 0 28 | // 4 1 3 0 2 29 | 30 | int m3[MAX][MAX] = { 31 | {0, 1, 1, 0, 0}, 32 | {1, 0, 1, 1, 0}, 33 | {1, 1, 0, 1, 0}, 34 | {0, 1, 1, 0, 1}, 35 | {0, 0, 0, 1, 0} 36 | }; 37 | // 0 1 2 3 4 38 | // 1 0 2 3 4 39 | // 2 0 1 3 4 40 | // 3 1 2 4 0 41 | // 4 3 1 2 0 42 | 43 | int m4[MAX][MAX] = { 44 | {0, 1, 0, 1, 0}, 45 | {0, 0, 1, 0, 0}, 46 | {0, 0, 0, 1, 0}, 47 | {0, 0, 0, 0, 1}, 48 | {0, 0, 1, 0, 0} 49 | }; 50 | // 0 1 3 2 4 51 | // 1 2 3 4 -1 52 | // 2 3 4 -1 -1 53 | // 3 4 2 -1 -1 54 | // 4 2 3 -1 -1 55 | 56 | printMatrix(m4); 57 | int *res = BFS(m4, 4); 58 | 59 | printArray(res); 60 | 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /examples/breadth-first-search/modules/iterative/util/queue/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_ARRAY 2 | #define QUEUE_ARRAY 3 | #include 4 | 5 | #define QUEUE_MAX 10 6 | 7 | typedef struct { 8 | int Elem[QUEUE_MAX]; 9 | int front; 10 | int rear; 11 | } QUEUE; 12 | 13 | void initialize(QUEUE *QP); 14 | void enqueue(int elem, QUEUE *QP); 15 | void dequeue(QUEUE *QP); 16 | int front (QUEUE Q); 17 | bool isEmpty(QUEUE Q); 18 | bool isFull(QUEUE Q); 19 | void displayList(QUEUE *QP); 20 | 21 | #endif -------------------------------------------------------------------------------- /examples/breadth-first-search/modules/iterative/util/queue/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(QUEUE *QP) { 6 | QP->rear = -1; 7 | QP->front = 0; 8 | } 9 | void enqueue(int elem, QUEUE *QP) { 10 | if (!isFull(*QP)) { 11 | QP->rear = (QP->rear + 1) % QUEUE_MAX; 12 | QP->Elem[QP->rear] = elem; 13 | } 14 | } 15 | void dequeue(QUEUE *QP) { 16 | if (!isEmpty(*QP)) { 17 | QP->front = (QP->front + 1) % QUEUE_MAX; 18 | } 19 | } 20 | int front (QUEUE Q) { 21 | int res = '\0'; 22 | 23 | if (!isEmpty(Q)) { 24 | res = Q.Elem[Q.front]; 25 | } 26 | } 27 | bool isEmpty(QUEUE Q) { 28 | return (Q.rear + 1) % QUEUE_MAX == Q.front; 29 | } 30 | bool isFull(QUEUE Q) { 31 | return (Q.rear + 2) % QUEUE_MAX == Q.front; 32 | } 33 | void displayList(QUEUE *QP) { 34 | QUEUE temp; 35 | initialize(&temp); 36 | int frontElem; 37 | 38 | printf("QUEUE: "); 39 | for (frontElem = front(*QP); !isEmpty(*QP); enqueue(frontElem, &temp), dequeue(QP), frontElem = front(*QP)) { 40 | printf("%c ", frontElem); 41 | } 42 | printf("\n"); 43 | 44 | for (frontElem = front(temp); !isEmpty(temp); enqueue(frontElem, QP), dequeue(&temp), frontElem = front(temp)) {} 45 | } -------------------------------------------------------------------------------- /examples/breadth-first-search/modules/iterative/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef BFS_ITERATIVE_V1 2 | #define BFS_ITERATIVE_V1 3 | 4 | #define MAX 5 5 | typedef int MATRIX[][MAX]; 6 | 7 | int* BFS(MATRIX matrix, int src); 8 | void printMatrix(MATRIX matrix); 9 | void printArray(int* array); 10 | 11 | #endif -------------------------------------------------------------------------------- /examples/breadth-first-search/modules/iterative/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include "../util/queue/_header.h" 3 | #include 4 | #include 5 | 6 | int* BFS(MATRIX matrix, int src) { 7 | int* res = (int*)malloc(MAX * sizeof(int)); 8 | int *visited = (int*)calloc(MAX, sizeof(int)); 9 | 10 | if (res != NULL && visited != NULL) { 11 | QUEUE queue; 12 | initialize(&queue); 13 | int node, neighbor, ctr; 14 | 15 | for (ctr = 0, node = src, enqueue(node, &queue); !isEmpty(queue); dequeue(&queue), node = front(queue)) { 16 | res[ctr++] = node; 17 | visited[node] = 1; 18 | 19 | for (neighbor = 0; neighbor < MAX; neighbor++) { 20 | if (matrix[node][neighbor] == 1 && visited[neighbor] == 0) { 21 | enqueue(neighbor, &queue); 22 | } 23 | } 24 | } 25 | } 26 | 27 | return res; 28 | } 29 | 30 | void printMatrix(MATRIX matrix) { 31 | int i, j; 32 | 33 | printf("\n----- MATRIX -----\n"); 34 | for (i = 0; i < MAX; i++) { 35 | printf("[ "); 36 | for (j = 0; j < MAX; j++) { 37 | printf("%15d ", matrix[i][j]); 38 | } 39 | printf("]\n"); 40 | } 41 | } 42 | void printArray(int* array) { 43 | int i; 44 | printf("\n----- ARRAY -----\n"); 45 | printf("[ "); 46 | for (i = 0; i < MAX; i++) { 47 | printf("%15d ", array[i]); 48 | } 49 | printf("]\n"); 50 | } -------------------------------------------------------------------------------- /examples/breadth-first-search/modules/iterative/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef BFS_ITERATIVE_V2 2 | #define BFS_ITERATIVE_V2 3 | 4 | #define MAX 5 5 | typedef int MATRIX[MAX][MAX]; 6 | 7 | int* BFS(MATRIX matrix, int src); 8 | void printMatrix(MATRIX matrix); 9 | void printArray(int* array); 10 | 11 | #endif -------------------------------------------------------------------------------- /examples/breadth-first-search/modules/iterative/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include "../util/queue/_header.h" 3 | #include 4 | #include 5 | 6 | int* BFS(MATRIX matrix, int src) { 7 | int *res = (int*)malloc(MAX * sizeof(int)); 8 | 9 | if (res != NULL) { 10 | QUEUE q; 11 | initialize(&q); 12 | 13 | int visited[MAX] = {0}, trav, ctr; 14 | for (ctr = 0, trav = src, enqueue(trav, &q); !isEmpty(q); dequeue(&q), trav = front(q)) { 15 | visited[trav] = 1; 16 | res[ctr++] = trav; 17 | 18 | int head; 19 | for (head = 0; head < MAX; head++) { 20 | if (matrix[trav][head] == 1 && visited[head] != 1) { 21 | enqueue(head, &q); 22 | } 23 | } 24 | } 25 | } 26 | 27 | return res; 28 | } 29 | 30 | void printMatrix(MATRIX matrix) { 31 | int i, j; 32 | 33 | printf("\n----- MATRIX -----\n"); 34 | for (i = 0; i < MAX; i++) { 35 | printf("[ "); 36 | for (j = 0; j < MAX; j++) { 37 | printf("%15d ", matrix[i][j]); 38 | } 39 | printf("]\n"); 40 | } 41 | } 42 | void printArray(int* array) { 43 | int i; 44 | printf("\n----- ARRAY -----\n"); 45 | printf("[ "); 46 | for (i = 0; i < MAX; i++) { 47 | printf("%15d ", array[i]); 48 | } 49 | printf("]\n"); 50 | } -------------------------------------------------------------------------------- /examples/breadth-first-search/modules/iterative/v3/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef BFS_ITERATIVE_V3 2 | #define BFS_ITERATIVE_V3 3 | 4 | #define MAX 5 5 | typedef int MATRIX[MAX][MAX]; 6 | 7 | int* BFS(MATRIX matrix, int src); 8 | void printMatrix(MATRIX matrix); 9 | void printArray(int* array); 10 | 11 | #endif -------------------------------------------------------------------------------- /examples/breadth-first-search/modules/iterative/v3/def.c: -------------------------------------------------------------------------------- 1 | #include "../util/queue/_header.h" 2 | #include "_header.h" 3 | #include 4 | #include 5 | 6 | int* BFS(MATRIX matrix, int src) { 7 | int *res = (int*)malloc(MAX * sizeof(int)); 8 | if (res != NULL) { 9 | int visited[MAX] = {0}; 10 | int queued[MAX] = {0}; 11 | QUEUE q; 12 | initialize(&q); 13 | enqueue(src, &q); 14 | int node, ctr; 15 | 16 | for (ctr = 0; ctr < MAX; ctr++) { 17 | res[ctr] = -1; 18 | } 19 | 20 | for (ctr = 0, node = front(q), queued[node] = 1; !isEmpty(q); node = front(q)) { 21 | printf("Node: %d\n", node); 22 | dequeue(&q); 23 | visited[node] = 1; 24 | res[ctr++] = node; 25 | int neighbor; 26 | for (neighbor = 0; neighbor < MAX; neighbor++) { 27 | printf("Neighbor: %d\n", neighbor); 28 | if (matrix[node][neighbor] == 1 && visited[neighbor] == 0 && queued[neighbor] == 0) { 29 | enqueue(neighbor, &q); 30 | queued[neighbor] = 1; 31 | printf("Considering!"); 32 | } 33 | printf("\n"); 34 | } 35 | } 36 | } 37 | return res; 38 | } 39 | void printMatrix(MATRIX matrix) { 40 | int i, j; 41 | 42 | printf("\n----- MATRIX -----\n"); 43 | for (i = 0; i < MAX; i++) { 44 | printf("[ "); 45 | for (j = 0; j < MAX; j++) { 46 | printf("%15d ", matrix[i][j]); 47 | } 48 | printf("]\n"); 49 | } 50 | } 51 | void printArray(int* array) { 52 | int i; 53 | printf("\n----- ARRAY -----\n"); 54 | printf("[ "); 55 | for (i = 0; i < MAX; i++) { 56 | printf("%15d ", array[i]); 57 | } 58 | printf("]\n"); 59 | } -------------------------------------------------------------------------------- /examples/depth-first-search/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/recursive/v3/_header.h" 2 | 3 | int main() { 4 | int m1[MAX][MAX] = { 5 | {0, 1, 0, 1, 1}, 6 | {0, 0, 1, 0, 0}, 7 | {1, 0, 0, 0, 1}, 8 | {0, 0, 1, 0, 1}, 9 | {0, 0, 0, 0, 0} 10 | }; 11 | // 0 1 2 4 3 12 | // 1 2 0 3 4 13 | // 2 0 1 3 4 14 | // 3 2 0 1 4 15 | // 4 -1 -1 -1 16 | 17 | int m2[MAX][MAX] = { 18 | {0, 1, 1, 0, 0}, 19 | {1, 0, 1, 1, 1}, 20 | {1, 1, 0, 1, 0}, 21 | {0, 1, 1, 0, 1}, 22 | {0, 1, 0, 1, 0} 23 | }; 24 | // 0 1 2 3 4 25 | // 1 0 2 3 4 26 | // 2 0 1 3 4 27 | // 3 1 0 2 4 28 | // 4 1 0 2 3 29 | 30 | int m3[MAX][MAX] = { 31 | {0, 1, 1, 0, 0}, 32 | {1, 0, 1, 1, 0}, 33 | {1, 1, 0, 1, 0}, 34 | {0, 1, 1, 0, 1}, 35 | {0, 0, 0, 1, 0} 36 | }; 37 | // 0 1 2 3 4 38 | // 1 0 2 3 4 39 | // 2 0 1 3 4 40 | // 3 1 0 2 4 41 | // 4 3 1 0 2 42 | 43 | int m4[MAX][MAX] = { 44 | {0, 1, 0, 1, 0}, 45 | {0, 0, 1, 0, 0}, 46 | {0, 0, 0, 1, 0}, 47 | {0, 0, 0, 0, 1}, 48 | {0, 0, 1, 0, 0} 49 | }; 50 | // 0 1 2 3 4 51 | // 1 2 3 4 -1 52 | // 2 3 4 -1 -1 53 | // 3 4 2 -1 -1 54 | // 4 2 3 -1 -1 55 | 56 | printMatrix(m2); 57 | int *res = DFS(m2, 1); 58 | 59 | printArray(res); 60 | 61 | return 0; 62 | } -------------------------------------------------------------------------------- /examples/depth-first-search/modules/iterative/util/stack/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_ARRAY_V3 2 | #define STACK_ARRAY_V3 3 | #include 4 | 5 | #define STACK_MAX 10 6 | 7 | typedef struct { 8 | int data[STACK_MAX]; 9 | int top; 10 | } STACK; 11 | 12 | void initialize(STACK *SP); 13 | void push(int elem, STACK *SP); 14 | void pop(STACK *SP); 15 | int top(STACK S); 16 | bool isEmpty(STACK S); 17 | bool isFull(STACK S); 18 | void displayStack(STACK* SP); 19 | 20 | #endif -------------------------------------------------------------------------------- /examples/depth-first-search/modules/iterative/util/stack/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(STACK *SP) { 6 | SP->top = -1; 7 | } 8 | void push(int elem, STACK *SP) { 9 | if (!isFull(*SP)) { 10 | SP->top++; 11 | SP->data[SP->top] = elem; 12 | } 13 | } 14 | void pop(STACK *SP) { 15 | if (!isEmpty(*SP)) { 16 | SP->top--; 17 | } 18 | } 19 | int top(STACK S) { 20 | int res = '\0'; 21 | 22 | if (!isEmpty(S)) { 23 | res = S.data[S.top]; 24 | } 25 | 26 | return res; 27 | } 28 | bool isEmpty(STACK S) { 29 | return S.top == -1; 30 | } 31 | bool isFull(STACK S) { 32 | return S.top == STACK_MAX - 1; 33 | } 34 | void displayStack(STACK* SP) { 35 | STACK temp; 36 | initialize(&temp); 37 | int topElem; 38 | 39 | printf("STACK: "); 40 | for (topElem = top(*SP); !isEmpty(*SP); push(topElem, &temp), pop(SP), topElem = top(*SP)) { 41 | printf("%c ", topElem); 42 | } 43 | 44 | printf("\n"); 45 | 46 | for (topElem = top(temp); !isEmpty(temp); push(topElem, SP), pop(&temp), topElem = top(temp)) {} 47 | } -------------------------------------------------------------------------------- /examples/depth-first-search/modules/iterative/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DFS_ITERATIVE_V1 2 | #define DFS_ITERATIVE_V1 3 | 4 | #define MAX 5 5 | typedef int MATRIX[][MAX]; 6 | 7 | int* DFS(MATRIX matrix, int src); 8 | void printMatrix(MATRIX matrix); 9 | void printArray(int* array); 10 | 11 | #endif -------------------------------------------------------------------------------- /examples/depth-first-search/modules/iterative/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include "../util/stack/_header.h" 3 | #include 4 | #include 5 | 6 | int* DFS(MATRIX matrix, int src) { 7 | int *res = (int*)malloc(MAX * sizeof(int)); 8 | int *visited = (int*)calloc(MAX, sizeof(int)); 9 | 10 | if (res != NULL && visited != NULL) { 11 | STACK stack; 12 | initialize(&stack); 13 | int node, neighbor, ctr; 14 | 15 | for (ctr = 0, node = src, push(node, &stack); !isEmpty(stack); node = top(stack), pop(&stack)) { 16 | res[ctr++] = node; 17 | visited[node] = 1; 18 | 19 | for (neighbor = MAX - 1; neighbor > -1; neighbor--) { 20 | if (matrix[node][neighbor] == 1 && visited[neighbor] == 0) { 21 | push(neighbor, &stack); 22 | } 23 | } 24 | } 25 | } 26 | 27 | return res; 28 | } 29 | 30 | void printMatrix(MATRIX matrix) { 31 | int i, j; 32 | 33 | printf("\n----- MATRIX -----\n"); 34 | for (i = 0; i < MAX; i++) { 35 | printf("[ "); 36 | for (j = 0; j < MAX; j++) { 37 | printf("%15d ", matrix[i][j]); 38 | } 39 | printf("]\n"); 40 | } 41 | } 42 | void printArray(int* array) { 43 | int i; 44 | printf("\n----- ARRAY -----\n"); 45 | printf("[ "); 46 | for (i = 0; i < MAX; i++) { 47 | printf("%15d ", array[i]); 48 | } 49 | printf("]\n"); 50 | } -------------------------------------------------------------------------------- /examples/depth-first-search/modules/iterative/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DFS_ITERATIVE_V2 2 | #define DFS_ITERATIVE_V2 3 | 4 | #define MAX 5 5 | typedef int MATRIX[MAX][MAX]; 6 | 7 | int* DFS(MATRIX matrix, int src); 8 | void printMatrix(MATRIX matrix); 9 | void printArray(int* array); 10 | 11 | #endif -------------------------------------------------------------------------------- /examples/depth-first-search/modules/iterative/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include "../util/stack/_header.h" 3 | #include 4 | #include 5 | 6 | int* DFS(MATRIX matrix, int src) { 7 | int *res = (int*)malloc(MAX * sizeof(int)); 8 | 9 | if (res != NULL) { 10 | STACK s; 11 | initialize(&s); 12 | int visited[MAX] = {0}; 13 | 14 | int trav, head, ctr; 15 | for (ctr = 0, trav = src, push(trav, &s); !isEmpty(s); trav = top(s), pop(&s)) { 16 | visited[trav] = 1; 17 | res[ctr++] = trav; 18 | 19 | for (head = MAX - 1; head > -1; head--) { 20 | if (matrix[trav][head] == 1 && visited[head] != 1) { 21 | push(head, &s); 22 | } 23 | } 24 | } 25 | } 26 | 27 | return res; 28 | } 29 | 30 | void printMatrix(MATRIX matrix) { 31 | int i, j; 32 | 33 | printf("\n----- MATRIX -----\n"); 34 | for (i = 0; i < MAX; i++) { 35 | printf("[ "); 36 | for (j = 0; j < MAX; j++) { 37 | printf("%15d ", matrix[i][j]); 38 | } 39 | printf("]\n"); 40 | } 41 | } 42 | void printArray(int* array) { 43 | int i; 44 | printf("\n----- ARRAY -----\n"); 45 | printf("[ "); 46 | for (i = 0; i < MAX; i++) { 47 | printf("%15d ", array[i]); 48 | } 49 | printf("]\n"); 50 | } -------------------------------------------------------------------------------- /examples/depth-first-search/modules/iterative/v3/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DFS_ITERATIVE_V3 2 | #define DFS_ITERATIVE_V3 3 | 4 | #define MAX 5 5 | typedef int MATRIX[MAX][MAX]; 6 | 7 | int* DFS(MATRIX matrix, int src); 8 | void printMatrix(MATRIX matrix); 9 | void printArray(int* array); 10 | 11 | #endif -------------------------------------------------------------------------------- /examples/depth-first-search/modules/iterative/v3/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include "../util/stack/_header.h" 3 | #include 4 | #include 5 | 6 | int* DFS(MATRIX matrix, int src) { 7 | int *res = (int*)malloc(MAX * sizeof(int)); 8 | 9 | if (res != NULL) { 10 | int visited[MAX] = {0}; 11 | int stacked[MAX] = {0}; 12 | STACK s; 13 | initialize(&s); 14 | int node, neighbor, ctr; 15 | 16 | for (ctr = 0; ctr < MAX; ctr++) { 17 | res[ctr] = -1; 18 | } 19 | 20 | for (ctr = 0, node = src, push(node, &s); !isEmpty(s); node = top(s)) { 21 | pop(&s); 22 | stacked[node] = 1; 23 | visited[node] = 1; 24 | res[ctr++] = node; 25 | 26 | for (neighbor = MAX - 1; neighbor > -1; neighbor--) { 27 | if (matrix[node][neighbor] == 1 && visited[neighbor] == 0 && stacked[neighbor] == 0) { 28 | push(neighbor, &s); 29 | stacked[neighbor] = 1; 30 | } 31 | } 32 | } 33 | } 34 | 35 | return res; 36 | } 37 | 38 | void printMatrix(MATRIX matrix) { 39 | int i, j; 40 | 41 | printf("\n----- MATRIX -----\n"); 42 | for (i = 0; i < MAX; i++) { 43 | printf("[ "); 44 | for (j = 0; j < MAX; j++) { 45 | printf("%15d ", matrix[i][j]); 46 | } 47 | printf("]\n"); 48 | } 49 | } 50 | void printArray(int* array) { 51 | int i; 52 | printf("\n----- ARRAY -----\n"); 53 | printf("[ "); 54 | for (i = 0; i < MAX; i++) { 55 | printf("%15d ", array[i]); 56 | } 57 | printf("]\n"); 58 | } -------------------------------------------------------------------------------- /examples/depth-first-search/modules/recursive/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DFS_RECURSIVE_V1 2 | #define DFS_RECRUSIVE_V1 3 | 4 | #define MAX 5 5 | typedef int MATRIX[][MAX]; 6 | 7 | int* DFS(MATRIX matrix, int src); 8 | void dfsHelper(MATRIX matrix, int vertex, int *visited, int *res, int *ctr); 9 | void printMatrix(MATRIX matrix); 10 | void printArray(int* array); 11 | 12 | 13 | #endif -------------------------------------------------------------------------------- /examples/depth-first-search/modules/recursive/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | int* DFS(MATRIX matrix, int src) { 6 | int *res = (int*)malloc(MAX * sizeof(int)); 7 | int *visited = (int*)calloc(MAX, sizeof(int)); 8 | 9 | if (res != NULL && visited != NULL) { 10 | int ctr = 0; 11 | dfsHelper(matrix, src, visited, res, &ctr); 12 | } 13 | 14 | return res; 15 | } 16 | 17 | void dfsHelper(MATRIX matrix, int vertex, int *visited, int *res, int *ctr) { 18 | res[(*ctr)++] = vertex; 19 | visited[vertex] = 1; 20 | int neighbor; 21 | 22 | for (neighbor = 0; neighbor < MAX; neighbor++) { 23 | if (matrix[vertex][neighbor] == 1 && visited[neighbor] == 0) { 24 | dfsHelper(matrix, neighbor, visited, res, ctr); 25 | } 26 | } 27 | } 28 | 29 | void printMatrix(MATRIX matrix) { 30 | int i, j; 31 | 32 | printf("\n----- MATRIX -----\n"); 33 | for (i = 0; i < MAX; i++) { 34 | printf("[ "); 35 | for (j = 0; j < MAX; j++) { 36 | printf("%15d ", matrix[i][j]); 37 | } 38 | printf("]\n"); 39 | } 40 | } 41 | void printArray(int* array) { 42 | int i; 43 | printf("\n----- ARRAY -----\n"); 44 | printf("[ "); 45 | for (i = 0; i < MAX; i++) { 46 | printf("%15d ", array[i]); 47 | } 48 | printf("]\n"); 49 | } -------------------------------------------------------------------------------- /examples/depth-first-search/modules/recursive/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DFS_RECURSIVE_V2 2 | #define DFS_RECURSIVE_V2 3 | 4 | #define MAX 5 5 | typedef int MATRIX[MAX][MAX]; 6 | 7 | int* DFS(MATRIX matrix, int src); 8 | void dfsHelper(MATRIX matrix, int vertex, int *visited, int *res, int *ctr); 9 | void printMatrix(MATRIX matrix); 10 | void printArray(int* array); 11 | 12 | 13 | #endif -------------------------------------------------------------------------------- /examples/depth-first-search/modules/recursive/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | int* DFS(MATRIX matrix, int src) { 6 | int *res = (int*)malloc(MAX * sizeof(int)); 7 | 8 | if (res != NULL) { 9 | int visited[MAX] = {0}, ctr = 0; 10 | dfsHelper(matrix, src, visited, res, &ctr); 11 | } 12 | 13 | return res; 14 | } 15 | 16 | void dfsHelper(MATRIX matrix, int vertex, int *visited, int *res, int *ctr) { 17 | if (*ctr == MAX) { 18 | return; 19 | } else { 20 | visited[vertex] = 1; 21 | res[(*ctr)++] = vertex; 22 | 23 | int head; 24 | for (head = 0; head < MAX; head++) { 25 | if (matrix[vertex][head] == 1 && visited[head] != 1) { 26 | dfsHelper(matrix, head, visited, res, ctr); 27 | } 28 | } 29 | } 30 | } 31 | 32 | void printMatrix(MATRIX matrix) { 33 | int i, j; 34 | 35 | printf("\n----- MATRIX -----\n"); 36 | for (i = 0; i < MAX; i++) { 37 | printf("[ "); 38 | for (j = 0; j < MAX; j++) { 39 | printf("%15d ", matrix[i][j]); 40 | } 41 | printf("]\n"); 42 | } 43 | } 44 | void printArray(int* array) { 45 | int i; 46 | printf("\n----- ARRAY -----\n"); 47 | printf("[ "); 48 | for (i = 0; i < MAX; i++) { 49 | printf("%15d ", array[i]); 50 | } 51 | printf("]\n"); 52 | } -------------------------------------------------------------------------------- /examples/depth-first-search/modules/recursive/v3/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DFS_RECURSIVE_V3 2 | #define DFS_RECURSIVE_V3 3 | 4 | #define MAX 5 5 | typedef int MATRIX[MAX][MAX]; 6 | 7 | int* DFS(MATRIX matrix, int src); 8 | void dfsHelper(MATRIX matrix, int vertex, int *visited, int *res, int *ctr); 9 | void printMatrix(MATRIX matrix); 10 | void printArray(int* array); 11 | 12 | 13 | #endif -------------------------------------------------------------------------------- /examples/depth-first-search/modules/recursive/v3/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | int* DFS(MATRIX matrix, int src) { 6 | int *res = (int*)malloc(sizeof(MAX * sizeof(int))); 7 | 8 | if (res != NULL) { 9 | int ctr = 0, visited[MAX] = {0}; 10 | dfsHelper(matrix, src, visited, res, &ctr); 11 | } 12 | 13 | return res; 14 | } 15 | void dfsHelper(MATRIX matrix, int vertex, int *visited, int *res, int *ctr) { 16 | visited[vertex] = 1; 17 | res[(*ctr)++] = vertex; 18 | 19 | int neighbor; 20 | for (neighbor = 0; neighbor < MAX; neighbor++) { 21 | if (matrix[vertex][neighbor] == 1 && visited[neighbor] == 0) { 22 | dfsHelper(matrix, neighbor, visited, res, ctr); 23 | } 24 | } 25 | } 26 | void printMatrix(MATRIX matrix) { 27 | int i, j; 28 | 29 | printf("\n----- MATRIX -----\n"); 30 | for (i = 0; i < MAX; i++) { 31 | printf("[ "); 32 | for (j = 0; j < MAX; j++) { 33 | printf("%15d ", matrix[i][j]); 34 | } 35 | printf("]\n"); 36 | } 37 | } 38 | void printArray(int* array) { 39 | int i; 40 | printf("\n----- ARRAY -----\n"); 41 | printf("[ "); 42 | for (i = 0; i < MAX; i++) { 43 | printf("%15d ", array[i]); 44 | } 45 | printf("]\n"); 46 | } -------------------------------------------------------------------------------- /examples/dictionary/open-hashing/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/linked-list/v1/_header.h" 2 | #include 3 | 4 | int main() { 5 | DICT D; 6 | 7 | initialize(&D); 8 | insertMember(&D, 3); 9 | insertMember(&D, 5); 10 | insertMember(&D, 33); 11 | insertMember(&D, 99); 12 | // deleteMember(&D, 3); 13 | displayDict(D); 14 | 15 | int member = 33; 16 | printf("Is %d member: %d\n", member, isMember(D, member)); 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /examples/dictionary/open-hashing/modules/linked-list/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DICTOPEN_LINKEDLIST_V1 2 | #define DICTOPEN_LINKEDLIST_V1 3 | #include 4 | 5 | #define MAX 10 6 | 7 | typedef struct node { 8 | int data; 9 | struct node *next; 10 | } NodeType, *LIST; 11 | 12 | typedef LIST DICT[MAX]; 13 | 14 | void initialize(DICT *D); 15 | int hash(int x); 16 | bool isMember(DICT D, int elem); 17 | void insertMember(DICT *D, int elem); 18 | void deleteMember(DICT *D, int elem); 19 | void displayDict(DICT D); 20 | 21 | #endif -------------------------------------------------------------------------------- /examples/dictionary/open-hashing/modules/linked-list/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void initialize(DICT *D) { 7 | int ctr; 8 | 9 | for (ctr = 0; ctr < MAX; ctr++) { 10 | (*D)[ctr] = NULL; 11 | } 12 | } 13 | 14 | int hash(int x) { 15 | return x % MAX; 16 | } 17 | 18 | bool isMember(DICT D, int elem) { 19 | int hashVal = hash(elem); 20 | LIST trav; 21 | 22 | for (trav = D[hashVal]; trav != NULL && trav->data != elem; trav = trav->next) {} 23 | 24 | return trav != NULL; 25 | } 26 | 27 | void insertMember(DICT *D, int elem) { 28 | int hashVal = hash(elem); 29 | LIST *trav; 30 | 31 | for (trav = &((*D)[hashVal]); *trav != NULL && (*trav)->data != elem; trav = &(*trav)->next) {} 32 | 33 | if (*trav == NULL) { 34 | LIST temp = (LIST)malloc(sizeof(NodeType)); 35 | 36 | if (temp != NULL) { 37 | temp->data = elem; 38 | temp->next = NULL; 39 | *trav = temp; 40 | } 41 | } 42 | } 43 | 44 | void deleteMember(DICT *D, int elem) { 45 | int hashVal = hash(elem); 46 | LIST *trav; 47 | 48 | for (trav = &((*D)[hashVal]); *trav != NULL && (*trav)->data != elem; trav = &(*trav)->next) {} 49 | 50 | if (*trav != NULL) { 51 | LIST temp = *trav; 52 | *trav = temp->next; 53 | free(temp); 54 | } 55 | } 56 | 57 | void displayDict(DICT D) { 58 | int ctr; 59 | LIST trav; 60 | 61 | for (ctr = 0; ctr < MAX; ctr++) { 62 | printf("[%d] => ", ctr); 63 | for (trav = D[ctr]; trav != NULL; trav = trav->next) { 64 | printf("%d ", trav->data); 65 | } 66 | printf("\n"); 67 | } 68 | } 69 | 70 | -------------------------------------------------------------------------------- /examples/dictionary/open-hashing/modules/linked-list/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef DICTCLOSED_LINKEDLIST_V2 2 | #define DICTCLOSED_LINKEDLIST_V2 3 | #include 4 | 5 | #define MAX 10 6 | typedef struct node { 7 | int data; 8 | struct node *link; 9 | } *LIST; 10 | 11 | typedef LIST DICT[MAX]; 12 | 13 | void initialize(DICT *D); 14 | int hash(int x); 15 | bool isMember(DICT D, int elem); 16 | void insertMember(DICT *D, int elem); 17 | void deleteMember(DICT *D, int elem); 18 | void displayDict(DICT D); 19 | 20 | #endif -------------------------------------------------------------------------------- /examples/dictionary/open-hashing/modules/linked-list/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void initialize(DICT *D) { 7 | int ctr; 8 | 9 | for (ctr = 0; ctr < MAX; ctr++) { 10 | (*D)[ctr] = NULL; 11 | } 12 | } 13 | 14 | int hash(int x) { 15 | return x % MAX; 16 | } 17 | 18 | bool isMember(DICT D, int elem) { 19 | int hashVal = hash(elem); 20 | LIST trav; 21 | 22 | for (trav = D[hashVal]; trav != NULL && trav->data != elem; trav = trav->link) {} 23 | 24 | return trav != NULL; 25 | } 26 | 27 | void insertMember(DICT *D, int elem) { 28 | int hashVal = hash(elem); 29 | LIST *trav; 30 | 31 | for (trav = *D + hashVal; *trav != NULL && (*trav)->data != elem; trav = &(*trav)->link) {} 32 | 33 | if (*trav == NULL) { 34 | LIST temp = (LIST)malloc(sizeof(struct node)); 35 | 36 | if (temp != NULL) { 37 | temp->data = elem; 38 | temp->link = NULL; 39 | *trav = temp; 40 | } 41 | } 42 | } 43 | 44 | void deleteMember(DICT *D, int elem) { 45 | int hashVal = hash(elem); 46 | LIST *trav; 47 | 48 | for (trav = *D + hashVal; *trav != NULL && (*trav)->data != elem; trav = &(*trav)->link) {} 49 | 50 | if (*trav != NULL) { 51 | LIST temp = *trav; 52 | *trav = temp->link; 53 | free(temp); 54 | } 55 | } 56 | 57 | void displayDict(DICT D) { 58 | int ctr; 59 | LIST trav; 60 | 61 | for (ctr = 0; ctr < MAX; ctr++) { 62 | printf("[%d] => ", ctr); 63 | for (trav = D[ctr]; trav != NULL; trav = trav->link) { 64 | printf("%d ", trav->data); 65 | } 66 | printf("\n"); 67 | } 68 | } -------------------------------------------------------------------------------- /examples/graph-implementation/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/_header.h" 2 | #include 3 | 4 | int main() { 5 | /*--------------------------------------------------------------------------------- 6 | * Problem #1 :: 1) Initializes the matrix * 2) 7 | * Displays the content of the matrix * 8 | *--------------------------------------------------------------------------------*/ 9 | printf("\n\n\nProblem #1:: "); 10 | printf("\n------------\n"); 11 | // Declare variables needed for Problem #1 12 | MATRIX m; 13 | 14 | // Function Calls for Problem #1 15 | initializeMatrix(m); 16 | displayMatrix(m); 17 | 18 | /*--------------------------------------------------------------------------------- 19 | * Problem #2 :: 1) Populates the matrix * 2) 20 | * Displays the content matrix * 21 | *--------------------------------------------------------------------------------*/ 22 | printf("\n\n\nProblem #2:: "); 23 | printf("\n------------\n"); 24 | // Declare variables needed for Problem #2 25 | 26 | // Function Calls for Problem #2 27 | populateMatrix(m); 28 | displayMatrix(m); 29 | /*--------------------------------------------------------------------------------- 30 | * Problem #3 :: 1) Converts the matrix into a list * 2) 31 | * Displays the list * 32 | *--------------------------------------------------------------------------------*/ 33 | printf("\n\n\nProblem #3:: "); 34 | printf("\n------------\n"); 35 | // Declare variables needed for Problem #3 36 | LIST *L; 37 | // Function Calls for Problem #3 38 | L = convertToList(m); 39 | displayList(*L); 40 | /*--------------------------------------------------------------------------------- 41 | * Problem #4 :: 1) Initializes the virtual heap and cursor-based graph * 2) 42 | * Displays the empty virtual heap * 43 | *--------------------------------------------------------------------------------*/ 44 | printf("\n\n\nProblem #4:: "); 45 | printf("\n------------\n"); 46 | // Declare variables needed for Problem #4 47 | VHeap VH; 48 | 49 | // Function Calls for Problem #4 50 | VH = initVHeap(); 51 | displayVHeap(VH); 52 | 53 | /*--------------------------------------------------------------------------------- 54 | * Problem #5 :: 1) Initializes the cursor-based graph * 2) 55 | * Displays the cursor-based graph * 56 | *--------------------------------------------------------------------------------*/ 57 | printf("\n\n\nProblem #5:: "); 58 | printf("\n------------\n"); 59 | // Declare variables needed for Problem #5 60 | CURSOR CS; 61 | 62 | // Function Calls for Problem #5 63 | initCursor(&CS, VH); 64 | displayCursor(CS); 65 | 66 | /*--------------------------------------------------------------------------------- 67 | * Problem #6 :: 1) Converts the list to a cursor-based graph * 2) 68 | * Displays the virtual heap and cursor-based graph * 69 | *--------------------------------------------------------------------------------*/ 70 | printf("\n\n\nProblem #6:: "); 71 | printf("\n------------\n"); 72 | // Declare variables needed for Problem #6 73 | 74 | // Function Calls for Problem #6 75 | convertToCursor(L, &CS); 76 | displayCursor(CS); 77 | displayVHeap(CS.VH); 78 | 79 | /*--------------------------------------------------------------------------------- 80 | * Problem #7 :: 1) Initializes a new adjacency matrix* 2) 81 | * Converts the list into a matrix* 3) Displays the matrix 82 | *--------------------------------------------------------------------------------*/ 83 | printf("\n\n\nProblem #7:: "); 84 | printf("\n------------\n"); 85 | // Declare variables needed for Problem #7 86 | MATRIX newMatrix; 87 | // Function Calls for Problem #7 88 | initializeMatrix(newMatrix); 89 | convertToMatrix(L, newMatrix); 90 | displayMatrix(newMatrix); 91 | return 0; 92 | } -------------------------------------------------------------------------------- /examples/graph-implementation/main_boilerplate.c: -------------------------------------------------------------------------------- 1 | #include "modules/_header.h" 2 | #include 3 | 4 | int main() { 5 | /*--------------------------------------------------------------------------------- 6 | * Problem #1 :: 1) Initializes the matrix * 2) 7 | * Displays the content of the matrix * 8 | *--------------------------------------------------------------------------------*/ 9 | printf("\n\n\nProblem #1:: "); 10 | printf("\n------------\n"); 11 | // Declare variables needed for Problem #1 12 | 13 | // Function Calls for Problem #1 14 | 15 | /*--------------------------------------------------------------------------------- 16 | * Problem #2 :: 1) Populates the matrix * 2) 17 | * Displays the content matrix * 18 | *--------------------------------------------------------------------------------*/ 19 | printf("\n\n\nProblem #2:: "); 20 | printf("\n------------\n"); 21 | // Declare variables needed for Problem #2 22 | 23 | // Function Calls for Problem #2 24 | 25 | /*--------------------------------------------------------------------------------- 26 | * Problem #3 :: 1) Converts the matrix into a list * 2) 27 | * Displays the list * 28 | *--------------------------------------------------------------------------------*/ 29 | printf("\n\n\nProblem #3:: "); 30 | printf("\n------------\n"); 31 | // Declare variables needed for Problem #3 32 | 33 | // Function Calls for Problem #3 34 | 35 | /*--------------------------------------------------------------------------------- 36 | * Problem #4 :: 1) Initializes the virtual heap and cursor-based graph * 2) 37 | * Displays the empty virtual heap * 38 | *--------------------------------------------------------------------------------*/ 39 | printf("\n\n\nProblem #4:: "); 40 | printf("\n------------\n"); 41 | // Declare variables needed for Problem #4 42 | 43 | // Function Calls for Problem #4 44 | 45 | /*--------------------------------------------------------------------------------- 46 | * Problem #5 :: 1) Initializes the cursor-based graph * 2) 47 | * Displays the cursor-based graph * 48 | *--------------------------------------------------------------------------------*/ 49 | printf("\n\n\nProblem #5:: "); 50 | printf("\n------------\n"); 51 | // Declare variables needed for Problem #5 52 | 53 | // Function Calls for Problem #5 54 | 55 | /*--------------------------------------------------------------------------------- 56 | * Problem #6 :: 1) Converts the list to a cursor-based graph * 2) 57 | * Displays the virtual heap and cursor-based graph * 58 | *--------------------------------------------------------------------------------*/ 59 | printf("\n\n\nProblem #6:: "); 60 | printf("\n------------\n"); 61 | // Declare variables needed for Problem #6 62 | 63 | // Function Calls for Problem #6 64 | 65 | /*--------------------------------------------------------------------------------- 66 | * Problem #7 :: 1) Initializes a new adjacency matrix* 2) 67 | * Converts the list into a matrix* 3) Displays the matrix 68 | *--------------------------------------------------------------------------------*/ 69 | printf("\n\n\nProblem #7:: "); 70 | printf("\n------------\n"); 71 | // Declare variables needed for Problem #7 72 | 73 | // Function Calls for Problem #7 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /examples/graph-implementation/modules/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHIMPLEMENTATION_V1 2 | #define GRAPHIMPLEMENTATION_V1 3 | 4 | #define MATRIX_MAX 5 5 | #define OPEN_MAX 5 6 | #define VH_MAX 15 7 | #define CLOSE_MAX 5 8 | #define INF __INT_MAX__ 9 | 10 | // Matrix implementation 11 | typedef int MATRIX[MATRIX_MAX][MATRIX_MAX]; 12 | 13 | // Open Dictionary implementation 14 | typedef struct node { 15 | int vertex; 16 | int cost; 17 | struct node *edge; 18 | } *Node; 19 | 20 | typedef Node LIST[OPEN_MAX]; 21 | 22 | // Cursor-based implementation 23 | typedef struct { 24 | int vertex; 25 | int cost; 26 | int edge; 27 | } VH_Node; 28 | 29 | typedef struct heap { 30 | VH_Node nodes[VH_MAX]; 31 | int avail; 32 | } *VHeap; 33 | 34 | typedef struct { 35 | int header[CLOSE_MAX]; 36 | VHeap VH; 37 | } CURSOR; 38 | 39 | /********************************************************** 40 | * Function Prototypes * 41 | *********************************************************/ 42 | //---Problem #1 --- 43 | void displayMatrix(MATRIX matrix); 44 | void initializeMatrix(MATRIX matrix); 45 | 46 | //---Problem #2 --- 47 | void populateMatrix(MATRIX matrix); 48 | 49 | //---Problem #3 --- 50 | void displayList(LIST list); 51 | void initList(LIST *L); 52 | LIST *convertToList(MATRIX matrix); 53 | 54 | //---Problem #4 --- 55 | void displayVHeap(VHeap VH); 56 | VHeap initVHeap(); 57 | 58 | //---Problem #5 --- 59 | void displayCursor(CURSOR cursor); 60 | void initCursor(CURSOR *cursor, VHeap VH); 61 | 62 | //---Problem #6 --- 63 | void convertToCursor(LIST *L, CURSOR *CS); 64 | 65 | //---Problem #7 --- 66 | void convertToMatrix(LIST *L, MATRIX matrix); 67 | 68 | #endif -------------------------------------------------------------------------------- /examples/graph-implementation/modules/def_boilerplate.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | /************************************************************ 6 | * Problem 1:: Function Definitions * 7 | ************************************************************/ 8 | void displayMatrix(MATRIX matrix) { 9 | printf("\n\nDetails of the Matrix :: "); 10 | printf("\n---------------------------"); 11 | printf("\n\n%-7s", "Source"); 12 | printf("%15s", "Cost"); 13 | printf("\n%-7s%15s\n", "------", "--------------------"); 14 | 15 | // Insert code here 16 | 17 | printf("\n\n"); 18 | system("Pause"); 19 | } 20 | 21 | void initializeMatrix(MATRIX matrix) { 22 | // Insert code here 23 | } 24 | 25 | /************************************************************ 26 | * Problem 2:: Function Definitions * 27 | ************************************************************/ 28 | void populateMatrix(MATRIX matrix) { 29 | MATRIX data = { 30 | {INF, 10, INF, 30, 100}, 31 | {INF, INF, 50, INF, INF}, 32 | {20, INF, INF, INF, 10}, 33 | {INF, INF, 20, INF, 60}, 34 | {INF, INF, INF, INF, INF} 35 | }; 36 | 37 | // Insert code here 38 | } 39 | 40 | /************************************************************ 41 | * Problem 3:: Function Definitions * 42 | ************************************************************/ 43 | void displayList(LIST list) { 44 | printf("\n\nDetails of the List :: "); 45 | printf("\n---------------------------"); 46 | printf("\n\n%-7s", "Source"); 47 | printf("%15s", "Connected Vertices (cost)"); 48 | printf("\n%-7s%15s\n", "------", "--------------------"); 49 | 50 | // Insert code here 51 | 52 | printf("\n\n"); 53 | system("Pause"); 54 | } 55 | void initList(LIST *L) { 56 | // Insert code here 57 | } 58 | LIST *convertToList(MATRIX matrix) { 59 | // Insert code here 60 | } 61 | 62 | /************************************************************ 63 | * Problem 4:: Function Definitions * 64 | ************************************************************/ 65 | void displayVHeap(VHeap VH) { 66 | printf("\n\nDetails of the Virtual Heap :: "); 67 | printf("\n------------------------------"); 68 | printf("\nAvailable Index :: %d", VH->avail); // Complete this statement 69 | printf("\nVHeap Address :: %x", VH); // Complete this statemet 70 | 71 | printf("\n\n%10s", "Index"); 72 | printf("%10s", "Vertex"); 73 | printf("%10s", "Cost"); 74 | printf("%15s", "Next Field"); 75 | printf("\n%10s%10s%10s%15s\n", "-----", "------", "----", "----------"); 76 | 77 | // Insert code here 78 | 79 | printf("\n\n"); 80 | system("Pause"); 81 | } 82 | VHeap initVHeap() { 83 | // Insert code here 84 | } 85 | 86 | /************************************************************ 87 | * Problem 5:: Function Definitions * 88 | ************************************************************/ 89 | void displayCursor(CURSOR cursor) { 90 | printf("\n\nDetails of the Cursor :: "); 91 | printf("\n---------------------------"); 92 | printf("\n\n%-7s", "Source"); 93 | printf("%15s", "Connected Vertices (cost)"); 94 | printf("\n%-7s%15s\n", "------", "--------------------"); 95 | 96 | // Insert code here 97 | 98 | printf("\n\n"); 99 | system("Pause"); 100 | } 101 | void initCursor(CURSOR *CS, VHeap VH) { 102 | // Insert code here 103 | } 104 | 105 | /************************************************************ 106 | * Problem 6:: Function Definitions * 107 | ************************************************************/ 108 | void convertToCursor(LIST *L, CURSOR *CS) { 109 | // Insert code here 110 | } 111 | 112 | /************************************************************ 113 | * Problem 7:: Function Definitions * 114 | ************************************************************/ 115 | void convertToMatrix(LIST *L, MATRIX matrix) { 116 | // Insert code here 117 | } 118 | -------------------------------------------------------------------------------- /examples/graph-implementation/modules/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "../_header.h" 2 | #include 3 | #include 4 | 5 | /************************************************************ 6 | * Problem 1:: Function Definitions * 7 | ************************************************************/ 8 | void displayMatrix(MATRIX matrix) { 9 | printf("\n\nDetails of the Matrix :: "); 10 | printf("\n---------------------------"); 11 | printf("\n\n%-7s", "Source"); 12 | printf("%15s", "Cost"); 13 | printf("\n%-7s%15s\n", "------", "--------------------"); 14 | 15 | // Insert code here 16 | int x, y; 17 | 18 | for (x = 0; x < MATRIX_MAX; x++) { 19 | printf("%-7d", x); 20 | for (y = 0; y < MATRIX_MAX; y++) { 21 | printf("%-5d ", matrix[x][y] == INF ? -1 : matrix[x][y]); 22 | } 23 | printf("\n"); 24 | } 25 | 26 | printf("\n\n"); 27 | system("Pause"); 28 | } 29 | 30 | void initializeMatrix(MATRIX matrix) { 31 | // Code here 32 | int x, y; 33 | 34 | for (x = 0; x < MATRIX_MAX; x++) { 35 | for (y = 0; y < MATRIX_MAX; y++) { 36 | matrix[x][y] = -1; 37 | } 38 | } 39 | } 40 | 41 | /************************************************************ 42 | * Problem 2:: Function Definitions * 43 | ************************************************************/ 44 | void populateMatrix(MATRIX matrix) { 45 | MATRIX data = { 46 | {INF, 10, INF, 30, 100}, 47 | {INF, INF, 50, INF, INF}, 48 | {20, INF, INF, INF, 10}, 49 | {INF, INF, 20, INF, 60}, 50 | {INF, INF, INF, INF, INF} 51 | }; 52 | 53 | // Logic here 54 | int x, y; 55 | for (x = 0; x < MATRIX_MAX; x++) { 56 | for (y = 0; y < MATRIX_MAX; y++) { 57 | matrix[x][y] = data[x][y]; 58 | } 59 | } 60 | } 61 | 62 | /************************************************************ 63 | * Problem 3:: Function Definitions * 64 | ************************************************************/ 65 | void displayList(LIST list) { 66 | printf("\n\nDetails of the List :: "); 67 | printf("\n---------------------------"); 68 | printf("\n\n%-7s", "Source"); 69 | printf("%15s", "Connected Vertices (cost)"); 70 | printf("\n%-7s%15s\n", "------", "--------------------"); 71 | 72 | // Insert code here 73 | int x; 74 | Node trav; 75 | for (x = 0; x < OPEN_MAX; x++) { 76 | printf("%-7d", x); 77 | for (trav = list[x]; trav != NULL; trav = trav->edge) { 78 | printf("%-2d (%-3d) | ", trav->vertex, trav->cost == INF ? -1 : trav->cost); 79 | } 80 | printf("\n"); 81 | } 82 | printf("\n\n"); 83 | system("Pause"); 84 | } 85 | void initList(LIST *L) { 86 | int x; 87 | 88 | for (x = 0; x < OPEN_MAX; x++) { 89 | (*L)[x] = NULL; 90 | } 91 | } 92 | LIST *convertToList(MATRIX matrix) { 93 | LIST *L = (LIST *)malloc(sizeof(LIST)); 94 | if (L != NULL) { 95 | initList(L); 96 | int x, y; 97 | 98 | for (x = 0; x < MATRIX_MAX; x++) { 99 | for (y = MATRIX_MAX - 1; y > -1; y--) { 100 | if (matrix[x][y] != INF) { 101 | Node temp = (Node)malloc(sizeof(struct node)); 102 | if (temp != NULL) { 103 | temp->vertex = y; 104 | temp->cost = matrix[x][y]; 105 | temp->edge = (*L)[x]; 106 | (*L)[x] = temp; 107 | } 108 | } 109 | } 110 | } 111 | } 112 | 113 | return L; 114 | } 115 | 116 | /************************************************************ 117 | * Problem 4:: Function Definitions * 118 | ************************************************************/ 119 | void displayVHeap(VHeap VH) { 120 | printf("\n\nDetails of the Virtual Heap :: "); 121 | printf("\n------------------------------"); 122 | printf("\nAvailable Index :: %d", VH->avail); // Complete this statement 123 | printf("\nVHeap Address :: %x", VH); // Complete this statemet 124 | 125 | printf("\n\n%10s", "Index"); 126 | printf("%10s", "Vertex"); 127 | printf("%10s", "Cost"); 128 | printf("%15s", "Next Field"); 129 | printf("\n%10s%10s%10s%15s\n", "-----", "------", "----", "----------"); 130 | 131 | // Insert code here 132 | int x; 133 | for (x = 0; x < VH_MAX; x++) { 134 | printf("%10d", x); 135 | printf("%10d", VH->nodes[x].vertex); 136 | printf("%10d", VH->nodes[x].cost); 137 | printf("%10d\n", VH->nodes[x].edge); 138 | 139 | } 140 | 141 | printf("\n\n"); 142 | system("Pause"); 143 | } 144 | VHeap initVHeap() { 145 | VHeap VH = (VHeap)malloc(sizeof(struct heap)); 146 | 147 | if (VH != NULL) { 148 | int x; 149 | 150 | for (x = 0; x < VH_MAX; x++) { 151 | VH->nodes[x].vertex = -1; 152 | VH->nodes[x].cost = -1; 153 | VH->nodes[x].edge = x - 1; 154 | } 155 | 156 | VH->avail = x - 1; 157 | } 158 | 159 | return VH; 160 | } 161 | 162 | /************************************************************ 163 | * Problem 5:: Function Definitions * 164 | ************************************************************/ 165 | void displayCursor(CURSOR cursor) { 166 | printf("\n\nDetails of the Cursor :: "); 167 | printf("\n---------------------------"); 168 | printf("\n\n%-7s", "Source"); 169 | printf("%15s", "Connected Vertices (cost)"); 170 | printf("\n%-7s%15s\n", "------", "--------------------"); 171 | 172 | // Insert code here 173 | int group, trav; 174 | for (group = 0; group < CLOSE_MAX; group++) { 175 | printf("%-7d", group); 176 | for (trav = cursor.header[group]; trav != -1; trav = cursor.VH->nodes[trav].edge) { 177 | printf("%-2d (%-3d) | ", cursor.VH->nodes[trav].vertex, cursor.VH->nodes[trav].cost); 178 | } 179 | printf("\n"); 180 | } 181 | printf("\n\n"); 182 | system("Pause"); 183 | } 184 | void initCursor(CURSOR *CS, VHeap VH) { 185 | CS->VH = VH; 186 | 187 | int x; 188 | for (x = 0; x < CLOSE_MAX; x++) { 189 | CS->header[x] = -1; 190 | } 191 | } 192 | 193 | /************************************************************ 194 | * Problem 6:: Function Definitions * 195 | ************************************************************/ 196 | void convertToCursor(LIST *L, CURSOR *CS) { 197 | int group; 198 | Node trav; 199 | for (group = 0; group < OPEN_MAX; group++) { 200 | int *cTrav = &CS->header[group]; 201 | for (trav = (*L)[group]; trav != NULL; trav = trav->edge) { 202 | if (CS->VH->avail != -1) { 203 | int temp = CS->VH->avail; 204 | CS->VH->avail = CS->VH->nodes[temp].edge; 205 | 206 | CS->VH->nodes[temp].vertex = trav->vertex; 207 | CS->VH->nodes[temp].cost = trav->cost; 208 | CS->VH->nodes[temp].edge = -1; 209 | *cTrav = temp; 210 | cTrav = &CS->VH->nodes[temp].edge; 211 | } 212 | } 213 | } 214 | } 215 | 216 | /************************************************************ 217 | * Problem 7:: Function Definitions * 218 | ************************************************************/ 219 | void convertToMatrix(LIST *L, MATRIX matrix) { 220 | int group; 221 | Node trav; 222 | for (group = 0; group < OPEN_MAX; group++) { 223 | for (trav = (*L)[group]; trav != NULL; trav = trav->edge) { 224 | matrix[group][trav->vertex] = trav->cost; 225 | } 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /examples/path-algorithms/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/v3/_header.h" 2 | 3 | int main() { 4 | int m1[MAX][MAX] = { 5 | {INF, 10, INF, 30, 100}, 6 | {INF, INF, 50, INF, INF}, 7 | {20, INF, INF, INF, 10}, 8 | {INF, INF, 20, INF, 60}, 9 | {INF, INF, INF, INF, INF} 10 | }; 11 | 12 | /* 13 | [ 0 10 50 30 60 ] 14 | [ 70 0 50 100 60 ] 15 | [ 20 30 0 50 10 ] 16 | [ 40 50 20 0 30 ] 17 | [ 2147483647 2147483647 2147483647 2147483647 0 ] 18 | */ 19 | 20 | int m2[MAX][MAX] = { 21 | {INF, 1, 4, INF, INF}, 22 | {1, INF, 4, 2, 2}, 23 | {4, 4, INF, 3, INF}, 24 | {INF, 2, 3, INF, 2}, 25 | {INF, 2, INF, 2, INF} 26 | }; 27 | 28 | /* 29 | [ 0 1 4 3 3 ] 30 | [ 1 0 4 2 2 ] 31 | [ 4 4 0 3 5 ] 32 | [ 3 2 3 0 2 ] 33 | [ 3 2 5 2 0 ] 34 | */ 35 | 36 | int m3[MAX][MAX] = { 37 | {INF, 1, 4, INF, INF}, 38 | {1, INF, 4, 2, INF}, 39 | {4, 4, INF, 3, INF}, 40 | {INF, 2, 3, INF, 2}, 41 | {INF, INF, INF, 2, INF} 42 | }; 43 | 44 | /* 45 | [ 0 1 4 3 5 ] 46 | [ 1 0 4 2 4 ] 47 | [ 4 4 0 3 5 ] 48 | [ 3 2 3 0 2 ] 49 | [ 5 4 5 2 0 ] 50 | */ 51 | 52 | int m4[MAX][MAX] = { 53 | {INF, 2, INF, 1, INF}, 54 | {INF, INF, 3, INF, INF}, 55 | {INF, INF, INF, 2, INF}, 56 | {INF, INF, INF, INF, 1}, 57 | {INF, INF, 1, INF, INF} 58 | }; 59 | 60 | /* 61 | [ 0 2 3 1 2 ] 62 | [ 2147483647 0 3 5 6 ] 63 | [ 2147483647 2147483647 0 2 3 ] 64 | [ 2147483647 2147483647 2 0 1 ] 65 | [ 2147483647 2147483647 1 3 0 ] 66 | */ 67 | 68 | // DIJKSTRA'S 69 | printMatrix(m1); 70 | int *minCosts = dijkstra(m1, 3); 71 | printArray(minCosts); 72 | 73 | // FLOYD'S 74 | MATRIX fRes = {}; 75 | floyd(fRes, m1); 76 | printMatrix(fRes); 77 | 78 | // WARSHALL'S 79 | MATRIX wRes = {}; 80 | warshall(wRes, m1); 81 | printMatrix(wRes); 82 | 83 | return 0; 84 | } -------------------------------------------------------------------------------- /examples/path-algorithms/modules/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef PATHALGOS_V1 2 | #define PATHALGOS_V1 3 | 4 | #define MAX 5 5 | #define INF __INT_MAX__ 6 | 7 | typedef int MATRIX[][MAX]; 8 | int* dijkstra(MATRIX matrix, int src); 9 | void floyd(MATRIX res, MATRIX costs); 10 | void warshall(MATRIX res, MATRIX costs); 11 | void printMatrix(MATRIX matrix); 12 | void printArray(int* array); 13 | 14 | #endif -------------------------------------------------------------------------------- /examples/path-algorithms/modules/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | int* dijkstra(MATRIX matrix, int src) { 6 | int *cost = (int *)calloc(MAX, sizeof(int)); 7 | 8 | // Initialize set to track visited nodes 9 | int *visited = (int *)calloc(MAX, sizeof(int)); 10 | 11 | if (cost != NULL && visited != NULL) { 12 | int ctr; 13 | 14 | // Initial cost to get to each node from src 15 | for (ctr = 0; ctr < MAX; ctr++) { 16 | cost[ctr] = ctr == src ? 0 : INF; 17 | } 18 | 19 | // currTail = current tail we're considering 20 | // head = vertices we can get to from currTail 21 | // nextTail = next tail we'll consider 22 | // currCost = cost to get from currTail to an unvisited head 23 | // minCost = smallest cost path from currTail to an unvisited head 24 | int currTail, head, nextTail, currCost, minCost; 25 | for (currTail = src, nextTail = src, minCost = INF; visited[currTail] != 1; currTail = nextTail, minCost = INF) { 26 | visited[currTail] = 1; 27 | for (head = 0; head < MAX; head++) { 28 | // Prevent integer overflows 29 | currCost = cost[currTail] == INF || matrix[currTail][head] == INF ? INF : cost[currTail] + matrix[currTail][head]; 30 | 31 | // Update cost to get to a head 32 | // if we found a cheaper path to it 33 | if (currCost < cost[head]) { 34 | cost[head] = currCost; 35 | } 36 | 37 | // Update cheapest path and next node to visit 38 | if (cost[head] < minCost && visited[head] == 0) { 39 | nextTail = head; 40 | minCost = cost[head]; 41 | } 42 | } 43 | } 44 | } 45 | 46 | return cost; 47 | } 48 | 49 | void floyd(MATRIX res, MATRIX cost) { 50 | int i, j, k; 51 | for (i = 0; i < MAX; i++) { 52 | for (j = 0; j < MAX; j++) { 53 | res[i][j] = cost[i][j]; 54 | } 55 | } 56 | 57 | for (i = 0; i < MAX; i++) { 58 | res[i][i] = 0; 59 | } 60 | 61 | for (k = 0; k < MAX; k++) { 62 | for (i = 0; i < MAX; i++) { 63 | for (j = 0; j < MAX; j++) { 64 | if (res[i][k] != INF && res[k][j] != INF && res[i][k] + res[k][j] < res[i][j]) { 65 | res[i][j] = res[i][k] + res[k][j]; 66 | } 67 | } 68 | } 69 | } 70 | } 71 | 72 | void warshall(MATRIX res, MATRIX costs) { 73 | int i, j, k; 74 | 75 | for (i = 0; i < MAX; i++) { 76 | for (j = 0; j < MAX; j++) { 77 | res[i][j] = costs[i][j] == INF ? 0 : 1; 78 | } 79 | } 80 | 81 | for (i = 0; i < MAX; i++) { 82 | res[i][i] = 1; 83 | } 84 | 85 | for (k = 0; k < MAX; k++) { 86 | for (i = 0; i < MAX; i++) { 87 | for (j = 0; j < MAX; j++) { 88 | if (res[i][j] == 0) { 89 | res[i][j] = res[i][k] && res[k][j]; 90 | } 91 | } 92 | } 93 | } 94 | } 95 | 96 | void printMatrix(MATRIX matrix) { 97 | int i, j; 98 | 99 | printf("\n----- MATRIX -----\n"); 100 | for (i = 0; i < MAX; i++) { 101 | printf("[ "); 102 | for (j = 0; j < MAX; j++) { 103 | printf("%11d ", matrix[i][j]); 104 | } 105 | printf("]\n"); 106 | } 107 | } 108 | void printArray(int* array) { 109 | int i; 110 | printf("\n----- ARRAY -----\n"); 111 | printf("[ "); 112 | for (i = 0; i < MAX; i++) { 113 | printf("%11d ", array[i]); 114 | } 115 | printf("]\n"); 116 | } -------------------------------------------------------------------------------- /examples/path-algorithms/modules/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef PATHALGOS_V2 2 | #define PATHALGOS_V2 3 | 4 | #define MAX 5 5 | #define INF __INT_MAX__ 6 | 7 | typedef int MATRIX[MAX][MAX]; 8 | 9 | int* dijkstra(MATRIX matrix, int src); 10 | void floyd(MATRIX res, MATRIX matrix); 11 | void warshall(MATRIX res, MATRIX matrix); 12 | void printMatrix(MATRIX matrix); 13 | void printArray(int* array); 14 | 15 | #endif -------------------------------------------------------------------------------- /examples/path-algorithms/modules/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | 6 | int* dijkstra(MATRIX matrix, int src) { 7 | int *costs = (int*)malloc(sizeof(MAX)); 8 | 9 | if (costs != NULL) { 10 | int visited[MAX] = {0}; 11 | int tail; 12 | 13 | for (tail = 0; tail < MAX; tail++) { 14 | costs[tail] = tail == src ? 0 : INF; 15 | } 16 | 17 | int next, head, minCost, currCost; 18 | for (tail = src, next = src, minCost = INF; visited[tail] != 1; tail = next, minCost = INF) { 19 | visited[tail] = 1; 20 | 21 | for (head = 0; head < MAX; head++) { 22 | currCost = costs[tail] == INF || matrix[tail][head] == INF ? INF : costs[tail] + matrix[tail][head]; 23 | if (currCost < costs[head]) { 24 | costs[head] = currCost; 25 | } 26 | 27 | if (costs[head] < minCost && visited[head] != 1) { 28 | minCost = costs[head]; 29 | next = head; 30 | } 31 | } 32 | } 33 | } 34 | 35 | return costs; 36 | } 37 | 38 | void floyd(MATRIX res, MATRIX matrix) { 39 | int tail, head, middle; 40 | 41 | for (tail = 0; tail < MAX; tail++) { 42 | for (head = 0; head < MAX; head++) { 43 | res[tail][head] = matrix[tail][head]; 44 | } 45 | } 46 | 47 | for (tail = 0; tail < MAX; tail++) { 48 | res[tail][tail] = 0; 49 | } 50 | 51 | for (middle = 0; middle < MAX; middle++) { 52 | for (tail = 0; tail < MAX; tail++) { 53 | for (head = 0; head < MAX; head++) { 54 | if (res[tail][middle] != INF && 55 | res[middle][head] != INF && 56 | res[tail][middle] + res[middle][head] < res[tail][head]) { 57 | res[tail][head] = res[tail][middle] + res[middle][head]; 58 | } 59 | } 60 | } 61 | } 62 | } 63 | void warshall(MATRIX res, MATRIX matrix) { 64 | int tail, head, mid; 65 | 66 | for (tail = 0; tail < MAX; tail++) { 67 | for (head = 0; head < MAX; head++) { 68 | res[tail][head] = matrix[tail][head] != INF || tail == head ? 1 : 0; 69 | } 70 | } 71 | 72 | for (mid = 0; mid < MAX; mid++) { 73 | for (tail = 0; tail < MAX; tail++) { 74 | for (head = 0; head < MAX; head++) { 75 | if (res[tail][mid] && res[mid][head]) { 76 | res[tail][head] = 1; 77 | } 78 | } 79 | } 80 | } 81 | } 82 | void printMatrix(MATRIX matrix) { 83 | int i, j; 84 | 85 | printf("\n----- MATRIX -----\n"); 86 | for (i = 0; i < MAX; i++) { 87 | printf("[ "); 88 | for (j = 0; j < MAX; j++) { 89 | printf("%11d ", matrix[i][j]); 90 | } 91 | printf("]\n"); 92 | } 93 | } 94 | void printArray(int* array) { 95 | int i; 96 | printf("\n----- ARRAY -----\n"); 97 | printf("[ "); 98 | for (i = 0; i < MAX; i++) { 99 | printf("%11d ", array[i]); 100 | } 101 | printf("]\n"); 102 | } -------------------------------------------------------------------------------- /examples/path-algorithms/modules/v3/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef PATHALGOS_V3 2 | #define PATHALGOS_V3 3 | 4 | #define MAX 5 5 | #define INF __INT_MAX__ 6 | 7 | typedef int MATRIX[MAX][MAX]; 8 | 9 | int *dijkstra(MATRIX matrix, int src); 10 | void floyd(MATRIX res, MATRIX matrix); 11 | void warshall(MATRIX res, MATRIX matrix); 12 | void printMatrix(MATRIX matrix); 13 | void printArray(int *arr); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /examples/path-algorithms/modules/v3/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | int *dijkstra(MATRIX matrix, int src) { 6 | int *costs = (int *)malloc(MAX * sizeof(int)); 7 | if (costs != NULL) { 8 | int visited[MAX] = {0}; 9 | int node; 10 | 11 | for (node = 0; node < MAX; node++) { 12 | costs[node] = node == src ? 0 : INF; 13 | } 14 | 15 | int next, neighbor, currCost, minCost; 16 | for (node = src; visited[node] == 0; node = next) { 17 | visited[node] = 1; 18 | minCost = INF; 19 | 20 | for (neighbor = 0; neighbor < MAX; neighbor++) { 21 | currCost = matrix[node][neighbor] == INF ? INF : costs[node] + matrix[node][neighbor]; 22 | if (currCost < costs[neighbor]) { 23 | costs[neighbor] = currCost; 24 | } 25 | 26 | if (costs[neighbor] < minCost && visited[neighbor] == 0) { 27 | minCost = costs[neighbor]; 28 | next = neighbor; 29 | } 30 | } 31 | } 32 | } 33 | return costs; 34 | } 35 | void floyd(MATRIX res, MATRIX matrix) { 36 | int node, neighbor, middle; 37 | for (node = 0; node < MAX; node++) { 38 | for (neighbor = 0; neighbor < MAX; neighbor++) { 39 | res[node][neighbor] = node == neighbor ? 0 : matrix[node][neighbor]; 40 | } 41 | } 42 | 43 | for (middle = 0; middle < MAX; middle++) { 44 | for (node = 0; node < MAX; node++) { 45 | for (neighbor = 0; neighbor < MAX; neighbor++) { 46 | if (res[node][middle] != INF && 47 | res[middle][neighbor] != INF && 48 | res[node][middle] + res[middle][neighbor] < res[node][neighbor]) { 49 | res[node][neighbor] = res[node][middle] + res[middle][neighbor]; 50 | } 51 | } 52 | } 53 | } 54 | } 55 | void warshall(MATRIX res, MATRIX matrix) { 56 | int node, middle, neighbor; 57 | for (node = 0; node < MAX; node++) { 58 | for (neighbor = 0; neighbor < MAX; neighbor++) { 59 | res[node][neighbor] = node != neighbor && matrix[node][neighbor] == INF ? 0 : 1; 60 | } 61 | } 62 | 63 | for (middle = 0; middle < MAX; middle++) { 64 | for (node = 0; node < MAX; node++) { 65 | for (neighbor = 0; neighbor < MAX; neighbor++) { 66 | if (res[node][middle] && res[middle][neighbor]) { 67 | res[node][neighbor] = 1; 68 | } 69 | } 70 | } 71 | } 72 | } 73 | 74 | void printMatrix(MATRIX matrix) { 75 | int i, j; 76 | 77 | printf("\n----- MATRIX -----\n"); 78 | for (i = 0; i < MAX; i++) { 79 | printf("[ "); 80 | for (j = 0; j < MAX; j++) { 81 | printf("%11d ", matrix[i][j]); 82 | } 83 | printf("]\n"); 84 | } 85 | } 86 | void printArray(int* array) { 87 | int i; 88 | printf("\n----- ARRAY -----\n"); 89 | printf("[ "); 90 | for (i = 0; i < MAX; i++) { 91 | printf("%11d ", array[i]); 92 | } 93 | printf("]\n"); 94 | } -------------------------------------------------------------------------------- /examples/priority-queue/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/heap/v1/_header.h" 2 | 3 | int main() { 4 | HEAP H; 5 | initialize(&H); 6 | 7 | insert(&H, 1); 8 | insert(&H, 6); 9 | insert(&H, 3); 10 | insert(&H, 5); 11 | insert(&H, 8); 12 | insert(&H, 4); 13 | insert(&H, 7); 14 | insert(&H, 2); 15 | 16 | displayHeap(H); 17 | 18 | deleteMin(&H); 19 | displayHeap(H); 20 | 21 | makeNull(&H); 22 | displayHeap(H); 23 | 24 | HEAP list; 25 | initialize(&list); 26 | insertList(&list, 7); 27 | insertList(&list, 5); 28 | insertList(&list, 4); 29 | insertList(&list, 3); 30 | insertList(&list, 6); 31 | insertList(&list, 2); 32 | insertList(&list, 1); 33 | insertList(&list, 8); 34 | insertList(&list, 1); 35 | 36 | displayHeap(list); 37 | 38 | buildMinHeap(&list); 39 | // insertAll(&list); 40 | displayHeap(list); 41 | 42 | heapSort(&list); 43 | displayHeap(list); 44 | } -------------------------------------------------------------------------------- /examples/priority-queue/modules/heap/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef PRIORITYQUEUE_HEAP_V1 2 | #define PRIORITYQUEUE_HEAP_V1 3 | 4 | #define MAX 14 5 | 6 | typedef struct { 7 | int Elem[MAX]; 8 | int lastNdx; 9 | } HEAP; 10 | 11 | void initialize(HEAP *H); 12 | void insert(HEAP *H, int elem); 13 | int deleteMin(HEAP *H); 14 | void minHeapify(HEAP *H, int idx); 15 | void makeNull(HEAP *H); 16 | 17 | void displayHeap(HEAP H); 18 | 19 | // Unsorted List Helper Functions 20 | void insertList(HEAP *H, int elem); 21 | void buildMinHeap(HEAP *H); 22 | void insertAll(HEAP *H); 23 | void heapSort(HEAP *H); 24 | #endif 25 | -------------------------------------------------------------------------------- /examples/priority-queue/modules/heap/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | 4 | void initialize(HEAP *H) { 5 | H->lastNdx = -1; 6 | } 7 | 8 | void insert(HEAP *H, int elem) { 9 | if (H->lastNdx < MAX - 1) { 10 | int idx, temp; 11 | H->Elem[++(H->lastNdx)] = elem; 12 | for (idx = H->lastNdx; idx > 0 && H->Elem[(idx - 1) / 2] > elem; idx = (idx - 1) / 2) { 13 | temp = H->Elem[idx]; 14 | H->Elem[idx] = H->Elem[(idx - 1) / 2]; 15 | H->Elem[(idx - 1) / 2] = temp; 16 | } 17 | } 18 | } 19 | 20 | int deleteMin(HEAP *H) { 21 | int res = -1; 22 | if (H->lastNdx > -1) { 23 | res = H->Elem[0]; 24 | H->Elem[0] = H->Elem[H->lastNdx]; 25 | H->Elem[H->lastNdx] = res; 26 | H->lastNdx--; 27 | minHeapify(H, 0); 28 | } 29 | } 30 | 31 | void minHeapify(HEAP *H, int idx) { 32 | int left = 2*idx + 1; 33 | int right = 2*idx + 2; 34 | int smallestIdx = idx; 35 | 36 | if (left <= H->lastNdx && H->Elem[left] < H->Elem[smallestIdx]) { 37 | smallestIdx = left; 38 | } 39 | 40 | if (right <= H->lastNdx && H->Elem[right] < H->Elem[smallestIdx]) { 41 | smallestIdx = right; 42 | } 43 | 44 | if (smallestIdx != idx) { 45 | int temp = H->Elem[idx]; 46 | H->Elem[idx] = H->Elem[smallestIdx]; 47 | H->Elem[smallestIdx] = temp; 48 | minHeapify(H, smallestIdx); 49 | } 50 | } 51 | 52 | void makeNull(HEAP *H) { 53 | H->lastNdx = -1; 54 | } 55 | 56 | void displayHeap(HEAP H) { 57 | int ctr; 58 | printf("[ "); 59 | for (ctr = 0; ctr <= H.lastNdx; ctr++) { 60 | printf("%d ", H.Elem[ctr]); 61 | } 62 | printf("]\n"); 63 | } 64 | 65 | void insertList(HEAP *H, int elem) { 66 | if (H->lastNdx < MAX - 1) { 67 | H->lastNdx++; 68 | H->Elem[H->lastNdx] = elem; 69 | } 70 | } 71 | void buildMinHeap(HEAP *H) { 72 | int idx; 73 | for (idx = (H->lastNdx - 1) / 2; idx >= 0; idx--) { 74 | minHeapify(H, idx); 75 | } 76 | } 77 | 78 | void insertAll(HEAP *H) { 79 | int last = H->lastNdx, idx; 80 | H->lastNdx = -1; 81 | 82 | for (idx = 0; idx <= last; idx++) { 83 | insert(H, H->Elem[idx]); 84 | } 85 | } 86 | 87 | void heapSort(HEAP *H) { 88 | int len = H->lastNdx, idx; 89 | 90 | for (idx = H->lastNdx; idx >= 0; idx--) { 91 | deleteMin(H); 92 | } 93 | 94 | H->lastNdx = len; 95 | } -------------------------------------------------------------------------------- /examples/priority-queue/modules/heap/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef PRIORITYQUEUE_HEAP_V2 2 | #define PRIORITYQUEUE_HEAP_V2 3 | 4 | #define MAX 14 5 | 6 | typedef struct { 7 | int Elem[MAX]; 8 | int lastNdx; 9 | } HEAP; 10 | 11 | void initialize(HEAP *H); 12 | void insert(HEAP *H, int elem); 13 | int deleteMin(HEAP *H); 14 | void heapify(HEAP *H, int idx); 15 | void makeNull(HEAP *H); 16 | 17 | void displayHeap(HEAP H); 18 | 19 | #endif -------------------------------------------------------------------------------- /examples/priority-queue/modules/heap/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | 4 | void initialize(HEAP *H) { 5 | H->lastNdx = -1; 6 | } 7 | void insert(HEAP *H, int elem) { 8 | if (H->lastNdx < MAX - 1) { 9 | H->lastNdx++; 10 | H->Elem[H->lastNdx] = elem; 11 | int idx, temp; 12 | 13 | for (idx = H->lastNdx; idx > 0 && H->Elem[idx] < H->Elem[(idx - 1) / 2]; idx = (idx - 1) / 2) { 14 | temp = H->Elem[idx]; 15 | H->Elem[idx] = H->Elem[(idx - 1) / 2]; 16 | H->Elem[(idx - 1) / 2] = temp; 17 | } 18 | } 19 | } 20 | int deleteMin(HEAP *H) { 21 | int res = -1; 22 | if (H->lastNdx > -1) { 23 | res = H->Elem[0]; 24 | H->Elem[0] = H->Elem[H->lastNdx--]; 25 | heapify(H, 0); 26 | } 27 | } 28 | void heapify(HEAP *H, int idx) { 29 | int left = 2*idx + 1; 30 | int right = 2*idx + 2; 31 | int smallest = idx; 32 | 33 | if (left <= H->lastNdx && H->Elem[left] < H->Elem[smallest]) { 34 | smallest = left; 35 | } 36 | 37 | if (right <= H->lastNdx && H->Elem[right] < H->Elem[smallest]) { 38 | smallest = right; 39 | } 40 | 41 | if (smallest != idx) { 42 | int temp = H->Elem[idx]; 43 | H->Elem[idx] = H->Elem[smallest]; 44 | H->Elem[smallest] = temp; 45 | heapify(H, smallest); 46 | } 47 | } 48 | void makeNull(HEAP *H) { 49 | H->lastNdx = -1; 50 | } 51 | 52 | void displayHeap(HEAP H) { 53 | int idx; 54 | 55 | printf("[ "); 56 | for (idx = 0; idx <= H.lastNdx; idx++) { 57 | printf("%d ", H.Elem[idx]); 58 | } 59 | printf("]\n"); 60 | } -------------------------------------------------------------------------------- /examples/queues/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/linked-list/v3/_header.h" 2 | #include 3 | 4 | int main() { 5 | 6 | QUEUE Q; 7 | initialize(&Q); 8 | 9 | enqueue('S', &Q); 10 | enqueue('C', &Q); 11 | enqueue('O', &Q); 12 | enqueue('O', &Q); 13 | enqueue('B', &Q); 14 | enqueue('Y', &Q); 15 | displayList(&Q); 16 | 17 | insertFront('A', &Q); 18 | 19 | displayList(&Q); 20 | 21 | insertAtPos(3, 'Z', &Q); 22 | 23 | dequeue(&Q); 24 | displayList(&Q); 25 | 26 | printf("Top: %c\n", front(Q)); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /examples/queues/modules/array/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_ARRAY_V1 2 | #define QUEUE_ARRAY_V1 3 | #include 4 | 5 | #define MAX 8 6 | 7 | typedef struct { 8 | char Elem[MAX]; 9 | int front; 10 | int rear; 11 | } Queue; 12 | 13 | void initialize(Queue *QP); 14 | void enqueue(char elem, Queue *QP); 15 | void dequeue(Queue *QP); 16 | char front(Queue Q); 17 | bool isEmpty(Queue Q); 18 | bool isFull(Queue Q); 19 | void makeNull(Queue *QP); 20 | void displayList(Queue *QP); 21 | 22 | #endif -------------------------------------------------------------------------------- /examples/queues/modules/array/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(Queue *QP) { 6 | QP->front = 0; 7 | QP->rear = MAX - 1; 8 | } 9 | 10 | void enqueue(char elem, Queue *QP) { 11 | if (!isFull(*QP)) { 12 | QP->rear = (QP->rear + 1) % MAX; 13 | QP->Elem[QP->rear] = elem; 14 | } 15 | } 16 | 17 | void dequeue(Queue *QP) { 18 | if (!isEmpty(*QP)) { 19 | QP->front = (QP->front + 1) % MAX; 20 | } 21 | } 22 | 23 | char front(Queue Q) { 24 | char res = '\0'; 25 | 26 | if (!isEmpty(Q)) { 27 | res = Q.Elem[Q.front]; 28 | } 29 | 30 | return res; 31 | } 32 | 33 | bool isEmpty(Queue Q) { 34 | return Q.front == (Q.rear + 1) % MAX; 35 | } 36 | 37 | bool isFull(Queue Q) { 38 | return Q.front == (Q.rear + 2) % MAX; 39 | } 40 | 41 | void makeNull(Queue *QP) { 42 | while (!isEmpty(*QP)) { 43 | dequeue(QP); 44 | } 45 | } 46 | 47 | void displayList(Queue *QP) { 48 | Queue temp; 49 | initialize(&temp); 50 | char frontElem; 51 | 52 | printf("QUEUE: "); 53 | for (frontElem = front(*QP); !isEmpty(*QP); frontElem = front(*QP)) { 54 | printf("%c ", frontElem); 55 | dequeue(QP); 56 | enqueue(frontElem, &temp); 57 | } 58 | printf("\n"); 59 | 60 | for (frontElem = front(temp); !isEmpty(temp); frontElem = front(temp)) { 61 | dequeue(&temp); 62 | enqueue(frontElem, QP); 63 | } 64 | } -------------------------------------------------------------------------------- /examples/queues/modules/array/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_ARRAY_V2 2 | #define QUEUE_ARRAY_V2 3 | #include 4 | 5 | #define MAX 10 6 | typedef struct { 7 | int elem[MAX]; 8 | int front; 9 | int rear; 10 | } QUEUE; 11 | 12 | void initialize(QUEUE *QP); 13 | void enqueue(char elem, QUEUE *QP); 14 | void insertFront(char elem, QUEUE *QP); 15 | void insertAtPos(int pos, char elem, QUEUE *QP); 16 | void dequeue(QUEUE *QP); 17 | char front(QUEUE Q); 18 | bool isEmpty(QUEUE Q); 19 | bool isFull(QUEUE Q); 20 | void makeNull(QUEUE *QP); 21 | void displayList(QUEUE *QP); 22 | 23 | 24 | 25 | #endif -------------------------------------------------------------------------------- /examples/queues/modules/array/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(QUEUE *QP) { 6 | QP->front = 0; 7 | QP->rear = MAX - 1; 8 | } 9 | 10 | void enqueue(char elem, QUEUE *QP) { 11 | if (!isFull(*QP)) { 12 | QP->rear = (QP->rear + 1) % MAX; 13 | QP->elem[QP->rear] = elem; 14 | } 15 | } 16 | 17 | void insertFront(char elem, QUEUE *QP) { 18 | QUEUE temp; 19 | initialize(&temp); 20 | 21 | for (; !isEmpty(*QP); enqueue(front(*QP), &temp), dequeue(QP)) {} 22 | 23 | enqueue(elem, &temp); 24 | 25 | for (; !isEmpty(temp); enqueue(front(temp), QP), dequeue(&temp)) {} 26 | 27 | *QP = temp; 28 | } 29 | 30 | void insertAtPos(int pos, char elem, QUEUE *QP) { 31 | QUEUE temp; 32 | initialize(&temp); 33 | int ctr; 34 | 35 | for (ctr = 0; ctr < pos; ctr++) { 36 | printf("Transferring %c\n", front(*QP)); 37 | enqueue(front(*QP), &temp); 38 | dequeue(QP); 39 | } 40 | 41 | enqueue(elem, QP); 42 | 43 | while (!isEmpty(temp)) { 44 | enqueue(front(temp), QP); 45 | dequeue(&temp); 46 | } 47 | } 48 | 49 | void dequeue(QUEUE *QP) { 50 | if (!isEmpty(*QP)) { 51 | QP->front = (QP->front + 1) % MAX; 52 | } 53 | } 54 | 55 | char front(QUEUE Q) { 56 | char res = '\0'; 57 | 58 | if (!isEmpty(Q)) { 59 | res = Q.elem[Q.front]; 60 | } 61 | 62 | return res; 63 | } 64 | 65 | bool isEmpty(QUEUE Q) { 66 | return Q.front == (Q.rear + 1) % MAX; 67 | } 68 | 69 | bool isFull(QUEUE Q) { 70 | return Q.front == (Q.rear + 2) % MAX; 71 | } 72 | 73 | void makeNull(QUEUE *QP) { 74 | for (; !isEmpty(*QP); dequeue(QP)) {} 75 | } 76 | 77 | void displayList(QUEUE *QP) { 78 | QUEUE temp; 79 | initialize(&temp); 80 | char data; 81 | 82 | printf("QUEUE: "); 83 | for (data = front(*QP); !isEmpty(*QP); enqueue(data, &temp), dequeue(QP), data = front(*QP)) { 84 | printf("%c ", data); 85 | } 86 | printf("\n"); 87 | 88 | for (data = front(temp); !isEmpty(temp); enqueue(data, QP), dequeue(&temp), data = front(temp)) {} 89 | } -------------------------------------------------------------------------------- /examples/queues/modules/array/v3/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_ARRAY_V3 2 | #define QUEUE_ARRAY_V3 3 | #include 4 | 5 | #define MAX 10 6 | 7 | typedef struct { 8 | char Elem[MAX]; 9 | int front; 10 | int rear; 11 | } QUEUE; 12 | 13 | void initialize(QUEUE *QP); 14 | void enqueue(char elem, QUEUE *QP); 15 | void dequeue(QUEUE *QP); 16 | char front (QUEUE Q); 17 | void insertFront(char elem, QUEUE *QP); 18 | void insertAtPos(int pos, char elem, QUEUE *QP); 19 | bool isEmpty(QUEUE Q); 20 | bool isFull(QUEUE Q); 21 | void makeNull(QUEUE *QP); 22 | void displayList(QUEUE *QP); 23 | 24 | #endif -------------------------------------------------------------------------------- /examples/queues/modules/array/v3/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(QUEUE *QP) { 6 | QP->rear = -1; 7 | QP->front = 0; 8 | } 9 | void enqueue(char elem, QUEUE *QP) { 10 | if (!isFull(*QP)) { 11 | QP->rear = (QP->rear + 1) % MAX; 12 | QP->Elem[QP->rear] = elem; 13 | } 14 | } 15 | void dequeue(QUEUE *QP) { 16 | if (!isEmpty(*QP)) { 17 | QP->front = (QP->front + 1) % MAX; 18 | } 19 | } 20 | char front (QUEUE Q) { 21 | char res = '\0'; 22 | 23 | if (!isEmpty(Q)) { 24 | res = Q.Elem[Q.front]; 25 | } 26 | } 27 | void insertFront(char elem, QUEUE *QP) { 28 | if (!isFull(*QP)) { 29 | QUEUE temp; 30 | initialize(&temp); 31 | 32 | enqueue(elem, &temp); 33 | char frontElem; 34 | 35 | for (frontElem = front(*QP); !isEmpty(*QP); enqueue(frontElem, &temp), dequeue(QP), frontElem = front(*QP)) {} 36 | 37 | *QP = temp; 38 | } 39 | } 40 | void insertAtPos(int pos, char elem, QUEUE *QP) { 41 | if (!isFull(*QP)) { 42 | QUEUE temp; 43 | initialize(&temp); 44 | char frontElem; 45 | int ctr; 46 | 47 | for (frontElem = front(*QP), ctr = 0; !isEmpty(*QP) && ctr < pos; enqueue(frontElem, &temp), dequeue(QP), frontElem = front(*QP), ctr++) {} 48 | 49 | enqueue(elem, &temp); 50 | 51 | for (frontElem = front(*QP); !isEmpty(*QP); enqueue(frontElem, &temp), dequeue(QP), frontElem = front(*QP)) {} 52 | 53 | *QP = temp; 54 | } 55 | } 56 | bool isEmpty(QUEUE Q) { 57 | return (Q.rear + 1) % MAX == Q.front; 58 | } 59 | bool isFull(QUEUE Q) { 60 | return (Q.rear + 2) % MAX == Q.front; 61 | } 62 | void makeNull(QUEUE *QP) { 63 | while (!isEmpty(*QP)) { 64 | dequeue(QP); 65 | } 66 | } 67 | void displayList(QUEUE *QP) { 68 | QUEUE temp; 69 | initialize(&temp); 70 | char frontElem; 71 | 72 | printf("QUEUE: "); 73 | for (frontElem = front(*QP); !isEmpty(*QP); enqueue(frontElem, &temp), dequeue(QP), frontElem = front(*QP)) { 74 | printf("%c ", frontElem); 75 | } 76 | printf("\n"); 77 | 78 | for (frontElem = front(temp); !isEmpty(temp); enqueue(frontElem, QP), dequeue(&temp), frontElem = front(temp)) {} 79 | } -------------------------------------------------------------------------------- /examples/queues/modules/cursor-based/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_CURSORBASED_V1 2 | #define QUEUE_CURSORBASED_V1 3 | #include 4 | 5 | #define MAX 8 6 | 7 | typedef int LIST; 8 | 9 | typedef struct node { 10 | char data; 11 | int next; 12 | } Node; 13 | 14 | typedef struct { 15 | Node Nodes[MAX]; 16 | int Avail; 17 | } VirtualHeap; 18 | 19 | typedef struct { 20 | LIST front; 21 | LIST rear; 22 | VirtualHeap VH; 23 | } Queue; 24 | 25 | void initialize(Queue *QP); 26 | void enqueue(char elem, Queue *QP); 27 | void dequeue(Queue *QP); 28 | void insertFront(char elem, Queue *QP); 29 | void insertPos(int pos, char elem, Queue *QP); 30 | char front(Queue Q); 31 | bool isEmpty(Queue Q); 32 | bool isFull(Queue Q); 33 | void makeNull(Queue *QP); 34 | void displayList(Queue *QP); 35 | 36 | int allocSpace(VirtualHeap *VHP); 37 | void deallocSpace(VirtualHeap *VHP, int idx); 38 | 39 | #endif -------------------------------------------------------------------------------- /examples/queues/modules/cursor-based/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(Queue *QP) { 6 | int ctr; 7 | 8 | for (ctr = 0; ctr < MAX; ctr++) { 9 | QP->VH.Nodes[ctr].next = ctr - 1; 10 | } 11 | 12 | QP->VH.Avail = ctr - 1; 13 | QP->front = -1; 14 | QP->rear = -1; 15 | } 16 | 17 | void enqueue(char elem, Queue *QP) { 18 | LIST temp = allocSpace(&(QP->VH)); 19 | 20 | if (temp != -1) { 21 | QP->VH.Nodes[temp].data = elem; 22 | QP->VH.Nodes[temp].next = -1; 23 | 24 | if (QP->front == -1) { 25 | QP->front = temp; 26 | } else { 27 | QP->VH.Nodes[QP->rear].next = temp; 28 | } 29 | 30 | QP->rear = temp; 31 | } 32 | } 33 | 34 | void dequeue(Queue *QP) { 35 | if (!isEmpty(*QP)) { 36 | LIST temp = QP->front; 37 | QP->front = QP->VH.Nodes[temp].next; 38 | deallocSpace(&(QP->VH), temp); 39 | } 40 | } 41 | 42 | void insertFront(char elem, Queue *QP) { 43 | if (!isFull(*QP)) { 44 | Queue tempQ; 45 | initialize(&tempQ); 46 | 47 | while (!isEmpty(*QP)) { 48 | enqueue(front(*QP), &tempQ); 49 | dequeue(QP); 50 | } 51 | 52 | enqueue(elem, QP); 53 | 54 | while (!isEmpty(tempQ)) { 55 | enqueue(front(tempQ), QP); 56 | dequeue(&tempQ); 57 | } 58 | } 59 | } 60 | 61 | void insertPos(int pos, char elem, Queue *QP) { 62 | 63 | } 64 | 65 | char front(Queue Q) { 66 | char res = '\0'; 67 | 68 | if (!isEmpty(Q)) { 69 | res = Q.VH.Nodes[Q.front].data; 70 | } 71 | 72 | return res; 73 | } 74 | 75 | bool isEmpty(Queue Q) { 76 | return Q.front == -1; 77 | } 78 | 79 | bool isFull(Queue Q) { 80 | return allocSpace(&Q.VH) == -1; 81 | } 82 | 83 | void makeNull(Queue *QP) { 84 | while (!isEmpty(*QP)) { 85 | dequeue(QP); 86 | } 87 | } 88 | 89 | void displayList(Queue *QP) { 90 | Queue tempQ; 91 | initialize(&tempQ); 92 | 93 | printf("QUEUE: "); 94 | while (!isEmpty(*QP)) { 95 | printf("%c ", front(*QP)); 96 | enqueue(front(*QP), &tempQ); 97 | dequeue(QP); 98 | } 99 | printf("\n"); 100 | 101 | while (!isEmpty(tempQ)) { 102 | enqueue(front(tempQ), QP); 103 | dequeue(&tempQ); 104 | } 105 | } 106 | 107 | int allocSpace(VirtualHeap *VHP) { 108 | int res = VHP->Avail; 109 | 110 | if (VHP->Avail != -1) { 111 | VHP->Avail = VHP->Nodes[res].next; 112 | } 113 | 114 | return res; 115 | } 116 | 117 | void deallocSpace(VirtualHeap *VHP, int idx) { 118 | if (idx > 0 && idx < MAX) { 119 | VHP->Nodes[idx].next = VHP->Avail; 120 | VHP->Avail = idx; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /examples/queues/modules/cursor-based/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_CURSORBASED_V2 2 | #define QUEUE_CURSORBASED_V2 3 | #include 4 | 5 | #define MAX 10 6 | 7 | typedef struct { 8 | char data; 9 | int next; 10 | } Node; 11 | 12 | typedef struct { 13 | Node Nodes[MAX]; 14 | int Avail; 15 | } VirtHeap; 16 | 17 | typedef int LIST; 18 | 19 | typedef struct { 20 | VirtHeap VH; 21 | LIST L; 22 | } QUEUE; 23 | 24 | void initialize(QUEUE *QP); 25 | void enqueue(char elem, QUEUE *QP); 26 | void dequeue(QUEUE *QP); 27 | void insertFront(char elem, QUEUE *QP); 28 | void insertPos(int pos, char elem, QUEUE *QP); 29 | char front(QUEUE Q); 30 | bool isEmpty(QUEUE Q); 31 | bool isFull(QUEUE Q); 32 | void makeNull(QUEUE *QP); 33 | void displayList(QUEUE *QP); 34 | 35 | int allocSpace(QUEUE *QP); 36 | void deallocSpace(QUEUE *QP, int idx); 37 | 38 | 39 | #endif -------------------------------------------------------------------------------- /examples/queues/modules/cursor-based/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(QUEUE *QP) { 6 | int ctr; 7 | 8 | for (ctr = 0; ctr < MAX; ctr++) { 9 | QP->VH.Nodes[ctr].next = ctr - 1; 10 | } 11 | 12 | QP->VH.Avail = MAX - 1; 13 | QP->L = -1; 14 | } 15 | 16 | void enqueue(char elem, QUEUE *QP) { 17 | int temp = allocSpace(QP); 18 | 19 | if (temp != -1) { 20 | int *trav; 21 | 22 | for (trav = &(QP->L); *trav != -1; trav = &(QP->VH.Nodes[*trav].next)) {} 23 | 24 | QP->VH.Nodes[temp].data = elem; 25 | QP->VH.Nodes[temp].next = -1; 26 | *trav = temp; 27 | } 28 | } 29 | 30 | void dequeue(QUEUE *QP) { 31 | if (!isEmpty(*QP)) { 32 | int temp = QP->L; 33 | QP->L = QP->VH.Nodes[temp].next; 34 | deallocSpace(QP, temp); 35 | } 36 | } 37 | 38 | void insertFront(char elem, QUEUE *QP) { 39 | QUEUE temp; 40 | initialize(&temp); 41 | 42 | for (; !isEmpty(*QP); enqueue(front(*QP), &temp), dequeue(QP)) {} 43 | enqueue(elem, QP); 44 | for (; !isEmpty(temp); enqueue(front(temp), QP), dequeue(&temp)) {} 45 | } 46 | 47 | // BUGGY 48 | void insertPos(int pos, char elem, QUEUE *QP) { 49 | if (pos >= 0 && pos < MAX) { 50 | QUEUE temp; 51 | initialize(&temp); 52 | int ctr; 53 | 54 | for (ctr = 0; ctr < pos && !isEmpty(*QP); ctr++, enqueue(front(*QP), &temp), dequeue(QP)) {} 55 | enqueue(elem, QP); 56 | for (; !isEmpty(temp); enqueue(front(temp), QP), dequeue(&temp)) {} 57 | } 58 | } 59 | 60 | char front(QUEUE Q) { 61 | char res = '\0'; 62 | 63 | if (!isEmpty(Q)) { 64 | res = Q.VH.Nodes[Q.L].data; 65 | } 66 | 67 | return res; 68 | } 69 | 70 | bool isEmpty(QUEUE Q) { 71 | return Q.L == -1; 72 | } 73 | 74 | bool isFull(QUEUE Q) { 75 | return Q.VH.Avail == -1; 76 | } 77 | 78 | void makeNull(QUEUE *QP) { 79 | for (; !isEmpty(*QP); dequeue(QP)) {} 80 | } 81 | 82 | void displayList(QUEUE *QP) { 83 | QUEUE temp; 84 | initialize(&temp); 85 | char data; 86 | 87 | printf("QUEUE: "); 88 | for (data = front(*QP); !isEmpty(*QP); enqueue(data, &temp), dequeue(QP), data = front(*QP)) { 89 | printf("%c ", data); 90 | } 91 | printf("\n"); 92 | for (; !isEmpty(temp); enqueue(front(temp), QP), dequeue(&temp)) {} 93 | } 94 | 95 | int allocSpace(QUEUE *QP) { 96 | int res = QP->VH.Avail; 97 | 98 | if (res != -1) { 99 | QP->VH.Avail = QP->VH.Nodes[res].next; 100 | } 101 | 102 | return res; 103 | } 104 | 105 | void deallocSpace(QUEUE *QP, int idx) { 106 | if (idx > -1 && idx < MAX) { 107 | QP->VH.Nodes[idx].next = QP->VH.Avail; 108 | QP->VH.Avail = idx; 109 | } 110 | } -------------------------------------------------------------------------------- /examples/queues/modules/linked-list/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_LINKEDLIST_V1 2 | #define QUEUE_LINKEDLIST_V1 3 | #include 4 | 5 | typedef struct node { 6 | char data; 7 | struct node *link; 8 | } Node; 9 | 10 | typedef struct { 11 | Node *front; 12 | Node *rear; 13 | } Queue; 14 | 15 | void initialize(Queue *QP); 16 | void enqueue(char elem, Queue *QP); 17 | void dequeue(Queue *QP); 18 | void insertFront(char elem, Queue *QP); 19 | void insertPos(int pos, char elem, Queue *QP); 20 | char front(Queue Q); 21 | bool isEmpty(Queue Q); 22 | void makeNull(Queue *QP); 23 | void displayList(Queue *QP); 24 | 25 | #endif -------------------------------------------------------------------------------- /examples/queues/modules/linked-list/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void initialize(Queue *QP) { 7 | QP->front = NULL; 8 | QP->rear = NULL; 9 | } 10 | 11 | void enqueue(char elem, Queue *QP) { 12 | Node *temp = (Node *)malloc(sizeof(Node)); 13 | 14 | if (temp != NULL) { 15 | temp->data = elem; 16 | temp->link = NULL; 17 | 18 | if (QP->front == NULL) { 19 | QP->front = temp; 20 | } else { 21 | QP->rear->link = temp; 22 | } 23 | 24 | QP->rear = temp; 25 | } 26 | } 27 | 28 | void dequeue(Queue *QP) { 29 | if (!isEmpty(*QP)) { 30 | Node *temp = QP->front; 31 | QP->front = temp->link; 32 | free(temp); 33 | } 34 | } 35 | 36 | void insertFront(char elem, Queue *QP) { 37 | Node *temp = (Node *)malloc(sizeof(Node)); 38 | 39 | if (temp != NULL) { 40 | Queue tempQ; 41 | initialize(&tempQ); 42 | 43 | while (!isEmpty(*QP)) { 44 | enqueue(front(*QP), &tempQ); 45 | dequeue(QP); 46 | } 47 | 48 | enqueue(elem, QP); 49 | 50 | while (!isEmpty(tempQ)) { 51 | enqueue(front(tempQ), QP); 52 | dequeue(&tempQ); 53 | } 54 | } 55 | 56 | } 57 | 58 | void insertPos(int pos, char elem, Queue *QP) { 59 | Node *temp = (Node *)malloc(sizeof(Node)); 60 | 61 | if (temp != NULL && pos >= 0) { 62 | int ctr; 63 | Queue tempQ; 64 | initialize(&tempQ); 65 | 66 | // TODO: Check if pos is valid 67 | for (ctr = 0; ctr < pos && !isEmpty(*QP); ctr++) { 68 | enqueue(front(*QP), &tempQ); 69 | dequeue(QP); 70 | } 71 | 72 | enqueue(elem, QP); 73 | 74 | while (!isEmpty(tempQ)) { 75 | enqueue(front(tempQ), QP); 76 | dequeue(&tempQ); 77 | } 78 | } 79 | } 80 | 81 | char front(Queue Q) { 82 | char res = '\0'; 83 | 84 | if (!isEmpty(Q)) { 85 | res = Q.front->data; 86 | } 87 | 88 | return res; 89 | } 90 | 91 | bool isEmpty(Queue Q) { 92 | return Q.front == NULL; 93 | } 94 | 95 | void makeNull(Queue *QP) { 96 | while (!isEmpty(*QP)) { 97 | dequeue(QP); 98 | } 99 | } 100 | 101 | void displayList(Queue *QP) { 102 | Queue tempQ; 103 | initialize(&tempQ); 104 | 105 | printf("QUEUE: "); 106 | while (!isEmpty(*QP)) { 107 | printf("%c ", front(*QP)); 108 | enqueue(front(*QP), &tempQ); 109 | dequeue(QP); 110 | } 111 | 112 | printf("\n"); 113 | 114 | while (!isEmpty(tempQ)) { 115 | enqueue(front(tempQ), QP); 116 | dequeue(&tempQ); 117 | } 118 | } -------------------------------------------------------------------------------- /examples/queues/modules/linked-list/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_LINKEDLIST_V2 2 | #define QUEUE_LINKEDLIST_V2 3 | #include 4 | 5 | typedef struct node { 6 | char data; 7 | struct node *next; 8 | } Node; 9 | 10 | typedef struct { 11 | Node *front; 12 | Node *rear; 13 | } QUEUE; 14 | 15 | void initialize(QUEUE *QP); 16 | void enqueue(char elem, QUEUE *QP); 17 | void dequeue(QUEUE *QP); 18 | void insertFront(char elem, QUEUE *QP); 19 | void insertPos(int pos, char elem, QUEUE *QP); 20 | char front(QUEUE Q); 21 | bool isEmpty(QUEUE Q); 22 | void makeNull(QUEUE *QP); 23 | void displayList(QUEUE *QP); 24 | 25 | #endif -------------------------------------------------------------------------------- /examples/queues/modules/linked-list/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void initialize(QUEUE *QP) { 7 | QP->front = NULL; 8 | QP->rear = NULL; 9 | } 10 | 11 | void enqueue(char elem, QUEUE *QP) { 12 | Node *temp = (Node *)malloc(sizeof(Node)); 13 | 14 | if (temp != NULL) { 15 | temp->data = elem; 16 | temp->next = NULL; 17 | 18 | if (QP->front == NULL) { 19 | QP->front = temp; 20 | } else { 21 | QP->rear->next = temp; 22 | 23 | } 24 | QP->rear = temp; 25 | } 26 | } 27 | 28 | void dequeue(QUEUE *QP) { 29 | if (!isEmpty(*QP)) { 30 | Node *temp = QP->front; 31 | QP->front = temp->next; 32 | free(temp); 33 | } 34 | } 35 | 36 | void insertFront(char elem, QUEUE *QP) { 37 | QUEUE temp; 38 | initialize(&temp); 39 | 40 | for (; !isEmpty(*QP); enqueue(front(*QP), &temp), dequeue(QP)) {} 41 | enqueue(elem, QP); 42 | for (; !isEmpty(temp); enqueue(front(temp), QP), dequeue(&temp)) {} 43 | } 44 | 45 | void insertPos(int pos, char elem, QUEUE *QP) { 46 | if (pos >= 0) { 47 | QUEUE temp; 48 | initialize(&temp); 49 | int ctr; 50 | 51 | for (ctr = 0; ctr < pos && !isEmpty(*QP); ctr++, enqueue(front(*QP), &temp), dequeue(QP)) {} 52 | enqueue(elem, QP); 53 | for (; !isEmpty(*QP); enqueue(front(*QP), &temp), dequeue(QP)) {} 54 | 55 | *QP = temp; 56 | } 57 | } 58 | 59 | char front(QUEUE Q) { 60 | char res = '\0'; 61 | 62 | if (!isEmpty(Q)) { 63 | res = Q.front->data; 64 | } 65 | 66 | return res; 67 | } 68 | 69 | bool isEmpty(QUEUE Q) { 70 | return Q.front == NULL; 71 | } 72 | 73 | void makeNull(QUEUE *QP) { 74 | for (; !isEmpty(*QP); dequeue(QP)) {} 75 | } 76 | 77 | void displayList(QUEUE *QP) { 78 | QUEUE temp; 79 | initialize(&temp); 80 | char data; 81 | 82 | printf("QUEUE: "); 83 | for (data = front(*QP); !isEmpty(*QP); enqueue(data, &temp), dequeue(QP), data = front(*QP)) { 84 | printf("%c ", data); 85 | } 86 | printf("\n"); 87 | for (; !isEmpty(temp); enqueue(front(temp), QP), dequeue(&temp)) {} 88 | } -------------------------------------------------------------------------------- /examples/queues/modules/linked-list/v3/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_LINKEDLIST_V3 2 | #define QUEUE_LINKEDLIST_V3 3 | #include 4 | 5 | typedef struct node { 6 | char data; 7 | struct node *next; 8 | } Node; 9 | 10 | typedef struct { 11 | Node *front; 12 | Node *rear; 13 | } QUEUE; 14 | 15 | void initialize(QUEUE *QP); 16 | void enqueue(char elem, QUEUE *QP); 17 | void dequeue(QUEUE *QP); 18 | void insertFront(char elem, QUEUE *QP); 19 | void insertAtPos(int pos, char elem, QUEUE *QP); 20 | char front(QUEUE Q); 21 | bool isEmpty(QUEUE Q); 22 | void makeNull(QUEUE *QP); 23 | void displayList(QUEUE *QP); 24 | 25 | #endif -------------------------------------------------------------------------------- /examples/queues/modules/linked-list/v3/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void initialize(QUEUE *QP) { 7 | QP->front = NULL; 8 | QP->rear = NULL; 9 | } 10 | void enqueue(char elem, QUEUE *QP) { 11 | Node *node = (Node *)malloc(sizeof(Node)); 12 | 13 | if (node != NULL) { 14 | node->data = elem; 15 | node->next = NULL; 16 | 17 | if (isEmpty(*QP)) { 18 | QP->front = node; 19 | } else { 20 | QP->rear->next = node; 21 | } 22 | 23 | QP->rear = node; 24 | } 25 | } 26 | void dequeue(QUEUE *QP) { 27 | if (!isEmpty(*QP)) { 28 | Node *temp = QP->front; 29 | QP->front = temp->next; 30 | free(temp); 31 | } 32 | } 33 | void insertFront(char elem, QUEUE *QP) { 34 | QUEUE temp; 35 | initialize(&temp); 36 | char frontElem; 37 | 38 | enqueue(elem, &temp); 39 | for (frontElem = front(*QP); !isEmpty(*QP); enqueue(frontElem, &temp), dequeue(QP), frontElem = front(*QP)) {} 40 | 41 | *QP = temp; 42 | } 43 | void insertAtPos(int pos, char elem, QUEUE *QP) { 44 | QUEUE temp; 45 | initialize(&temp); 46 | int ctr; 47 | char frontElem; 48 | 49 | for (frontElem = front(*QP), ctr = 0; !isEmpty(*QP) && ctr < pos; enqueue(frontElem, &temp), dequeue(QP), frontElem = front(*QP), ctr++) {} 50 | 51 | enqueue(elem, &temp); 52 | 53 | for (frontElem = front(*QP); !isEmpty(*QP); enqueue(frontElem, &temp), dequeue(QP), frontElem = front(*QP)) {} 54 | 55 | *QP = temp; 56 | } 57 | char front(QUEUE Q) { 58 | char res = '\0'; 59 | 60 | if (!isEmpty(Q)) { 61 | res = Q.front->data; 62 | } 63 | 64 | return res; 65 | } 66 | bool isEmpty(QUEUE Q) { 67 | return Q.front == NULL; 68 | } 69 | void makeNull(QUEUE *QP) { 70 | while (!isEmpty(*QP)) { 71 | dequeue(QP); 72 | } 73 | } 74 | void displayList(QUEUE *QP) { 75 | QUEUE temp; 76 | initialize(&temp); 77 | char frontElem; 78 | 79 | printf("QUEUE: "); 80 | for (frontElem = front(*QP); !isEmpty(*QP); enqueue(frontElem, &temp), dequeue(QP), frontElem = front(*QP)) { 81 | printf("%c ", frontElem); 82 | } 83 | 84 | printf("\n"); 85 | 86 | for (frontElem = front(temp); !isEmpty(temp); enqueue(frontElem, QP), dequeue(&temp), frontElem = front(temp)) {} 87 | } -------------------------------------------------------------------------------- /examples/spanning-tree/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/v3/_header.h" 2 | #include 3 | 4 | int main() { 5 | MATRIX m1 = { 6 | { INF, 1, 5, 4, 6, 5 }, 7 | { 1, INF, 5, INF, INF, 6 }, 8 | { 5, 5, INF, 2, INF, INF }, 9 | { 4, INF, 2, INF, 1, INF }, 10 | { 6, INF, INF, 1, INF, 3 }, 11 | { 5, 6, INF, INF, 3, INF } 12 | }; // 11 13 | 14 | MATRIX m2 = { 15 | {INF, 2, INF, 6, INF, INF}, 16 | {2, INF, 3, 8, 5, INF}, 17 | {INF, 3, INF, INF, 7, INF}, 18 | {6, 8, INF, INF, 9, INF}, 19 | {INF, 5, 7, 9, INF, 1}, 20 | {INF, INF, INF, INF, 1, INF}, 21 | }; // 17 22 | 23 | MATRIX m3 = { 24 | {INF, 4, INF, 6, INF, INF}, 25 | {4, INF, 8, INF, 5, INF}, 26 | {INF, 8, INF, 7, INF, 4}, 27 | {6, INF, 7, INF, 9, INF}, 28 | {INF, 5, INF, 9, INF, 1}, 29 | {INF, INF, 4, INF, 1, INF}, 30 | }; // 20 31 | 32 | MATRIX m4 = { 33 | {INF, INF, INF, INF, INF, INF}, 34 | {INF, INF, INF, INF, INF, INF}, 35 | {INF, INF, INF, INF, INF, INF}, 36 | {INF, INF, INF, INF, INF, INF}, 37 | {INF, INF, INF, INF, INF, INF}, 38 | {INF, INF, INF, INF, INF, INF}, 39 | }; // 20 40 | 41 | int mcP, mcK; 42 | mcP = prim(m1, 0); 43 | printf("Prim minimum cost: %d\n", mcP); 44 | 45 | mcK = kruskal(m1); 46 | printf("Kruskal minimum cost: %d\n", mcK); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /examples/spanning-tree/modules/util/minHeap/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef PRIORITYQUEUE_HEAP 2 | #define PRIORITYQUEUE_HEAP 3 | 4 | #define HEAP_MAX 100 5 | 6 | typedef struct { 7 | int from; 8 | int to; 9 | } EdgeInfo; 10 | 11 | typedef struct { 12 | int cost; 13 | EdgeInfo path; 14 | } Edge; 15 | 16 | typedef struct { 17 | Edge edges[HEAP_MAX]; 18 | int lastIdx; 19 | } HEAP; 20 | 21 | void initialize(HEAP *H); 22 | void insert(HEAP *H, Edge data); 23 | Edge deleteMin(HEAP *H); 24 | void heapify(HEAP *H, int idx); 25 | void displayHeap(HEAP H); 26 | void displayEdge(Edge e); 27 | 28 | #endif -------------------------------------------------------------------------------- /examples/spanning-tree/modules/util/minHeap/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | 4 | 5 | void initialize(HEAP *H) { 6 | H->lastIdx = -1; 7 | } 8 | 9 | void insert(HEAP *H, Edge data) { 10 | if (H->lastIdx >= HEAP_MAX - 1) { 11 | printf("HEAP IS FULL\n"); 12 | } else { 13 | H->edges[++H->lastIdx] = data; 14 | int idx; 15 | 16 | for (idx = H->lastIdx; idx > 0 && data.cost < H->edges[(idx - 1) / 2].cost; idx = (idx - 1) / 2) { 17 | Edge temp = H->edges[idx]; 18 | H->edges[idx] = H->edges[(idx - 1) / 2]; 19 | H->edges[(idx - 1) / 2] = temp; 20 | } 21 | } 22 | } 23 | Edge deleteMin(HEAP *H) { 24 | Edge res = {-1,{-1,-1}}; 25 | if (H->lastIdx > -1) { 26 | res = H->edges[0]; 27 | H->edges[0] = H->edges[H->lastIdx]; 28 | H->edges[H->lastIdx--] = res; 29 | heapify(H, 0); 30 | } 31 | 32 | return res; 33 | } 34 | void heapify(HEAP *H, int idx) { 35 | int left = 2 * idx + 1; 36 | int right = 2 * idx + 2; 37 | int smallest = idx; 38 | 39 | if (left <= H->lastIdx && H->edges[left].cost < H->edges[smallest].cost) { 40 | smallest = left; 41 | } 42 | 43 | if (right <= H->lastIdx && H->edges[right].cost < H->edges[smallest].cost) { 44 | smallest = right; 45 | } 46 | 47 | if (smallest != idx) { 48 | Edge temp = H->edges[idx]; 49 | H->edges[idx] = H->edges[smallest]; 50 | H->edges[smallest] = temp; 51 | heapify(H, smallest); 52 | } 53 | } 54 | void displayHeap(HEAP H) { 55 | int ctr; 56 | 57 | printf("----- HEAP -----\n"); 58 | for (ctr = 0; ctr <= H.lastIdx; ctr++) { 59 | printf("IDX: %d\n", ctr); 60 | displayEdge(H.edges[ctr]); 61 | } 62 | printf("----------------\n"); 63 | } 64 | 65 | void displayEdge(Edge e) { 66 | printf("{\n"); 67 | printf("%10s:", "Cost"); 68 | printf("%5d\n", e.cost); 69 | printf("%10s:", "From"); 70 | printf("%5d\n", e.path.from); 71 | printf("%10s:", "To"); 72 | printf("%5d\n", e.path.to); 73 | printf("}\n"); 74 | } -------------------------------------------------------------------------------- /examples/spanning-tree/modules/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef SPANNINGTREE_V1 2 | #define SPANNINGTREE_V1 3 | 4 | #define MAX 6 5 | #define INF __INT_MAX__ 6 | 7 | typedef int MATRIX[][MAX]; 8 | int prim(MATRIX matrix, int src); 9 | int kruskal(MATRIX matrix); 10 | 11 | #endif -------------------------------------------------------------------------------- /examples/spanning-tree/modules/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include "../util/minHeap/_header.h" 3 | #include 4 | 5 | int prim(MATRIX matrix, int src) { 6 | int res = 0, visited[MAX] = {0}; 7 | HEAP heap; 8 | initialize(&heap); 9 | 10 | int currNode, neighbor; 11 | for (currNode = src; visited[currNode] == 0;) { 12 | // Mark node as visited 13 | visited[currNode] = 1; 14 | 15 | // Only add edges that lead to unvisited nodes 16 | for (neighbor = 0; neighbor < MAX; neighbor++) { 17 | int cost = matrix[currNode][neighbor]; 18 | if (cost != INF && visited[neighbor] == 0) { 19 | Edge edge = {cost, {currNode, neighbor}}; 20 | insert(&heap, edge); 21 | } 22 | } 23 | 24 | // Update cost based on cheapest edge 25 | Edge minEdge = deleteMin(&heap); 26 | if (visited[minEdge.path.from] != 1 || visited[minEdge.path.to] != 1) { 27 | res += minEdge.cost; 28 | // If both nodes are visited, currNode gets set to a visited node, 29 | // terminating the loop 30 | currNode = minEdge.path.to; 31 | } 32 | } 33 | 34 | return res; 35 | } 36 | 37 | // TODO: Optimize (merging components takes O(V) time 38 | // and we do this V times) 39 | int kruskal(MATRIX matrix) { 40 | int res = 0, components[MAX] = {0}, from, to; 41 | Edge data; 42 | HEAP heap; 43 | initialize(&heap); 44 | 45 | // Values are components that vertex is part of 46 | // Initially, each vertex is its own component 47 | for (from = 0; from < MAX; from++) { 48 | components[from] = from; 49 | } 50 | 51 | // Build min heap containing all edges 52 | for (from = 0; from < MAX; from++) { 53 | for (to = 0; to < MAX; to++) { 54 | if (matrix[from][to] != INF) { 55 | data.cost = matrix[from][to]; 56 | data.path.from = from; 57 | data.path.to = to; 58 | insert(&heap, data); 59 | } 60 | 61 | } 62 | } 63 | 64 | int compNum; 65 | // Stop when number of components is 1 66 | for (compNum = MAX, data = deleteMin(&heap); compNum > 1; data = deleteMin(&heap)) { 67 | from = data.path.from; 68 | to = data.path.to; 69 | 70 | // Edge joins two vertices from different components 71 | if (components[from] != components[to]) { 72 | res += data.cost; 73 | 74 | // Merge `to` component vertices into the 75 | // `from` component 76 | int ctr; 77 | for (ctr = 0; ctr < MAX; ctr++) { 78 | if (components[ctr] == components[to]) { 79 | components[ctr] = components[from]; 80 | } 81 | } 82 | compNum--; 83 | } 84 | } 85 | 86 | return res; 87 | } 88 | -------------------------------------------------------------------------------- /examples/spanning-tree/modules/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef SPANNINGTREE_V2 2 | #define SPANNINGTREE_V2 3 | 4 | #define MAX 6 5 | #define INF __INT_MAX__ 6 | 7 | typedef int MATRIX[MAX][MAX]; 8 | int prim(MATRIX matrix, int src); 9 | int kruskal(MATRIX matrix); 10 | 11 | #endif -------------------------------------------------------------------------------- /examples/spanning-tree/modules/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "../util/minHeap/_header.h" 2 | #include "_header.h" 3 | 4 | int prim(MATRIX matrix, int src) { 5 | int res = 0, visited[MAX] = {0}; 6 | HEAP heap; 7 | initialize(&heap); 8 | 9 | int tail; 10 | for (tail = src; visited[tail] != 1;) { 11 | visited[tail] = 1; 12 | 13 | int head, cost; 14 | for (head = 0; head < MAX; head++) { 15 | cost = matrix[tail][head]; 16 | if (cost != INF && visited[head] != 1) { 17 | Edge edge = {cost, {tail, head}}; 18 | insert(&heap, edge); 19 | } 20 | } 21 | 22 | Edge minEdge = deleteMin(&heap); 23 | if (visited[minEdge.path.from] == 0 || visited[minEdge.path.to] == 0) { 24 | res += minEdge.cost; 25 | tail = minEdge.path.to; 26 | } 27 | 28 | } 29 | 30 | return res; 31 | } 32 | 33 | int kruskal(MATRIX matrix) { 34 | int res = 0, components[MAX] = {0}; 35 | HEAP heap; 36 | initialize(&heap); 37 | 38 | int tail, head; 39 | for (tail = 0; tail < MAX; tail++) { 40 | components[tail] = tail; 41 | } 42 | 43 | // Adds links twice (bc matrix is symmetric) 44 | for (tail = 0; tail < MAX; tail++) { 45 | for (head = 0; head < MAX; head++) { 46 | if (matrix[tail][head] != INF) { 47 | Edge edge = {matrix[tail][head], {tail, head}}; 48 | insert(&heap, edge); 49 | } 50 | } 51 | } 52 | 53 | Edge trav; 54 | int compNum; 55 | for (compNum = MAX, trav = deleteMin(&heap); 56 | compNum > 1; 57 | trav = deleteMin(&heap)) { 58 | tail = trav.path.from; 59 | head = trav.path.to; 60 | 61 | if (components[tail] != components[head]) { 62 | res += trav.cost; 63 | 64 | int ctr; 65 | for (ctr = 0; ctr < MAX; ctr++) { 66 | if (components[ctr] == components[head]) { 67 | components[ctr] = components[tail]; 68 | } 69 | } 70 | 71 | compNum--; 72 | } 73 | } 74 | 75 | return res; 76 | } -------------------------------------------------------------------------------- /examples/spanning-tree/modules/v3/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef SPANNINGTREE_V3 2 | #define SPANNINGTREE_V3 3 | 4 | #define MAX 6 5 | #define INF __INT_MAX__ 6 | typedef int MATRIX[MAX][MAX]; 7 | 8 | int prim(MATRIX matrix, int src); 9 | int kruskal(MATRIX matrix); 10 | 11 | #endif -------------------------------------------------------------------------------- /examples/spanning-tree/modules/v3/def.c: -------------------------------------------------------------------------------- 1 | #include "../util/minHeap/_header.h" 2 | #include "_header.h" 3 | 4 | int prim(MATRIX matrix, int src) { 5 | int res = 0; 6 | int visited[MAX] = {0}; 7 | HEAP H; 8 | initialize(&H); 9 | 10 | int node, next, neighbor; 11 | for (node = src; visited[node] != 1;) { 12 | visited[node] = 1; 13 | 14 | for (neighbor = 0; neighbor < MAX; neighbor++) { 15 | int cost = matrix[node][neighbor]; 16 | if (cost != INF && visited[neighbor] != 1) { 17 | Edge e = {cost, {node, neighbor}}; 18 | insert(&H, e); 19 | } 20 | } 21 | 22 | Edge edge = deleteMin(&H); 23 | if (visited[edge.path.to] != 1) { 24 | res += edge.cost; 25 | node = edge.path.to; 26 | } 27 | } 28 | 29 | return res; 30 | } 31 | 32 | int kruskal(MATRIX matrix) { 33 | int res = 0; 34 | int components[MAX] = {0}; 35 | int node, neighbor; 36 | 37 | for (node = 0; node < MAX; node++) { 38 | components[node] = node; 39 | } 40 | 41 | HEAP H; 42 | initialize(&H); 43 | 44 | for (node = 0; node < MAX; node++) { 45 | for (neighbor = 0; neighbor < MAX; neighbor++) { 46 | int cost = matrix[node][neighbor]; 47 | if (cost != INF) { 48 | Edge e = {cost, {node, neighbor}}; 49 | insert(&H, e); 50 | } 51 | } 52 | } 53 | 54 | int compNum; 55 | Edge e; 56 | for (compNum = MAX, e = deleteMin(&H); e.cost != -1 && compNum > 1; e = deleteMin(&H)) { 57 | node = e.path.from; 58 | neighbor = e.path.to; 59 | if (components[node] != components[neighbor]) { 60 | res += e.cost; 61 | int ctr; 62 | 63 | for (ctr = 0; ctr < MAX; ctr++) { 64 | if (components[ctr] == components[neighbor]) { 65 | components[ctr] = components[node]; 66 | } 67 | } 68 | 69 | compNum--; 70 | } 71 | } 72 | return res; 73 | } -------------------------------------------------------------------------------- /examples/stacks/main.c: -------------------------------------------------------------------------------- 1 | #include "modules/array/v3/_header.h" 2 | #include 3 | 4 | int main() { 5 | STACK S; 6 | 7 | initialize(&S); 8 | push('S', &S); 9 | push('C', &S); 10 | push('O', &S); 11 | push('O', &S); 12 | push('B', &S); 13 | insertBottom('Y', &S); 14 | 15 | displayStack(&S); 16 | 17 | printf("%c\n", top(S)); 18 | pop(&S); 19 | printf("%c\n", top(S)); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /examples/stacks/modules/array/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_ARRAY_V1 2 | #define STACK_ARRAY_V1 3 | #include 4 | 5 | #define MAX 10 6 | typedef struct node { 7 | char Elem[MAX]; 8 | int top; // Grows from 0 to MAX - 1 9 | } STACK; 10 | 11 | void initialize(STACK *SP); 12 | void push(char elem, STACK *SP); 13 | void insertBottom(char elem, STACK *SP); 14 | void pop(STACK *SP); 15 | char top(STACK S); 16 | bool isEmpty(STACK S); 17 | bool isFull(STACK S); 18 | void displayStack(STACK* SP); 19 | 20 | #endif -------------------------------------------------------------------------------- /examples/stacks/modules/array/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | 4 | void initialize(STACK *SP) { 5 | SP->top = -1; 6 | } 7 | 8 | void push(char elem, STACK *SP) { 9 | if (SP->top < MAX - 1) { 10 | SP->top++; 11 | SP->Elem[SP->top] = elem; 12 | } 13 | } 14 | 15 | void pop(STACK *SP) { 16 | if (SP->top != -1) { 17 | SP->top--; 18 | } 19 | } 20 | 21 | char top(STACK S) { 22 | char res = '\0'; 23 | if (!isEmpty(S)) { 24 | res = S.Elem[S.top]; 25 | } 26 | 27 | return res; 28 | } 29 | 30 | bool isEmpty(STACK S) { 31 | return S.top == -1; 32 | } 33 | 34 | bool isFull(STACK S) { 35 | return S.top == MAX - 1; 36 | } 37 | 38 | void displayStack(STACK* SP) { 39 | STACK temp; 40 | initialize(&temp); 41 | char elem; 42 | 43 | printf("STACK: "); 44 | for (elem = top(*SP); !isEmpty(*SP); pop(SP), push(elem, &temp), elem = top(*SP)) { 45 | printf("%c ", elem); 46 | } 47 | printf("\n"); 48 | 49 | for (elem = top(temp); !isEmpty(temp); pop(&temp), push(elem, SP), elem = top(temp)) {} 50 | } 51 | 52 | void insertBottom(char elem, STACK *SP) { 53 | STACK temp; 54 | initialize(&temp); 55 | 56 | for (; !isEmpty(*SP); push(top(*SP), &temp), pop(SP)) {} 57 | 58 | push(elem, SP); 59 | 60 | for (; !isEmpty(temp); push(top(temp), SP), pop(&temp)) {} 61 | } 62 | -------------------------------------------------------------------------------- /examples/stacks/modules/array/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_ARRAY_V2 2 | #define STACK_ARRAY_V2 3 | #include 4 | 5 | #define MAX 10 6 | typedef struct { 7 | char data[MAX]; 8 | int top; 9 | } STACK; 10 | 11 | void initialize(STACK *SP); 12 | void push(char elem, STACK *SP); 13 | void pop(STACK *SP); 14 | void insertBottom(char elem, STACK *SP); 15 | char top(STACK S); 16 | bool isEmpty(STACK S); 17 | bool isFull(STACK S); 18 | void displayStack(STACK* SP); 19 | 20 | #endif -------------------------------------------------------------------------------- /examples/stacks/modules/array/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(STACK *SP) { 6 | SP->top = -1; 7 | } 8 | 9 | void push(char elem, STACK *SP) { 10 | if (!isFull(*SP)) { 11 | SP->top++; 12 | SP->data[SP->top] = elem; 13 | } 14 | } 15 | 16 | void pop(STACK *SP) { 17 | if (!isEmpty(*SP)) { 18 | SP->top--; 19 | } 20 | } 21 | 22 | char top(STACK S) { 23 | char res = -1; 24 | 25 | if (!isEmpty(S)) { 26 | res = S.data[S.top]; 27 | } 28 | 29 | return res; 30 | } 31 | 32 | bool isEmpty(STACK S) { 33 | return S.top == -1; 34 | } 35 | 36 | bool isFull(STACK S) { 37 | return S.top == MAX; 38 | } 39 | 40 | void displayStack(STACK* SP) { 41 | STACK temp; 42 | initialize(&temp); 43 | int elem; 44 | 45 | printf("STACK: "); 46 | for (elem = top(*SP); !isEmpty(*SP); pop(SP), push(elem, &temp), elem = top(*SP)) { 47 | printf("%c ", elem); 48 | } 49 | printf("\n"); 50 | 51 | for (elem = top(temp); !isEmpty(temp); pop(&temp), push(elem, SP), elem = top(temp)) { 52 | } 53 | } 54 | 55 | void insertBottom(char elem, STACK *SP) { 56 | STACK temp; 57 | initialize(&temp); 58 | 59 | for (; !isEmpty(*SP); push(top(*SP), &temp), pop(SP)) {} 60 | 61 | push(elem, SP); 62 | 63 | for (; !isEmpty(temp); push(top(temp), SP), pop(&temp)) {} 64 | } -------------------------------------------------------------------------------- /examples/stacks/modules/array/v3/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_ARRAY_V3 2 | #define STACK_ARRAY_V3 3 | #include 4 | 5 | #define MAX 10 6 | 7 | typedef struct { 8 | char data[MAX]; 9 | int top; 10 | } STACK; 11 | 12 | void initialize(STACK *SP); 13 | void push(char elem, STACK *SP); 14 | void pop(STACK *SP); 15 | void insertBottom(char elem, STACK *SP); 16 | char top(STACK S); 17 | bool isEmpty(STACK S); 18 | bool isFull(STACK S); 19 | void displayStack(STACK* SP); 20 | 21 | #endif -------------------------------------------------------------------------------- /examples/stacks/modules/array/v3/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(STACK *SP) { 6 | SP->top = -1; 7 | } 8 | void push(char elem, STACK *SP) { 9 | if (!isFull(*SP)) { 10 | SP->top++; 11 | SP->data[SP->top] = elem; 12 | } 13 | } 14 | void pop(STACK *SP) { 15 | if (!isEmpty(*SP)) { 16 | SP->top--; 17 | } 18 | } 19 | void insertBottom(char elem, STACK *SP) { 20 | STACK temp; 21 | initialize(&temp); 22 | char topElem; 23 | 24 | for (topElem = top(*SP); !isEmpty(*SP); push(topElem, &temp), pop(SP), topElem = top(*SP)) {} 25 | 26 | push(elem, SP); 27 | 28 | for (topElem = top(temp); !isEmpty(temp); push(topElem, SP), pop(&temp), topElem = top(temp)) {} 29 | } 30 | char top(STACK S) { 31 | char res = '\0'; 32 | 33 | if (!isEmpty(S)) { 34 | res = S.data[S.top]; 35 | } 36 | 37 | return res; 38 | } 39 | bool isEmpty(STACK S) { 40 | return S.top == -1; 41 | } 42 | bool isFull(STACK S) { 43 | return S.top == MAX - 1; 44 | } 45 | void displayStack(STACK* SP) { 46 | STACK temp; 47 | initialize(&temp); 48 | char topElem; 49 | 50 | printf("STACK: "); 51 | for (topElem = top(*SP); !isEmpty(*SP); push(topElem, &temp), pop(SP), topElem = top(*SP)) { 52 | printf("%c ", topElem); 53 | } 54 | 55 | printf("\n"); 56 | 57 | for (topElem = top(temp); !isEmpty(temp); push(topElem, SP), pop(&temp), topElem = top(temp)) {} 58 | } -------------------------------------------------------------------------------- /examples/stacks/modules/cursor-based/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_CURSORBASED_V1 2 | #define STACK_CURSORBASED_V1 3 | #include 4 | 5 | #define MAX 10 6 | typedef struct { 7 | char data; 8 | int link; 9 | } Node; 10 | 11 | typedef struct { 12 | Node Nodes[MAX]; 13 | int Avail; 14 | } VirtHeap; 15 | 16 | typedef int LIST; 17 | 18 | typedef struct { 19 | VirtHeap VH; 20 | LIST L; 21 | } STACK; 22 | 23 | void initialize(STACK *SP); 24 | void push(char elem, STACK *SP); 25 | void insertBottom(char elem, STACK *SP); 26 | void pop(STACK *SP); 27 | char top(STACK S); 28 | bool isEmpty(STACK S); 29 | bool isFull(STACK S); 30 | void displayStack(STACK* SP); 31 | 32 | int allocSpace(STACK *SP); 33 | void deallocSpace(int ndx, STACK *SP); 34 | 35 | #endif -------------------------------------------------------------------------------- /examples/stacks/modules/cursor-based/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(STACK *SP) { 6 | int ctr; 7 | 8 | for (ctr = 0; ctr < MAX; ctr++) { 9 | SP->VH.Nodes[ctr].link = ctr - 1; 10 | } 11 | 12 | SP->VH.Avail = ctr - 1; 13 | SP->L = -1; 14 | } 15 | 16 | void push(char elem, STACK *SP) { 17 | int temp = allocSpace(SP); 18 | 19 | if (temp != -1) { 20 | SP->VH.Nodes[temp].data = elem; 21 | SP->VH.Nodes[temp].link = SP->L; 22 | SP->L = temp; 23 | } 24 | } 25 | 26 | void insertBottom(char elem, STACK *SP) { 27 | STACK temp; 28 | initialize(&temp); 29 | int topElem; 30 | 31 | for (topElem = top(*SP); !isEmpty(*SP); push(topElem, &temp), pop(SP), topElem = top(*SP)) {} 32 | push(elem, SP); 33 | for (topElem = top(temp); !isEmpty(temp); push(topElem, SP), pop(&temp), topElem = top(temp)) {} 34 | } 35 | 36 | void pop(STACK *SP) { 37 | if (!isEmpty(*SP)) { 38 | int temp = SP->L; 39 | SP->L = SP->VH.Nodes[temp].link; 40 | deallocSpace(temp, SP); 41 | } 42 | } 43 | 44 | char top(STACK S) { 45 | char res = '\0'; 46 | 47 | if (!isEmpty(S)) { 48 | res = S.VH.Nodes[S.L].data; 49 | } 50 | 51 | return res; 52 | } 53 | 54 | bool isEmpty(STACK S) { 55 | return S.L == -1; 56 | } 57 | 58 | bool isFull(STACK S) { 59 | return S.VH.Avail == -1; 60 | } 61 | 62 | void displayStack(STACK* SP) { 63 | STACK temp; 64 | initialize(&temp); 65 | int elem; 66 | 67 | printf("STACK: "); 68 | for (elem = top(*SP); !isEmpty(*SP); push(elem, &temp), pop(SP), elem = top(*SP)) { 69 | printf("%c ", elem); 70 | } 71 | printf("\n"); 72 | 73 | for (elem = top(temp); !isEmpty(temp); push(elem, SP), pop(&temp), elem = top(temp)) {} 74 | 75 | } 76 | 77 | int allocSpace(STACK *SP) { 78 | int res = SP->VH.Avail; 79 | 80 | if (res != -1) { 81 | SP->VH.Avail = SP->VH.Nodes[res].link; 82 | } 83 | 84 | return res; 85 | } 86 | 87 | void deallocSpace(int ndx, STACK *SP) { 88 | if (ndx >= 0 && ndx < MAX) { 89 | SP->VH.Nodes[ndx].link = SP->VH.Avail; 90 | SP->VH.Avail = ndx; 91 | } 92 | } -------------------------------------------------------------------------------- /examples/stacks/modules/cursor-based/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef STACKS_CURSORBASED_V2 2 | #define STACKS_CURSORBASED_V2 3 | #include 4 | 5 | #define MAX 10 6 | 7 | typedef struct { 8 | char elem; 9 | int link; 10 | } Node; 11 | 12 | typedef struct { 13 | Node Nodes[MAX]; 14 | int Avail; 15 | } VHeap; 16 | 17 | typedef int LIST; 18 | 19 | typedef struct { 20 | VHeap VH; 21 | LIST L; 22 | } STACK; 23 | 24 | void initialize(STACK *SP); 25 | void push(char elem, STACK *SP); 26 | void insertBottom(char elem, STACK *SP); 27 | void pop(STACK *SP); 28 | char top(STACK S); 29 | bool isEmpty(STACK S); 30 | bool isFull(STACK S); 31 | void displayStack(STACK* SP); 32 | 33 | int allocSpace(STACK *SP); 34 | void deallocSpace(int ndx, STACK *SP); 35 | 36 | #endif -------------------------------------------------------------------------------- /examples/stacks/modules/cursor-based/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(STACK *SP) { 6 | int idx; 7 | 8 | for (idx = 0; idx < MAX; idx++) { 9 | SP->VH.Nodes[idx].link = idx - 1; 10 | } 11 | 12 | SP->VH.Avail = MAX - 1; 13 | SP->L = -1; 14 | } 15 | 16 | void push(char elem, STACK *SP) { 17 | int idx = allocSpace(SP); 18 | 19 | if (idx != -1) { 20 | SP->VH.Nodes[idx].elem = elem; 21 | SP->VH.Nodes[idx].link = SP->L; 22 | SP->L = idx; 23 | } 24 | } 25 | 26 | void insertBottom(char elem, STACK *SP) { 27 | int idx = allocSpace(SP); 28 | 29 | if (idx != -1) { 30 | STACK temp; 31 | initialize(&temp); 32 | char topElem; 33 | 34 | for (topElem = top(*SP); !isEmpty(*SP); pop(SP), push(topElem, &temp), topElem = top(*SP)) {} 35 | 36 | push(elem, SP); 37 | 38 | for (topElem = top(temp); !isEmpty(temp); pop(&temp), push(topElem, SP), topElem = top(temp)) {} 39 | } 40 | } 41 | void pop(STACK *SP) { 42 | if (!isEmpty(*SP)) { 43 | int temp = SP->L; 44 | SP->L = SP->VH.Nodes[temp].link; 45 | 46 | deallocSpace(temp, SP); 47 | } 48 | } 49 | char top(STACK S) { 50 | char res = '\0'; 51 | 52 | if (!isEmpty(S)) { 53 | res = S.VH.Nodes[S.L].elem; 54 | } 55 | 56 | return res; 57 | } 58 | bool isEmpty(STACK S) { 59 | return S.L == -1; 60 | } 61 | bool isFull(STACK S) { 62 | return S.VH.Avail == -1; 63 | } 64 | void displayStack(STACK* SP) { 65 | STACK temp; 66 | initialize(&temp); 67 | char topElem; 68 | 69 | printf("STACK: "); 70 | for (topElem = top(*SP); !isEmpty(*SP); push(topElem, &temp), pop(SP), topElem = top(*SP)) { 71 | printf("%c ", topElem); 72 | } 73 | printf("\n"); 74 | 75 | for (topElem = top(temp); !isEmpty(temp); push(topElem, SP), pop(&temp), topElem = top(temp)) {} 76 | } 77 | 78 | int allocSpace(STACK *SP) { 79 | int res = SP->VH.Avail; 80 | 81 | if (res != -1) { 82 | SP->VH.Avail = SP->VH.Nodes[res].link; 83 | } 84 | 85 | return res; 86 | } 87 | void deallocSpace(int ndx, STACK *SP) { 88 | if (ndx > -1 && ndx < MAX) { 89 | SP->VH.Nodes[ndx].link = SP->VH.Avail; 90 | SP->VH.Avail = ndx; 91 | } 92 | } -------------------------------------------------------------------------------- /examples/stacks/modules/linked-list/v1/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_LINKEDLIST_V1 2 | #define STACK_LINKEDLIST_V1 3 | 4 | typedef struct node { 5 | char elem; 6 | struct node* link; 7 | } NodeType, *STACK; 8 | 9 | void initialize(STACK *SP); 10 | void push(char elem, STACK *SP); 11 | void pop(STACK *SP); 12 | char top(STACK S); 13 | int isEmpty(STACK S); 14 | void displayStack(STACK* SP); 15 | 16 | #endif -------------------------------------------------------------------------------- /examples/stacks/modules/linked-list/v1/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | 5 | void initialize(STACK *SP) { 6 | *SP = NULL; 7 | } 8 | 9 | void push(char elem, STACK *SP) { 10 | NodeType *node = (NodeType*)malloc(sizeof(NodeType)); 11 | 12 | if (node != NULL) { 13 | node->elem = elem; 14 | node->link = *SP; 15 | *SP = node; 16 | } 17 | } 18 | 19 | void pop(STACK *SP) { 20 | if (*SP != NULL) { 21 | NodeType *node = *SP; 22 | *SP = node->link; 23 | free(node); 24 | } 25 | } 26 | 27 | char top(STACK S) { 28 | char res = '\0'; 29 | 30 | if (!isEmpty(S)) { 31 | res = S->elem; 32 | } 33 | 34 | return res; 35 | } 36 | 37 | int isEmpty(STACK S) { 38 | return S == NULL; 39 | } 40 | 41 | void displayStack(STACK* SP) { 42 | STACK temp; 43 | char elem; 44 | 45 | initialize(&temp); 46 | printf("STACK: "); 47 | for (elem = top(*SP); !isEmpty(*SP); pop(SP), push(elem, &temp), elem = top(*SP)) { 48 | printf("%c ", elem); 49 | } 50 | printf("\n"); 51 | 52 | for (elem = top(temp); !isEmpty(temp); pop(&temp), push(elem, SP), elem = top(temp)) {} 53 | } 54 | -------------------------------------------------------------------------------- /examples/stacks/modules/linked-list/v2/_header.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_LINKEDLIST_V2 2 | #define STACK_LINKEDLIST_V2 3 | #include 4 | 5 | typedef struct node { 6 | char data; 7 | struct node *link; 8 | } Node, *STACK; 9 | 10 | void initialize(STACK *SP); 11 | void push(char elem, STACK *SP); 12 | void insertBottom(char elem, STACK *SP); 13 | void pop(STACK *SP); 14 | char top(STACK S); 15 | bool isEmpty(STACK S); 16 | void displayStack(STACK* SP); 17 | 18 | #endif -------------------------------------------------------------------------------- /examples/stacks/modules/linked-list/v2/def.c: -------------------------------------------------------------------------------- 1 | #include "_header.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void initialize(STACK *SP) { 7 | *SP = NULL; 8 | } 9 | 10 | void push(char elem, STACK *SP) { 11 | Node *temp = (Node *)malloc(sizeof(Node)); 12 | 13 | if (temp != NULL) { 14 | temp->data = elem; 15 | temp->link = *SP; 16 | *SP = temp; 17 | } 18 | } 19 | 20 | void insertBottom(char elem, STACK *SP) { 21 | STACK temp; 22 | initialize(&temp); 23 | 24 | for (; !isEmpty(*SP); push(top(*SP), &temp), pop(SP)) {} 25 | 26 | push(elem, SP); 27 | 28 | for (; !isEmpty(temp); push(top(temp), SP), pop(&temp)) {} 29 | } 30 | 31 | void pop(STACK *SP) { 32 | if (!isEmpty(*SP)) { 33 | Node *temp = *SP; 34 | *SP = temp->link; 35 | free(temp); 36 | } 37 | } 38 | 39 | char top(STACK S) { 40 | char res = -1; 41 | 42 | if (!isEmpty(S)) { 43 | res = S->data; 44 | } 45 | 46 | return res; 47 | } 48 | 49 | bool isEmpty(STACK S) { 50 | return S == NULL; 51 | } 52 | 53 | void displayStack(STACK* SP) { 54 | STACK temp; 55 | initialize(&temp); 56 | char elem; 57 | 58 | printf("STACK: "); 59 | for (elem = top(*SP); !isEmpty(*SP); push(elem, &temp), pop(SP), elem = top(*SP)) { 60 | printf("%c ", elem); 61 | } 62 | printf("\n"); 63 | 64 | for (elem = top(temp); !isEmpty(temp); push(elem, SP), pop(&temp), elem = top(temp)) {} 65 | } --------------------------------------------------------------------------------