├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENCE ├── README.md ├── fractions ├── .gitignore ├── README.md ├── build │ └── Makefile ├── src │ ├── fractions.c │ └── fractions.h └── tests │ ├── Makefile │ └── units.c ├── images └── logo.png ├── macros_data_structs ├── .gitignore ├── README.md ├── documentation │ ├── MAVL.md │ ├── MBST.md │ ├── MDLIST.md │ ├── MLIST.md │ ├── MPQUEUE.md │ ├── MQUEUE.md │ ├── MRBK.md │ └── MSTACK.md ├── examples │ ├── README.md │ ├── mavl │ │ ├── Makefile │ │ ├── example1.c │ │ ├── example2.c │ │ ├── example2.in │ │ └── example3.c │ ├── mbst │ │ ├── Makefile │ │ ├── example1.c │ │ ├── example2.c │ │ ├── example2.in │ │ └── example3.c │ ├── mdlist │ │ ├── Makefile │ │ ├── example1.c │ │ ├── example2.c │ │ ├── example2.in │ │ └── example3.c │ ├── mlist │ │ ├── Makefile │ │ ├── example1.c │ │ ├── example2.c │ │ ├── example2.in │ │ └── example3.c │ ├── mpqueue │ │ ├── Makefile │ │ ├── example1.c │ │ ├── example1.in │ │ └── example2.c │ ├── mqueue │ │ ├── Makefile │ │ ├── example1.c │ │ ├── example2.c │ │ └── example3.c │ ├── mrbk │ │ ├── Makefile │ │ ├── example1.c │ │ ├── example2.c │ │ ├── example2.in │ │ └── example3.c │ └── mstack │ │ ├── Makefile │ │ ├── example1.c │ │ ├── example2.c │ │ └── example3.c └── src │ ├── m_avl.h │ ├── m_bst.h │ ├── m_config.h │ ├── m_dlist.h │ ├── m_list.h │ ├── m_pqueue.h │ ├── m_queue.h │ ├── m_rbk.h │ └── m_stack.h └── voidptr_data_structs ├── .gitignore ├── README.md ├── build └── Makefile ├── documentation ├── AVL_TREE.md ├── BST_TREE.md ├── DOUBLE_LINKED_LIST.md ├── FUNCTION_TYPES.md ├── GRAPH.md ├── HASH_TABLE.md ├── PRIORITY_QUEUE.md ├── QUEUE.md ├── RED_BLACK_TREE.md ├── SINGLE_LINKED_LIST.md ├── SORT_ALGORITHMS.md └── STACK.md ├── examples ├── README.md ├── avl_tree │ ├── Makefile │ ├── example1.c │ ├── example2.c │ ├── example2.in │ └── example3.c ├── bst_tree │ ├── Makefile │ ├── example1.c │ ├── example2.c │ ├── example2.in │ └── example3.c ├── dlist │ ├── Makefile │ ├── example1.c │ ├── example2.c │ ├── example2.in │ └── example3.c ├── list │ ├── Makefile │ ├── example1.c │ ├── example2.c │ ├── example2.in │ └── example3.c ├── priority_queue │ ├── Makefile │ ├── example1.c │ ├── example1.in │ └── example2.c ├── queue │ ├── Makefile │ ├── example1.c │ ├── example2.c │ └── example3.c ├── red_black_tree │ ├── Makefile │ ├── example1.c │ ├── example2.c │ ├── example2.in │ └── example3.c ├── sort_algorithms │ ├── Makefile │ └── example1.c └── stack │ ├── Makefile │ ├── example1.c │ ├── example2.c │ └── example3.c └── src ├── include ├── scl_avl_tree.h ├── scl_bst_tree.h ├── scl_config.h ├── scl_datastruc.h ├── scl_dlist.h ├── scl_func_types.h ├── scl_graph.h ├── scl_hash_table.h ├── scl_list.h ├── scl_priority_queue.h ├── scl_queue.h ├── scl_red_black_tree.h ├── scl_sort_algo.h └── scl_stack.h ├── scl_avl_tree.c ├── scl_bst_tree.c ├── scl_config.c ├── scl_dlist.c ├── scl_func_types.c ├── scl_graph.c ├── scl_hash_table.c ├── scl_list.c ├── scl_priority_queue.c ├── scl_queue.c ├── scl_red_black_tree.c ├── scl_sort_algo.c └── scl_stack.c /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | 2 | # Contributor Covenant Code of Conduct 3 | 4 | ## Our Pledge 5 | 6 | In the interest of fostering an open and welcoming environment, we as 7 | contributors and maintainers pledge to make participation in our project and 8 | our community a harassment-free experience for everyone, regardless of age, body 9 | size, disability, ethnicity, sex characteristics, gender identity and expression, 10 | level of experience, education, socio-economic status, nationality, personal 11 | appearance, race, religion, or sexual identity and orientation. 12 | 13 | ## Our Standards 14 | 15 | Examples of behavior that contributes to creating a positive environment 16 | include: 17 | 18 | * Using welcoming and inclusive language 19 | * Being respectful of differing viewpoints and experiences 20 | * Gracefully accepting constructive criticism 21 | * Focusing on what is best for the community 22 | * Showing empathy towards other community members 23 | 24 | Examples of unacceptable behavior by participants include: 25 | 26 | * The use of sexualized language or imagery and unwelcome sexual attention or 27 | advances 28 | * Trolling, insulting/derogatory comments, and personal or political attacks 29 | * Public or private harassment 30 | * Publishing others' private information, such as a physical or electronic 31 | address, without explicit permission 32 | * Other conduct which could reasonably be considered inappropriate in a 33 | professional setting 34 | 35 | ## Our Responsibilities 36 | 37 | Project maintainers are responsible for clarifying the standards of acceptable 38 | behavior and are expected to take appropriate and fair corrective action in 39 | response to any instances of unacceptable behavior. 40 | 41 | Project maintainers have the right and responsibility to remove, edit, or 42 | reject comments, commits, code, wiki edits, issues, and other contributions 43 | that are not aligned to this Code of Conduct, or to ban temporarily or 44 | permanently any contributor for other behaviors that they deem inappropriate, 45 | threatening, offensive, or harmful. 46 | 47 | ## Scope 48 | 49 | This Code of Conduct applies within all project spaces, and it also applies when 50 | an individual is representing the project or its community in public spaces. 51 | Examples of representing a project or community include using an official 52 | project e-mail address, posting via an official social media account, or acting 53 | as an appointed representative at an online or offline event. Representation of 54 | a project may be further defined and clarified by project maintainers. 55 | 56 | ## Enforcement 57 | 58 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 59 | reported by contacting the project team at [INSERT EMAIL ADDRESS]. All 60 | complaints will be reviewed and investigated and will result in a response that 61 | is deemed necessary and appropriate to the circumstances. The project team is 62 | obligated to maintain confidentiality with regard to the reporter of an incident. 63 | Further details of specific enforcement policies may be posted separately. 64 | 65 | Project maintainers who do not follow or enforce the Code of Conduct in good 66 | faith may face temporary or permanent repercussions as determined by other 67 | members of the project's leadership. 68 | 69 | ## Attribution 70 | 71 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 72 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 73 | 74 | [homepage]: https://www.contributor-covenant.org 75 | 76 | For answers to common questions about this code of conduct, see 77 | https://www.contributor-covenant.org/faq 78 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # c-language-data-structures 2 | 3 | ![Projec Logo](images/logo.png) 4 | 5 | [![GitHub issues](https://img.shields.io/github/issues/mihai-negru/c-language-data-structures?label=Issues)](https://github.com/mihai-negru/c-language-data-structures/issues) 6 | [![GitHub forks](https://img.shields.io/github/forks/mihai-negru/c-language-data-structures?label=Forks)](https://github.com/mihai-negru/c-language-data-structures/network) 7 | [![GitHub stars](https://img.shields.io/github/stars/mihai-negru/c-language-data-structures?label=Stars)](https://github.com/mihai-negru/c-language-data-structures/stargazers) 8 | [![GitHub license](https://img.shields.io/github/license/mihai-negru/c-language-data-structures?label=License)](https://github.com/mihai-negru/c-language-data-structures/blob/master/LICENCE) 9 | 10 | This project is a collection of small objects that consist into a big library. 11 | 12 | The project is divided in two main sub-projects: 13 | 14 | * *[Void Pointer data structures](voidptr_data_structs/README.md)* - which are generic data structures implemented using the void pointer. 15 | * *[Macros data structures](macros_data_structs/README.md)* - which are the void pointer tructures reimplemented using macros. 16 | * *[Fractions codification](fractions/README.md)* - which are an implementation of the fractions using the numerator and denumerator, in order to preserve data loss at every operation. 17 | 18 | ## Getting Started 19 | 20 | In orther to use the c-language-data-structures project, make sure to navigate to the readmes of every project appart and to take step by step every documentation of a different data struction, even though they practically represent the same idea, the implementations and the examples are quite different from structure to structure. 21 | 22 | ## Contributing 23 | 24 | I am very open for contributing, however if you want to take part of this project you will have to follow some set of rules: 25 | 26 | * First you should respect the code styling, some references are [C Style](https://github.com/mcinglis/c-style) 27 | * You should provide documentation (respecting doxygen standard) for every function or piece of code 28 | * You should come with some tests or examples to show how to work with your new functions 29 | * Be happy and don't stop coding 30 | -------------------------------------------------------------------------------- /fractions/.gitignore: -------------------------------------------------------------------------------- 1 | test.c 2 | test 3 | units 4 | 5 | libs -------------------------------------------------------------------------------- /fractions/README.md: -------------------------------------------------------------------------------- 1 | # fractions project 2 | 3 | ## Getting Started 4 | 5 | This project represents a collection of basic operations on fractions. The problem that this project tries to solve is that upon dividing, multiplying and other operations, a lot of information is lost on every step, now the project api covers the most used functions for fractions, and keep the double data as fractions. 6 | 7 | For example *0.5*, will be codified as *(1, 2, plus)* 8 | 9 | Every double number can be represented as a tuple of numerator, denumerator and sign. 10 | 11 | **If you do not care about** aproximations and data loss, then you should not use the *frac_t* structure. The structure stores 2 unsigned ints on 32 bits and enum on 1 byte, so the total ammount of memory at most is 3 bytes, which is only a byte more than the double data type, however the frac structure can store upon an infinite amount of digits after the comma, by encoding the fractions as shown before. 12 | 13 | The API consists in the following functions: 14 | 15 | * *fxy* - creates a fraction. 16 | * *feval* - evauluates a fraction as double. 17 | * *fadd* - adds two fractions and returns the result. 18 | * *fsub* - subtracts two fractions and returns the result. 19 | * *fmul* - multiplies two fractions and returns the result. 20 | * *fdiv* - divides two fractions and returns the result. 21 | * *fmconst* - multiplies a fraction with a constant. 22 | * *fdconst* - divides a fraction with a constant. 23 | 24 | The API also comes with *pointer* functions, that do the same thing, however upates the first fraction instead of returning a new fraction. 25 | 26 | The methods are: 27 | 28 | * *faddp* - adds two fractions and returns the result. 29 | * *fsubp* - subtracts two fractions and returns the result. 30 | * *fmulp* - multiplies two fractions and returns the result. 31 | * *fdivp* - divides two fractions and returns the result. 32 | * *fmconstp* - multiplies a fraction with a constant. 33 | * *fdconstp* - divides a fraction with a constant. 34 | 35 | ## Building the project 36 | 37 | The project can be compiled in: 38 | 39 | * *dynamic library* 40 | * *static library* 41 | 42 | The builder of the project will create the both of them and you can choose which to use. 43 | 44 | **In order** to compile the project, enter the prompt of an terminal and change directory inside the `c-language-data-structures`. 45 | 46 | ```bash 47 | cd path/to/dir/c-language-data-structures 48 | ``` 49 | 50 | Enter the `fractions` folder and enter the `build` directory: 51 | 52 | ```bash 53 | cd fractions 54 | cd build 55 | ``` 56 | 57 | Build the project by running the `make` utility: 58 | 59 | ```bash 60 | make 61 | ``` 62 | 63 | In the root of the *fractions* project a `libs` folder will appear, which will contain the dynamic and static libraries. 64 | 65 | ## Testing the project 66 | 67 | This project comes with a wide unit testing file. 68 | 69 | In order to test the functionalities of the fractions API and to see some examples of how to use the fractions in your future projects, enter the *tests* folder and run *make* to run the tests: 70 | 71 | ```bash 72 | cd tests 73 | make 74 | ``` 75 | 76 | The libraries will be deleted if there was a previous *build* and the makefile will call again the *make* from the build folder and will start the tests, that can be found under the **units.c** file. 77 | 78 | ## Infinity and NaN 79 | 80 | The fractions API allows for *infinity* and *not a number* principles. All the operators test if the number is Nan or infinity and performs the action regarding the mathematics principles, like **inf + inf = inf** 81 | 82 | I encourge you to look upon the tests, because they discribe very well for every method what are the corner cases, and how can infinity and nan be used. 83 | 84 | ## fbool 85 | 86 | Because we are working with infinity and NaN, we need a new layer of abstraction, some functions like comparing and equalities, would not know what the result can be *true* or *false*. 87 | 88 | For example: 89 | 90 | ```text 91 | inf == inf -> this calls and indeterminated operation, because we can reduce it to the following expression: 92 | 93 | inf - inf == 0, as we know the inf - inf can be any number including zero, so this statement has not a truth value, so in exchange we return a `funknown` state. 94 | ``` 95 | 96 | A loot of examples like this are covered in the test cases. 97 | -------------------------------------------------------------------------------- /fractions/build/Makefile: -------------------------------------------------------------------------------- 1 | CC := gcc 2 | CFLAGS := -g -Wall -Wextra -Wpedantic \ 3 | -Wformat=2 -Wno-unused-parameter \ 4 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 5 | -Wold-style-definition -Wredundant-decls \ 6 | -Wnested-externs -Wmissing-include-dirs \ 7 | -Wjump-misses-init -Wlogical-op -O2 8 | 9 | PADDING := ............................................................ 10 | 11 | SRC_PATH := ../src 12 | SRC_FILES := $(wildcard $(SRC_PATH)/*.c) 13 | 14 | LIBS_PATH := ../libs 15 | 16 | DYNAMIC_LIB_NAME := libfrac.so 17 | DYNAMIC_LIB := $(LIBS_PATH)/$(DYNAMIC_LIB_NAME) 18 | DYNAMIC_OBJ_PATH := dynamic_obj 19 | DYNAMIC_OFILES := $(patsubst $(SRC_PATH)/%.c,$(DYNAMIC_OBJ_PATH)/%.o,$(SRC_FILES)) 20 | 21 | STATIC_LIB_NAME := libfrac.a 22 | STATIC_LIB := $(LIBS_PATH)/$(STATIC_LIB_NAME) 23 | STATIC_OBJ_PATH := static_obj 24 | STATIC_OFILES := $(patsubst $(SRC_PATH)/%.c,$(STATIC_OBJ_PATH)/%.o,$(SRC_FILES)) 25 | 26 | .PHONY: all build clean_o clean 27 | 28 | all: header_print clean build clean_o 29 | @printf "finishing project " 30 | @printf "%0.21s" $(PADDING) 31 | @printf "\033[0;32m" 32 | @printf "%s\n" " passed" 33 | @printf "\033[0m" 34 | @printf "%0.46s\n\n" $(PADDING) 35 | 36 | header_print: 37 | @printf "%0.17s" $(PADDING) 38 | @printf "\033[0;32m FRAC BUILD \033[0m" 39 | @printf "%0.17s\n" $(PADDING) 40 | 41 | build: $(LIBS_PATH) $(DYNAMIC_OBJ_PATH) $(DYNAMIC_LIB) $(STATIC_OBJ_PATH) $(STATIC_LIB) 42 | 43 | $(LIBS_PATH): 44 | @mkdir -p $@ 45 | 46 | $(DYNAMIC_OBJ_PATH): 47 | @mkdir -p $@ 48 | 49 | $(DYNAMIC_LIB): $(DYNAMIC_OFILES) 50 | @$(CC) -shared -o $@ $^ 51 | 52 | @printf "%s" "building dynamic library" 53 | @printf "%0.15s" $(PADDING) 54 | @printf "\033[0;32m" 55 | @printf "%s\n" " passed" 56 | @printf "\033[0m" 57 | 58 | $(DYNAMIC_OBJ_PATH)/%.o: $(SRC_PATH)/%.c 59 | @gcc -fPIC $(CFLAGS) -o $@ -c $< 60 | 61 | @printf "%s" "building dynamic $(shell basename $@ .o)-file" 62 | @file=$@ && printf "%0.$$((31 - $${#file}))s" $(PADDING) 63 | @printf "\033[0;32m" 64 | @printf "%s\n" " passed" 65 | @printf "\033[0m" 66 | 67 | $(STATIC_OBJ_PATH): 68 | @mkdir -p $@ 69 | 70 | $(STATIC_LIB): $(STATIC_OFILES) 71 | @ar -rc $@ $^ 72 | 73 | @printf "%s" "building static library " 74 | @printf "%0.15s" $(PADDING) 75 | @printf "\033[0;32m" 76 | @printf "%s\n" " passed" 77 | @printf "\033[0m" 78 | 79 | $(STATIC_OBJ_PATH)/%.o: $(SRC_PATH)/%.c 80 | @gcc $(CFLAGS) -o $@ -c $< 81 | 82 | @printf "%s" "building static $(shell basename $@ .o)-file " 83 | @file=$@ && printf "%0.$$((30 - $${#file}))s" $(PADDING) 84 | @printf "\033[0;32m" 85 | @printf "%s\n" " passed" 86 | @printf "\033[0m" 87 | 88 | clean_o: 89 | @rm -rf $(DYNAMIC_OBJ_PATH) $(STATIC_OBJ_PATH) 90 | 91 | clean: 92 | @rm -rf $(DYNAMIC_OBJ_PATH) $(STATIC_OBJ_PATH) $(LIBS_PATH) 93 | -------------------------------------------------------------------------------- /fractions/tests/Makefile: -------------------------------------------------------------------------------- 1 | CC := gcc 2 | CFLAGS := -g -Wall -Wextra -Wpedantic \ 3 | -Wformat=2 -Wno-unused-parameter \ 4 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 5 | -Wold-style-definition -Wredundant-decls \ 6 | -Wnested-externs -Wmissing-include-dirs \ 7 | -Wjump-misses-init -Wlogical-op -O2 8 | 9 | MAKEFLAGS += --no-print-directory 10 | 11 | all: create_lib run_test destroy_lib clean 12 | 13 | run_test: units 14 | @./units 15 | 16 | create_lib: 17 | @make -C ../build 18 | 19 | destroy_lib: 20 | @make -C ../build clean 21 | 22 | units: units.o 23 | @$(CC) -static units.o -L../libs -lfrac -o units 24 | 25 | units.o: units.c 26 | @$(CC) $(CFLAGS) -c units.c 27 | 28 | clean: 29 | @rm -rf units.o units 30 | -------------------------------------------------------------------------------- /images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mihai-negru/c-language-data-structures/d0f1ae2710cb1fc1ecbaf20bb399088150d08066/images/logo.png -------------------------------------------------------------------------------- /macros_data_structs/.gitignore: -------------------------------------------------------------------------------- 1 | test.c 2 | test 3 | *.txt -------------------------------------------------------------------------------- /macros_data_structs/README.md: -------------------------------------------------------------------------------- 1 | # macro generic data structures 2 | 3 | ![Projec Logo](../images/logo.png) 4 | 5 | ## **Table of Contents** 6 | 7 | 1. [`Getting Started`](#getting-started) 8 | 2. [`Using the library`](#using-the-library) 9 | 3. [`Running examples`](#running-examples) 10 | 11 | ## **Getting Started** 12 | 13 | Project "c-language-data-structures" has come as an idea to supply C Language with some basic **generic** data structures, as follows: 14 | 15 | | Content/documentation | Header/Source Macro File | 16 | | :------------- | :---------: | 17 | | [AVL Tree](documentation/MAVL.md) | [m_avl.h](src/m_avl.h) | 18 | | [Binary Search Tree](documentation/MBST.md) | [m_bst.h](src/m_bst.h) | 19 | | Config File (Basic Utils for Error Handling) | [m_config.h](src/m_config.h) | 20 | | [Double Linked List](documentation/MDLIST.md) | [m_dlist.h](src/m_dlist.h) | 21 | | [Single Linked List](documentation/MLIST.md) | [m_list.h](src/m_list.h) | 22 | | [Priority Queue](documentation/MPQUEUE.md) | [m_pqueue.h](src/m_pqueue.h) | 23 | | [Queue](documentation/MQUEUE.md) | [m_queue.h](src/m_queue.h) | 24 | | [Red Black Tree](documentation/MRBK.md) | [m_rbk.h](src/m_rbk.h) | 25 | | [Stack](documentation/MSTACK.md) | [m_stack.h](src/m_stack.h) | 26 | 27 | Every single **data structure** from this project can be used in any scopes and with **different** data types, however you must follow a set of rules so you don't break the program. 28 | Every set of rules for every data structure can be found in [documentation](documentation/) folder from current project. 29 | 30 | I tried for every known object to keep their function definitions as in **C++** and also followed the standard naming of methods according to specific data structure. 31 | 32 | **It is very important** not to use directly the members of the structure objects, however if you feel safe to access them by yourself there should be no problem. 33 | 34 | ### **Dependencies** 35 | 36 | * Linux, MacOS or WSL System 37 | * GCC Compiler 38 | 39 | ## **Using the library** 40 | 41 | The only thing is to copy the *header* file of the desired data structure into your project into a specified directory, 42 | then to add to a file by: 43 | 44 | ```c 45 | #include "/path/to/file/m_avl.h" // or any other m_name.h 46 | ``` 47 | 48 | As you may have observed all the methods return a **merr_t** type, which is used for error handling. 49 | 50 | ```c 51 | // For example 52 | M_OK - when the method executed successfully 53 | M_NULL_INPUT - when the input is null 54 | ... 55 | 56 | // In order to get a message for any error you have to call the macro 57 | 58 | merr_t err = function_from_library(of_some_input); 59 | 60 | if (err != M_OK) { 61 | MERROR(err); // Which will print to stderr the error and the meaning of it 62 | } 63 | ``` 64 | 65 | ## **Running examples** 66 | 67 | Some data structures have some **examples** for you to undestand what you should and what you should not. Also the examples 68 | will show you some **untraditional** ways to solve some things. In every example folder for different data structure exists one 69 | source file that will be a benchmark to test the power of data structures on your system. The benchmarks can run a little slow 70 | depending on your system. 71 | 72 | For examples how to build and how to run the examples you must read the documentation related to the examples section. 73 | The link to documentation **[HERE](examples/README.md)**. 74 | 75 | Make sure after you are done with one data structure exaxmple to run: 76 | 77 | ```BASH 78 | make clean 79 | ``` 80 | -------------------------------------------------------------------------------- /macros_data_structs/examples/README.md: -------------------------------------------------------------------------------- 1 | # How to run the examples of a specific data structure ? 2 | 3 | First change the directory to the specific data structure, for example: 4 | 5 | ```BASH 6 | cd mavl 7 | ``` 8 | 9 | In every data structure example directory you willl find one **Makefile** and some source files labeled as **example1.c** and so on. 10 | 11 | If you want just to execute one example, for example the first one you should type in your terminal: 12 | 13 | ```BASH 14 | make e1 15 | ``` 16 | 17 | If you want to execute all examples you shall type in your terminal: 18 | 19 | ```BASH 20 | make 21 | ``` 22 | 23 | >**NOTE:** I recommend you to execute all of examples at once. 24 | >**NOTE:** After every successfully executed example a message will be prompt in the terminal to inform you that execution went successfully 25 | 26 | After compiling and running the examples some files will be generated in current working directory, such as: 27 | 28 | * `out_**X**.txt` - where **X** is the number of the example, all the output generated by the example number **X** will be redirected in that file. 29 | 30 | * `valgrind_log_**X**.txt` - where **X** is the number of the example, the valgrind output log information will be redirected in that file. 31 | 32 | If you want to remove all the **txt** files and to clear all the directory from junk files you shall run: 33 | 34 | ```BASH 35 | make clean 36 | ``` 37 | 38 | >**NOTE:** Same actions you can do in any example data structure directory from **/examples/** directory 39 | >**NOTE:** Some examples are benchmarks, usually example number **3**, so do not worry if the running time is a bit to much. 40 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mavl/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | VALGRIND_FLAGS += --leak-check=full \ 9 | --show-leak-kinds=all \ 10 | --track-origins=yes 11 | 12 | VALGRIND += valgrind $(VALGRIND_FLAGS) 13 | 14 | .PHONY: build e1 e2 e3 clean 15 | 16 | build: e1 e2 e3 17 | 18 | e1: example1 19 | @printf "Run MAVL example 1: " 20 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 21 | @printf "Done\n" 22 | @rm -rf example1 23 | 24 | e2: example2 25 | @printf "Run MAVL example 2: " 26 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 27 | @printf "Done\n" 28 | @rm -rf example2 29 | 30 | e3: example3 31 | @printf "Run MAVL example 3: " 32 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 33 | @printf "Done\n" 34 | @rm -rf example3 35 | 36 | example%: example%.c 37 | @gcc $(CFLAGS) $< -o $@ 38 | 39 | clean: 40 | @rm -rf *.txt example1 example2 example3 41 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mavl/example1.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_avl.h" 2 | #include 3 | #include 4 | 5 | int32_t compare_int(const int *const a, const int *const b) { return *a - *b; } 6 | 7 | void print_int(const int *const a) { printf(" %d", *a); } 8 | 9 | MAVL_ALL(test, int) 10 | 11 | int main(void) { 12 | FILE *fout = NULL; 13 | 14 | if ((fout = freopen("out_1.txt", "w", stdout)) == NULL) { 15 | exit(EXIT_FAILURE); 16 | } 17 | 18 | test_mavl_t my_tree = test_mavl(&compare_int, NULL); 19 | 20 | if (NULL == my_tree) { 21 | printf("AVL tree was not allocated\n"); 22 | exit(EXIT_FAILURE); 23 | } 24 | 25 | srand(time(NULL)); 26 | 27 | merr_t err = M_OK; 28 | 29 | size_t number_of_elem = 100; 30 | 31 | printf("Generated data:\n"); 32 | for (size_t i = 1; i <= number_of_elem; ++i) { 33 | if (i % 10 == 0) { 34 | printf("\n"); 35 | } 36 | 37 | int data = rand() % 2000 + 100; 38 | 39 | printf("%d ", data); 40 | 41 | err = test_mavl_push(my_tree, data); 42 | 43 | if (M_OK != err) { 44 | MERROR(err); 45 | } 46 | } 47 | printf("\n"); 48 | 49 | if (test_mavl_empty(my_tree) == mtrue) { 50 | printf("Something went really wrong try once again\n"); 51 | exit(EXIT_FAILURE); 52 | } 53 | 54 | printf("\nThe AVL tree printed by postorder method is:\n"); 55 | test_mavl_traverse_postorder(my_tree, &print_int); 56 | printf("\n\n"); 57 | 58 | printf("In the avl tree there exist %lu unique random integers\n\n", 59 | test_mavl_size(my_tree)); 60 | 61 | int root_data; 62 | err = test_mavl_root(my_tree, &root_data); 63 | 64 | if (err == M_OK) { 65 | printf("The root value of the avl tree is %d\n", root_data); 66 | 67 | int min_max = 0; 68 | test_mavl_max(my_tree, root_data, &min_max); 69 | printf("The maximum random number generated is %d\n", min_max); 70 | 71 | test_mavl_min(my_tree, root_data, &min_max); 72 | printf("The minimum random number generated is %d\n", min_max); 73 | 74 | int root_pred, root_succ; 75 | 76 | if (test_mavl_pred(my_tree, root_data, &root_pred) == M_OK) { 77 | printf("The inorder predecessor of the root value is %d\n", root_pred); 78 | } else { 79 | printf("The root node has no predecessor\n"); 80 | } 81 | 82 | if (test_mavl_succ(my_tree, root_data, &root_succ) == M_OK) { 83 | printf("The inorder successor of the root value is %d\n\n", root_succ); 84 | } else { 85 | printf("The root node has no successor\n\n"); 86 | } 87 | 88 | int anc; 89 | if (test_mavl_lca(my_tree, root_pred, root_succ, &anc) == M_OK) { 90 | printf("The lowest common ancestor for the predecessor and successor of " 91 | "the root is %d\n\n", 92 | anc); 93 | } else { 94 | printf("Could not find the lowest comon ancestor for predecessor and " 95 | "successor of the root node\n\n"); 96 | } 97 | } else { 98 | printf("Could not find the root node\n\n"); 99 | } 100 | 101 | printf("Let's remove some elements:\n"); 102 | 103 | for (size_t i = 0; i < 10; ++i) { 104 | int del; 105 | 106 | if (test_mavl_root(my_tree, &del) == M_OK) { 107 | printf("Removing %d value from AVL\n", del); 108 | 109 | err = test_mavl_pop(my_tree, del); 110 | 111 | if (err != M_OK) { 112 | MERROR(err); 113 | } 114 | } 115 | } 116 | 117 | printf("\n"); 118 | 119 | printf("After deletion the AVL tree shows like (inorder):\n"); 120 | test_mavl_traverse_inorder(my_tree, &print_int); 121 | printf("\n\n"); 122 | 123 | fclose(fout); 124 | test_mavl_free(&my_tree); 125 | 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mavl/example2.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_avl.h" 2 | #include 3 | #include 4 | 5 | #define MAX_NAME 20 6 | #define MAX_SURNAME 30 7 | 8 | typedef struct student_s { 9 | char *name; 10 | char *surname; 11 | unsigned int age; 12 | unsigned int class; 13 | unsigned char class_label; 14 | double GPA; 15 | int index; 16 | } student_t; 17 | 18 | int32_t compare_student(const student_t *const s1, const student_t *const s2) { 19 | int cmp = strncmp(s1->name, s2->name, MAX_NAME); 20 | 21 | if (0 != cmp) { 22 | return cmp; 23 | } 24 | 25 | cmp = strncmp(s1->surname, s2->surname, MAX_SURNAME); 26 | 27 | if (0 != cmp) { 28 | return cmp; 29 | } 30 | 31 | if (s1->GPA > s2->GPA) { 32 | cmp = 1; 33 | } else if (s1->GPA < s2->GPA) { 34 | cmp = -1; 35 | } else { 36 | cmp = 0; 37 | } 38 | 39 | if (cmp == 0) { 40 | return s1->index - s2->index; 41 | } 42 | 43 | return cmp; 44 | } 45 | 46 | void free_student(student_t *st) { 47 | if (NULL != st) { 48 | 49 | free(st->name); 50 | st->name = NULL; 51 | 52 | free(st->surname); 53 | st->surname = NULL; 54 | 55 | st->age = 0; 56 | st->class = 0; 57 | st->class_label = '\0'; 58 | st->GPA = 0.0; 59 | st->index = -1; 60 | } 61 | } 62 | 63 | void print_student(const student_t *const st) { 64 | if (NULL != st) { 65 | printf("Student no. %d\n", st->index); 66 | printf("NAME: %s\n", st->name); 67 | printf("SURNAME: %s\n", st->surname); 68 | printf("Student GPA: %lf\n", st->GPA); 69 | printf("Student age: %u\n\n", st->age); 70 | } 71 | } 72 | 73 | MAVL_ALL(test, student_t) 74 | 75 | int main(void) { 76 | FILE *fin = NULL; 77 | 78 | if ((fin = freopen("example2.in", "r", stdin)) == NULL) { 79 | exit(EXIT_FAILURE); 80 | } 81 | 82 | test_mavl_t data_base = test_mavl(&compare_student, &free_student); 83 | 84 | if (NULL == data_base) { 85 | exit(EXIT_FAILURE); 86 | } 87 | 88 | merr_t err = M_OK; 89 | 90 | int number_of_students = 0; 91 | int read = scanf("%d", &number_of_students); 92 | 93 | if (read > 1) { 94 | exit(EXIT_FAILURE); 95 | } 96 | 97 | student_t st; // Encapsulate the reading 98 | 99 | for (int i = 0; i < number_of_students; ++i) { 100 | st.name = malloc(MAX_NAME); 101 | st.surname = malloc(MAX_SURNAME); 102 | 103 | read = scanf("%s %s", st.name, st.surname); 104 | 105 | if (read > 2) { 106 | exit(EXIT_FAILURE); 107 | } 108 | 109 | read = scanf("%u %u %c %lf", &st.age, &st.class, &st.class_label, &st.GPA); 110 | 111 | if (read > 4) { 112 | exit(EXIT_FAILURE); 113 | } 114 | 115 | st.index = i; 116 | 117 | err = test_mavl_push(data_base, st); 118 | 119 | if (M_OK != err) { 120 | MERROR(err); 121 | } 122 | } 123 | 124 | fclose(fin); 125 | 126 | FILE *fout = NULL; 127 | 128 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 129 | exit(EXIT_FAILURE); 130 | } 131 | 132 | printf("In the data base are %lu students\n\n", test_mavl_size(data_base)); 133 | 134 | test_mavl_traverse_inorder(data_base, &print_student); 135 | printf("\n\n"); 136 | 137 | fclose(fout); 138 | 139 | test_mavl_free(&data_base); 140 | 141 | return 0; 142 | } 143 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mavl/example2.in: -------------------------------------------------------------------------------- 1 | 40 2 | Rahul Vickers 3 | 14 12 A 4.0 4 | Ranveer Ramirez 5 | 15 12 B 3.7 6 | Bluebell Lawrence 7 | 14 10 C 1.3 8 | Avni Sharma 9 | 15 9 A 0.5 10 | Georgie Beltran 11 | 17 11 B 4.0 12 | Avery Pitt 13 | 19 20 C 4.0 14 | Jimmie Dalton 15 | 15 22 A 3.2 16 | Tazmin Skinner 17 | 16 43 B 3.1 18 | Tyla Dolan 19 | 14 21 C 3.0 20 | Meadow Mendoza 21 | 18 23 A 2.1 22 | Connah Farmer 23 | 19 12 B 2.5 24 | Aurora Sadler 25 | 15 22 C 2.8 26 | Cairon Ratliff 27 | 16 10 A 3.8 28 | Marie Mair 29 | 17 11 B 4.0 30 | Findlay Small 31 | 13 24 C 3.8 32 | Jazmine Avery 33 | 13 54 A 3.8 34 | Naomi Lara 35 | 16 21 B 1.1 36 | Nasir Mccormick 37 | 17 45 C 2.4 38 | Glen Becker 39 | 18 22 A 2.2 40 | Brent Coates 41 | 19 12 B 3.6 42 | Brielle Carson 43 | 14 10 C 4.0 44 | Lucy Joyner 45 | 13 11 A 3.7 46 | Rebekka Shepard 47 | 14 12 B 3.7 48 | Keaton Traynor 49 | 13 13 C 3.7 50 | Sufyaan Espinoza 51 | 13 14 A 2.2 52 | Zaina Webb 53 | 18 15 B 3.4 54 | Roxy Stevenson 55 | 19 16 C 4.0 56 | Donovan Blevins 57 | 19 17 A 1.1 58 | Phoenix Snow 59 | 17 18 B 1.9 60 | Agnes Doyle 61 | 19 19 C 2.0 62 | Tonisha Solis 63 | 17 20 A 3.0 64 | Aariz Allen 65 | 14 21 B 4.0 66 | Weronika Ho 67 | 16 22 C 2.0 68 | Milton Greaves 69 | 14 22 A 2.2 70 | Kwame Gilmore 71 | 14 22 B 2.5 72 | Eamon West 73 | 17 43 C 2.9 74 | Willem Kennedy 75 | 17 12 A 3.1 76 | Ben Chan 77 | 13 12 B 1.9 78 | Cherie Ochoa 79 | 15 43 C 2.2 80 | Priyanka Talley 81 | 18 12 A 4.0 -------------------------------------------------------------------------------- /macros_data_structs/examples/mavl/example3.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_avl.h" 2 | #include 3 | #include 4 | 5 | int32_t compare_int(const int *const a, const int *const b) { return *a - *b; } 6 | 7 | MAVL_ALL(test, int) 8 | 9 | int main(void) { 10 | FILE *fout = NULL; 11 | 12 | if ((fout = freopen("out_3.txt", "w", stdout)) == NULL) { 13 | exit(EXIT_FAILURE); 14 | } 15 | 16 | printf("In this example we will try to do different operations on AVL Tree " 17 | "and measure the time\n"); 18 | 19 | /* Init the working strucutres */ 20 | double exec_time = 0.0; 21 | 22 | test_mavl_t my_tree = test_mavl(&compare_int, NULL); 23 | /* End to init the working structures */ 24 | 25 | /* Insert 100 ints into the avl and count the time */ 26 | clock_t begin = clock(); 27 | 28 | for (int i = 0; i < 100; ++i) { 29 | test_mavl_push(my_tree, i); 30 | } 31 | 32 | clock_t end = clock(); 33 | 34 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 35 | printf("Inserting 100 ints into avl: %lf sec\n", exec_time); 36 | /* End to insert 100 ints into the avl */ 37 | 38 | /* Delete 100 roots from avl and count the time */ 39 | begin = clock(); 40 | 41 | for (int i = 0; i < 100; ++i) { 42 | test_mavl_pop(my_tree, my_tree->root->data); 43 | } 44 | 45 | end = clock(); 46 | 47 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 48 | printf("Deleting 100 ints roots from avl: %lf sec\n", exec_time); 49 | /* End to delete 100 roots from avl */ 50 | 51 | /* Free avl tree with size of 100 and count time */ 52 | for (int i = 0; i < 100; ++i) { 53 | test_mavl_push(my_tree, i); 54 | } 55 | 56 | begin = clock(); 57 | 58 | test_mavl_free(&my_tree); 59 | 60 | end = clock(); 61 | 62 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 63 | printf("Freeing avl with 100 ints: %lf sec\n\n", exec_time); 64 | 65 | /* End to free avl tree with size of 100 */ 66 | 67 | ////////////////////////////////////////////////////////////// Increase size 1 68 | 69 | my_tree = test_mavl(&compare_int, NULL); 70 | 71 | /* Insert 100000 ints into the avl and count the time */ 72 | begin = clock(); 73 | 74 | for (int i = 0; i < 100000; ++i) { 75 | test_mavl_push(my_tree, i); 76 | } 77 | 78 | end = clock(); 79 | 80 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 81 | printf("Inserting 100000 ints into avl: %lf sec\n", exec_time); 82 | /* End to insert 100000 ints into the avl */ 83 | 84 | /* Delete 100000 roots from avl and count the time */ 85 | begin = clock(); 86 | 87 | for (int i = 0; i < 100000; ++i) { 88 | test_mavl_pop(my_tree, my_tree->root->data); 89 | } 90 | 91 | end = clock(); 92 | 93 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 94 | printf("Deleting 100000 ints roots from avl: %lf sec\n", exec_time); 95 | /* End to delete 100000 roots from avl */ 96 | 97 | /* Free avl tree with size of 100000 and count time */ 98 | for (int i = 0; i < 100000; ++i) { 99 | test_mavl_push(my_tree, i); 100 | } 101 | 102 | begin = clock(); 103 | 104 | test_mavl_free(&my_tree); 105 | 106 | end = clock(); 107 | 108 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 109 | printf("Freeing avl with 100000 ints: %lf sec\n\n", exec_time); 110 | 111 | /* End to free avl tree with size of 100000 */ 112 | 113 | ////////////////////////////////////////////////////////////// Increase size 2 114 | 115 | my_tree = test_mavl(&compare_int, NULL); 116 | 117 | /* Insert 8000000 ints into the avl and count the time */ 118 | begin = clock(); 119 | 120 | for (int i = 0; i < 8000000; ++i) { 121 | test_mavl_push(my_tree, i); 122 | } 123 | 124 | end = clock(); 125 | 126 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 127 | printf("Inserting 8000000 ints into avl: %lf sec\n", exec_time); 128 | /* End to insert 8000000 ints into the avl */ 129 | 130 | /* Delete 8000000 roots from avl and count the time */ 131 | begin = clock(); 132 | 133 | for (int i = 0; i < 8000000; ++i) { 134 | test_mavl_pop(my_tree, my_tree->root->data); 135 | } 136 | 137 | end = clock(); 138 | 139 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 140 | printf("Deleting 8000000 ints roots from avl: %lf sec\n", exec_time); 141 | /* End to delete 8000000 roots from avl */ 142 | 143 | /* Free avl tree with size of 8000000 and count time */ 144 | for (int i = 0; i < 8000000; ++i) { 145 | test_mavl_push(my_tree, i); 146 | } 147 | 148 | begin = clock(); 149 | 150 | test_mavl_free(&my_tree); 151 | 152 | end = clock(); 153 | 154 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 155 | printf("Freeing avl with 8000000 ints: %lf sec\n", exec_time); 156 | 157 | /* End to free avl tree with size of 8000000 */ 158 | 159 | /* Close output file */ 160 | fclose(fout); 161 | 162 | return 0; 163 | } 164 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mbst/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | VALGRIND_FLAGS += --leak-check=full \ 9 | --show-leak-kinds=all \ 10 | --track-origins=yes 11 | 12 | VALGRIND += valgrind $(VALGRIND_FLAGS) 13 | 14 | .PHONY: build e1 e2 e3 clean 15 | 16 | build: e1 e2 e3 17 | 18 | e1: example1 19 | @printf "Run MBST example 1: " 20 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 21 | @printf "Done\n" 22 | @rm -rf example1 23 | 24 | e2: example2 25 | @printf "Run MBST example 2: " 26 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 27 | @printf "Done\n" 28 | @rm -rf example2 29 | 30 | e3: example3 31 | @printf "Run MBST example 3: " 32 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 33 | @printf "Done\n" 34 | @rm -rf example3 35 | 36 | example%: example%.c 37 | @gcc $(CFLAGS) $< -o $@ 38 | 39 | clean: 40 | @rm -rf *.txt example1 example2 example3 41 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mbst/example1.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_bst.h" 2 | #include 3 | #include 4 | 5 | int32_t compare_int(const int *const a, const int *const b) { return *a - *b; } 6 | 7 | void print_int(const int *const a) { printf(" %d", *a); } 8 | 9 | MBST_ALL(test, int) 10 | 11 | int main(void) { 12 | FILE *fout = NULL; 13 | 14 | if ((fout = freopen("out_1.txt", "w", stdout)) == NULL) { 15 | exit(EXIT_FAILURE); 16 | } 17 | 18 | test_mbst_t my_tree = test_mbst(&compare_int, NULL); 19 | 20 | if (NULL == my_tree) { 21 | printf("BST tree was not allocated\n"); 22 | exit(EXIT_FAILURE); 23 | } 24 | 25 | srand(time(NULL)); 26 | 27 | merr_t err = M_OK; 28 | 29 | size_t number_of_elem = 100; 30 | 31 | printf("Generated data:\n"); 32 | for (size_t i = 1; i <= number_of_elem; ++i) { 33 | if (i % 10 == 0) { 34 | printf("\n"); 35 | } 36 | 37 | int data = rand() % 2000 + 100; 38 | 39 | printf("%d ", data); 40 | 41 | err = test_mbst_push(my_tree, data); 42 | 43 | if (M_OK != err) { 44 | MERROR(err); 45 | } 46 | } 47 | printf("\n"); 48 | 49 | if (test_mbst_empty(my_tree) == mtrue) { 50 | printf("Something went really wrong try once again\n"); 51 | exit(EXIT_FAILURE); 52 | } 53 | 54 | printf("\nThe BST tree printed by postorder method is:\n"); 55 | test_mbst_traverse_postorder(my_tree, &print_int); 56 | printf("\n\n"); 57 | 58 | printf("In the BST tree there exist %lu unique random integers\n\n", 59 | test_mbst_size(my_tree)); 60 | 61 | int root_data; 62 | err = test_mbst_root(my_tree, &root_data); 63 | 64 | if (err == M_OK) { 65 | printf("The root value of the BST tree is %d\n", root_data); 66 | 67 | int min_max = 0; 68 | test_mbst_max(my_tree, root_data, &min_max); 69 | printf("The maximum random number generated is %d\n", min_max); 70 | 71 | test_mbst_min(my_tree, root_data, &min_max); 72 | printf("The minimum random number generated is %d\n", min_max); 73 | 74 | int root_pred, root_succ; 75 | 76 | if (test_mbst_pred(my_tree, root_data, &root_pred) == M_OK) { 77 | printf("The inorder predecessor of the root value is %d\n", root_pred); 78 | } else { 79 | printf("The root node has no predecessor\n"); 80 | } 81 | 82 | if (test_mbst_succ(my_tree, root_data, &root_succ) == M_OK) { 83 | printf("The inorder successor of the root value is %d\n\n", root_succ); 84 | } else { 85 | printf("The root node has no successor\n\n"); 86 | } 87 | 88 | int anc; 89 | if (test_mbst_lca(my_tree, root_pred, root_succ, &anc) == M_OK) { 90 | printf("The lowest common ancestor for the predecessor and successor of " 91 | "the root is %d\n\n", 92 | anc); 93 | } else { 94 | printf("Could not find the lowest comon ancestor for predecessor and " 95 | "successor of the root node\n\n"); 96 | } 97 | } else { 98 | printf("Could not find the root node\n\n"); 99 | } 100 | 101 | printf("Let's remove some elements:\n"); 102 | 103 | for (size_t i = 0; i < 10; ++i) { 104 | int del; 105 | 106 | if (test_mbst_root(my_tree, &del) == M_OK) { 107 | printf("Removing %d value from BST\n", del); 108 | 109 | err = test_mbst_pop(my_tree, del); 110 | 111 | if (err != M_OK) { 112 | MERROR(err); 113 | } 114 | } 115 | } 116 | 117 | printf("\n"); 118 | 119 | printf("After deletion the BST tree shows like (inorder):\n"); 120 | test_mbst_traverse_inorder(my_tree, &print_int); 121 | printf("\n\n"); 122 | 123 | fclose(fout); 124 | test_mbst_free(&my_tree); 125 | 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mbst/example2.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_bst.h" 2 | #include 3 | #include 4 | 5 | #define MAX_NAME 20 6 | #define MAX_SURNAME 30 7 | 8 | typedef struct student_s { 9 | char *name; 10 | char *surname; 11 | unsigned int age; 12 | unsigned int class; 13 | unsigned char class_label; 14 | double GPA; 15 | int index; 16 | } student_t; 17 | 18 | int32_t compare_student(const student_t *const s1, const student_t *const s2) { 19 | int cmp = strncmp(s1->name, s2->name, MAX_NAME); 20 | 21 | if (0 != cmp) { 22 | return cmp; 23 | } 24 | 25 | cmp = strncmp(s1->surname, s2->surname, MAX_SURNAME); 26 | 27 | if (0 != cmp) { 28 | return cmp; 29 | } 30 | 31 | if (s1->GPA > s2->GPA) { 32 | cmp = 1; 33 | } else if (s1->GPA < s2->GPA) { 34 | cmp = -1; 35 | } else { 36 | cmp = 0; 37 | } 38 | 39 | if (cmp == 0) { 40 | return s1->index - s2->index; 41 | } 42 | 43 | return cmp; 44 | } 45 | 46 | void free_student(student_t *st) { 47 | if (NULL != st) { 48 | 49 | free(st->name); 50 | st->name = NULL; 51 | 52 | free(st->surname); 53 | st->surname = NULL; 54 | 55 | st->age = 0; 56 | st->class = 0; 57 | st->class_label = '\0'; 58 | st->GPA = 0.0; 59 | st->index = -1; 60 | } 61 | } 62 | 63 | void print_student(const student_t *const st) { 64 | if (NULL != st) { 65 | printf("Student no. %d\n", st->index); 66 | printf("NAME: %s\n", st->name); 67 | printf("SURNAME: %s\n", st->surname); 68 | printf("Student GPA: %lf\n", st->GPA); 69 | printf("Student age: %u\n\n", st->age); 70 | } 71 | } 72 | 73 | MBST_ALL(test, student_t) 74 | 75 | int main(void) { 76 | FILE *fin = NULL; 77 | 78 | if ((fin = freopen("example2.in", "r", stdin)) == NULL) { 79 | exit(EXIT_FAILURE); 80 | } 81 | 82 | test_mbst_t data_base = test_mbst(&compare_student, &free_student); 83 | 84 | if (NULL == data_base) { 85 | exit(EXIT_FAILURE); 86 | } 87 | 88 | merr_t err = M_OK; 89 | 90 | int number_of_students = 0; 91 | int read = scanf("%d", &number_of_students); 92 | 93 | if (read > 1) { 94 | exit(EXIT_FAILURE); 95 | } 96 | 97 | student_t st; // Encapsulate the reading 98 | 99 | for (int i = 0; i < number_of_students; ++i) { 100 | st.name = malloc(MAX_NAME); 101 | st.surname = malloc(MAX_SURNAME); 102 | 103 | read = scanf("%s %s", st.name, st.surname); 104 | 105 | if (read > 2) { 106 | exit(EXIT_FAILURE); 107 | } 108 | 109 | read = scanf("%u %u %c %lf", &st.age, &st.class, &st.class_label, &st.GPA); 110 | 111 | if (read > 4) { 112 | exit(EXIT_FAILURE); 113 | } 114 | 115 | st.index = i; 116 | 117 | err = test_mbst_push(data_base, st); 118 | 119 | if (M_OK != err) { 120 | MERROR(err); 121 | } 122 | } 123 | 124 | fclose(fin); 125 | 126 | FILE *fout = NULL; 127 | 128 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 129 | exit(EXIT_FAILURE); 130 | } 131 | 132 | printf("In the data base are %lu students\n\n", test_mbst_size(data_base)); 133 | 134 | test_mbst_traverse_inorder(data_base, &print_student); 135 | printf("\n\n"); 136 | 137 | fclose(fout); 138 | 139 | test_mbst_free(&data_base); 140 | 141 | return 0; 142 | } 143 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mbst/example2.in: -------------------------------------------------------------------------------- 1 | 40 2 | Rahul Vickers 3 | 14 12 A 4.0 4 | Ranveer Ramirez 5 | 15 12 B 3.7 6 | Bluebell Lawrence 7 | 14 10 C 1.3 8 | Avni Sharma 9 | 15 9 A 0.5 10 | Georgie Beltran 11 | 17 11 B 4.0 12 | Avery Pitt 13 | 19 20 C 4.0 14 | Jimmie Dalton 15 | 15 22 A 3.2 16 | Tazmin Skinner 17 | 16 43 B 3.1 18 | Tyla Dolan 19 | 14 21 C 3.0 20 | Meadow Mendoza 21 | 18 23 A 2.1 22 | Connah Farmer 23 | 19 12 B 2.5 24 | Aurora Sadler 25 | 15 22 C 2.8 26 | Cairon Ratliff 27 | 16 10 A 3.8 28 | Marie Mair 29 | 17 11 B 4.0 30 | Findlay Small 31 | 13 24 C 3.8 32 | Jazmine Avery 33 | 13 54 A 3.8 34 | Naomi Lara 35 | 16 21 B 1.1 36 | Nasir Mccormick 37 | 17 45 C 2.4 38 | Glen Becker 39 | 18 22 A 2.2 40 | Brent Coates 41 | 19 12 B 3.6 42 | Brielle Carson 43 | 14 10 C 4.0 44 | Lucy Joyner 45 | 13 11 A 3.7 46 | Rebekka Shepard 47 | 14 12 B 3.7 48 | Keaton Traynor 49 | 13 13 C 3.7 50 | Sufyaan Espinoza 51 | 13 14 A 2.2 52 | Zaina Webb 53 | 18 15 B 3.4 54 | Roxy Stevenson 55 | 19 16 C 4.0 56 | Donovan Blevins 57 | 19 17 A 1.1 58 | Phoenix Snow 59 | 17 18 B 1.9 60 | Agnes Doyle 61 | 19 19 C 2.0 62 | Tonisha Solis 63 | 17 20 A 3.0 64 | Aariz Allen 65 | 14 21 B 4.0 66 | Weronika Ho 67 | 16 22 C 2.0 68 | Milton Greaves 69 | 14 22 A 2.2 70 | Kwame Gilmore 71 | 14 22 B 2.5 72 | Eamon West 73 | 17 43 C 2.9 74 | Willem Kennedy 75 | 17 12 A 3.1 76 | Ben Chan 77 | 13 12 B 1.9 78 | Cherie Ochoa 79 | 15 43 C 2.2 80 | Priyanka Talley 81 | 18 12 A 4.0 -------------------------------------------------------------------------------- /macros_data_structs/examples/mbst/example3.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_bst.h" 2 | #include 3 | #include 4 | 5 | int32_t compare_int(const int *const a, const int *const b) { return *a - *b; } 6 | 7 | MBST_ALL(test, int) 8 | 9 | int main(void) { 10 | FILE *fout = NULL; 11 | 12 | if ((fout = freopen("out_3.txt", "w", stdout)) == NULL) { 13 | exit(EXIT_FAILURE); 14 | } 15 | 16 | printf("In this example we will try to do different operations on BST Tree " 17 | "and measure the time\n"); 18 | 19 | /* Init the working strucutres */ 20 | double exec_time = 0.0; 21 | 22 | test_mbst_t my_tree = test_mbst(&compare_int, NULL); 23 | /* End to init the working structures */ 24 | 25 | /* Insert 100 ints into the BST and count the time */ 26 | clock_t begin = clock(); 27 | 28 | for (int i = 0; i < 100; ++i) { 29 | test_mbst_push(my_tree, i); 30 | } 31 | 32 | clock_t end = clock(); 33 | 34 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 35 | printf("Inserting 100 ints into BST: %lf sec\n", exec_time); 36 | /* End to insert 100 ints into the BST */ 37 | 38 | /* Delete 100 roots from BST and count the time */ 39 | begin = clock(); 40 | 41 | for (int i = 0; i < 100; ++i) { 42 | test_mbst_pop(my_tree, my_tree->root->data); 43 | } 44 | 45 | end = clock(); 46 | 47 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 48 | printf("Deleting 100 ints roots from BST: %lf sec\n", exec_time); 49 | /* End to delete 100 roots from BST */ 50 | 51 | /* Free BST tree with size of 100 and count time */ 52 | for (int i = 0; i < 100; ++i) { 53 | test_mbst_push(my_tree, i); 54 | } 55 | 56 | begin = clock(); 57 | 58 | test_mbst_free(&my_tree); 59 | 60 | end = clock(); 61 | 62 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 63 | printf("Freeing BST with 100 ints: %lf sec\n\n", exec_time); 64 | 65 | /* End to free BST tree with size of 100 */ 66 | 67 | ////////////////////////////////////////////////////////////// Increase size 1 68 | 69 | my_tree = test_mbst(&compare_int, NULL); 70 | 71 | /* Insert 100000 ints into the BST and count the time */ 72 | begin = clock(); 73 | 74 | for (int i = 0; i < 100000; ++i) { 75 | test_mbst_push(my_tree, i); 76 | } 77 | 78 | end = clock(); 79 | 80 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 81 | printf("Inserting 100000 ints into BST: %lf sec\n", exec_time); 82 | /* End to insert 100000 ints into the BST */ 83 | 84 | /* Delete 100000 roots from BST and count the time */ 85 | begin = clock(); 86 | 87 | for (int i = 0; i < 100000; ++i) { 88 | test_mbst_pop(my_tree, my_tree->root->data); 89 | } 90 | 91 | end = clock(); 92 | 93 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 94 | printf("Deleting 100000 ints roots from BST: %lf sec\n", exec_time); 95 | /* End to delete 100000 roots from BST */ 96 | 97 | /* Free BST tree with size of 100000 and count time */ 98 | for (int i = 0; i < 100000; ++i) { 99 | test_mbst_push(my_tree, i); 100 | } 101 | 102 | begin = clock(); 103 | 104 | test_mbst_free(&my_tree); 105 | 106 | end = clock(); 107 | 108 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 109 | printf("Freeing BST with 100000 ints: %lf sec\n\n", exec_time); 110 | 111 | /* End to free BST tree with size of 100000 */ 112 | 113 | ////////////////////////////////////////////////////////////// Increase size 2 114 | 115 | my_tree = test_mbst(&compare_int, NULL); 116 | 117 | /* Insert 8000000 ints into the BST and count the time */ 118 | begin = clock(); 119 | 120 | for (int i = 0; i < 8000000; ++i) { 121 | test_mbst_push(my_tree, i); 122 | } 123 | 124 | end = clock(); 125 | 126 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 127 | printf("Inserting 8000000 ints into BST: %lf sec\n", exec_time); 128 | /* End to insert 8000000 ints into the BST */ 129 | 130 | /* Delete 8000000 roots from BST and count the time */ 131 | begin = clock(); 132 | 133 | for (int i = 0; i < 8000000; ++i) { 134 | test_mbst_pop(my_tree, my_tree->root->data); 135 | } 136 | 137 | end = clock(); 138 | 139 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 140 | printf("Deleting 8000000 ints roots from BST: %lf sec\n", exec_time); 141 | /* End to delete 8000000 roots from BST */ 142 | 143 | /* Free BST tree with size of 8000000 and count time */ 144 | for (int i = 0; i < 8000000; ++i) { 145 | test_mbst_push(my_tree, i); 146 | } 147 | 148 | begin = clock(); 149 | 150 | test_mbst_free(&my_tree); 151 | 152 | end = clock(); 153 | 154 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 155 | printf("Freeing BST with 8000000 ints: %lf sec\n", exec_time); 156 | 157 | /* End to free BST tree with size of 8000000 */ 158 | 159 | /* Close output file */ 160 | fclose(fout); 161 | 162 | return 0; 163 | } 164 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mdlist/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | VALGRIND_FLAGS += --leak-check=full \ 9 | --show-leak-kinds=all \ 10 | --track-origins=yes 11 | 12 | VALGRIND += valgrind $(VALGRIND_FLAGS) 13 | 14 | .PHONY: build e1 e2 e3 clean 15 | 16 | build: e1 e2 e3 17 | 18 | e1: example1 19 | @printf "Run MDLIST example 1: " 20 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 21 | @printf "Done\n" 22 | @rm -rf example1 23 | 24 | e2: example2 25 | @printf "Run MDLIST example 2: " 26 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 27 | @printf "Done\n" 28 | @rm -rf example2 29 | 30 | e3: example3 31 | @printf "Run MDLIST example 3: " 32 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 33 | @printf "Done\n" 34 | @rm -rf example3 35 | 36 | example%: example%.c 37 | @gcc $(CFLAGS) $< -o $@ 38 | 39 | clean: 40 | @rm -rf *.txt example1 example2 example3 41 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mdlist/example2.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_dlist.h" 2 | #include 3 | 4 | #define MAX_STRING_SIZE 100 5 | 6 | typedef char *str; 7 | 8 | int32_t compare_ptr_str(const str *const elem1, const str *const elem2) { 9 | return strncmp(*elem1, *elem2, MAX_STRING_SIZE); 10 | } 11 | 12 | void free_str(str *data) { 13 | if (NULL != data) { 14 | free(*data); 15 | *data = '\0'; 16 | } 17 | } 18 | 19 | void print_str(const str *const data) { 20 | if (NULL != data) { 21 | printf(" %s\n", *data); 22 | } 23 | } 24 | 25 | MDLIST_ALL(test, str) 26 | 27 | int main(void) { 28 | FILE *fout = NULL; 29 | 30 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 31 | exit(EXIT_FAILURE); 32 | } 33 | 34 | FILE *fin = NULL; 35 | 36 | if ((fin = freopen("example2.in", "r", stdin)) == NULL) { 37 | printf("Could not open for reading\n"); 38 | exit(EXIT_FAILURE); 39 | } 40 | 41 | merr_t err = M_OK; 42 | 43 | fseek(fin, 0, SEEK_SET); 44 | 45 | test_mdlist_t strings = test_mdlist(&compare_ptr_str, &free_str); 46 | 47 | size_t str_num = 0; 48 | int check = 0; 49 | 50 | check = scanf("%lu", &str_num); 51 | 52 | if (check > 1) { 53 | exit(EXIT_FAILURE); 54 | } 55 | 56 | for (size_t i = 0; i < str_num; ++i) { 57 | char *ptr_data = malloc(MAX_STRING_SIZE); 58 | check = scanf("%s", ptr_data); 59 | 60 | if (check > 1) { 61 | exit(EXIT_FAILURE); 62 | } 63 | 64 | err = test_mdlist_push(strings, ptr_data); 65 | 66 | if (M_OK != err) { 67 | MERROR(err); 68 | } 69 | } 70 | 71 | fclose(fin); 72 | 73 | printf("Print strings dlist:\n"); 74 | test_mdlist_traverse(strings, &print_str); 75 | printf("\n"); 76 | 77 | char *find; 78 | if (test_mdlist_find_idx(strings, 4, &find) == M_OK) { 79 | printf("String from pos 4 is %s\n", find); 80 | } 81 | 82 | test_mdlist_free(&strings); 83 | 84 | fclose(fin); 85 | fclose(fout); 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mdlist/example2.in: -------------------------------------------------------------------------------- 1 | 20 2 | Davila 3 | Jacobs 4 | Hobbs 5 | Schmidt 6 | Ochoa 7 | Harding 8 | Benjamin 9 | Lawson 10 | Mcpherson 11 | Fleming 12 | Mcintosh 13 | Lozano 14 | Patrick 15 | Pratt 16 | Powers 17 | Mack 18 | Simmons 19 | Herman 20 | Walter 21 | Kennedy -------------------------------------------------------------------------------- /macros_data_structs/examples/mdlist/example3.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_dlist.h" 2 | #include 3 | #include 4 | 5 | MDLIST_ALL(test, int) 6 | 7 | int32_t compare_int(const int *const a, const int *const b) { return *a - *b; } 8 | 9 | int main(void) { 10 | FILE *fout = NULL; 11 | 12 | if ((fout = freopen("out_3.txt", "w", stdout)) == NULL) { 13 | exit(EXIT_FAILURE); 14 | } 15 | 16 | printf("In this example we will try to do different operations on Double " 17 | "linked lists and measure the time\n\n"); 18 | 19 | /* Init the working strucutres */ 20 | double exec_time = 0.0; 21 | 22 | test_mdlist_t ll = test_mdlist(&compare_int, NULL); 23 | /* End to init the working structures */ 24 | 25 | /* Insert 100 ints into the dlist and count the time */ 26 | clock_t begin = clock(); 27 | 28 | for (int i = 0; i < 100; ++i) { 29 | test_mdlist_push(ll, i); 30 | } 31 | 32 | clock_t end = clock(); 33 | 34 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 35 | printf("Inserting 100 ints into dlist: %lf sec\n", exec_time); 36 | /* End to insert 100 ints into the dlist */ 37 | 38 | /* Delete 100 heads from dlist and count the time */ 39 | begin = clock(); 40 | 41 | for (int i = 0; i < 100; ++i) { 42 | test_mdlist_pop(ll, ll->head->data); 43 | } 44 | 45 | end = clock(); 46 | 47 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 48 | printf("Deleting 100 ints heads from dlist: %lf sec\n", exec_time); 49 | /* End to delete 100 heads from dlist */ 50 | 51 | /* Free dlist with size of 100 and count time */ 52 | for (int i = 0; i < 100; ++i) { 53 | test_mdlist_push(ll, i); 54 | } 55 | 56 | begin = clock(); 57 | 58 | test_mdlist_free(&ll); 59 | 60 | end = clock(); 61 | 62 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 63 | printf("Freeing dlist with 100 ints: %lf sec\n\n", exec_time); 64 | 65 | /* End to free dlist with size of 100 */ 66 | 67 | ////////////////////////////////////////////////////////////// Increase size 1 68 | 69 | ll = test_mdlist(&compare_int, NULL); 70 | 71 | /* Insert 100000 ints into the dlist and count the time */ 72 | begin = clock(); 73 | 74 | for (int i = 0; i < 100000; ++i) { 75 | test_mdlist_push(ll, i); 76 | } 77 | 78 | end = clock(); 79 | 80 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 81 | printf("Inserting 100000 ints into dlist: %lf sec\n", exec_time); 82 | /* End to insert 100000 ints into the dlist */ 83 | 84 | /* Delete 100000 heads from dlist and count the time */ 85 | begin = clock(); 86 | 87 | for (int i = 0; i < 100000; ++i) { 88 | test_mdlist_pop(ll, ll->head->data); 89 | } 90 | 91 | end = clock(); 92 | 93 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 94 | printf("Deleting 100000 ints heads from dlist: %lf sec\n", exec_time); 95 | /* End to delete 100000 heads from dlist */ 96 | 97 | /* Free dlist with size of 100000 and count time */ 98 | for (int i = 0; i < 100000; ++i) { 99 | test_mdlist_push(ll, i); 100 | } 101 | 102 | begin = clock(); 103 | 104 | test_mdlist_free(&ll); 105 | 106 | end = clock(); 107 | 108 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 109 | printf("Freeing dlist with 100000 ints: %lf sec\n\n", exec_time); 110 | 111 | /* End to free dlist with size of 100000 */ 112 | 113 | ////////////////////////////////////////////////////////////// Increase size 2 114 | 115 | ll = test_mdlist(&compare_int, NULL); 116 | 117 | /* Insert 8000000 ints into the dlist and count the time */ 118 | begin = clock(); 119 | 120 | for (int i = 0; i < 8000000; ++i) { 121 | test_mdlist_push(ll, i); 122 | } 123 | 124 | end = clock(); 125 | 126 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 127 | printf("Inserting 8000000 ints into dlist: %lf sec\n", exec_time); 128 | /* End to insert 8000000 ints into the dlist */ 129 | 130 | /* Delete 8000000 heads from dlist and count the time */ 131 | begin = clock(); 132 | 133 | for (int i = 0; i < 8000000; ++i) { 134 | test_mdlist_pop(ll, ll->head->data); 135 | } 136 | 137 | end = clock(); 138 | 139 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 140 | printf("Deleting 8000000 ints heads from dlist: %lf sec\n", exec_time); 141 | /* End to delete 8000000 heads from dlist */ 142 | 143 | /* Free dlist with size of 8000000 and count time */ 144 | for (int i = 0; i < 8000000; ++i) { 145 | test_mdlist_push(ll, i); 146 | } 147 | 148 | begin = clock(); 149 | 150 | test_mdlist_free(&ll); 151 | 152 | end = clock(); 153 | 154 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 155 | printf("Freeing dlist with 8000000 ints: %lf sec\n", exec_time); 156 | 157 | /* End to free dlist with size of 8000000 */ 158 | 159 | /* Close output file */ 160 | fclose(fout); 161 | 162 | return 0; 163 | } 164 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mlist/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | VALGRIND_FLAGS += --leak-check=full \ 9 | --show-leak-kinds=all \ 10 | --track-origins=yes 11 | 12 | VALGRIND += valgrind $(VALGRIND_FLAGS) 13 | 14 | .PHONY: build e1 e2 e3 clean 15 | 16 | build: e1 e2 e3 17 | 18 | e1: example1 19 | @printf "Run MLIST example 1: " 20 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 21 | @printf "Done\n" 22 | @rm -rf example1 23 | 24 | e2: example2 25 | @printf "Run MLIST example 2: " 26 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 27 | @printf "Done\n" 28 | @rm -rf example2 29 | 30 | e3: example3 31 | @printf "Run MLIST example 3: " 32 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 33 | @printf "Done\n" 34 | @rm -rf example3 35 | 36 | example%: example%.c 37 | @gcc $(CFLAGS) $< -o $@ 38 | 39 | clean: 40 | @rm -rf *.txt example1 example2 example3 41 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mlist/example1.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_list.h" 2 | #include 3 | #include 4 | 5 | int32_t compare_int(const int *const a, const int *const b) { return *a - *b; } 6 | 7 | void print_int(const int *const a) { printf(" %d", *a); } 8 | 9 | mbool_t fil(const int *const data) { 10 | if (NULL != data) { 11 | return (*data % 2) == 1; 12 | } 13 | 14 | return 0; 15 | } 16 | 17 | int map(const int *const data) { return (*data) * (*data); } 18 | 19 | MLIST_ALL(test, int) 20 | 21 | int main(void) { 22 | FILE *fout = NULL; 23 | 24 | if ((fout = freopen("out_1.txt", "w", stdout)) == NULL) { 25 | exit(EXIT_FAILURE); 26 | } 27 | 28 | test_mlist_t ll = test_mlist(&compare_int, NULL); 29 | 30 | if (NULL == ll) { 31 | printf("Something went wrong on creation\n"); 32 | fclose(fout); 33 | 34 | exit(EXIT_FAILURE); 35 | } 36 | 37 | srand(time(NULL)); 38 | 39 | merr_t err = M_OK; 40 | 41 | printf("Inserting some random numbers:\n"); 42 | for (int i = 0; i < 10; ++i) { 43 | int data = rand() % 100 + 10; 44 | 45 | err = test_mlist_push(ll, data); 46 | 47 | if (M_OK != err) { 48 | MERROR(err); 49 | } 50 | } 51 | 52 | test_mlist_traverse(ll, &print_int); 53 | printf("\n\n"); 54 | 55 | printf("Inserting some random numbers in fornt of the dlist:\n"); 56 | for (int i = 0; i < 10; ++i) { 57 | int data = rand() % 100 + 10; 58 | 59 | err = test_mlist_push_front(ll, data); 60 | 61 | if (err != M_OK) { 62 | MERROR(err); 63 | } 64 | } 65 | 66 | test_mlist_traverse(ll, &print_int); 67 | printf("\n\n"); 68 | 69 | printf("Inserting some random number in order:\n"); 70 | for (int i = 0; i < 10; ++i) { 71 | int data = rand() % 100 + 10; 72 | 73 | err = test_mlist_push_order(ll, data); 74 | 75 | if (err != M_OK) { 76 | MERROR(err); 77 | } 78 | } 79 | 80 | test_mlist_traverse(ll, &print_int); 81 | printf("\n\n"); 82 | 83 | printf("Inserting some random number at different positions:\n"); 84 | for (int i = 0; i < 10; ++i) { 85 | int data = rand() % 100 + 10; 86 | size_t index = rand() % test_mlist_size(ll); 87 | 88 | err = test_mlist_push_idx(ll, data, index); 89 | 90 | if (err != M_OK) { 91 | MERROR(err); 92 | } 93 | } 94 | 95 | test_mlist_traverse(ll, &print_int); 96 | printf("\n\n"); 97 | 98 | if (mtrue == test_mlist_empty(ll)) { 99 | printf("Something went wrong\n"); 100 | test_mlist_free(&ll); 101 | fclose(fout); 102 | 103 | exit(EXIT_FAILURE); 104 | } 105 | 106 | int head = 0, tail = 0; 107 | 108 | if (test_mlist_head(ll, &head) == M_OK) { 109 | printf("The head of the dlist is: %d\n", head); 110 | } else { 111 | printf("The head could not be fetched\n"); 112 | } 113 | 114 | if (test_mlist_tail(ll, &tail) == M_OK) { 115 | printf("The tail of the dlist is: %d\n\n", tail); 116 | } else { 117 | printf("The tail could not be fetched\n\n"); 118 | } 119 | 120 | printf("Let's swap head with te tail:\n"); 121 | err = test_mlist_swap(ll, head, tail); 122 | 123 | if (M_OK != err) { 124 | MERROR(err); 125 | } 126 | 127 | test_mlist_traverse(ll, &print_int); 128 | printf("\n\n"); 129 | 130 | printf("Now we will erase a good chunk from dlist from [10, 40] range:\n"); 131 | err = test_mlist_erase(ll, 10, 20); 132 | 133 | if (err != M_OK) { 134 | MERROR(err); 135 | } 136 | 137 | test_mlist_traverse(ll, &print_int); 138 | printf("\n\n"); 139 | 140 | int find1 = rand() % 100 + 10; 141 | int find2 = rand() % 100 + 10; 142 | 143 | printf("I want to find %d and %d in the dlist:\n", find1, find2); 144 | 145 | if (test_mlist_find(ll, find1, NULL) == M_OK) { 146 | printf("I found %d\n", find1); 147 | } else { 148 | printf("I did not find the %d value in current dlist\n", find1); 149 | } 150 | 151 | if (test_mlist_find(ll, find2, NULL) == M_OK) { 152 | printf("I found %d\n\n", find2); 153 | } else { 154 | printf("I did not find the %d value in current dlist\n\n", find2); 155 | } 156 | 157 | if (test_mlist_find_idx(ll, 4, &find1) == M_OK) { 158 | printf("Data element from index 4 is %d\n", find1); 159 | } else { 160 | printf("Could not fetch index 4's data\n"); 161 | } 162 | 163 | if (test_mlist_find_idx(ll, 420, &find2) == M_OK) { 164 | printf("Data element from index 420 is %d\n", find2); 165 | } else { 166 | printf("Could not fetch index 420's data\n\n"); 167 | } 168 | 169 | test_mlist_t odd_dlist = test_mlist_filter(ll, &fil); 170 | 171 | printf("Created a new dlist with just odd elements:\n"); 172 | test_mlist_traverse(odd_dlist, &print_int); 173 | printf("\n\n"); 174 | 175 | test_mlist_t square = 176 | test_to_test_mlist_map(odd_dlist, &map, &compare_int, NULL); 177 | printf("Let's find the square number of odd elements:\n"); 178 | test_mlist_traverse(square, &print_int); 179 | printf("\n"); 180 | 181 | // As seen here is used one traverse call function to modify and to print the 182 | // values 183 | 184 | test_mlist_free(&ll); 185 | test_mlist_free(&odd_dlist); 186 | test_mlist_free(&square); 187 | 188 | fclose(fout); 189 | 190 | return 0; 191 | } 192 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mlist/example2.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_list.h" 2 | #include 3 | 4 | #define MAX_STRING_SIZE 100 5 | 6 | typedef char *str; 7 | 8 | int32_t compare_ptr_str(const str *const elem1, const str *const elem2) { 9 | return strncmp(*elem1, *elem2, MAX_STRING_SIZE); 10 | } 11 | 12 | void free_str(str *data) { 13 | if (NULL != data) { 14 | free(*data); 15 | *data = '\0'; 16 | } 17 | } 18 | 19 | void print_str(const str *const data) { 20 | if (NULL != data) { 21 | printf(" %s\n", *data); 22 | } 23 | } 24 | 25 | MLIST_ALL(test, str) 26 | 27 | int main(void) { 28 | FILE *fout = NULL; 29 | 30 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 31 | exit(EXIT_FAILURE); 32 | } 33 | 34 | FILE *fin = NULL; 35 | 36 | if ((fin = freopen("example2.in", "r", stdin)) == NULL) { 37 | printf("Could not open for reading\n"); 38 | exit(EXIT_FAILURE); 39 | } 40 | 41 | merr_t err = M_OK; 42 | 43 | fseek(fin, 0, SEEK_SET); 44 | 45 | test_mlist_t strings = test_mlist(&compare_ptr_str, &free_str); 46 | 47 | size_t str_num = 0; 48 | int check = 0; 49 | 50 | check = scanf("%lu", &str_num); 51 | 52 | if (check > 1) { 53 | exit(EXIT_FAILURE); 54 | } 55 | 56 | for (size_t i = 0; i < str_num; ++i) { 57 | char *ptr_data = malloc(MAX_STRING_SIZE); 58 | check = scanf("%s", ptr_data); 59 | 60 | if (check > 1) { 61 | exit(EXIT_FAILURE); 62 | } 63 | 64 | err = test_mlist_push(strings, ptr_data); 65 | 66 | if (M_OK != err) { 67 | MERROR(err); 68 | } 69 | } 70 | 71 | fclose(fin); 72 | 73 | printf("Print strings dlist:\n"); 74 | test_mlist_traverse(strings, &print_str); 75 | printf("\n"); 76 | 77 | char *find; 78 | if (test_mlist_find_idx(strings, 4, &find) == M_OK) { 79 | printf("String from pos 4 is %s\n", find); 80 | } 81 | 82 | test_mlist_free(&strings); 83 | 84 | fclose(fin); 85 | fclose(fout); 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mlist/example2.in: -------------------------------------------------------------------------------- 1 | 20 2 | Davila 3 | Jacobs 4 | Hobbs 5 | Schmidt 6 | Ochoa 7 | Harding 8 | Benjamin 9 | Lawson 10 | Mcpherson 11 | Fleming 12 | Mcintosh 13 | Lozano 14 | Patrick 15 | Pratt 16 | Powers 17 | Mack 18 | Simmons 19 | Herman 20 | Walter 21 | Kennedy -------------------------------------------------------------------------------- /macros_data_structs/examples/mlist/example3.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_list.h" 2 | #include 3 | #include 4 | 5 | MLIST_ALL(test, int) 6 | 7 | int32_t compare_int(const int *const a, const int *const b) { return *a - *b; } 8 | 9 | int main(void) { 10 | FILE *fout = NULL; 11 | 12 | if ((fout = freopen("out_3.txt", "w", stdout)) == NULL) { 13 | exit(EXIT_FAILURE); 14 | } 15 | 16 | printf("In this example we will try to do different operations on " 17 | "linked lists and measure the time\n\n"); 18 | 19 | /* Init the working strucutres */ 20 | double exec_time = 0.0; 21 | 22 | test_mlist_t ll = test_mlist(&compare_int, NULL); 23 | /* End to init the working structures */ 24 | 25 | /* Insert 100 ints into the list and count the time */ 26 | clock_t begin = clock(); 27 | 28 | for (int i = 0; i < 100; ++i) { 29 | test_mlist_push(ll, i); 30 | } 31 | 32 | clock_t end = clock(); 33 | 34 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 35 | printf("Inserting 100 ints into list: %lf sec\n", exec_time); 36 | /* End to insert 100 ints into the list */ 37 | 38 | /* Delete 100 heads from list and count the time */ 39 | begin = clock(); 40 | 41 | for (int i = 0; i < 100; ++i) { 42 | test_mlist_pop(ll, ll->head->data); 43 | } 44 | 45 | end = clock(); 46 | 47 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 48 | printf("Deleting 100 ints heads from list: %lf sec\n", exec_time); 49 | /* End to delete 100 heads from list */ 50 | 51 | /* Free list with size of 100 and count time */ 52 | for (int i = 0; i < 100; ++i) { 53 | test_mlist_push(ll, i); 54 | } 55 | 56 | begin = clock(); 57 | 58 | test_mlist_free(&ll); 59 | 60 | end = clock(); 61 | 62 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 63 | printf("Freeing list with 100 ints: %lf sec\n\n", exec_time); 64 | 65 | /* End to free list with size of 100 */ 66 | 67 | ////////////////////////////////////////////////////////////// Increase size 1 68 | 69 | ll = test_mlist(&compare_int, NULL); 70 | 71 | /* Insert 100000 ints into the list and count the time */ 72 | begin = clock(); 73 | 74 | for (int i = 0; i < 100000; ++i) { 75 | test_mlist_push(ll, i); 76 | } 77 | 78 | end = clock(); 79 | 80 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 81 | printf("Inserting 100000 ints into list: %lf sec\n", exec_time); 82 | /* End to insert 100000 ints into the list */ 83 | 84 | /* Delete 100000 heads from list and count the time */ 85 | begin = clock(); 86 | 87 | for (int i = 0; i < 100000; ++i) { 88 | test_mlist_pop(ll, ll->head->data); 89 | } 90 | 91 | end = clock(); 92 | 93 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 94 | printf("Deleting 100000 ints heads from list: %lf sec\n", exec_time); 95 | /* End to delete 100000 heads from list */ 96 | 97 | /* Free list with size of 100000 and count time */ 98 | for (int i = 0; i < 100000; ++i) { 99 | test_mlist_push(ll, i); 100 | } 101 | 102 | begin = clock(); 103 | 104 | test_mlist_free(&ll); 105 | 106 | end = clock(); 107 | 108 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 109 | printf("Freeing list with 100000 ints: %lf sec\n\n", exec_time); 110 | 111 | /* End to free list with size of 100000 */ 112 | 113 | ////////////////////////////////////////////////////////////// Increase size 2 114 | 115 | ll = test_mlist(&compare_int, NULL); 116 | 117 | /* Insert 8000000 ints into the list and count the time */ 118 | begin = clock(); 119 | 120 | for (int i = 0; i < 8000000; ++i) { 121 | test_mlist_push(ll, i); 122 | } 123 | 124 | end = clock(); 125 | 126 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 127 | printf("Inserting 8000000 ints into list: %lf sec\n", exec_time); 128 | /* End to insert 8000000 ints into the list */ 129 | 130 | /* Delete 8000000 heads from list and count the time */ 131 | begin = clock(); 132 | 133 | for (int i = 0; i < 8000000; ++i) { 134 | test_mlist_pop(ll, ll->head->data); 135 | } 136 | 137 | end = clock(); 138 | 139 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 140 | printf("Deleting 8000000 ints heads from list: %lf sec\n", exec_time); 141 | /* End to delete 8000000 heads from list */ 142 | 143 | /* Free list with size of 8000000 and count time */ 144 | for (int i = 0; i < 8000000; ++i) { 145 | test_mlist_push(ll, i); 146 | } 147 | 148 | begin = clock(); 149 | 150 | test_mlist_free(&ll); 151 | 152 | end = clock(); 153 | 154 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 155 | printf("Freeing list with 8000000 ints: %lf sec\n", exec_time); 156 | 157 | /* End to free list with size of 8000000 */ 158 | 159 | /* Close output file */ 160 | fclose(fout); 161 | 162 | return 0; 163 | } 164 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mpqueue/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | VALGRIND_FLAGS += --leak-check=full \ 9 | --show-leak-kinds=all \ 10 | --track-origins=yes -s 11 | 12 | VALGRIND += valgrind $(VALGRIND_FLAGS) 13 | 14 | .PHONY: build e1 e2 clean 15 | 16 | build: e1 e2 17 | 18 | e1: example1 19 | @printf "Run MPQUEUE example 1: " 20 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 21 | @printf "Done\n" 22 | @rm -rf example1 23 | 24 | e2: example2 25 | @printf "Run MPQUEUE example 2: " 26 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 27 | @printf "Done\n" 28 | @rm -rf example2 29 | 30 | example%: example%.c 31 | @gcc $(CFLAGS) $< -o $@ 32 | 33 | clean: 34 | @rm -rf *.txt example1 example2 35 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mpqueue/example1.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_pqueue.h" 2 | 3 | #define MAX_ELEMENTS 100 4 | #define MAX_STRING 100 5 | 6 | typedef char *string; 7 | 8 | MPQUEUE_ALL(test, int, string) 9 | 10 | int32_t compare_int(const int *const a, const int *const b) { return *a - *b; } 11 | 12 | int32_t compare_string(const string *const str1, const string *const str2) { 13 | return strncmp(*str1, *str2, MAX_STRING); 14 | } 15 | 16 | void free_string(string *str) { 17 | if (NULL != str) { 18 | free(*str); 19 | } 20 | } 21 | 22 | void print_string(const string *const str) { 23 | if (str != NULL) { 24 | printf("%s, ", *str); 25 | } else { 26 | printf("Not ok\n"); 27 | } 28 | } 29 | 30 | int32_t compute_voc(string str) { 31 | int32_t voc_diff = 0; 32 | 33 | for (size_t iter = 0; str[iter] != 0; ++iter) { 34 | if (('a' == str[iter]) || ('e' == str[iter]) || ('i' == str[iter]) || 35 | ('o' == str[iter]) || ('u' == str[iter]) || ('A' == str[iter]) || 36 | ('E' == str[iter]) || ('I' == str[iter]) || ('O' == str[iter]) || 37 | ('U' == str[iter])) { 38 | ++voc_diff; 39 | } 40 | } 41 | 42 | return voc_diff; 43 | } 44 | 45 | int main(void) { 46 | FILE *fout = NULL; 47 | 48 | if ((fout = freopen("out_1.txt", "w", stdout)) == NULL) { 49 | exit(EXIT_FAILURE); 50 | } 51 | 52 | FILE *fin = NULL; 53 | 54 | if ((fin = freopen("example1.in", "r", stdin)) == NULL) { 55 | fclose(fout); 56 | exit(EXIT_FAILURE); 57 | } 58 | 59 | test_mpqueue_t pq = test_mpqueue(MAX_ELEMENTS, &compare_int, NULL, 60 | &compare_string, &free_string); 61 | 62 | int elem_num = 0; 63 | 64 | int check = scanf("%d", &elem_num); 65 | 66 | if (check > 1) { 67 | exit(EXIT_FAILURE); 68 | } 69 | 70 | merr_t err = M_OK; 71 | 72 | for (int i = 0; i < elem_num; ++i) { 73 | string read_data = malloc(MAX_STRING); 74 | 75 | check = scanf("%s", read_data); 76 | 77 | if (check > 1) { 78 | exit(EXIT_FAILURE); 79 | } 80 | 81 | err = test_mpqueue_push(pq, compute_voc(read_data), read_data); 82 | 83 | if (M_OK != err) { 84 | MERROR(err); 85 | } 86 | } 87 | 88 | printf("Priority queue data:\n"); 89 | test_mpqueue_traverse(pq, &print_string); 90 | printf("\n\n"); 91 | 92 | string acc = NULL; 93 | 94 | if (test_mpqueue_top(pq, &acc) == M_OK) { 95 | int voc = -1; 96 | 97 | test_mpqueue_top_pri(pq, &voc); 98 | 99 | printf("The top elements is \"%s\" and has %d vowels:\n\n", acc, voc); 100 | } 101 | 102 | printf("Let's remove the top element:\n"); 103 | err = test_mpqueue_pop(pq); 104 | 105 | if (M_OK != err) { 106 | MERROR(err); 107 | } 108 | 109 | test_mpqueue_traverse(pq, &print_string); 110 | printf("\n\n"); 111 | 112 | printf("Let's change now the top element into \"!!!!HELLO!!!!\" and also to" 113 | " change its priority\n"); 114 | 115 | string chg = malloc(MAX_STRING); 116 | strcpy(chg, "communication"); 117 | 118 | size_t top_index = 0; 119 | test_mpqueue_find_idx(pq, chg, &top_index); 120 | 121 | strcpy(chg, "!!!!HELLO!!!!"); 122 | err = test_mpqueue_change(pq, top_index, chg); 123 | 124 | if (M_OK != err) { 125 | MERROR(err); 126 | } 127 | 128 | err = test_mpqueue_change_pri(pq, top_index, compute_voc(chg)); 129 | 130 | if (M_OK != err) { 131 | MERROR(err); 132 | } 133 | 134 | test_mpqueue_traverse(pq, &print_string); 135 | printf("\n\n"); 136 | 137 | printf("Let's pop 10 elems from priority queue:\n"); 138 | for (size_t i = 0; i < 10; ++i) { 139 | err = test_mpqueue_pop(pq); 140 | 141 | if (M_OK != err) { 142 | MERROR(err); 143 | } 144 | } 145 | 146 | test_mpqueue_traverse(pq, &print_string); 147 | fclose(fin); 148 | 149 | test_mpqueue_free(&pq); 150 | 151 | fclose(fout); 152 | 153 | return 0; 154 | } 155 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mpqueue/example1.in: -------------------------------------------------------------------------------- 1 | 40 2 | depth 3 | republic 4 | guest 5 | thought 6 | bathroom 7 | poet 8 | law 9 | scene 10 | appearance 11 | concept 12 | foundation 13 | hotel 14 | grocery 15 | method 16 | association 17 | girlfriend 18 | consequence 19 | knowledge 20 | refrigerator 21 | piano 22 | woman 23 | communication 24 | hospital 25 | supermarket 26 | owner 27 | theory 28 | article 29 | drawing 30 | two 31 | food 32 | beer 33 | recommendation 34 | accident 35 | manufacturer 36 | dinner 37 | outcome 38 | skill 39 | region 40 | decision 41 | news -------------------------------------------------------------------------------- /macros_data_structs/examples/mpqueue/example2.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_pqueue.h" 2 | #include 3 | 4 | typedef void *dummy; 5 | 6 | MPQUEUE_ALL(test, int, dummy) 7 | 8 | int32_t compare_int(const int *const a, const int *const b) { return *a - *b; } 9 | 10 | int main(void) { 11 | FILE *fout = NULL; 12 | 13 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 14 | exit(EXIT_FAILURE); 15 | } 16 | 17 | printf("In this example we will try to do different operations on Priority " 18 | "Queues and measure the time\n\n"); 19 | 20 | /* Init the working strucutres */ 21 | double exec_time = 0.0; 22 | 23 | test_mpqueue_t pq = test_mpqueue(100, &compare_int, NULL, NULL, NULL); 24 | 25 | /* End to init the working structures */ 26 | 27 | /* Insert 100 ints into the pri queue and count the time */ 28 | clock_t begin = clock(); 29 | 30 | for (int i = 0; i < 100; ++i) { 31 | test_mpqueue_push(pq, i, NULL); 32 | } 33 | 34 | clock_t end = clock(); 35 | 36 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 37 | printf("Inserting 100 ints into pri queue: %lf sec\n", exec_time); 38 | /* End to insert 100 ints into the pri queue */ 39 | 40 | /* Delete 100 fronts from pri queue and count the time */ 41 | begin = clock(); 42 | 43 | for (int i = 0; i < 100; ++i) { 44 | test_mpqueue_pop(pq); 45 | } 46 | 47 | end = clock(); 48 | 49 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 50 | printf("Deleting 100 ints fronts from pri queue: %lf sec\n", exec_time); 51 | /* End to delete 100 fronts from pri queue */ 52 | 53 | /* Free pri queue with size of 100 and count time */ 54 | for (int i = 0; i < 100; ++i) { 55 | test_mpqueue_push(pq, i, NULL); 56 | } 57 | 58 | begin = clock(); 59 | 60 | test_mpqueue_free(&pq); 61 | 62 | end = clock(); 63 | 64 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 65 | printf("Freeing pri queue with 100 ints: %lf sec\n\n", exec_time); 66 | 67 | /* End to free pri queue with size of 100 */ 68 | 69 | ////////////////////////////////////////////////////////////// Increase size 1 70 | 71 | pq = test_mpqueue(100000, &compare_int, NULL, NULL, NULL); 72 | 73 | /* Insert 100000 ints into the pri queue and count the time */ 74 | begin = clock(); 75 | 76 | for (int i = 0; i < 100000; ++i) { 77 | test_mpqueue_push(pq, i, NULL); 78 | } 79 | 80 | end = clock(); 81 | 82 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 83 | printf("Inserting 100000 ints into pri queue: %lf sec\n", exec_time); 84 | /* End to insert 100000 ints into the pri queue */ 85 | 86 | /* Delete 100000 fronts from pri queue and count the time */ 87 | begin = clock(); 88 | 89 | for (int i = 0; i < 100000; ++i) { 90 | test_mpqueue_pop(pq); 91 | } 92 | 93 | end = clock(); 94 | 95 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 96 | printf("Deleting 100000 ints fronts from pri queue: %lf sec\n", exec_time); 97 | /* End to delete 100000 fronts from pri queue */ 98 | 99 | /* Free pri queue with size of 100000 and count time */ 100 | for (int i = 0; i < 100000; ++i) { 101 | test_mpqueue_push(pq, i, NULL); 102 | } 103 | 104 | begin = clock(); 105 | 106 | test_mpqueue_free(&pq); 107 | 108 | end = clock(); 109 | 110 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 111 | printf("Freeing pri queue with 100000 ints: %lf sec\n\n", exec_time); 112 | 113 | /* End to free pri queue with size of 100000 */ 114 | 115 | ////////////////////////////////////////////////////////////// Increase size 2 116 | 117 | pq = test_mpqueue(8000000, &compare_int, NULL, NULL, NULL); 118 | 119 | /* Insert 8000000 ints into the pri queue and count the time */ 120 | begin = clock(); 121 | 122 | for (int i = 0; i < 8000000; ++i) { 123 | test_mpqueue_push(pq, i, NULL); 124 | } 125 | 126 | end = clock(); 127 | 128 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 129 | printf("Inserting 8000000 ints into pri queue: %lf sec\n", exec_time); 130 | /* End to insert 8000000 ints into the pri queue */ 131 | 132 | /* Delete 8000000 fronts from pri queue and count the time */ 133 | begin = clock(); 134 | 135 | for (int i = 0; i < 8000000; ++i) { 136 | test_mpqueue_pop(pq); 137 | } 138 | 139 | end = clock(); 140 | 141 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 142 | printf("Deleting 8000000 ints fronts from pri queue: %lf sec\n", exec_time); 143 | /* End to delete 8000000 fronts from pri queue */ 144 | 145 | /* Free pri queue with size of 8000000 and count time */ 146 | for (int i = 0; i < 8000000; ++i) { 147 | test_mpqueue_push(pq, i, NULL); 148 | } 149 | 150 | begin = clock(); 151 | 152 | test_mpqueue_free(&pq); 153 | 154 | end = clock(); 155 | 156 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 157 | printf("Freeing pri queue with 8000000 ints: %lf sec\n", exec_time); 158 | 159 | /* End to free pri queue with size of 8000000 */ 160 | 161 | /* Close output file */ 162 | fclose(fout); 163 | 164 | return 0; 165 | } 166 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mqueue/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | VALGRIND_FLAGS += --leak-check=full \ 9 | --show-leak-kinds=all \ 10 | --track-origins=yes 11 | 12 | VALGRIND += valgrind $(VALGRIND_FLAGS) 13 | 14 | .PHONY: build e1 e2 e3 clean 15 | 16 | build: e1 e2 e3 17 | 18 | e1: example1 19 | @printf "Run MQUEUE example 1: " 20 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 21 | @printf "Done\n" 22 | @rm -rf example1 23 | 24 | e2: example2 25 | @printf "Run MQUEUE example 2: " 26 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 27 | @printf "Done\n" 28 | @rm -rf example2 29 | 30 | e3: example3 31 | @printf "Run MQUEUE example 3: " 32 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 33 | @printf "Done\n" 34 | @rm -rf example3 35 | 36 | example%: example%.c 37 | @gcc $(CFLAGS) $< -o $@ 38 | 39 | clean: 40 | @rm -rf *.txt example1 example2 example3 41 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mqueue/example1.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_queue.h" 2 | #include 3 | 4 | MQUEUE_ALL(test, int) 5 | 6 | void print_int_br(const int *const data) { 7 | if (NULL != data) { 8 | static int br = 0; 9 | 10 | if ((br % 10 == 0) && (0 != br)) { 11 | printf("\n"); 12 | } 13 | 14 | printf("%d ", *data); 15 | 16 | ++br; 17 | } 18 | } 19 | 20 | int main(void) { 21 | FILE *fout = NULL; 22 | 23 | if ((fout = freopen("out_1.txt", "w", stdout)) == NULL) { 24 | exit(EXIT_FAILURE); 25 | } 26 | 27 | srand(time(NULL)); 28 | 29 | test_mqueue_t qq = test_mqueue(NULL); 30 | 31 | if (NULL == qq) { 32 | exit(EXIT_FAILURE); 33 | } 34 | 35 | merr_t err = M_OK; 36 | 37 | for (int i = 0; i < 100; ++i) { 38 | err = test_mqueue_push(qq, rand() % 100 + 10); 39 | 40 | if (M_OK != err) { 41 | MERROR(err); 42 | } 43 | } 44 | 45 | if (mtrue == test_mqueue_empty(qq)) { 46 | printf("Oops !!! Something went wring with insertion\n"); 47 | exit(EXIT_FAILURE); 48 | } 49 | 50 | printf("Generated queue has %lu elements:\n", test_mqueue_size(qq)); 51 | test_mqueue_traverse(qq, &print_int_br); 52 | printf("\n"); 53 | 54 | int front = 0, back = 0; 55 | 56 | if (test_mqueue_front(qq, &front) == M_OK) { 57 | printf("Front element is %d\n", front); 58 | } else { 59 | printf("Could not fetch front element\n"); 60 | } 61 | 62 | if (test_mqueue_back(qq, &back) == M_OK) { 63 | printf("Back element is %d\n\n", back); 64 | } else { 65 | printf("Could not fetch back element\n\n"); 66 | } 67 | 68 | printf("Let's pop half of the queue:\n"); 69 | 70 | for (int i = 0; i < 50; ++i) { 71 | err = test_mqueue_pop(qq); 72 | 73 | if (M_OK != err) { 74 | MERROR(err); 75 | } 76 | } 77 | 78 | test_mqueue_traverse(qq, &print_int_br); 79 | printf("\n"); 80 | 81 | test_mqueue_free(&qq); 82 | 83 | fclose(fout); 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mqueue/example2.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_queue.h" 2 | #include 3 | 4 | MQUEUE_ALL(inner, int) 5 | 6 | MQUEUE_ALL(test, inner_mqueue_t) 7 | 8 | void free_inner(inner_mqueue_t *q) { 9 | if (q != NULL) { 10 | inner_mqueue_free(q); 11 | } 12 | } 13 | 14 | int main(void) { 15 | FILE *fout = NULL; 16 | 17 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 18 | exit(EXIT_FAILURE); 19 | } 20 | 21 | test_mqueue_t qqs = test_mqueue(&free_inner); 22 | 23 | if (NULL != qqs) { 24 | srand(time(NULL)); 25 | 26 | merr_t err = M_OK; 27 | 28 | for (int i = 0; i < 3; ++i) { 29 | inner_mqueue_t qq = inner_mqueue(NULL); 30 | 31 | for (int j = 0; j < 10; ++j) { 32 | err = inner_mqueue_push(qq, rand() % 100 + 10); 33 | 34 | if (M_OK != err) { 35 | MERROR(err); 36 | } 37 | } 38 | 39 | err = test_mqueue_push(qqs, qq); 40 | 41 | if (M_OK != err) { 42 | MERROR(err); 43 | } 44 | } 45 | 46 | int queue_num = 0; 47 | 48 | while (!test_mqueue_empty(qqs)) { 49 | printf("Queue number %d:\n", queue_num++); 50 | 51 | inner_mqueue_t front = NULL; 52 | test_mqueue_front(qqs, &front); 53 | 54 | while (!inner_mqueue_empty(front)) { 55 | int front_elem = 0; 56 | inner_mqueue_front(front, &front_elem); 57 | 58 | printf("%d ", front_elem); 59 | 60 | err = inner_mqueue_pop(front); 61 | 62 | if (M_OK != err) { 63 | MERROR(err); 64 | } 65 | } 66 | 67 | printf("\n\n"); 68 | 69 | err = test_mqueue_pop(qqs); 70 | 71 | if (M_OK != err) { 72 | MERROR(err); 73 | } 74 | } 75 | 76 | test_mqueue_free(&qqs); 77 | } 78 | 79 | fclose(fout); 80 | 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mqueue/example3.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_queue.h" 2 | #include 3 | 4 | MQUEUE_ALL(test, int) 5 | 6 | int main(void) { 7 | FILE *fout = NULL; 8 | 9 | if ((fout = freopen("out_3.txt", "w", stdout)) == NULL) { 10 | exit(EXIT_FAILURE); 11 | } 12 | 13 | printf("In this example we will try to do different operations on Queues and " 14 | "measure the time\n\n"); 15 | 16 | /* Init the working strucutres */ 17 | double exec_time = 0.0; 18 | 19 | merr_t err = M_OK; 20 | 21 | test_mqueue_t qq = test_mqueue(NULL); 22 | /* End to init the working structures */ 23 | 24 | /* Insert 100 ints into the queue and count the time */ 25 | clock_t begin = clock(); 26 | 27 | for (int i = 0; i < 100; ++i) { 28 | err = test_mqueue_push(qq, i); 29 | 30 | if (M_OK != err) { 31 | MERROR(err); 32 | } 33 | } 34 | 35 | clock_t end = clock(); 36 | 37 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 38 | printf("Inserting 100 ints into queue: %lf sec\n", exec_time); 39 | /* End to insert 100 ints into the queue */ 40 | 41 | /* Delete 100 fronts from queue and count the time */ 42 | begin = clock(); 43 | 44 | for (int i = 0; i < 100; ++i) { 45 | err = test_mqueue_pop(qq); 46 | 47 | if (M_OK != err) { 48 | MERROR(err); 49 | } 50 | } 51 | 52 | end = clock(); 53 | 54 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 55 | printf("Deleting 100 ints fronts from queue: %lf sec\n", exec_time); 56 | /* End to delete 100 fronts from queue */ 57 | 58 | /* Free queue with size of 100 and count time */ 59 | for (int i = 0; i < 100; ++i) { 60 | test_mqueue_push(qq, i); 61 | } 62 | 63 | begin = clock(); 64 | 65 | test_mqueue_free(&qq); 66 | 67 | end = clock(); 68 | 69 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 70 | printf("Freeing queue with 100 ints: %lf sec\n\n", exec_time); 71 | 72 | /* End to free queue with size of 100 */ 73 | 74 | ////////////////////////////////////////////////////////////// Increase size 1 75 | 76 | qq = test_mqueue(NULL); 77 | 78 | /* Insert 100000 ints into the queue and count the time */ 79 | begin = clock(); 80 | 81 | for (int i = 0; i < 100000; ++i) { 82 | err = test_mqueue_push(qq, i); 83 | 84 | if (M_OK != err) { 85 | MERROR(err); 86 | } 87 | } 88 | 89 | end = clock(); 90 | 91 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 92 | printf("Inserting 100000 ints into queue: %lf sec\n", exec_time); 93 | /* End to insert 100000 ints into the queue */ 94 | 95 | /* Delete 100000 fronts from queue and count the time */ 96 | begin = clock(); 97 | 98 | for (int i = 0; i < 100000; ++i) { 99 | err = test_mqueue_pop(qq); 100 | 101 | if (M_OK != err) { 102 | MERROR(err); 103 | } 104 | } 105 | 106 | end = clock(); 107 | 108 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 109 | printf("Deleting 100000 ints fronts from queue: %lf sec\n", exec_time); 110 | /* End to delete 100000 fronts from queue */ 111 | 112 | /* Free queue with size of 100000 and count time */ 113 | for (int i = 0; i < 100000; ++i) { 114 | test_mqueue_push(qq, i); 115 | } 116 | 117 | begin = clock(); 118 | 119 | test_mqueue_free(&qq); 120 | 121 | end = clock(); 122 | 123 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 124 | printf("Freeing queue with 100000 ints: %lf sec\n\n", exec_time); 125 | 126 | /* End to free queue with size of 100000 */ 127 | 128 | ////////////////////////////////////////////////////////////// Increase size 2 129 | 130 | qq = test_mqueue(NULL); 131 | 132 | /* Insert 8000000 ints into the queue and count the time */ 133 | begin = clock(); 134 | 135 | for (int i = 0; i < 8000000; ++i) { 136 | err = test_mqueue_push(qq, i); 137 | 138 | if (M_OK != err) { 139 | MERROR(err); 140 | } 141 | } 142 | 143 | end = clock(); 144 | 145 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 146 | printf("Inserting 8000000 ints into queue: %lf sec\n", exec_time); 147 | /* End to insert 8000000 ints into the queue */ 148 | 149 | /* Delete 8000000 fronts from queue and count the time */ 150 | begin = clock(); 151 | 152 | for (int i = 0; i < 8000000; ++i) { 153 | err = test_mqueue_pop(qq); 154 | 155 | if (M_OK != err) { 156 | MERROR(err); 157 | } 158 | } 159 | 160 | end = clock(); 161 | 162 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 163 | printf("Deleting 8000000 ints fronts from queue: %lf sec\n", exec_time); 164 | /* End to delete 8000000 fronts from queue */ 165 | 166 | /* Free queue with size of 8000000 and count time */ 167 | for (int i = 0; i < 8000000; ++i) { 168 | test_mqueue_push(qq, i); 169 | } 170 | 171 | begin = clock(); 172 | 173 | test_mqueue_free(&qq); 174 | 175 | end = clock(); 176 | 177 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 178 | printf("Freeing queue with 8000000 ints: %lf sec\n", exec_time); 179 | 180 | /* End to free queue with size of 8000000 */ 181 | 182 | /* Close output file */ 183 | fclose(fout); 184 | 185 | return 0; 186 | } 187 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mrbk/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | VALGRIND_FLAGS += --leak-check=full \ 9 | --show-leak-kinds=all \ 10 | --track-origins=yes 11 | 12 | VALGRIND += valgrind $(VALGRIND_FLAGS) 13 | 14 | .PHONY: build e1 e2 e3 clean 15 | 16 | build: e1 e2 e3 17 | 18 | e1: example1 19 | @printf "Run MRBK example 1: " 20 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 21 | @printf "Done\n" 22 | @rm -rf example1 23 | 24 | e2: example2 25 | @printf "Run MRBK example 2: " 26 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 27 | @printf "Done\n" 28 | @rm -rf example2 29 | 30 | e3: example3 31 | @printf "Run MRBK example 3: " 32 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 33 | @printf "Done\n" 34 | @rm -rf example3 35 | 36 | example%: example%.c 37 | @gcc $(CFLAGS) $< -o $@ 38 | 39 | clean: 40 | @rm -rf *.txt example1 example2 example3 41 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mrbk/example1.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_rbk.h" 2 | #include 3 | #include 4 | 5 | int32_t compare_int(const int *const a, const int *const b) { return *a - *b; } 6 | 7 | void print_int(const int *const a) { printf(" %d", *a); } 8 | 9 | MRBK_ALL(test, int) 10 | 11 | int main(void) { 12 | FILE *fout = NULL; 13 | 14 | if ((fout = freopen("out_1.txt", "w", stdout)) == NULL) { 15 | exit(EXIT_FAILURE); 16 | } 17 | 18 | test_mrbk_t my_tree = test_mrbk(&compare_int, NULL); 19 | 20 | if (NULL == my_tree) { 21 | printf("RBK tree was not allocated\n"); 22 | exit(EXIT_FAILURE); 23 | } 24 | 25 | srand(time(NULL)); 26 | 27 | merr_t err = M_OK; 28 | 29 | size_t number_of_elem = 100; 30 | 31 | printf("Generated data:\n"); 32 | for (size_t i = 1; i <= number_of_elem; ++i) { 33 | if (i % 10 == 0) { 34 | printf("\n"); 35 | } 36 | 37 | int data = rand() % 2000 + 100; 38 | 39 | printf("%d ", data); 40 | 41 | err = test_mrbk_push(my_tree, data); 42 | 43 | if (M_OK != err) { 44 | MERROR(err); 45 | } 46 | } 47 | printf("\n"); 48 | 49 | if (test_mrbk_empty(my_tree) == mtrue) { 50 | printf("Something went really wrong try once again\n"); 51 | exit(EXIT_FAILURE); 52 | } 53 | 54 | printf("\nThe RBK tree printed by postorder method is:\n"); 55 | test_mrbk_traverse_postorder(my_tree, &print_int); 56 | printf("\n\n"); 57 | 58 | printf("In the RBK tree there exist %lu unique random integers\n\n", 59 | test_mrbk_size(my_tree)); 60 | 61 | int root_data; 62 | err = test_mrbk_root(my_tree, &root_data); 63 | 64 | if (err == M_OK) { 65 | printf("The root value of the RBK tree is %d\n", root_data); 66 | 67 | int min_max = 0; 68 | test_mrbk_max(my_tree, root_data, &min_max); 69 | printf("The maximum random number generated is %d\n", min_max); 70 | 71 | test_mrbk_min(my_tree, root_data, &min_max); 72 | printf("The minimum random number generated is %d\n", min_max); 73 | 74 | int root_pred, root_succ; 75 | 76 | if (test_mrbk_pred(my_tree, root_data, &root_pred) == M_OK) { 77 | printf("The inorder predecessor of the root value is %d\n", root_pred); 78 | } else { 79 | printf("The root node has no predecessor\n"); 80 | } 81 | 82 | if (test_mrbk_succ(my_tree, root_data, &root_succ) == M_OK) { 83 | printf("The inorder successor of the root value is %d\n\n", root_succ); 84 | } else { 85 | printf("The root node has no successor\n\n"); 86 | } 87 | 88 | int anc; 89 | if (test_mrbk_lca(my_tree, root_pred, root_succ, &anc) == M_OK) { 90 | printf("The lowest common ancestor for the predecessor and successor of " 91 | "the root is %d\n\n", 92 | anc); 93 | } else { 94 | printf("Could not find the lowest comon ancestor for predecessor and " 95 | "successor of the root node\n\n"); 96 | } 97 | } else { 98 | printf("Could not find the root node\n\n"); 99 | } 100 | 101 | printf("Let's remove some elements:\n"); 102 | 103 | for (size_t i = 0; i < 10; ++i) { 104 | int del; 105 | 106 | if (test_mrbk_root(my_tree, &del) == M_OK) { 107 | printf("Removing %d value from RBK\n", del); 108 | 109 | err = test_mrbk_pop(my_tree, del); 110 | 111 | if (err != M_OK) { 112 | MERROR(err); 113 | } 114 | } 115 | } 116 | 117 | printf("\n"); 118 | 119 | printf("After deletion the RBK tree shows like (inorder):\n"); 120 | test_mrbk_traverse_inorder(my_tree, &print_int); 121 | printf("\n\n"); 122 | 123 | fclose(fout); 124 | test_mrbk_free(&my_tree); 125 | 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mrbk/example2.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_rbk.h" 2 | #include 3 | #include 4 | 5 | #define MAX_NAME 20 6 | #define MAX_SURNAME 30 7 | 8 | typedef struct student_s { 9 | char *name; 10 | char *surname; 11 | unsigned int age; 12 | unsigned int class; 13 | unsigned char class_label; 14 | double GPA; 15 | int index; 16 | } student_t; 17 | 18 | int32_t compare_student(const student_t *const s1, const student_t *const s2) { 19 | int cmp = strncmp(s1->name, s2->name, MAX_NAME); 20 | 21 | if (0 != cmp) { 22 | return cmp; 23 | } 24 | 25 | cmp = strncmp(s1->surname, s2->surname, MAX_SURNAME); 26 | 27 | if (0 != cmp) { 28 | return cmp; 29 | } 30 | 31 | if (s1->GPA > s2->GPA) { 32 | cmp = 1; 33 | } else if (s1->GPA < s2->GPA) { 34 | cmp = -1; 35 | } else { 36 | cmp = 0; 37 | } 38 | 39 | if (cmp == 0) { 40 | return s1->index - s2->index; 41 | } 42 | 43 | return cmp; 44 | } 45 | 46 | void free_student(student_t *st) { 47 | if (NULL != st) { 48 | 49 | free(st->name); 50 | st->name = NULL; 51 | 52 | free(st->surname); 53 | st->surname = NULL; 54 | 55 | st->age = 0; 56 | st->class = 0; 57 | st->class_label = '\0'; 58 | st->GPA = 0.0; 59 | st->index = -1; 60 | } 61 | } 62 | 63 | void print_student(const student_t *const st) { 64 | if (NULL != st) { 65 | printf("Student no. %d\n", st->index); 66 | printf("NAME: %s\n", st->name); 67 | printf("SURNAME: %s\n", st->surname); 68 | printf("Student GPA: %lf\n", st->GPA); 69 | printf("Student age: %u\n\n", st->age); 70 | } 71 | } 72 | 73 | MRBK_ALL(test, student_t) 74 | 75 | int main(void) { 76 | FILE *fin = NULL; 77 | 78 | if ((fin = freopen("example2.in", "r", stdin)) == NULL) { 79 | exit(EXIT_FAILURE); 80 | } 81 | 82 | test_mrbk_t data_base = test_mrbk(&compare_student, &free_student); 83 | 84 | if (NULL == data_base) { 85 | exit(EXIT_FAILURE); 86 | } 87 | 88 | merr_t err = M_OK; 89 | 90 | int number_of_students = 0; 91 | int read = scanf("%d", &number_of_students); 92 | 93 | if (read > 1) { 94 | exit(EXIT_FAILURE); 95 | } 96 | 97 | student_t st; // Encapsulate the reading 98 | 99 | for (int i = 0; i < number_of_students; ++i) { 100 | st.name = malloc(MAX_NAME); 101 | st.surname = malloc(MAX_SURNAME); 102 | 103 | read = scanf("%s %s", st.name, st.surname); 104 | 105 | if (read > 2) { 106 | exit(EXIT_FAILURE); 107 | } 108 | 109 | read = scanf("%u %u %c %lf", &st.age, &st.class, &st.class_label, &st.GPA); 110 | 111 | if (read > 4) { 112 | exit(EXIT_FAILURE); 113 | } 114 | 115 | st.index = i; 116 | 117 | err = test_mrbk_push(data_base, st); 118 | 119 | if (M_OK != err) { 120 | MERROR(err); 121 | } 122 | } 123 | 124 | fclose(fin); 125 | 126 | FILE *fout = NULL; 127 | 128 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 129 | exit(EXIT_FAILURE); 130 | } 131 | 132 | printf("In the data base are %lu students\n\n", test_mrbk_size(data_base)); 133 | 134 | test_mrbk_traverse_inorder(data_base, &print_student); 135 | printf("\n\n"); 136 | 137 | fclose(fout); 138 | 139 | test_mrbk_free(&data_base); 140 | 141 | return 0; 142 | } 143 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mrbk/example2.in: -------------------------------------------------------------------------------- 1 | 40 2 | Rahul Vickers 3 | 14 12 A 4.0 4 | Ranveer Ramirez 5 | 15 12 B 3.7 6 | Bluebell Lawrence 7 | 14 10 C 1.3 8 | Avni Sharma 9 | 15 9 A 0.5 10 | Georgie Beltran 11 | 17 11 B 4.0 12 | Avery Pitt 13 | 19 20 C 4.0 14 | Jimmie Dalton 15 | 15 22 A 3.2 16 | Tazmin Skinner 17 | 16 43 B 3.1 18 | Tyla Dolan 19 | 14 21 C 3.0 20 | Meadow Mendoza 21 | 18 23 A 2.1 22 | Connah Farmer 23 | 19 12 B 2.5 24 | Aurora Sadler 25 | 15 22 C 2.8 26 | Cairon Ratliff 27 | 16 10 A 3.8 28 | Marie Mair 29 | 17 11 B 4.0 30 | Findlay Small 31 | 13 24 C 3.8 32 | Jazmine Avery 33 | 13 54 A 3.8 34 | Naomi Lara 35 | 16 21 B 1.1 36 | Nasir Mccormick 37 | 17 45 C 2.4 38 | Glen Becker 39 | 18 22 A 2.2 40 | Brent Coates 41 | 19 12 B 3.6 42 | Brielle Carson 43 | 14 10 C 4.0 44 | Lucy Joyner 45 | 13 11 A 3.7 46 | Rebekka Shepard 47 | 14 12 B 3.7 48 | Keaton Traynor 49 | 13 13 C 3.7 50 | Sufyaan Espinoza 51 | 13 14 A 2.2 52 | Zaina Webb 53 | 18 15 B 3.4 54 | Roxy Stevenson 55 | 19 16 C 4.0 56 | Donovan Blevins 57 | 19 17 A 1.1 58 | Phoenix Snow 59 | 17 18 B 1.9 60 | Agnes Doyle 61 | 19 19 C 2.0 62 | Tonisha Solis 63 | 17 20 A 3.0 64 | Aariz Allen 65 | 14 21 B 4.0 66 | Weronika Ho 67 | 16 22 C 2.0 68 | Milton Greaves 69 | 14 22 A 2.2 70 | Kwame Gilmore 71 | 14 22 B 2.5 72 | Eamon West 73 | 17 43 C 2.9 74 | Willem Kennedy 75 | 17 12 A 3.1 76 | Ben Chan 77 | 13 12 B 1.9 78 | Cherie Ochoa 79 | 15 43 C 2.2 80 | Priyanka Talley 81 | 18 12 A 4.0 -------------------------------------------------------------------------------- /macros_data_structs/examples/mrbk/example3.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_rbk.h" 2 | #include 3 | #include 4 | 5 | int32_t compare_int(const int *const a, const int *const b) { return *a - *b; } 6 | 7 | MRBK_ALL(test, int) 8 | 9 | int main(void) { 10 | FILE *fout = NULL; 11 | 12 | if ((fout = freopen("out_3.txt", "w", stdout)) == NULL) { 13 | exit(EXIT_FAILURE); 14 | } 15 | 16 | printf("In this example we will try to do different operations on RBK Tree " 17 | "and measure the time\n"); 18 | 19 | /* Init the working strucutres */ 20 | double exec_time = 0.0; 21 | 22 | test_mrbk_t my_tree = test_mrbk(&compare_int, NULL); 23 | /* End to init the working structures */ 24 | 25 | /* Insert 100 ints into the RBK and count the time */ 26 | clock_t begin = clock(); 27 | 28 | for (int i = 0; i < 100; ++i) { 29 | test_mrbk_push(my_tree, i); 30 | } 31 | 32 | clock_t end = clock(); 33 | 34 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 35 | printf("Inserting 100 ints into RBK: %lf sec\n", exec_time); 36 | /* End to insert 100 ints into the RBK */ 37 | 38 | /* Delete 100 roots from RBK and count the time */ 39 | begin = clock(); 40 | 41 | for (int i = 0; i < 100; ++i) { 42 | test_mrbk_pop(my_tree, my_tree->root->data); 43 | } 44 | 45 | end = clock(); 46 | 47 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 48 | printf("Deleting 100 ints roots from RBK: %lf sec\n", exec_time); 49 | /* End to delete 100 roots from RBK */ 50 | 51 | /* Free RBK tree with size of 100 and count time */ 52 | for (int i = 0; i < 100; ++i) { 53 | test_mrbk_push(my_tree, i); 54 | } 55 | 56 | begin = clock(); 57 | 58 | test_mrbk_free(&my_tree); 59 | 60 | end = clock(); 61 | 62 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 63 | printf("Freeing RBK with 100 ints: %lf sec\n\n", exec_time); 64 | 65 | /* End to free RBK tree with size of 100 */ 66 | 67 | ////////////////////////////////////////////////////////////// Increase size 1 68 | 69 | my_tree = test_mrbk(&compare_int, NULL); 70 | 71 | /* Insert 100000 ints into the RBK and count the time */ 72 | begin = clock(); 73 | 74 | for (int i = 0; i < 100000; ++i) { 75 | test_mrbk_push(my_tree, i); 76 | } 77 | 78 | end = clock(); 79 | 80 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 81 | printf("Inserting 100000 ints into RBK: %lf sec\n", exec_time); 82 | /* End to insert 100000 ints into the RBK */ 83 | 84 | /* Delete 100000 roots from RBK and count the time */ 85 | begin = clock(); 86 | 87 | for (int i = 0; i < 100000; ++i) { 88 | test_mrbk_pop(my_tree, my_tree->root->data); 89 | } 90 | 91 | end = clock(); 92 | 93 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 94 | printf("Deleting 100000 ints roots from RBK: %lf sec\n", exec_time); 95 | /* End to delete 100000 roots from RBK */ 96 | 97 | /* Free RBK tree with size of 100000 and count time */ 98 | for (int i = 0; i < 100000; ++i) { 99 | test_mrbk_push(my_tree, i); 100 | } 101 | 102 | begin = clock(); 103 | 104 | test_mrbk_free(&my_tree); 105 | 106 | end = clock(); 107 | 108 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 109 | printf("Freeing RBK with 100000 ints: %lf sec\n\n", exec_time); 110 | 111 | /* End to free RBK tree with size of 100000 */ 112 | 113 | ////////////////////////////////////////////////////////////// Increase size 2 114 | 115 | my_tree = test_mrbk(&compare_int, NULL); 116 | 117 | /* Insert 8000000 ints into the RBK and count the time */ 118 | begin = clock(); 119 | 120 | for (int i = 0; i < 8000000; ++i) { 121 | test_mrbk_push(my_tree, i); 122 | } 123 | 124 | end = clock(); 125 | 126 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 127 | printf("Inserting 8000000 ints into RBK: %lf sec\n", exec_time); 128 | /* End to insert 8000000 ints into the RBK */ 129 | 130 | /* Delete 8000000 roots from RBK and count the time */ 131 | begin = clock(); 132 | 133 | for (int i = 0; i < 8000000; ++i) { 134 | test_mrbk_pop(my_tree, my_tree->root->data); 135 | } 136 | 137 | end = clock(); 138 | 139 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 140 | printf("Deleting 8000000 ints roots from RBK: %lf sec\n", exec_time); 141 | /* End to delete 8000000 roots from RBK */ 142 | 143 | /* Free RBK tree with size of 8000000 and count time */ 144 | for (int i = 0; i < 8000000; ++i) { 145 | test_mrbk_push(my_tree, i); 146 | } 147 | 148 | begin = clock(); 149 | 150 | test_mrbk_free(&my_tree); 151 | 152 | end = clock(); 153 | 154 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 155 | printf("Freeing RBK with 8000000 ints: %lf sec\n", exec_time); 156 | 157 | /* End to free RBK tree with size of 8000000 */ 158 | 159 | /* Close output file */ 160 | fclose(fout); 161 | 162 | return 0; 163 | } 164 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mstack/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | VALGRIND_FLAGS += --leak-check=full \ 9 | --show-leak-kinds=all \ 10 | --track-origins=yes 11 | 12 | VALGRIND += valgrind $(VALGRIND_FLAGS) 13 | 14 | .PHONY: build e1 e2 e3 clean 15 | 16 | build: e1 e2 e3 17 | 18 | e1: example1 19 | @printf "Run MSTACK example 1: " 20 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 21 | @printf "Done\n" 22 | @rm -rf example1 23 | 24 | e2: example2 25 | @printf "Run MSTACK example 2: " 26 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 27 | @printf "Done\n" 28 | @rm -rf example2 29 | 30 | e3: example3 31 | @printf "Run MSTACK example 3: " 32 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 33 | @printf "Done\n" 34 | @rm -rf example3 35 | 36 | example%: example%.c 37 | @gcc $(CFLAGS) $< -o $@ 38 | 39 | clean: 40 | @rm -rf *.txt example1 example2 example3 41 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mstack/example1.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_stack.h" 2 | #include 3 | 4 | MSTACK_ALL(test, int) 5 | 6 | void print_int_br(const int *const data) { 7 | if (NULL != data) { 8 | static int br = 0; 9 | 10 | if ((br % 10 == 0) && (0 != br)) { 11 | printf("\n"); 12 | } 13 | 14 | printf("%d ", *data); 15 | 16 | ++br; 17 | } 18 | } 19 | 20 | int main(void) { 21 | FILE *fout = NULL; 22 | 23 | if ((fout = freopen("out_1.txt", "w", stdout)) == NULL) { 24 | exit(EXIT_FAILURE); 25 | } 26 | 27 | srand(time(NULL)); 28 | 29 | test_mstack_t st = test_mstack(NULL); 30 | 31 | if (NULL == st) { 32 | exit(EXIT_FAILURE); 33 | } 34 | 35 | merr_t err = M_OK; 36 | 37 | for (int i = 0; i < 100; ++i) { 38 | err = test_mstack_push(st, rand() % 100 + 10); 39 | 40 | if (M_OK != err) { 41 | MERROR(err); 42 | } 43 | } 44 | 45 | if (mtrue == test_mstack_empty(st)) { 46 | printf("Oops !!! Something went wring with insertion\n"); 47 | exit(EXIT_FAILURE); 48 | } 49 | 50 | printf("Generated queue has %lu elements:\n", test_mstack_size(st)); 51 | test_mstack_traverse(st, &print_int_br); 52 | printf("\n"); 53 | 54 | int top = 0; 55 | 56 | if (test_mstack_top(st, &top) == M_OK) { 57 | printf("Top element is %d\n\n", top); 58 | } else { 59 | printf("Could not fetch top element\n\n"); 60 | } 61 | 62 | printf("Let's pop half of the queue:\n"); 63 | 64 | for (int i = 0; i < 50; ++i) { 65 | err = test_mstack_pop(st); 66 | 67 | if (M_OK != err) { 68 | MERROR(err); 69 | } 70 | } 71 | 72 | test_mstack_traverse(st, &print_int_br); 73 | printf("\n"); 74 | 75 | test_mstack_free(&st); 76 | 77 | fclose(fout); 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mstack/example2.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_stack.h" 2 | #include 3 | 4 | MSTACK_ALL(inner, int) 5 | 6 | MSTACK_ALL(test, inner_mstack_t) 7 | 8 | void free_inner(inner_mstack_t *st) { 9 | if (st != NULL) { 10 | inner_mstack_free(st); 11 | } 12 | } 13 | 14 | int main(void) { 15 | FILE *fout = NULL; 16 | 17 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 18 | exit(EXIT_FAILURE); 19 | } 20 | 21 | test_mstack_t sts = test_mstack( 22 | &free_inner); // Can be NULL however you should take care of freeing 23 | 24 | if (NULL != sts) { 25 | srand(time(NULL)); 26 | 27 | merr_t err = M_OK; 28 | 29 | for (int i = 0; i < 3; ++i) { 30 | inner_mstack_t st = inner_mstack(NULL); 31 | 32 | for (int j = 0; j < 10; ++j) { 33 | err = inner_mstack_push(st, rand() % 100 + 10); 34 | 35 | if (M_OK != err) { 36 | MERROR(err); 37 | } 38 | } 39 | 40 | err = test_mstack_push(sts, st); 41 | 42 | if (M_OK != err) { 43 | MERROR(err); 44 | } 45 | } 46 | 47 | int queue_num = 0; 48 | 49 | while (!test_mstack_empty(sts)) { 50 | printf("Stack number %d:\n", queue_num++); 51 | 52 | inner_mstack_t top = NULL; 53 | test_mstack_top(sts, &top); 54 | 55 | while (!inner_mstack_empty(top)) { 56 | int top_elem = 0; 57 | inner_mstack_top(top, &top_elem); 58 | 59 | printf("%d ", top_elem); 60 | 61 | err = inner_mstack_pop(top); 62 | 63 | if (M_OK != err) { 64 | MERROR(err); 65 | } 66 | } 67 | 68 | printf("\n\n"); 69 | 70 | err = test_mstack_pop(sts); 71 | 72 | if (M_OK != err) { 73 | MERROR(err); 74 | } 75 | 76 | // inner_mstack_free(&top);If you set the free_inner to NULL you should 77 | // take care of freeing 78 | } 79 | 80 | test_mstack_free(&sts); 81 | } 82 | 83 | fclose(fout); 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /macros_data_structs/examples/mstack/example3.c: -------------------------------------------------------------------------------- 1 | #include "../../src/m_stack.h" 2 | #include 3 | 4 | MSTACK_ALL(test, int) 5 | 6 | int main(void) { 7 | FILE *fout = NULL; 8 | 9 | if ((fout = freopen("out_3.txt", "w", stdout)) == NULL) { 10 | exit(EXIT_FAILURE); 11 | } 12 | 13 | printf("In this example we will try to do different operations on stacks and " 14 | "measure the time\n\n"); 15 | 16 | /* Init the working strucutres */ 17 | double exec_time = 0.0; 18 | 19 | merr_t err = M_OK; 20 | 21 | test_mstack_t qq = test_mstack(NULL); 22 | /* End to init the working structures */ 23 | 24 | /* Insert 100 ints into the stack and count the time */ 25 | clock_t begin = clock(); 26 | 27 | for (int i = 0; i < 100; ++i) { 28 | err = test_mstack_push(qq, i); 29 | 30 | if (M_OK != err) { 31 | MERROR(err); 32 | } 33 | } 34 | 35 | clock_t end = clock(); 36 | 37 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 38 | printf("Inserting 100 ints into stack: %lf sec\n", exec_time); 39 | /* End to insert 100 ints into the stack */ 40 | 41 | /* Delete 100 fronts from stack and count the time */ 42 | begin = clock(); 43 | 44 | for (int i = 0; i < 100; ++i) { 45 | err = test_mstack_pop(qq); 46 | 47 | if (M_OK != err) { 48 | MERROR(err); 49 | } 50 | } 51 | 52 | end = clock(); 53 | 54 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 55 | printf("Deleting 100 ints fronts from stack: %lf sec\n", exec_time); 56 | /* End to delete 100 fronts from stack */ 57 | 58 | /* Free stack with size of 100 and count time */ 59 | for (int i = 0; i < 100; ++i) { 60 | test_mstack_push(qq, i); 61 | } 62 | 63 | begin = clock(); 64 | 65 | test_mstack_free(&qq); 66 | 67 | end = clock(); 68 | 69 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 70 | printf("Freeing stack with 100 ints: %lf sec\n\n", exec_time); 71 | 72 | /* End to free stack with size of 100 */ 73 | 74 | ////////////////////////////////////////////////////////////// Increase size 1 75 | 76 | qq = test_mstack(NULL); 77 | 78 | /* Insert 100000 ints into the stack and count the time */ 79 | begin = clock(); 80 | 81 | for (int i = 0; i < 100000; ++i) { 82 | err = test_mstack_push(qq, i); 83 | 84 | if (M_OK != err) { 85 | MERROR(err); 86 | } 87 | } 88 | 89 | end = clock(); 90 | 91 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 92 | printf("Inserting 100000 ints into stack: %lf sec\n", exec_time); 93 | /* End to insert 100000 ints into the stack */ 94 | 95 | /* Delete 100000 fronts from stack and count the time */ 96 | begin = clock(); 97 | 98 | for (int i = 0; i < 100000; ++i) { 99 | err = test_mstack_pop(qq); 100 | 101 | if (M_OK != err) { 102 | MERROR(err); 103 | } 104 | } 105 | 106 | end = clock(); 107 | 108 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 109 | printf("Deleting 100000 ints fronts from stack: %lf sec\n", exec_time); 110 | /* End to delete 100000 fronts from stack */ 111 | 112 | /* Free stack with size of 100000 and count time */ 113 | for (int i = 0; i < 100000; ++i) { 114 | test_mstack_push(qq, i); 115 | } 116 | 117 | begin = clock(); 118 | 119 | test_mstack_free(&qq); 120 | 121 | end = clock(); 122 | 123 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 124 | printf("Freeing stack with 100000 ints: %lf sec\n\n", exec_time); 125 | 126 | /* End to free stack with size of 100000 */ 127 | 128 | ////////////////////////////////////////////////////////////// Increase size 2 129 | 130 | qq = test_mstack(NULL); 131 | 132 | /* Insert 8000000 ints into the stack and count the time */ 133 | begin = clock(); 134 | 135 | for (int i = 0; i < 8000000; ++i) { 136 | err = test_mstack_push(qq, i); 137 | 138 | if (M_OK != err) { 139 | MERROR(err); 140 | } 141 | } 142 | 143 | end = clock(); 144 | 145 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 146 | printf("Inserting 8000000 ints into stack: %lf sec\n", exec_time); 147 | /* End to insert 8000000 ints into the stack */ 148 | 149 | /* Delete 8000000 fronts from stack and count the time */ 150 | begin = clock(); 151 | 152 | for (int i = 0; i < 8000000; ++i) { 153 | err = test_mstack_pop(qq); 154 | 155 | if (M_OK != err) { 156 | MERROR(err); 157 | } 158 | } 159 | 160 | end = clock(); 161 | 162 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 163 | printf("Deleting 8000000 ints fronts from stack: %lf sec\n", exec_time); 164 | /* End to delete 8000000 fronts from stack */ 165 | 166 | /* Free stack with size of 8000000 and count time */ 167 | for (int i = 0; i < 8000000; ++i) { 168 | test_mstack_push(qq, i); 169 | } 170 | 171 | begin = clock(); 172 | 173 | test_mstack_free(&qq); 174 | 175 | end = clock(); 176 | 177 | exec_time = (double)(end - begin) / CLOCKS_PER_SEC; 178 | printf("Freeing stack with 8000000 ints: %lf sec\n", exec_time); 179 | 180 | /* End to free stack with size of 8000000 */ 181 | 182 | /* Close output file */ 183 | fclose(fout); 184 | 185 | return 0; 186 | } 187 | -------------------------------------------------------------------------------- /voidptr_data_structs/.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | 3 | libdstruc.a 4 | libdstruc.so 5 | 6 | examples/avl_tree/* 7 | examples/bst_tree/* 8 | examples/dlist/* 9 | examples/graph/* 10 | examples/hash_table/* 11 | examples/list/* 12 | examples/priority_queue/* 13 | examples/queue/* 14 | examples/red_black_tree/* 15 | examples/sort_algorithms/* 16 | examples/stack/* 17 | 18 | !*.in 19 | !*.c 20 | *.txt 21 | !*.h 22 | !*.md 23 | !Makefile 24 | 25 | dynamic_obj 26 | static_obj 27 | libs -------------------------------------------------------------------------------- /voidptr_data_structs/documentation/FUNCTION_TYPES.md: -------------------------------------------------------------------------------- 1 | # What is scl function types series? 2 | 3 | ## Prototypes of printing and comparing same data types, also some special macros that will make your code lookmore beautiful. 4 | 5 | >**NOTE:** Also you have different function to print data types for example: 6 | 7 | * print_int 8 | * print_short_int 9 | * print_double 10 | * print_long_double 11 | * ...... 12 | 13 | >**NOTE:** functiontypes Series contains also function to compare two data of the same type for example: 14 | 15 | * compare_int 16 | * compare_short_int 17 | * compare_double 18 | * ... 19 | 20 | Example: 21 | 22 | ```C 23 | int main() { 24 | int a[] = {7, -1, 4, 1, 3, 9, 8, 4, -1}; 25 | 26 | qsort(a, 9, sizeof(int), &compare_int); 27 | } 28 | ``` 29 | 30 | ## Three macros in the file 31 | 32 | 1. toptr -> this macro will take a variabile and will calculate its address to pass into special function from this current working project 33 | 34 | 2. ltoptr -> will take a type and a lvalue and will calculate an address having the same value as specified value 35 | 36 | 3. make_pair -> this macro does not do something special, it is usually used in data structures that have {key data} members, so this looks more beautiful and explains better what happens with the created pair. -------------------------------------------------------------------------------- /voidptr_data_structs/documentation/QUEUE.md: -------------------------------------------------------------------------------- 1 | # Documentation for queue object ([scl_queue.h](../src/include/scl_queue.h)) 2 | 3 | ## How to create a queue and how to destroy it? 4 | 5 | In the scl_queue.h you have two functions that will help you by creating a queue and destroying it. 6 | 7 | ```C 8 | queue_t* create_queue(free_func frd, size_t data_size); 9 | scl_error_t free_queue(queue_t * const __restrict__ queue); 10 | ``` 11 | 12 | First function will take a pointer to a function that will free the content of the actual data. However you can send NULL(0) instead of a pointer. For base types as **int**, **float**, **double**, **char**, **static arrays**, **static structures**, `free_data` function should be **NULL**, if a structure that contains a pointer (and it is allocated with malloc or calloc) then a free_data function (*not NULL*) should be provided. 13 | 14 | Example of creating a queue: 15 | 16 | ```C 17 | typedef struct { 18 | char *name; 19 | int age; 20 | } person; 21 | 22 | void free_person(void *a) { 23 | person *fa = (person *)a; 24 | 25 | free(fa->name); 26 | } 27 | 28 | int main() { 29 | queue_t *queue = create_queue(&free_person, sizeof(person)); 30 | free_queue(queue); 31 | 32 | return 0; 33 | } 34 | ``` 35 | 36 | >**NOTE:** If you want to print whole queue you should call print *print_queue* function that takes a valid pointer to a queue structure location and prints it from top to bottom. 37 | 38 | >**NOTE:** All elements of the queue should be of the same type.' 39 | 40 | ## How to insert and how to remove elements from queue? 41 | 42 | You have 3 function that will maintain a queue: 43 | 44 | ```C 45 | scl_error_t queue_push (queue_t * const __restrict__ queue, const void * __restrict__ data); 46 | const void* queue_top (const queue_t * const __restrict__ queue); 47 | scl_error_t queue_pop (queue_t * const __restrict__ queue); 48 | ``` 49 | 50 | Function `queue_push` will insert one element into the queue. You should pass an allocated queue into push function, however if queue pointer is NULL than no operation will be executed. 51 | 52 | Function `queue_top` will return a pointer to the data content of the first element from the queue (the top of the queue). You will have to manually convert the return pointer to your working pointer, or to print int using a print_data function, as follows: 53 | 54 | ```C 55 | void print_int(const void * const a) { 56 | if (a == NULL) return; 57 | 58 | const int * const fa = (const int * const)a; 59 | 60 | printf("%d\n", *fa); 61 | } 62 | 63 | int main() { 64 | 65 | scl_error_t err = SCL_OK; 66 | 67 | queue_t *queue = create_queue(NULL, sizeof(int)); 68 | 69 | for (int i = 0; i < 10; ++i) 70 | if ((err = queue_push(queue, toptr(i))) != SCL_OK) { 71 | scl_error_message(err); 72 | } 73 | 74 | int top = *(const int *)queue_front(queue); 75 | printf("%d\n", top); 76 | 77 | // or using the print function 78 | 79 | print_int(queue_front(queue)); 80 | 81 | free_queue(queue); // You may not check for errors but if you want go on 82 | } 83 | ``` 84 | 85 | >**NOTE:** If you are using a print function to print data than you also should check if returned pointer is not NULL. If queue is empty or it does not exists then queue_top will return a NULL pointer and it will break the program. 86 | 87 | Function `queue_pop` will remove one element from the queue. Firstly function will free content of the data pointer if it is necessary (if free_data function is not NULL). The data content will be destroyed according to free_data function, than program will remove data pointer and node pointer and will update the new top of the queue. 88 | 89 | Example of using queue_pop: 90 | 91 | ```C 92 | int main() { 93 | queue_t *queue = create_queue(NULL, sizeof(int)); 94 | 95 | for (int i = 0; i < 10; ++i) 96 | if (queue_push(queue, toptr(i)) != SCL_OK) { 97 | printf("Something went wrong inserting &i element\n", i); 98 | return EXIT_FAILURE; 99 | } 100 | 101 | for (int i = 0; i < 10; ++i) 102 | if (queue_pop(queue) != SCL_OK) { 103 | printf("Something went wrong removing %d element\n", i); 104 | return EXIT_FAILURE; 105 | } 106 | 107 | free_queue(queue); // In this case it will free just the queue structure 108 | } 109 | ``` 110 | 111 | ## Other functions 112 | 113 | Some functions that also are important for queue maintaining are: 114 | 115 | ```C 116 | uint8_t is_queue_empty(const queue_t * const __restrict__ queue); 117 | size_t get_queue_size(const queue_t * const __restrict__ queue); 118 | ``` 119 | 120 | First function will check if queue exists and if it is empty. 121 | 122 | >**NOTE:** A non-existing queue is also considered as an empty queue. 123 | 124 | The second function will return the size of the queue or **SIZE_MAX** if queue does not exist. 125 | 126 | ## For some other examples of using queues you can look up at [examples](../examples/queue/) 127 | -------------------------------------------------------------------------------- /voidptr_data_structs/documentation/SORT_ALGORITHMS.md: -------------------------------------------------------------------------------- 1 | # Documentation for sorting algorithms ([scl_sort_algo.h](../src/include/scl_sort_algo.h)) 2 | 3 | ## What sort methods am I able to use ? 4 | 5 | Well you have a range of some sort methods, as follows: 6 | 7 | ```C 8 | scl_error_t quick_sort(void *arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 9 | 10 | scl_error_t heap_sort(void* arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); // Imported from scl_priority_queue.h 11 | 12 | scl_error_t merge_sort(void *arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 13 | 14 | scl_error_t bubble_sort(void *arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 15 | 16 | scl_error_t radix_sort(uint64_t *arr, size_t number_of_elem); 17 | 18 | scl_error_t insertion_sort(void *arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 19 | 20 | scl_error_t selection_sort(void *arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 21 | ``` 22 | 23 | >**NOTE:** I tried to preserve the syntax as standart qsort method for sorting 24 | 25 | >**NOTE:** Radix sort is just for sorting uint64_t (unsigned long) data types. 26 | 27 | ## I want to sort just a part of the array not the entire array what should I do ? 28 | 29 | The solution is very simple just to as follows: 30 | 31 | ```C 32 | // let the array be {23, 54, 11, 76, -1, -22, 43, 76, -19} 33 | int arr = {23, 54, 11, 76, -1, -22, 43, 76, -19}; 34 | 35 | // Now let's start sorting from index 4 and sort 3 elements 36 | 37 | quick_sort(arr + 4, 3, sizeof(*arr), &compare_int); // compare_int is a function from func_types.h 38 | 39 | // So after running it you should get 40 | 41 | // arr = {23, 54, 11, 76, -22, -1, 43, 76, -19} 42 | 43 | // You shall not pass the boundaries of the array because you will get a segmenation fault 44 | // like quick_sort(arr + 4, 10, arr_size, &compare_int); 45 | ``` 46 | 47 | >**NOTE:** You can use the same definition for every sorting method to sort just parts of the array not the intire array 48 | 49 | 50 | ## Some function not related to sorting 51 | 52 | ```C 53 | scl_error_t reverse_array(void *arr, size_t number_of_elem, size_t arr_elem_size); 54 | void* binary_search(void *arr, void *data, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 55 | ``` 56 | 57 | These two function are to reverse an array or just a part of it and the second is to find data from a sorted array (or subarray) 58 | 59 | Examples: 60 | 61 | ```C 62 | int arr = {23, 54, 11, 76, -1, -22, 43, 76, -19}; 63 | 64 | reverse_array(arr + 2, 4, sizeof(*arr)); 65 | 66 | // arr = {23, 54, -22, -1, 76, 11, 43, 76, -19} 67 | ``` 68 | 69 | ```C 70 | int arr = {23, 54, 11, 76, -1, -22, 43, 76, -19}; 71 | 72 | // First we sort the array or the subarray needed 73 | quick_sort(arr, sizeof(arr)/sizeof(*arr), sizeof(*arr), &compare_int); 74 | 75 | int *my_data = binary_search(arr + 1, &int{23}, 8, &compare_int); // compare function should match as compare function of the sorting method 76 | 77 | if (NULL != my_data) { 78 | *my_data = *my_data + 1000; 79 | } 80 | 81 | // If binary search function didn't fail the final result should be 82 | 83 | // arr = {-22, -19, -1, 11, 1023, 43, 54, 76, 76} 84 | ``` 85 | 86 | ## For some other examples of using sorting methods you can look up at [examples](../examples/sort_algorithms/) -------------------------------------------------------------------------------- /voidptr_data_structs/examples/README.md: -------------------------------------------------------------------------------- 1 | # How to run the examples of a specific data structure ? 2 | 3 | First change the directory to the specific data structure, for example: 4 | 5 | ```BASH 6 | cd avl_tree 7 | ``` 8 | 9 | In every data structure example directory you willl find one **Makefile** and some source files labeled as **example1.c** and so on. 10 | 11 | If you want just to execute one example, for example the first one you should type in your terminal: 12 | 13 | ```BASH 14 | make e1 15 | ``` 16 | 17 | If you want to execute all examples you shall type in your terminal: 18 | 19 | ```BASH 20 | make 21 | ``` 22 | 23 | >**NOTE:** I recommend you to execute all of examples at once. 24 | 25 | >**NOTE:** After every successfully executed example a message will be prompt in the terminal to inform you that execution went successfully 26 | 27 | After compiling and running the examples some files will be generated in current working directory, such as: 28 | 29 | * `out_**X**.txt` - where **X** is the number of the example, all the output generated by the example number **X** will be redirected in that file. 30 | 31 | * `valgrind_log_**X**.txt` - where **X** is the number of the example, the valgrind output log information will be redirected in that file. 32 | 33 | If you want to remove all the **txt** files and to clear all the directory from junk files you shall run: 34 | 35 | ```BASH 36 | make clean 37 | ``` 38 | 39 | >**NOTE:** Same actions you can do in any example data structure directory from **/examples/** directory 40 | 41 | >**NOTE:** Some examples are benchmarks, usually example number **3**, so do not worry if the running time is a bit to much. -------------------------------------------------------------------------------- /voidptr_data_structs/examples/avl_tree/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -c -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | DYNAMIC_LIB += /usr/local/lib/libdstruc.so 9 | 10 | VALGRIND_FLAGS += --leak-check=full \ 11 | --show-leak-kinds=all \ 12 | --track-origins=yes 13 | 14 | VALGRIND += valgrind $(VALGRIND_FLAGS) 15 | 16 | .PHONY: build e1 e2 e3 clean 17 | 18 | build: e1 e2 e3 19 | 20 | e1: example1 21 | @printf "Run AVL TREE example no. 1: " 22 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 23 | @printf "Done\n" 24 | @rm -rf example1 example1.o 25 | 26 | example1: example1.o $(DYNAMIC_LIB) 27 | @gcc example1.o -ldstruc -o example1 28 | 29 | example1.o: example1.c 30 | @gcc $(CFLAGS) example1.c 31 | 32 | e2: example2 33 | @printf "Run AVL TREE example no. 2: " 34 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 35 | @printf "Done\n" 36 | @rm -rf example2 example2.o 37 | 38 | example2: example2.o $(DYNAMIC_LIB) 39 | @gcc example2.o -ldstruc -o example2 40 | 41 | example2.o: example2.c 42 | @gcc $(CFLAGS) example2.c 43 | 44 | e3: example3 45 | @printf "Run AVL TREE example no. 3: " 46 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 47 | @printf "Done\n" 48 | @rm -rf example3 example3.o 49 | 50 | example3: example3.o $(DYNAMIC_LIB) 51 | @gcc example3.o -ldstruc -o example3 52 | 53 | example3.o: example3.c 54 | @gcc $(CFLAGS) example3.c 55 | 56 | clean: 57 | @rm -rf *.txt *o example1 example2 example3 58 | -------------------------------------------------------------------------------- /voidptr_data_structs/examples/avl_tree/example2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_NAME 20 5 | #define MAX_SURNAME 30 6 | 7 | typedef struct student_s { 8 | char *name; 9 | char *surname; 10 | unsigned int age; 11 | unsigned int class; 12 | unsigned char class_label; 13 | double GPA; 14 | int index; 15 | } student_t; 16 | 17 | int32_t compare_student(const void * const elem1, const void * const elem2) { 18 | const student_t * const s1 = elem1; 19 | const student_t * const s2 = elem2; 20 | 21 | int cmp = strncmp(s1->name, s2->name, MAX_NAME); 22 | 23 | if (0 != cmp) { 24 | return cmp; 25 | } 26 | 27 | cmp = strncmp(s1->surname, s2->surname, MAX_SURNAME); 28 | 29 | if (0 != cmp) { 30 | return cmp; 31 | } 32 | 33 | if (s1->GPA > s2->GPA) { 34 | cmp = 1; 35 | } else if (s1->GPA < s2->GPA) { 36 | cmp = -1; 37 | } else { 38 | cmp = 0; 39 | } 40 | 41 | if (cmp == 0) { 42 | return s1->index - s2->index; 43 | } 44 | 45 | return cmp; 46 | } 47 | 48 | void free_student(void * const elem) { 49 | if (NULL != elem) { 50 | student_t * const st = elem; 51 | 52 | free(st->name); 53 | st->name = NULL; 54 | 55 | free(st->surname); 56 | st->surname = NULL; 57 | 58 | st->age = 0; 59 | st->class = 0; 60 | st->class_label = '\0'; 61 | st->GPA = 0.0; 62 | st->index = -1; 63 | } 64 | } 65 | 66 | void print_student(void * const elem) { 67 | if (NULL != elem) { 68 | const student_t * const st = elem; 69 | 70 | printf("Student no. %d\n", st->index); 71 | printf("NAME: %s\n", st->name); 72 | printf("SURNAME: %s\n", st->surname); 73 | printf("Student GPA: %lf\n", st->GPA); 74 | printf("Student age: %u\n\n", st->age); 75 | } 76 | } 77 | 78 | void two_years_passed(void * const elem) { 79 | if (NULL != elem) { 80 | student_t * const st = elem; 81 | 82 | st->age = st->age + 2; 83 | 84 | print_student(st); 85 | } 86 | } 87 | 88 | int main(void) { 89 | FILE *fin = NULL; 90 | 91 | if ((fin = freopen("example2.in", "r", stdin)) == NULL) { 92 | exit(EXIT_FAILURE); 93 | } 94 | 95 | avl_tree_t *data_base = create_avl(&compare_student, &free_student, sizeof(student_t)); 96 | 97 | if (NULL == data_base) { 98 | exit(EXIT_FAILURE); 99 | } 100 | 101 | scl_error_t err = SCL_OK; 102 | 103 | 104 | int number_of_students = 0; 105 | int read = scanf("%d", &number_of_students); 106 | 107 | if (read > 1) { 108 | exit(EXIT_FAILURE); 109 | } 110 | 111 | student_t st; // Encapsulate the reading 112 | 113 | for (int i = 0; i < number_of_students; ++i) { 114 | st.name = malloc(MAX_NAME); 115 | st.surname = malloc(MAX_SURNAME); 116 | 117 | read = scanf("%s %s", st.name, st.surname); 118 | 119 | if (read > 2) { 120 | exit(EXIT_FAILURE); 121 | } 122 | 123 | read = scanf("%u %u %c %lf", &st.age, &st.class, &st.class_label, &st.GPA); 124 | 125 | if (read > 4) { 126 | exit(EXIT_FAILURE); 127 | } 128 | 129 | st.index = i; 130 | 131 | err = avl_insert(data_base, toptr(st)); 132 | 133 | if (SCL_OK != err) { 134 | scl_error_message(err); 135 | } 136 | } 137 | 138 | fclose(fin); 139 | 140 | FILE *fout = NULL; 141 | 142 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 143 | exit(EXIT_FAILURE); 144 | } 145 | 146 | printf("In the data base are %lu students\n\n", get_avl_size(data_base)); 147 | 148 | avl_traverse_inorder(data_base, &print_student); 149 | printf("\n\n"); 150 | 151 | printf("After two years our students got older so let's change their age\n\n"); 152 | avl_traverse_inorder(data_base, &two_years_passed); 153 | 154 | fclose(fout); 155 | 156 | free_avl(data_base); 157 | 158 | return 0; 159 | } -------------------------------------------------------------------------------- /voidptr_data_structs/examples/avl_tree/example2.in: -------------------------------------------------------------------------------- 1 | 40 2 | Rahul Vickers 3 | 14 12 A 4.0 4 | Ranveer Ramirez 5 | 15 12 B 3.7 6 | Bluebell Lawrence 7 | 14 10 C 1.3 8 | Avni Sharma 9 | 15 9 A 0.5 10 | Georgie Beltran 11 | 17 11 B 4.0 12 | Avery Pitt 13 | 19 20 C 4.0 14 | Jimmie Dalton 15 | 15 22 A 3.2 16 | Tazmin Skinner 17 | 16 43 B 3.1 18 | Tyla Dolan 19 | 14 21 C 3.0 20 | Meadow Mendoza 21 | 18 23 A 2.1 22 | Connah Farmer 23 | 19 12 B 2.5 24 | Aurora Sadler 25 | 15 22 C 2.8 26 | Cairon Ratliff 27 | 16 10 A 3.8 28 | Marie Mair 29 | 17 11 B 4.0 30 | Findlay Small 31 | 13 24 C 3.8 32 | Jazmine Avery 33 | 13 54 A 3.8 34 | Naomi Lara 35 | 16 21 B 1.1 36 | Nasir Mccormick 37 | 17 45 C 2.4 38 | Glen Becker 39 | 18 22 A 2.2 40 | Brent Coates 41 | 19 12 B 3.6 42 | Brielle Carson 43 | 14 10 C 4.0 44 | Lucy Joyner 45 | 13 11 A 3.7 46 | Rebekka Shepard 47 | 14 12 B 3.7 48 | Keaton Traynor 49 | 13 13 C 3.7 50 | Sufyaan Espinoza 51 | 13 14 A 2.2 52 | Zaina Webb 53 | 18 15 B 3.4 54 | Roxy Stevenson 55 | 19 16 C 4.0 56 | Donovan Blevins 57 | 19 17 A 1.1 58 | Phoenix Snow 59 | 17 18 B 1.9 60 | Agnes Doyle 61 | 19 19 C 2.0 62 | Tonisha Solis 63 | 17 20 A 3.0 64 | Aariz Allen 65 | 14 21 B 4.0 66 | Weronika Ho 67 | 16 22 C 2.0 68 | Milton Greaves 69 | 14 22 A 2.2 70 | Kwame Gilmore 71 | 14 22 B 2.5 72 | Eamon West 73 | 17 43 C 2.9 74 | Willem Kennedy 75 | 17 12 A 3.1 76 | Ben Chan 77 | 13 12 B 1.9 78 | Cherie Ochoa 79 | 15 43 C 2.2 80 | Priyanka Talley 81 | 18 12 A 4.0 -------------------------------------------------------------------------------- /voidptr_data_structs/examples/bst_tree/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -c -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | DYNAMIC_LIB += /usr/local/lib/libdstruc.so 9 | 10 | VALGRIND_FLAGS += --leak-check=full \ 11 | --show-leak-kinds=all \ 12 | --track-origins=yes 13 | 14 | VALGRIND += valgrind $(VALGRIND_FLAGS) 15 | 16 | .PHONY: build e1 e2 e3 clean 17 | 18 | build: e1 e2 e3 19 | 20 | e1: example1 21 | @printf "Run BST TREE example no. 1: " 22 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 23 | @printf "Done\n" 24 | @rm -rf example1 example1.o 25 | 26 | example1: example1.o $(DYNAMIC_LIB) 27 | @gcc example1.o -ldstruc -o example1 28 | 29 | example1.o: example1.c 30 | @gcc $(CFLAGS) example1.c 31 | 32 | e2: example2 33 | @printf "Run BST TREE example no. 2: " 34 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 35 | @printf "Done\n" 36 | @rm -rf example2 example2.o 37 | 38 | example2: example2.o $(DYNAMIC_LIB) 39 | @gcc example2.o -ldstruc -o example2 40 | 41 | example2.o: example2.c 42 | @gcc $(CFLAGS) example2.c 43 | 44 | e3: example3 45 | @printf "Run BST TREE example no. 3: " 46 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 47 | @printf "Done\n" 48 | @rm -rf example3 example3.o 49 | 50 | example3: example3.o $(DYNAMIC_LIB) 51 | @gcc example3.o -ldstruc -o example3 52 | 53 | example3.o: example3.c 54 | @gcc $(CFLAGS) example3.c 55 | 56 | clean: 57 | @rm -rf *.txt *o example1 example2 example3 58 | -------------------------------------------------------------------------------- /voidptr_data_structs/examples/bst_tree/example2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_NAME 20 5 | #define MAX_SURNAME 30 6 | 7 | typedef struct student_s { 8 | char *name; 9 | char *surname; 10 | unsigned int age; 11 | unsigned int class; 12 | unsigned char class_label; 13 | double GPA; 14 | int index; 15 | } student_t; 16 | 17 | int32_t compare_student(const void * const elem1, const void * const elem2) { 18 | const student_t * const s1 = elem1; 19 | const student_t * const s2 = elem2; 20 | 21 | int cmp = strncmp(s1->name, s2->name, MAX_NAME); 22 | 23 | if (0 != cmp) { 24 | return cmp; 25 | } 26 | 27 | cmp = strncmp(s1->surname, s2->surname, MAX_SURNAME); 28 | 29 | if (0 != cmp) { 30 | return cmp; 31 | } 32 | 33 | if (s1->GPA > s2->GPA) { 34 | cmp = 1; 35 | } else if (s1->GPA < s2->GPA) { 36 | cmp = -1; 37 | } else { 38 | cmp = 0; 39 | } 40 | 41 | if (cmp == 0) { 42 | return s1->index - s2->index; 43 | } 44 | 45 | return cmp; 46 | } 47 | 48 | void free_student(void * const elem) { 49 | if (NULL != elem) { 50 | student_t * const st = elem; 51 | 52 | free(st->name); 53 | st->name = NULL; 54 | 55 | free(st->surname); 56 | st->surname = NULL; 57 | 58 | st->age = 0; 59 | st->class = 0; 60 | st->class_label = '\0'; 61 | st->GPA = 0.0; 62 | st->index = -1; 63 | } 64 | } 65 | 66 | void print_student(void * const elem) { 67 | if (NULL != elem) { 68 | const student_t * const st = elem; 69 | 70 | printf("Student no. %d\n", st->index); 71 | printf("NAME: %s\n", st->name); 72 | printf("SURNAME: %s\n", st->surname); 73 | printf("Student GPA: %lf\n", st->GPA); 74 | printf("Student age: %u\n\n", st->age); 75 | } 76 | } 77 | 78 | void two_years_passed(void * const elem) { 79 | if (NULL != elem) { 80 | student_t * const st = elem; 81 | 82 | st->age = st->age + 2; 83 | 84 | print_student(st); 85 | } 86 | } 87 | 88 | int main(void) { 89 | FILE *fin = NULL; 90 | 91 | if ((fin = freopen("example2.in", "r", stdin)) == NULL) { 92 | exit(EXIT_FAILURE); 93 | } 94 | 95 | bst_tree_t *data_base = create_bst(&compare_student, &free_student, sizeof(student_t)); 96 | 97 | if (NULL == data_base) { 98 | exit(EXIT_FAILURE); 99 | } 100 | 101 | scl_error_t err = SCL_OK; 102 | 103 | 104 | int number_of_students = 0; 105 | int read = scanf("%d", &number_of_students); 106 | 107 | if (read > 1) { 108 | exit(EXIT_FAILURE); 109 | } 110 | 111 | student_t st; // Encapsulate the reading 112 | 113 | for (int i = 0; i < number_of_students; ++i) { 114 | st.name = malloc(MAX_NAME); 115 | st.surname = malloc(MAX_SURNAME); 116 | 117 | read = scanf("%s %s", st.name, st.surname); 118 | 119 | if (read > 2) { 120 | exit(EXIT_FAILURE); 121 | } 122 | 123 | read = scanf("%u %u %c %lf", &st.age, &st.class, &st.class_label, &st.GPA); 124 | 125 | if (read > 4) { 126 | exit(EXIT_FAILURE); 127 | } 128 | 129 | st.index = i; 130 | 131 | err = bst_insert(data_base, toptr(st)); 132 | 133 | if (SCL_OK != err) { 134 | scl_error_message(err); 135 | } 136 | } 137 | 138 | fclose(fin); 139 | 140 | FILE *fout = NULL; 141 | 142 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 143 | exit(EXIT_FAILURE); 144 | } 145 | 146 | printf("In the data base are %lu students\n\n", get_bst_size(data_base)); 147 | 148 | bst_traverse_inorder(data_base, &print_student); 149 | printf("\n\n"); 150 | 151 | printf("After two years our students got older so let's change their age\n\n"); 152 | bst_traverse_inorder(data_base, &two_years_passed); 153 | 154 | fclose(fout); 155 | 156 | free_bst(data_base); 157 | 158 | return 0; 159 | } -------------------------------------------------------------------------------- /voidptr_data_structs/examples/bst_tree/example2.in: -------------------------------------------------------------------------------- 1 | 40 2 | Rahul Vickers 3 | 14 12 A 4.0 4 | Ranveer Ramirez 5 | 15 12 B 3.7 6 | Bluebell Lawrence 7 | 14 10 C 1.3 8 | Avni Sharma 9 | 15 9 A 0.5 10 | Georgie Beltran 11 | 17 11 B 4.0 12 | Avery Pitt 13 | 19 20 C 4.0 14 | Jimmie Dalton 15 | 15 22 A 3.2 16 | Tazmin Skinner 17 | 16 43 B 3.1 18 | Tyla Dolan 19 | 14 21 C 3.0 20 | Meadow Mendoza 21 | 18 23 A 2.1 22 | Connah Farmer 23 | 19 12 B 2.5 24 | Aurora Sadler 25 | 15 22 C 2.8 26 | Cairon Ratliff 27 | 16 10 A 3.8 28 | Marie Mair 29 | 17 11 B 4.0 30 | Findlay Small 31 | 13 24 C 3.8 32 | Jazmine Avery 33 | 13 54 A 3.8 34 | Naomi Lara 35 | 16 21 B 1.1 36 | Nasir Mccormick 37 | 17 45 C 2.4 38 | Glen Becker 39 | 18 22 A 2.2 40 | Brent Coates 41 | 19 12 B 3.6 42 | Brielle Carson 43 | 14 10 C 4.0 44 | Lucy Joyner 45 | 13 11 A 3.7 46 | Rebekka Shepard 47 | 14 12 B 3.7 48 | Keaton Traynor 49 | 13 13 C 3.7 50 | Sufyaan Espinoza 51 | 13 14 A 2.2 52 | Zaina Webb 53 | 18 15 B 3.4 54 | Roxy Stevenson 55 | 19 16 C 4.0 56 | Donovan Blevins 57 | 19 17 A 1.1 58 | Phoenix Snow 59 | 17 18 B 1.9 60 | Agnes Doyle 61 | 19 19 C 2.0 62 | Tonisha Solis 63 | 17 20 A 3.0 64 | Aariz Allen 65 | 14 21 B 4.0 66 | Weronika Ho 67 | 16 22 C 2.0 68 | Milton Greaves 69 | 14 22 A 2.2 70 | Kwame Gilmore 71 | 14 22 B 2.5 72 | Eamon West 73 | 17 43 C 2.9 74 | Willem Kennedy 75 | 17 12 A 3.1 76 | Ben Chan 77 | 13 12 B 1.9 78 | Cherie Ochoa 79 | 15 43 C 2.2 80 | Priyanka Talley 81 | 18 12 A 4.0 -------------------------------------------------------------------------------- /voidptr_data_structs/examples/dlist/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -c -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | DYNAMIC_LIB += /usr/local/lib/libdstruc.so 9 | 10 | VALGRIND_FLAGS += --leak-check=full \ 11 | --show-leak-kinds=all \ 12 | --track-origins=yes 13 | 14 | VALGRIND += valgrind $(VALGRIND_FLAGS) 15 | 16 | .PHONY: build e1 e2 e3 clean 17 | 18 | build: e1 e2 e3 19 | 20 | e1: example1 21 | @printf "Run DLIST example no. 1: " 22 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 23 | @printf "Done\n" 24 | @rm -rf example1 example1.o 25 | 26 | example1: example1.o $(DYNAMIC_LIB) 27 | @gcc example1.o -ldstruc -o example1 28 | 29 | example1.o: example1.c 30 | @gcc $(CFLAGS) example1.c 31 | 32 | e2: example2 33 | @printf "Run DLIST example no. 2: " 34 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 35 | @printf "Done\n" 36 | @rm -rf example2 example2.o 37 | 38 | example2: example2.o $(DYNAMIC_LIB) 39 | @gcc example2.o -ldstruc -o example2 40 | 41 | example2.o: example2.c 42 | @gcc $(CFLAGS) example2.c 43 | 44 | e3: example3 45 | @printf "Run DLIST example no. 3: " 46 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 47 | @printf "Done\n" 48 | @rm -rf example3 example3.o 49 | 50 | example3: example3.o $(DYNAMIC_LIB) 51 | @gcc example3.o -ldstruc -o example3 52 | 53 | example3.o: example3.c 54 | @gcc $(CFLAGS) example3.c 55 | 56 | clean: 57 | @rm -rf *.txt *o example1 example2 example3 58 | -------------------------------------------------------------------------------- /voidptr_data_structs/examples/dlist/example2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_STRING_SIZE 100 4 | 5 | int32_t compare_ptr_str(const void * const elem1, const void * const elem2) { 6 | const char * const * const f1 = elem1; 7 | const char * const * const f2 = elem2; 8 | 9 | return strncmp(*f1, *f2, MAX_STRING_SIZE); 10 | } 11 | 12 | void free_str(void * const data) { 13 | if (NULL != data) { 14 | char ** const t_d = data; 15 | free(*t_d); 16 | *t_d = '\0'; 17 | } 18 | } 19 | 20 | void print_str(void * const data) { 21 | if (NULL != data) { 22 | const char * const * const t_d = data; 23 | 24 | printf("%s ", *t_d); 25 | } 26 | } 27 | 28 | int main(void) { 29 | FILE *fout = NULL; 30 | 31 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 32 | exit(EXIT_FAILURE); 33 | } 34 | 35 | FILE *fin = NULL; 36 | 37 | if ((fin = freopen("example2.in", "r", stdin)) == NULL) { 38 | printf("Could not open for reading\n"); 39 | exit(EXIT_FAILURE); 40 | } 41 | 42 | dlist_t *strings = create_dlist(&compare_string, NULL, MAX_STRING_SIZE); 43 | 44 | size_t str_num = 0; 45 | int check = 0; 46 | 47 | check = scanf("%lu", &str_num); 48 | 49 | if (check > 1) { 50 | exit(EXIT_FAILURE); 51 | } 52 | 53 | /////////////////////////////// Method 1 to work with strings 54 | 55 | char data[MAX_STRING_SIZE]; 56 | scl_error_t err = SCL_OK; 57 | 58 | for (size_t i = 0; i < str_num; ++i) { 59 | check = scanf("%s", data); 60 | 61 | if (check > 1) { 62 | exit(EXIT_FAILURE); 63 | } 64 | 65 | err = dlist_insert(strings, data); 66 | 67 | if (SCL_OK != err) { 68 | scl_error_message(err); 69 | } 70 | } 71 | 72 | //////////////////////////////// Method 2 to work with strings 73 | 74 | fseek(fin, 0, SEEK_SET); 75 | 76 | dlist_t *strings_2 = create_dlist(&compare_ptr_str, &free_str, sizeof(char *)); 77 | 78 | str_num = 0; 79 | check = 0; 80 | 81 | check = scanf("%lu", &str_num); 82 | 83 | if (check > 1) { 84 | exit(EXIT_FAILURE); 85 | } 86 | 87 | for (size_t i = 0; i < str_num; ++i) { 88 | char *ptr_data = malloc(MAX_STRING_SIZE); 89 | check = scanf("%s", ptr_data); 90 | 91 | if (check > 1) { 92 | exit(EXIT_FAILURE); 93 | } 94 | 95 | err = dlist_insert(strings_2, toptr(ptr_data)); 96 | 97 | if (SCL_OK != err) { 98 | scl_error_message(err); 99 | } 100 | } 101 | 102 | fclose(fin); 103 | 104 | printf("Print strings dlist by first method:\n"); 105 | dlist_traverse(strings, &print_string); 106 | printf("\n\n"); 107 | 108 | printf("Print strings dlist by second method:\n"); 109 | dlist_traverse(strings_2, &print_str); 110 | printf("\n"); 111 | 112 | char * const * find_1 = dlist_find_index(strings_2, 4); 113 | 114 | if (NULL != find_1) { 115 | printf("String from pos 4 is %s\n", *find_1); 116 | } 117 | 118 | free_dlist(strings); 119 | free_dlist(strings_2); 120 | 121 | fclose(fin); 122 | fclose(fout); 123 | 124 | return 0; 125 | } -------------------------------------------------------------------------------- /voidptr_data_structs/examples/dlist/example2.in: -------------------------------------------------------------------------------- 1 | 20 2 | Davila 3 | Jacobs 4 | Hobbs 5 | Schmidt 6 | Ochoa 7 | Harding 8 | Benjamin 9 | Lawson 10 | Mcpherson 11 | Fleming 12 | Mcintosh 13 | Lozano 14 | Patrick 15 | Pratt 16 | Powers 17 | Mack 18 | Simmons 19 | Herman 20 | Walter 21 | Kennedy -------------------------------------------------------------------------------- /voidptr_data_structs/examples/list/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -c -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | DYNAMIC_LIB += /usr/local/lib/libdstruc.so 9 | 10 | VALGRIND_FLAGS += --leak-check=full \ 11 | --show-leak-kinds=all \ 12 | --track-origins=yes 13 | 14 | VALGRIND += valgrind $(VALGRIND_FLAGS) 15 | 16 | .PHONY: build e1 e2 e3 clean 17 | 18 | build: e1 e2 e3 19 | 20 | e1: example1 21 | @printf "Run LIST example no. 1: " 22 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 23 | @printf "Done\n" 24 | @rm -rf example1 example1.o 25 | 26 | example1: example1.o $(DYNAMIC_LIB) 27 | @gcc example1.o -ldstruc -o example1 28 | 29 | example1.o: example1.c 30 | @gcc $(CFLAGS) example1.c 31 | 32 | e2: example2 33 | @printf "Run LIST example no. 2: " 34 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 35 | @printf "Done\n" 36 | @rm -rf example2 example2.o 37 | 38 | example2: example2.o $(DYNAMIC_LIB) 39 | @gcc example2.o -ldstruc -o example2 40 | 41 | example2.o: example2.c 42 | @gcc $(CFLAGS) example2.c 43 | 44 | e3: example3 45 | @printf "Run LIST example no. 3: " 46 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 47 | @printf "Done\n" 48 | @rm -rf example3 example3.o 49 | 50 | example3: example3.o $(DYNAMIC_LIB) 51 | @gcc example3.o -ldstruc -o example3 52 | 53 | example3.o: example3.c 54 | @gcc $(CFLAGS) example3.c 55 | 56 | clean: 57 | @rm -rf *.txt *o example1 example2 example3 58 | -------------------------------------------------------------------------------- /voidptr_data_structs/examples/list/example2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_STRING_SIZE 100 4 | 5 | int32_t compare_ptr_str(const void * const elem1, const void * const elem2) { 6 | const char * const * const f1 = elem1; 7 | const char * const * const f2 = elem2; 8 | 9 | return strncmp(*f1, *f2, MAX_STRING_SIZE); 10 | } 11 | 12 | void free_str(void * const data) { 13 | if (NULL != data) { 14 | char ** const t_d = data; 15 | free(*t_d); 16 | *t_d = '\0'; 17 | } 18 | } 19 | 20 | void print_str(void * const data) { 21 | if (NULL != data) { 22 | const char * const * const t_d = data; 23 | 24 | printf("%s ", *t_d); 25 | } 26 | } 27 | 28 | int main(void) { 29 | FILE *fout = NULL; 30 | 31 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 32 | exit(EXIT_FAILURE); 33 | } 34 | 35 | FILE *fin = NULL; 36 | 37 | if ((fin = freopen("example2.in", "r", stdin)) == NULL) { 38 | printf("Could not open for reading\n"); 39 | exit(EXIT_FAILURE); 40 | } 41 | 42 | list_t *strings = create_list(&compare_string, NULL, MAX_STRING_SIZE); 43 | 44 | size_t str_num = 0; 45 | int check = 0; 46 | 47 | check = scanf("%lu", &str_num); 48 | 49 | if (check > 1) { 50 | exit(EXIT_FAILURE); 51 | } 52 | 53 | /////////////////////////////// Method 1 to work with strings 54 | 55 | char data[MAX_STRING_SIZE]; 56 | scl_error_t err = SCL_OK; 57 | 58 | for (size_t i = 0; i < str_num; ++i) { 59 | check = scanf("%s", data); 60 | 61 | if (check > 1) { 62 | exit(EXIT_FAILURE); 63 | } 64 | 65 | err = list_insert(strings, data); 66 | 67 | if (SCL_OK != err) { 68 | scl_error_message(err); 69 | } 70 | } 71 | 72 | //////////////////////////////// Method 2 to work with strings 73 | 74 | fseek(fin, 0, SEEK_SET); 75 | 76 | list_t *strings_2 = create_list(&compare_ptr_str, &free_str, sizeof(char *)); 77 | 78 | str_num = 0; 79 | check = 0; 80 | 81 | check = scanf("%lu", &str_num); 82 | 83 | if (check > 1) { 84 | exit(EXIT_FAILURE); 85 | } 86 | 87 | for (size_t i = 0; i < str_num; ++i) { 88 | char *ptr_data = malloc(MAX_STRING_SIZE); 89 | check = scanf("%s", ptr_data); 90 | 91 | if (check > 1) { 92 | exit(EXIT_FAILURE); 93 | } 94 | 95 | err = list_insert(strings_2, toptr(ptr_data)); 96 | 97 | if (SCL_OK != err) { 98 | scl_error_message(err); 99 | } 100 | } 101 | 102 | fclose(fin); 103 | 104 | printf("Print strings list by first method:\n"); 105 | list_traverse(strings, &print_string); 106 | printf("\n\n"); 107 | 108 | printf("Print strings list by second method:\n"); 109 | list_traverse(strings_2, &print_str); 110 | printf("\n"); 111 | 112 | char * const * find_1 = list_find_index(strings_2, 4); 113 | 114 | if (NULL != find_1) { 115 | printf("String from pos 4 is %s\n", *find_1); 116 | } 117 | 118 | free_list(strings); 119 | free_list(strings_2); 120 | 121 | fclose(fin); 122 | fclose(fout); 123 | 124 | return 0; 125 | } -------------------------------------------------------------------------------- /voidptr_data_structs/examples/list/example2.in: -------------------------------------------------------------------------------- 1 | 20 2 | Davila 3 | Jacobs 4 | Hobbs 5 | Schmidt 6 | Ochoa 7 | Harding 8 | Benjamin 9 | Lawson 10 | Mcpherson 11 | Fleming 12 | Mcintosh 13 | Lozano 14 | Patrick 15 | Pratt 16 | Powers 17 | Mack 18 | Simmons 19 | Herman 20 | Walter 21 | Kennedy -------------------------------------------------------------------------------- /voidptr_data_structs/examples/priority_queue/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -c -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | DYNAMIC_LIB += /usr/local/lib/libdstruc.so 9 | 10 | VALGRIND_FLAGS += --leak-check=full \ 11 | --show-leak-kinds=all \ 12 | --track-origins=yes 13 | 14 | VALGRIND += valgrind $(VALGRIND_FLAGS) 15 | 16 | .PHONY: build e1 e2 clean 17 | 18 | build: e1 e2 19 | 20 | e1: example1 21 | @printf "Run PRIORITY QUEUE example no. 1: " 22 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 23 | @printf "Done\n" 24 | @rm -rf example1 example1.o 25 | 26 | example1: example1.o $(DYNAMIC_LIB) 27 | @gcc example1.o -ldstruc -o example1 28 | 29 | example1.o: example1.c 30 | @gcc $(CFLAGS) example1.c 31 | 32 | e2: example2 33 | @printf "Run PRIORITY QUEUE example no. 2: " 34 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 35 | @printf "Done\n" 36 | @rm -rf example2 example2.o 37 | 38 | example2: example2.o $(DYNAMIC_LIB) 39 | @gcc example2.o -ldstruc -o example2 40 | 41 | example2.o: example2.c 42 | @gcc $(CFLAGS) example2.c 43 | 44 | clean: 45 | @rm -rf *.txt *o example1 example2 46 | -------------------------------------------------------------------------------- /voidptr_data_structs/examples/priority_queue/example1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_ELEMENTS 100 4 | #define MAX_STRING 100 5 | 6 | int32_t compute_voc(const char * const str) { 7 | int32_t voc_diff = 0; 8 | 9 | for (size_t iter = 0; str[iter] != 0; ++iter) { 10 | if (('a' == str[iter]) || 11 | ('e' == str[iter]) || 12 | ('i' == str[iter]) || 13 | ('o' == str[iter]) || 14 | ('u' == str[iter]) || 15 | ('A' == str[iter]) || 16 | ('E' == str[iter]) || 17 | ('I' == str[iter]) || 18 | ('O' == str[iter]) || 19 | ('U' == str[iter])) { 20 | ++voc_diff; 21 | } 22 | } 23 | 24 | return voc_diff; 25 | } 26 | 27 | void capitalize(void * const data) { 28 | if (NULL != data) { 29 | char * const t_d = data; 30 | 31 | if ((t_d[0] >= 97) && (t_d[0] <= 122)) { 32 | t_d[0] -= 32; 33 | } 34 | } 35 | } 36 | 37 | int main(void) { 38 | FILE *fout = NULL; 39 | 40 | if ((fout = freopen("out_1.txt", "w", stdout)) == NULL) { 41 | exit(EXIT_FAILURE); 42 | } 43 | 44 | FILE *fin = NULL; 45 | 46 | if ((fin = freopen("example1.in", "r", stdin)) == NULL) { 47 | fclose(fout); 48 | exit(EXIT_FAILURE); 49 | } 50 | 51 | priority_queue_t *pq = create_priority_queue(MAX_ELEMENTS, &compare_int, &compare_string, NULL, NULL, sizeof(int), MAX_STRING); 52 | 53 | int elem_num = 0; 54 | 55 | int check = scanf("%d", &elem_num); 56 | 57 | if (check > 1) { 58 | exit(EXIT_FAILURE); 59 | } 60 | 61 | 62 | scl_error_t err = SCL_OK; 63 | 64 | for (int i = 0; i < elem_num; ++i) { 65 | char read_data[MAX_STRING]; 66 | 67 | check = scanf("%s", read_data); 68 | 69 | if (check > 1) { 70 | exit(EXIT_FAILURE); 71 | } 72 | 73 | err = pri_queue_push(pq, ltoptr(int32_t, compute_voc(read_data)), read_data); 74 | 75 | if (SCL_OK != err) { 76 | scl_error_message(err); 77 | } 78 | } 79 | 80 | printf("Priority queue data:\n"); 81 | pri_queue_traverse(pq, &print_string); 82 | printf("\n\n"); 83 | 84 | const char *top = pri_queue_top(pq); 85 | 86 | if (NULL != top) { 87 | const int *voc = pri_queue_top_pri(pq); 88 | 89 | printf("The top elements is \"%s\" and has %d vowels:\n\n", top, *voc); 90 | } 91 | 92 | printf("Let's remove the top element:\n"); 93 | err = pri_queue_pop(pq); 94 | 95 | if (SCL_OK != err) { 96 | scl_error_message(err); 97 | } 98 | 99 | pri_queue_traverse(pq, &print_string); 100 | printf("\n\n"); 101 | 102 | printf("Let's change now the top element into \"!!!!HELLO!!!!\" and also to change it's priority\n"); 103 | 104 | size_t top_index = pri_find_data_index(pq, "communication"); 105 | 106 | err = change_node_data(pq, top_index, "!!!!HELLO!!!!"); 107 | 108 | if (SCL_OK != err) { 109 | scl_error_message(err); 110 | } 111 | 112 | err = change_node_priority(pq, top_index, ltoptr(int32_t, compute_voc("!!!!HELLO!!!!"))); 113 | 114 | if (SCL_OK != err) { 115 | scl_error_message(err); 116 | } 117 | 118 | pri_queue_traverse(pq, &print_string); 119 | printf("\n\n"); 120 | 121 | printf("Let's pop 10 elems from priority queue:\n"); 122 | for (size_t i = 0; i < 10; ++i) { 123 | err = pri_queue_pop(pq); 124 | 125 | if (SCL_OK != err) { 126 | scl_error_message(err); 127 | } 128 | } 129 | 130 | pri_queue_traverse(pq, &print_string); 131 | printf("\n\n"); 132 | 133 | printf("For the remaining strings let's capitalize them\n"); 134 | pri_queue_traverse(pq, &capitalize); 135 | pri_queue_traverse(pq, &print_string); 136 | printf("\n"); 137 | 138 | fclose(fin); 139 | 140 | free_priority_queue(pq); 141 | 142 | fclose(fout); 143 | 144 | return 0; 145 | } -------------------------------------------------------------------------------- /voidptr_data_structs/examples/priority_queue/example1.in: -------------------------------------------------------------------------------- 1 | 40 2 | depth 3 | republic 4 | guest 5 | thought 6 | bathroom 7 | poet 8 | law 9 | scene 10 | appearance 11 | concept 12 | foundation 13 | hotel 14 | grocery 15 | method 16 | association 17 | girlfriend 18 | consequence 19 | knowledge 20 | refrigerator 21 | piano 22 | woman 23 | communication 24 | hospital 25 | supermarket 26 | owner 27 | theory 28 | article 29 | drawing 30 | two 31 | food 32 | beer 33 | recommendation 34 | accident 35 | manufacturer 36 | dinner 37 | outcome 38 | skill 39 | region 40 | decision 41 | news -------------------------------------------------------------------------------- /voidptr_data_structs/examples/queue/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -c -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | DYNAMIC_LIB += /usr/local/lib/libdstruc.so 9 | 10 | VALGRIND_FLAGS += --leak-check=full \ 11 | --show-leak-kinds=all \ 12 | --track-origins=yes 13 | 14 | VALGRIND += valgrind $(VALGRIND_FLAGS) 15 | 16 | .PHONY: build e1 e2 e3 clean 17 | 18 | build: e1 e2 e3 19 | 20 | e1: example1 21 | @printf "Run QUEUE example no. 1: " 22 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 23 | @printf "Done\n" 24 | @rm -rf example1 example1.o 25 | 26 | example1: example1.o $(DYNAMIC_LIB) 27 | @gcc example1.o -ldstruc -o example1 28 | 29 | example1.o: example1.c 30 | @gcc $(CFLAGS) example1.c 31 | 32 | e2: example2 33 | @printf "Run QUEUE example no. 2: " 34 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 35 | @printf "Done\n" 36 | @rm -rf example2 example2.o 37 | 38 | example2: example2.o $(DYNAMIC_LIB) 39 | @gcc example2.o -ldstruc -o example2 40 | 41 | example2.o: example2.c 42 | @gcc $(CFLAGS) example2.c 43 | 44 | e3: example3 45 | @printf "Run QUEUE example no. 3: " 46 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 47 | @printf "Done\n" 48 | @rm -rf example3 example3.o 49 | 50 | example3: example3.o $(DYNAMIC_LIB) 51 | @gcc example3.o -ldstruc -o example3 52 | 53 | example3.o: example3.c 54 | @gcc $(CFLAGS) example3.c 55 | 56 | clean: 57 | @rm -rf *.txt *o example1 example2 example3 58 | -------------------------------------------------------------------------------- /voidptr_data_structs/examples/queue/example1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void print_int_br(void * const data) { 5 | if (NULL != data) { 6 | static int br = 0; 7 | 8 | if ((br % 10 == 0) && (0 != br)) { 9 | printf("\n"); 10 | } 11 | 12 | printf("%d ", *(const int * const)data); 13 | 14 | ++br; 15 | } 16 | } 17 | 18 | int main(void) { 19 | FILE *fout = NULL; 20 | 21 | if ((fout = freopen("out_1.txt", "w", stdout)) == NULL) { 22 | exit(EXIT_FAILURE); 23 | } 24 | 25 | srand(time(NULL)); 26 | 27 | queue_t *qq = create_queue(NULL, sizeof(int)); 28 | 29 | if (NULL == qq) { 30 | exit(EXIT_FAILURE); 31 | } 32 | 33 | scl_error_t err = SCL_OK; 34 | 35 | for (int i = 0; i < 100; ++i) { 36 | int data = rand() % 100 + 10; 37 | 38 | err = queue_push(qq, toptr(data)); 39 | // queue_push(qq, ltoptr(int, data)); 40 | 41 | if (SCL_OK != err) { 42 | scl_error_message(err); 43 | } 44 | } 45 | 46 | if (1 == is_queue_empty(qq)) { 47 | printf("Oops !!! Something went wring with insertion\n"); 48 | exit(EXIT_FAILURE); 49 | } 50 | 51 | printf("Generated queue has %lu elements:\n", get_queue_size(qq)); 52 | print_queue(qq, &print_int_br); 53 | printf("\n"); 54 | 55 | const int *front = queue_front(qq); 56 | const int *back = queue_back(qq); 57 | 58 | if (NULL != front) { 59 | printf("Front element is %d\n", *front); 60 | } else { 61 | printf("Could not fetch front element\n"); 62 | } 63 | 64 | if (NULL != back) { 65 | printf("Back element is %d\n\n", *back); 66 | } else { 67 | printf("Could not fetch back element\n\n"); 68 | } 69 | 70 | printf("Let's pop half of the queue:\n"); 71 | 72 | for (int i = 0; i < 50; ++i) { 73 | err = queue_pop(qq); 74 | 75 | if (SCL_OK != err) { 76 | scl_error_message(err); 77 | } 78 | } 79 | 80 | print_queue(qq, &print_int); 81 | printf("\n"); 82 | 83 | free_queue(qq); 84 | 85 | fclose(fout); 86 | 87 | return 0; 88 | } -------------------------------------------------------------------------------- /voidptr_data_structs/examples/queue/example2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void free_q(void * const q) { 5 | if (NULL != q) { 6 | queue_t ** const t_q = q; 7 | 8 | free_queue(*t_q); 9 | } 10 | } 11 | 12 | int main(void) { 13 | FILE *fout = NULL; 14 | 15 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 16 | exit(EXIT_FAILURE); 17 | } 18 | 19 | queue_t *qqs = create_queue(&free_q, sizeof(qqs)); 20 | 21 | if (NULL != qqs) { 22 | srand(time(NULL)); 23 | 24 | scl_error_t err = SCL_OK; 25 | 26 | for (int i = 0; i < 3; ++i) { 27 | queue_t *qq = create_queue(NULL, sizeof(int)); 28 | 29 | for (int j = 0; j < 10; ++j) { 30 | err = queue_push(qq, ltoptr(int, rand() % 100 + 10)); 31 | 32 | if (SCL_OK != err) { 33 | scl_error_message(err); 34 | } 35 | } 36 | 37 | err = queue_push(qqs, toptr(qq)); 38 | 39 | if (SCL_OK != err) { 40 | scl_error_message(err); 41 | } 42 | } 43 | 44 | int queue_num = 0; 45 | 46 | while (!is_queue_empty(qqs)) { 47 | printf("Queue number %d:\n", queue_num++); 48 | 49 | queue_t * const * front = queue_front(qqs); 50 | 51 | while (!is_queue_empty(*front)) { 52 | const int *front_elem = queue_front(*front); 53 | 54 | printf("%d ", *front_elem); 55 | 56 | err = queue_pop(*front); 57 | 58 | if (SCL_OK != err) { 59 | scl_error_message(err); 60 | } 61 | } 62 | 63 | printf("\n\n"); 64 | 65 | err = queue_pop(qqs); 66 | 67 | if (SCL_OK != err) { 68 | scl_error_message(err); 69 | } 70 | } 71 | 72 | free_queue(qqs); 73 | } 74 | 75 | fclose(fout); 76 | 77 | return 0; 78 | } -------------------------------------------------------------------------------- /voidptr_data_structs/examples/red_black_tree/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -c -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | DYNAMIC_LIB += /usr/local/lib/libdstruc.so 9 | 10 | VALGRIND_FLAGS += --leak-check=full \ 11 | --show-leak-kinds=all \ 12 | --track-origins=yes 13 | 14 | VALGRIND += valgrind $(VALGRIND_FLAGS) 15 | 16 | .PHONY: build e1 e2 e3 clean 17 | 18 | build: e1 e2 e3 19 | 20 | e1: example1 21 | @printf "Run RBK TREE example no. 1: " 22 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 23 | @printf "Done\n" 24 | @rm -rf example1 example1.o 25 | 26 | example1: example1.o $(DYNAMIC_LIB) 27 | @gcc example1.o -ldstruc -o example1 28 | 29 | example1.o: example1.c 30 | @gcc $(CFLAGS) example1.c 31 | 32 | e2: example2 33 | @printf "Run RBK TREE example no. 2: " 34 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 35 | @printf "Done\n" 36 | @rm -rf example2 example2.o 37 | 38 | example2: example2.o $(DYNAMIC_LIB) 39 | @gcc example2.o -ldstruc -o example2 40 | 41 | example2.o: example2.c 42 | @gcc $(CFLAGS) example2.c 43 | 44 | e3: example3 45 | @printf "Run RBK TREE example no. 3: " 46 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 47 | @printf "Done\n" 48 | @rm -rf example3 example3.o 49 | 50 | example3: example3.o $(DYNAMIC_LIB) 51 | @gcc example3.o -ldstruc -o example3 52 | 53 | example3.o: example3.c 54 | @gcc $(CFLAGS) example3.c 55 | 56 | clean: 57 | @rm -rf *.txt *o example1 example2 example3 58 | -------------------------------------------------------------------------------- /voidptr_data_structs/examples/red_black_tree/example2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_NAME 20 5 | #define MAX_SURNAME 30 6 | 7 | typedef struct student_s { 8 | char *name; 9 | char *surname; 10 | unsigned int age; 11 | unsigned int class; 12 | unsigned char class_label; 13 | double GPA; 14 | int index; 15 | } student_t; 16 | 17 | int32_t compare_student(const void * const elem1, const void * const elem2) { 18 | const student_t * const s1 = elem1; 19 | const student_t * const s2 = elem2; 20 | 21 | int cmp = strncmp(s1->name, s2->name, MAX_NAME); 22 | 23 | if (0 != cmp) { 24 | return cmp; 25 | } 26 | 27 | cmp = strncmp(s1->surname, s2->surname, MAX_SURNAME); 28 | 29 | if (0 != cmp) { 30 | return cmp; 31 | } 32 | 33 | if (s1->GPA > s2->GPA) { 34 | cmp = 1; 35 | } else if (s1->GPA < s2->GPA) { 36 | cmp = -1; 37 | } else { 38 | cmp = 0; 39 | } 40 | 41 | if (cmp == 0) { 42 | return s1->index - s2->index; 43 | } 44 | 45 | return cmp; 46 | } 47 | 48 | void free_student(void * const elem) { 49 | if (NULL != elem) { 50 | student_t * const st = elem; 51 | 52 | free(st->name); 53 | st->name = NULL; 54 | 55 | free(st->surname); 56 | st->surname = NULL; 57 | 58 | st->age = 0; 59 | st->class = 0; 60 | st->class_label = '\0'; 61 | st->GPA = 0.0; 62 | st->index = -1; 63 | } 64 | } 65 | 66 | void print_student(void * const elem) { 67 | if (NULL != elem) { 68 | const student_t * const st = elem; 69 | 70 | printf("Student no. %d\n", st->index); 71 | printf("NAME: %s\n", st->name); 72 | printf("SURNAME: %s\n", st->surname); 73 | printf("Student GPA: %lf\n", st->GPA); 74 | printf("Student age: %u\n\n", st->age); 75 | } 76 | } 77 | 78 | void two_years_passed(void * const elem) { 79 | if (NULL != elem) { 80 | student_t * const st = elem; 81 | 82 | st->age = st->age + 2; 83 | 84 | print_student(st); 85 | } 86 | } 87 | 88 | int main(void) { 89 | FILE *fin = NULL; 90 | 91 | if ((fin = freopen("example2.in", "r", stdin)) == NULL) { 92 | exit(EXIT_FAILURE); 93 | } 94 | 95 | rbk_tree_t *data_base = create_rbk(&compare_student, &free_student, sizeof(student_t)); 96 | 97 | if (NULL == data_base) { 98 | exit(EXIT_FAILURE); 99 | } 100 | 101 | scl_error_t err = SCL_OK; 102 | 103 | 104 | int number_of_students = 0; 105 | int read = scanf("%d", &number_of_students); 106 | 107 | if (read > 1) { 108 | exit(EXIT_FAILURE); 109 | } 110 | 111 | student_t st; // Encapsulate the reading 112 | 113 | for (int i = 0; i < number_of_students; ++i) { 114 | st.name = malloc(MAX_NAME); 115 | st.surname = malloc(MAX_SURNAME); 116 | 117 | read = scanf("%s %s", st.name, st.surname); 118 | 119 | if (read > 2) { 120 | exit(EXIT_FAILURE); 121 | } 122 | 123 | read = scanf("%u %u %c %lf", &st.age, &st.class, &st.class_label, &st.GPA); 124 | 125 | if (read > 4) { 126 | exit(EXIT_FAILURE); 127 | } 128 | 129 | st.index = i; 130 | 131 | err = rbk_insert(data_base, toptr(st)); 132 | 133 | if (SCL_OK != err) { 134 | scl_error_message(err); 135 | } 136 | } 137 | 138 | fclose(fin); 139 | 140 | FILE *fout = NULL; 141 | 142 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 143 | exit(EXIT_FAILURE); 144 | } 145 | 146 | printf("In the data base are %lu students\n\n", get_rbk_size(data_base)); 147 | 148 | rbk_traverse_inorder(data_base, &print_student); 149 | printf("\n\n"); 150 | 151 | printf("After two years our students got older so let's change their age\n\n"); 152 | rbk_traverse_inorder(data_base, &two_years_passed); 153 | 154 | fclose(fout); 155 | 156 | free_rbk(data_base); 157 | 158 | return 0; 159 | } -------------------------------------------------------------------------------- /voidptr_data_structs/examples/red_black_tree/example2.in: -------------------------------------------------------------------------------- 1 | 40 2 | Rahul Vickers 3 | 14 12 A 4.0 4 | Ranveer Ramirez 5 | 15 12 B 3.7 6 | Bluebell Lawrence 7 | 14 10 C 1.3 8 | Avni Sharma 9 | 15 9 A 0.5 10 | Georgie Beltran 11 | 17 11 B 4.0 12 | Avery Pitt 13 | 19 20 C 4.0 14 | Jimmie Dalton 15 | 15 22 A 3.2 16 | Tazmin Skinner 17 | 16 43 B 3.1 18 | Tyla Dolan 19 | 14 21 C 3.0 20 | Meadow Mendoza 21 | 18 23 A 2.1 22 | Connah Farmer 23 | 19 12 B 2.5 24 | Aurora Sadler 25 | 15 22 C 2.8 26 | Cairon Ratliff 27 | 16 10 A 3.8 28 | Marie Mair 29 | 17 11 B 4.0 30 | Findlay Small 31 | 13 24 C 3.8 32 | Jazmine Avery 33 | 13 54 A 3.8 34 | Naomi Lara 35 | 16 21 B 1.1 36 | Nasir Mccormick 37 | 17 45 C 2.4 38 | Glen Becker 39 | 18 22 A 2.2 40 | Brent Coates 41 | 19 12 B 3.6 42 | Brielle Carson 43 | 14 10 C 4.0 44 | Lucy Joyner 45 | 13 11 A 3.7 46 | Rebekka Shepard 47 | 14 12 B 3.7 48 | Keaton Traynor 49 | 13 13 C 3.7 50 | Sufyaan Espinoza 51 | 13 14 A 2.2 52 | Zaina Webb 53 | 18 15 B 3.4 54 | Roxy Stevenson 55 | 19 16 C 4.0 56 | Donovan Blevins 57 | 19 17 A 1.1 58 | Phoenix Snow 59 | 17 18 B 1.9 60 | Agnes Doyle 61 | 19 19 C 2.0 62 | Tonisha Solis 63 | 17 20 A 3.0 64 | Aariz Allen 65 | 14 21 B 4.0 66 | Weronika Ho 67 | 16 22 C 2.0 68 | Milton Greaves 69 | 14 22 A 2.2 70 | Kwame Gilmore 71 | 14 22 B 2.5 72 | Eamon West 73 | 17 43 C 2.9 74 | Willem Kennedy 75 | 17 12 A 3.1 76 | Ben Chan 77 | 13 12 B 1.9 78 | Cherie Ochoa 79 | 15 43 C 2.2 80 | Priyanka Talley 81 | 18 12 A 4.0 -------------------------------------------------------------------------------- /voidptr_data_structs/examples/sort_algorithms/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -c -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | DYNAMIC_LIB += /usr/local/lib/libdstruc.so 9 | 10 | VALGRIND_FLAGS += --leak-check=full \ 11 | --show-leak-kinds=all \ 12 | --track-origins=yes 13 | 14 | VALGRIND += valgrind $(VALGRIND_FLAGS) 15 | 16 | .PHONY: build e1 clean 17 | 18 | build: e1 19 | 20 | e1: example1 21 | @printf "Run SORT ALGOS example no. 1: " 22 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 23 | @printf "Done\n" 24 | @rm -rf example1 example1.o 25 | 26 | example1: example1.o $(DYNAMIC_LIB) 27 | @gcc example1.o -ldstruc -o example1 28 | 29 | example1.o: example1.c 30 | @gcc $(CFLAGS) example1.c 31 | 32 | clean: 33 | @rm -rf *.txt *o example1 34 | -------------------------------------------------------------------------------- /voidptr_data_structs/examples/sort_algorithms/example1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_ARR 40 5 | 6 | int32_t cmp(const void * const data1, const void * const data2) { 7 | /* Check if data1 and data2 are valid */ 8 | if ((NULL == data1) || (NULL == data2)) { 9 | errno = ENODATA; 10 | perror("Data is not allocated"); 11 | exit(EXIT_FAILURE); 12 | 13 | return 0; 14 | } 15 | 16 | /* Cast data types to working type */ 17 | const int * const typed_data1 = data1; 18 | const int * const typed_data2 = data2; 19 | 20 | /* Compare data type */ 21 | if (*typed_data1 > *typed_data2) { 22 | return -1; 23 | } else if (*typed_data1 < *typed_data2) { 24 | return 1; 25 | } else { 26 | return 0; 27 | } 28 | } 29 | 30 | int main(void) { 31 | FILE *fout = NULL; 32 | 33 | if ((fout = freopen("out_1.txt", "w", stdout)) == NULL) { 34 | exit(EXIT_FAILURE); 35 | } 36 | 37 | int arr[MAX_ARR]; 38 | 39 | srand(time(NULL)); 40 | 41 | printf("Generated array is:\n"); 42 | for (int i = 0; i < MAX_ARR; ++i) { 43 | arr[i] = rand() % 100 + 10; 44 | 45 | printf("%d ", arr[i]); 46 | } 47 | 48 | printf("\n\n"); 49 | 50 | printf("Let's sort the first 10 elements by quick sort (Increase order):\n"); 51 | quick_sort(arr, 10, sizeof(*arr), &compare_int); 52 | for (int i = 0; i < MAX_ARR; ++i) { 53 | printf("%d ", arr[i]); 54 | } 55 | printf("\n\n"); 56 | 57 | printf("Let's sort the next 10 elements by heap sort (Deacrease order):\n"); 58 | 59 | // Because heap sort is a priority queue and by default 60 | // a pri queue is a max pri queue then you should specify 61 | // the same compare_int function for heap sort to sort 62 | // elements into deacreasing order. 63 | heap_sort(arr + 10, 10, sizeof(*arr), &compare_int); 64 | for (int i = 0; i < MAX_ARR; ++i) { 65 | printf("%d ", arr[i]); 66 | } 67 | printf("\n\n"); 68 | 69 | printf("Let's reverse the part of the array that we just sorted:\n"); 70 | reverse_array(arr + 10, 10, sizeof(*arr)); 71 | for (int i = 0; i < MAX_ARR; ++i) { 72 | printf("%d ", arr[i]); 73 | } 74 | printf("\n\n"); 75 | 76 | printf("Let's sort now all array by quick sort again (Deacrease):\n"); 77 | quick_sort(arr, MAX_ARR, sizeof(*arr), &cmp); 78 | for (int i = 0; i < MAX_ARR; ++i) { 79 | printf("%d ", arr[i]); 80 | } 81 | printf("\n\n"); 82 | 83 | printf("Let's find some values in array with binary search method:\n"); 84 | 85 | for (int i = 10; i <= 110; ++i) { 86 | 87 | // Function specified with binary search to compare data 88 | // has to be same as the function specified in the sorting method 89 | // otherwise unknown result can be printed 90 | int *find = binary_search(arr, toptr(i), MAX_ARR, sizeof(*arr), &cmp); 91 | 92 | if (NULL != find) { 93 | printf("Binary search found %d value in array\n", i); 94 | } else { 95 | printf("Binary search did not find %d value in array\n", i); 96 | } 97 | } 98 | 99 | fclose(fout); 100 | 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /voidptr_data_structs/examples/stack/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -c -g -Wall -Wextra -Wpedantic \ 2 | -Wformat=2 -Wno-unused-parameter \ 3 | -Wshadow -Wwrite-strings -Wstrict-prototypes \ 4 | -Wold-style-definition -Wredundant-decls \ 5 | -Wnested-externs -Wmissing-include-dirs \ 6 | -Wjump-misses-init -Wlogical-op -O2 7 | 8 | DYNAMIC_LIB += /usr/local/lib/libdstruc.so 9 | 10 | VALGRIND_FLAGS += --leak-check=full \ 11 | --show-leak-kinds=all \ 12 | --track-origins=yes 13 | 14 | VALGRIND += valgrind $(VALGRIND_FLAGS) 15 | 16 | .PHONY: build e1 e2 e3 clean 17 | 18 | build: e1 e2 e3 19 | 20 | e1: example1 21 | @printf "Run STACK example no. 1: " 22 | @$(VALGRIND) --log-file=valgrind_log_1.txt ./example1 23 | @printf "Done\n" 24 | @rm -rf example1 example1.o 25 | 26 | example1: example1.o $(DYNAMIC_LIB) 27 | @gcc example1.o -ldstruc -o example1 28 | 29 | example1.o: example1.c 30 | @gcc $(CFLAGS) example1.c 31 | 32 | e2: example2 33 | @printf "Run STACK example no. 2: " 34 | @$(VALGRIND) --log-file=valgrind_log_2.txt ./example2 35 | @printf "Done\n" 36 | @rm -rf example2 example2.o 37 | 38 | example2: example2.o $(DYNAMIC_LIB) 39 | @gcc example2.o -ldstruc -o example2 40 | 41 | example2.o: example2.c 42 | @gcc $(CFLAGS) example2.c 43 | 44 | e3: example3 45 | @printf "Run STACK example no. 3: " 46 | @$(VALGRIND) --log-file=valgrind_log_3.txt ./example3 47 | @printf "Done\n" 48 | @rm -rf example3 example3.o 49 | 50 | example3: example3.o $(DYNAMIC_LIB) 51 | @gcc example3.o -ldstruc -o example3 52 | 53 | example3.o: example3.c 54 | @gcc $(CFLAGS) example3.c 55 | 56 | clean: 57 | @rm -rf *.txt *o example1 example2 example3 58 | -------------------------------------------------------------------------------- /voidptr_data_structs/examples/stack/example1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void print_int_br(void * const data) { 5 | if (NULL != data) { 6 | static int br = 0; 7 | 8 | if ((br % 10 == 0) && (0 != br)) { 9 | printf("\n"); 10 | } 11 | 12 | printf("%d ", *(const int * const)data); 13 | 14 | ++br; 15 | } 16 | } 17 | 18 | int main(void) { 19 | FILE *fout = NULL; 20 | 21 | if ((fout = freopen("out_1.txt", "w", stdout)) == NULL) { 22 | exit(EXIT_FAILURE); 23 | } 24 | 25 | srand(time(NULL)); 26 | 27 | sstack_t *st = create_stack(NULL, sizeof(int)); 28 | 29 | if (NULL == st) { 30 | exit(EXIT_FAILURE); 31 | } 32 | 33 | scl_error_t err = SCL_OK; 34 | 35 | for (int i = 0; i < 100; ++i) { 36 | err = stack_push(st, ltoptr(int, rand() % 100 + 10)); 37 | 38 | if (SCL_OK != err) { 39 | scl_error_message(err); 40 | } 41 | } 42 | 43 | if (1 == is_stack_empty(st)) { 44 | printf("Oops !!! Something went wrong with insertion\n"); 45 | exit(EXIT_FAILURE); 46 | } 47 | 48 | printf("Generated stack has %lu elements:\n", get_stack_size(st)); 49 | print_stack(st, &print_int_br); 50 | printf("\n\n"); 51 | 52 | const int *top = stack_top(st); 53 | 54 | if (NULL != top) { 55 | printf("Top element is %d\n\n", *top); 56 | } else { 57 | printf("Could not fetch top element\n\n"); 58 | } 59 | 60 | printf("Let's pop half of the stack:\n"); 61 | 62 | for (int i = 0; i < 50; ++i) { 63 | err = stack_pop(st); 64 | 65 | if (SCL_OK != err) { 66 | scl_error_message(err); 67 | } 68 | } 69 | 70 | print_stack(st, &print_int); 71 | printf("\n"); 72 | 73 | free_stack(st); 74 | 75 | fclose(fout); 76 | 77 | return 0; 78 | } -------------------------------------------------------------------------------- /voidptr_data_structs/examples/stack/example2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void free_st(void * const st) { 5 | if (NULL != st) { 6 | sstack_t ** const t_st = st; 7 | 8 | free_stack(*t_st); 9 | } 10 | } 11 | 12 | int main(void) { 13 | FILE *fout = NULL; 14 | 15 | if ((fout = freopen("out_2.txt", "w", stdout)) == NULL) { 16 | exit(EXIT_FAILURE); 17 | } 18 | 19 | sstack_t *sts = create_stack(&free_st, sizeof(sts)); 20 | 21 | if (NULL != sts) { 22 | srand(time(NULL)); 23 | 24 | scl_error_t err = SCL_OK; 25 | 26 | for (int i = 0; i < 3; ++i) { 27 | sstack_t *st = create_stack(NULL, sizeof(int)); 28 | 29 | for (int j = 0; j < 10; ++j) { 30 | err = stack_push(st, ltoptr(int, rand() % 100 + 10)); 31 | 32 | if (SCL_OK != err) { 33 | scl_error_message(err); 34 | } 35 | } 36 | 37 | err = stack_push(sts, toptr(st)); 38 | 39 | if (SCL_OK != err) { 40 | scl_error_message(err); 41 | } 42 | } 43 | 44 | int stacks_num = 0; 45 | 46 | while (!is_stack_empty(sts)) { 47 | printf("Queue number %d:\n", stacks_num++); 48 | 49 | sstack_t * const * top = stack_top(sts); 50 | 51 | while (!is_stack_empty(*top)) { 52 | const int *top_elem = stack_top(*top); 53 | 54 | printf("%d ", *top_elem); 55 | 56 | err = stack_pop(*top); 57 | 58 | if (SCL_OK != err) { 59 | scl_error_message(err); 60 | } 61 | } 62 | 63 | printf("\n\n"); 64 | 65 | // Popping the stack has to be the last operation 66 | // because popping will remove stack from memory 67 | // If you do not want pop to remove stack from heap 68 | // You shall not specify a free function into the 69 | // craettion of stack, and then the free will be 70 | // done manually by you 71 | err = stack_pop(sts); 72 | 73 | if (SCL_OK != err) { 74 | scl_error_message(err); 75 | } 76 | } 77 | 78 | free_stack(sts); 79 | } 80 | 81 | fclose(fout); 82 | 83 | return 0; 84 | } -------------------------------------------------------------------------------- /voidptr_data_structs/src/include/scl_avl_tree.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file scl_avl_tree.h 3 | * @author Mihai Negru (determinant289@gmail.com) 4 | * @version 1.0.0 5 | * @date 2022-06-21 6 | * 7 | * @copyright Copyright (C) 2022-2023 Mihai Negru 8 | * This file is part of C-language-Data-Structures. 9 | * 10 | * C-language-Data-Structures is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU Lesser General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * C-language-Data-Structures is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public License 21 | * along with C-language-Data-Structures. If not, see . 22 | * 23 | */ 24 | 25 | #ifndef AVLTREE_UTILS_H_ 26 | #define AVLTREE_UTILS_H_ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "scl_config.h" 34 | 35 | /** 36 | * @brief Adelson-Velsky-Landis Tree Node object definition 37 | * 38 | */ 39 | typedef struct avl_tree_node_s { 40 | void *data; /* Pointer to data */ 41 | struct avl_tree_node_s *parent; /* Pointer to parent node */ 42 | struct avl_tree_node_s *left; /* Pointer to left child node */ 43 | struct avl_tree_node_s *right; /* Pointer to right child node */ 44 | uint32_t count; /* Number of nodes with the same data value */ 45 | uint32_t height; /* Height of a node */ 46 | } avl_tree_node_t; 47 | 48 | /** 49 | * @brief Adelson-Velsky-Landis Tree object definition 50 | * 51 | */ 52 | typedef struct avl_tree_s { 53 | avl_tree_node_t *root; /* Pointer to tree root */ 54 | avl_tree_node_t *nil; /* Black hole pointer */ 55 | compare_func cmp; /* Function to compare two elements */ 56 | free_func frd; /* Function to free content of data */ 57 | size_t data_size; /* Length in bytes of the data data type */ 58 | size_t size; /* Size of the avl tree */ 59 | } avl_tree_t; 60 | 61 | avl_tree_t* create_avl (compare_func cmp, free_func frd, size_t data_size); 62 | scl_error_t free_avl (avl_tree_t * const __restrict__ tree); 63 | 64 | scl_error_t avl_insert (avl_tree_t * const __restrict__ tree, const void * __restrict__ data); 65 | const void* avl_find_data (const avl_tree_t * const __restrict__ tree, const void * const __restrict__ data); 66 | int32_t avl_data_level (const avl_tree_t * const __restrict__ tree, const void * const __restrict__ data); 67 | 68 | uint8_t is_avl_empty (const avl_tree_t * const __restrict__ tree); 69 | const void* get_avl_root (const avl_tree_t * const __restrict__ tree); 70 | size_t get_avl_size (const avl_tree_t * const __restrict__ tree); 71 | 72 | const void* avl_max_data (const avl_tree_t * const __restrict__ tree, const void * const __restrict__ subroot_data); 73 | const void* avl_min_data (const avl_tree_t * const __restrict__ tree, const void * const __restrict__ subroot_data); 74 | 75 | scl_error_t avl_delete (avl_tree_t * const __restrict__ tree, const void * const __restrict__ data); 76 | 77 | const void* avl_predecessor_data (const avl_tree_t * const __restrict__ tree, const void * const __restrict__ data); 78 | const void* avl_successor_data (const avl_tree_t * const __restrict__ tree, const void * const __restrict__ data); 79 | const void* avl_lowest_common_ancestor_data (const avl_tree_t * const __restrict__ tree, const void * const __restrict__ data1, const void * const __restrict__ data2); 80 | 81 | scl_error_t avl_traverse_inorder (const avl_tree_t * const __restrict__ tree, action_func action); 82 | scl_error_t avl_traverse_preorder (const avl_tree_t * const __restrict__ tree, action_func action); 83 | scl_error_t avl_traverse_postorder (const avl_tree_t * const __restrict__ tree, action_func action); 84 | scl_error_t avl_traverse_level (const avl_tree_t * const __restrict__ tree, action_func action); 85 | 86 | #endif /* AVLTREE_UTILS_H_ */ 87 | -------------------------------------------------------------------------------- /voidptr_data_structs/src/include/scl_bst_tree.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file scl_bst_tree.h 3 | * @author Mihai Negru (determinant289@gmail.com) 4 | * @version 1.0.0 5 | * @date 2022-06-21 6 | * 7 | * @copyright Copyright (C) 2022-2023 Mihai Negru 8 | * This file is part of C-language-Data-Structures. 9 | * 10 | * C-language-Data-Structures is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU Lesser General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * C-language-Data-Structures is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public License 21 | * along with C-language-Data-Structures. If not, see . 22 | * 23 | */ 24 | 25 | #ifndef BST_UTILS_H_ 26 | #define BST_UTILS_H_ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "scl_config.h" 34 | 35 | /** 36 | * @brief Binary Search Tree Node object definition 37 | * 38 | */ 39 | typedef struct bst_tree_node_s { 40 | void *data; /* Pointer to data */ 41 | struct bst_tree_node_s *parent; /* Pointer to parent node */ 42 | struct bst_tree_node_s *left; /* Pointer to left child node */ 43 | struct bst_tree_node_s *right; /* Pointer to right child node */ 44 | uint32_t count; /* Number of nodes with the same data value */ 45 | } bst_tree_node_t; 46 | 47 | /** 48 | * @brief Binary Search Tree object definition 49 | * 50 | */ 51 | typedef struct bst_tree_s { 52 | bst_tree_node_t *root; /* Pointer to tree root */ 53 | bst_tree_node_t *nil; /* Black hole pointer */ 54 | compare_func cmp; /* Function to compare two elements */ 55 | free_func frd; /* Function to free content of data */ 56 | size_t data_size; /* Length in bytes of the data data type */ 57 | size_t size; /* Size of the binary search tree */ 58 | } bst_tree_t; 59 | 60 | bst_tree_t* create_bst (compare_func cmp, free_func frd, size_t data_size); 61 | scl_error_t free_bst (bst_tree_t * const __restrict__ tree); 62 | 63 | scl_error_t bst_insert (bst_tree_t * const __restrict__ tree, const void * const __restrict__ data); 64 | const void* bst_find_data (const bst_tree_t * const __restrict__ tree, const void * __restrict__ data); 65 | int32_t bst_data_level (const bst_tree_t * const __restrict__ tree, const void * const __restrict__ data); 66 | 67 | uint8_t is_bst_empty (const bst_tree_t * const __restrict__ tree); 68 | const void* get_bst_root (const bst_tree_t * const __restrict__ tree); 69 | size_t get_bst_size (const bst_tree_t * const __restrict__ tree); 70 | 71 | const void* bst_max_data (const bst_tree_t * const __restrict__ tree, const void * const __restrict__ subroot_data); 72 | const void* bst_min_data (const bst_tree_t * const __restrict__ tree, const void * const __restrict__ subroot_data); 73 | 74 | scl_error_t bst_delete (bst_tree_t * const __restrict__ tree, const void * const __restrict__ data); 75 | 76 | const void* bst_predecessor_data (const bst_tree_t * const __restrict__ tree, const void * const __restrict__ data); 77 | const void* bst_successor_data (const bst_tree_t * const __restrict__ tree, const void * const __restrict__ data); 78 | const void* bst_lowest_common_ancestor_data (const bst_tree_t * const __restrict__ tree, const void * const __restrict__ data1, const void * const __restrict__ data2); 79 | 80 | scl_error_t bst_traverse_inorder (const bst_tree_t * const __restrict__ tree, action_func action); 81 | scl_error_t bst_traverse_preorder (const bst_tree_t * const __restrict__ tree, action_func action); 82 | scl_error_t bst_traverse_postorder (const bst_tree_t * const __restrict__ tree, action_func action); 83 | scl_error_t bst_traverse_level (const bst_tree_t * const __restrict__ tree, action_func action); 84 | 85 | #endif /* BST_UTILS_H_ */ 86 | -------------------------------------------------------------------------------- /voidptr_data_structs/src/include/scl_config.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file scl_config.h 3 | * @author Mihai Negru (determinant289@gmail.com) 4 | * @version 1.0.0 5 | * @date 2022-06-21 6 | * 7 | * @copyright Copyright (C) 2022-2023 Mihai Negru 8 | * This file is part of C-language-Data-Structures. 9 | * 10 | * C-language-Data-Structures is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU Lesser General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * C-language-Data-Structures is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public License 21 | * along with C-language-Data-Structures. If not, see . 22 | * 23 | */ 24 | 25 | #ifndef _CONFIG_UTILS_H_ 26 | #define _CONFIG_UTILS_H_ 27 | 28 | #include 29 | #include 30 | 31 | /** 32 | * @brief Definition of error table handler 33 | * 34 | */ 35 | typedef enum scl_error_s { 36 | SCL_OK = 0, 37 | 38 | SCL_INVALID_INPUT = -1, 39 | SCL_INVALID_DATA = -2, 40 | 41 | SCL_CANNOT_SWAP_DATA = -3, 42 | SCL_SWAP_SAME_DATA = -4, 43 | SCL_CANNOT_CHANGE_DATA = -5, 44 | SCL_UNDEFINED_DATA = -6, 45 | SCL_DATA_NOT_FOUND_FOR_DELETE = -7, 46 | SCL_DATA_NOT_FOUND = -8, 47 | 48 | SCL_NULL_LIST = -9, 49 | SCL_NULL_DLIST = -10, 50 | 51 | SCL_NULL_STACK = -11, 52 | SCL_NULL_QUEUE = -12, 53 | 54 | SCL_NULL_PRIORITY_QUEUE = -13, 55 | SCL_NULL_PQUEUE_NODES = -14, 56 | SCL_CHANGE_PRIORITY_TO_NULL = -15, 57 | SCL_REALLOC_PQNODES_FAIL = -16, 58 | SCL_PQUEUE_CAPACITY_ZERO = -17, 59 | SCL_UNDEFINED_PRIORITY = -18, 60 | SCL_INVALID_PRIORITY = -19, 61 | SCL_NULL_COMPARE_PRIORITY_FUNC = -20, 62 | SCL_NOT_EMPTY_PRIORITY_QUEUE = -21, 63 | 64 | SCL_NULL_BST = -22, 65 | SCL_NULL_RBK = -23, 66 | SCL_UNKNOWN_RBK_COLOR = -24, 67 | SCL_NULL_AVL = -25, 68 | SCL_FIXING_NULL_TREE_NODE = -26, 69 | 70 | SCL_NULL_ACTION_FUNC = -27, 71 | 72 | SCL_NOT_ENOUGHT_MEM_FOR_NODE = -28, 73 | SCL_NOT_ENOUGHT_MEM_FOR_OBJ = -29, 74 | 75 | SCL_DELETE_FROM_EMPTY_OBJECT = -30, 76 | 77 | SCL_INDEX_OVERFLOWS_SIZE = -31, 78 | 79 | SCL_NULL_SIMPLE_ARRAY = -32, 80 | SCL_NUMBER_OF_ELEMS_ZERO = -33, 81 | SCL_SIMPLE_ELEM_ARRAY_SIZE_ZERO = -34, 82 | SCL_SIMPLE_ARRAY_COMPAR_FUNC_NULL = -35, 83 | 84 | SCL_NULL_HASH_TABLE = -36, 85 | SCL_UNKNOWN_HASH_NODE_COLOR = -37, 86 | SCL_INVALID_KEY = -38, 87 | SCL_NULL_HASH_ROOTS = -39, 88 | SCL_NULL_HASH_FUNCTION = -40, 89 | SCL_REHASHING_FAILED = -41, 90 | 91 | SCL_NULL_GRAPH = -42, 92 | SCL_INVALID_EDGE_LENGTH = -43, 93 | SCL_NULL_GRAPH_VERTICES = -44, 94 | SCL_NULL_GRAPH_VERTEX = -45, 95 | SCL_REALLOC_GRAPH_VERTICES_FAIL = -46, 96 | SCL_VERTEX_OUT_OF_BOUND = -47, 97 | SCL_EDGE_NOT_FOUND = -48, 98 | SCL_NULL_VERTICES_DISTANCES = -49, 99 | SCL_NULL_VERTICES_PARENTS = -50, 100 | SCL_NULL_PATH_MATRIX = -51, 101 | SCL_GRAPH_INVALID_NEW_VERTICES = -52 102 | } scl_error_t; 103 | 104 | /** 105 | * @brief Definition of the most used functions 106 | * 107 | */ 108 | typedef size_t (*hash_func) (const void * const); 109 | typedef int32_t (*compare_func) (const void * const, const void * const); 110 | typedef void (*free_func) (void *); 111 | typedef void (*action_func) (void * const); 112 | typedef int32_t (*filter_func) (const void * const); 113 | 114 | void scl_error_message (scl_error_t error_message); 115 | 116 | #endif /* _CONFIG_UTILS_H_ */ 117 | -------------------------------------------------------------------------------- /voidptr_data_structs/src/include/scl_datastruc.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file scl_datastruc.h 3 | * @author Mihai Negru (determinant289@gmail.com) 4 | * @version 1.0.0 5 | * @date 2022-06-21 6 | * 7 | * @copyright Copyright (C) 2022-2023 Mihai Negru 8 | * This file is part of C-language-Data-Structures. 9 | * 10 | * C-language-Data-Structures is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU Lesser General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * C-language-Data-Structures is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public License 21 | * along with C-language-Data-Structures. If not, see . 22 | * 23 | */ 24 | 25 | #ifndef DATA_STRUCTURES_H_ 26 | #define DATA_STRUCTURES_H_ 27 | 28 | #include "scl_avl_tree.h" 29 | #include "scl_bst_tree.h" 30 | #include "scl_dlist.h" 31 | #include "scl_func_types.h" 32 | #include "scl_graph.h" 33 | #include "scl_hash_table.h" 34 | #include "scl_list.h" 35 | #include "scl_priority_queue.h" 36 | #include "scl_queue.h" 37 | #include "scl_red_black_tree.h" 38 | #include "scl_sort_algo.h" 39 | #include "scl_stack.h" 40 | 41 | #endif /* DATA_STRUCTURES_H_ */ 42 | -------------------------------------------------------------------------------- /voidptr_data_structs/src/include/scl_dlist.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file scl_dlist.h 3 | * @author Mihai Negru (determinant289@gmail.com) 4 | * @version 1.0.0 5 | * @date 2022-06-21 6 | * 7 | * @copyright Copyright (C) 2022-2023 Mihai Negru 8 | * This file is part of C-language-Data-Structures. 9 | * 10 | * C-language-Data-Structures is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU Lesser General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * C-language-Data-Structures is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public License 21 | * along with C-language-Data-Structures. If not, see . 22 | * 23 | */ 24 | 25 | #ifndef DOUBLE_LIST_UTILS_H_ 26 | #define DOUBLE_LIST_UTILS_H_ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "scl_config.h" 34 | 35 | /** 36 | * @brief Double Linked List Node object definition 37 | * 38 | */ 39 | typedef struct dlist_node_s { 40 | void *data; /* Pointer to data */ 41 | struct dlist_node_s *prev; /* Pointer to previous node */ 42 | struct dlist_node_s *next; /* Pointer to next node */ 43 | } dlist_node_t; 44 | 45 | /** 46 | * @brief Double Linked List object definition 47 | * 48 | */ 49 | typedef struct dlist_s { 50 | dlist_node_t *head; /* head of linked list */ 51 | dlist_node_t *tail; /* tail of linked list */ 52 | compare_func cmp; /* function to compare items */ 53 | free_func frd; /* function to free item */ 54 | size_t data_size; /* Length in bytes of the data data type */ 55 | size_t size; /* size of linked list */ 56 | } dlist_t; 57 | 58 | dlist_t* create_dlist (compare_func cmp, free_func frd, size_t data_size); 59 | scl_error_t free_dlist (dlist_t * const __restrict__ list); 60 | 61 | uint8_t is_dlist_empty (const dlist_t * const __restrict__ list); 62 | size_t get_dlist_size (const dlist_t * const __restrict__ list); 63 | const void* get_dlist_head (const dlist_t * const __restrict__ list); 64 | const void* get_dlist_tail (const dlist_t * const __restrict__ list); 65 | 66 | scl_error_t dlist_swap_data (const dlist_t * const __restrict__ list, const void * const __restrict__ first_data, const void * const __restrict__ second_data); 67 | scl_error_t dlist_change_data (const dlist_t * const __restrict__ list, const void * const __restrict__ base_data, const void * __restrict__ new_data); 68 | scl_error_t dlist_insert (dlist_t * const __restrict__ list, const void * __restrict__ data); 69 | scl_error_t dlist_insert_order (dlist_t * const __restrict__ list, const void * __restrict__ data); 70 | scl_error_t dlist_insert_front (dlist_t * const __restrict__ list, const void * __restrict__ data); 71 | scl_error_t dlist_insert_index (dlist_t * const __restrict__ list, const void * __restrict__ data, size_t data_index); 72 | 73 | const void* dlist_find_index (const dlist_t * const __restrict__ list, size_t data_index); 74 | const void* dlist_find_data (const dlist_t * const __restrict__ list, const void * const data); 75 | 76 | scl_error_t dlist_delete_data (dlist_t * const __restrict__ list, const void * const __restrict__ data); 77 | scl_error_t dlist_delete_index (dlist_t * const __restrict__ list, size_t data_index); 78 | scl_error_t dlist_erase (dlist_t * const __restrict__ list, size_t left_index, size_t right_index); 79 | 80 | dlist_t* dlist_filter (const dlist_t * const __restrict__ list, filter_func filter); 81 | scl_error_t dlist_traverse (const dlist_t * const __restrict__ list, action_func action); 82 | 83 | #endif /* DOUBLE_LIST_UTILS_H_ */ 84 | -------------------------------------------------------------------------------- /voidptr_data_structs/src/include/scl_func_types.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file scl_func_types.h 3 | * @author Mihai Negru (determinant289@gmail.com) 4 | * @version 1.0.0 5 | * @date 2022-06-21 6 | * 7 | * @copyright Copyright (C) 2022-2023 Mihai Negru 8 | * This file is part of C-language-Data-Structures. 9 | * 10 | * C-language-Data-Structures is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU Lesser General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * C-language-Data-Structures is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public License 21 | * along with C-language-Data-Structures. If not, see . 22 | * 23 | */ 24 | 25 | #ifndef _FUNCTION_TYPES_H_ 26 | #define _FUNCTION_TYPES_H_ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #define toptr(var) (&(var)) 35 | #define ltoptr(type, lval) (&(type){(lval)}) 36 | #define make_pair(m1, m2) (m1), (m2) 37 | #define make_triplet(m1, m2, m3) (m1), (m2), (m3) 38 | 39 | void print_short_int (void * const data); 40 | void print_ushort_int (void * const data); 41 | void print_uint (void * const data); 42 | void print_int (void * const data); 43 | void print_long_int (void * const data); 44 | void print_ulong_int (void * const data); 45 | void print_llong_int (void * const data); 46 | void print_ullong_int (void * const data); 47 | void print_char (void * const data); 48 | void print_uchar (void * const data); 49 | void print_float (void * const data); 50 | void print_double (void * const data); 51 | void print_long_double (void * const data); 52 | void print_string (void * const data); 53 | 54 | int32_t compare_short_int (const void * const data1, const void * const data2); 55 | int32_t compare_ushort_int (const void * const data1, const void * const data2); 56 | int32_t compare_uint (const void * const data1, const void * const data2); 57 | int32_t compare_int (const void * const data1, const void * const data2); 58 | int32_t compare_long_int (const void * const data1, const void * const data2); 59 | int32_t compare_ulong_int (const void * const data1, const void * const data2); 60 | int32_t compare_llong_int (const void * const data1, const void * const data2); 61 | int32_t compare_ullong_int (const void * const data1, const void * const data2); 62 | int32_t compare_char (const void * const data1, const void * const data2); 63 | int32_t compare_uchar (const void * const data1, const void * const data2); 64 | int32_t compare_float (const void * const data1, const void * const data2); 65 | int32_t compare_double (const void * const data1, const void * const data2); 66 | int32_t compare_long_double (const void * const data1, const void * const data2); 67 | int32_t compare_string_size (const void * const data1, const void * const data2); 68 | int32_t compare_string_lexi (const void * const data1, const void * const data2); 69 | int32_t compare_string (const void * const data1, const void * const data2); 70 | 71 | #endif /* _FUNCTION_TYPES_H_ */ 72 | -------------------------------------------------------------------------------- /voidptr_data_structs/src/include/scl_list.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file scl_list.h 3 | * @author Mihai Negru (determinant289@gmail.com) 4 | * @version 1.0.0 5 | * @date 2022-06-21 6 | * 7 | * @copyright Copyright (C) 2022-2023 Mihai Negru 8 | * This file is part of C-language-Data-Structures. 9 | * 10 | * C-language-Data-Structures is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU Lesser General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * C-language-Data-Structures is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public License 21 | * along with C-language-Data-Structures. If not, see . 22 | * 23 | */ 24 | 25 | #ifndef LIST_UTILS_H_ 26 | #define LIST_UTILS_H_ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "scl_config.h" 34 | 35 | /** 36 | * @brief Linked List Node object definition 37 | * 38 | */ 39 | typedef struct list_node_s { 40 | void *data; /* Pointer to data */ 41 | struct list_node_s *next; /* Pointer to next node */ 42 | } list_node_t; 43 | 44 | /** 45 | * @brief Linked List object definition 46 | * 47 | */ 48 | typedef struct list_s { 49 | list_node_t *head; /* head of linked list */ 50 | list_node_t *tail; /* tail of linked list */ 51 | compare_func cmp; /* function to compare items */ 52 | free_func frd; /* function to free item */ 53 | size_t data_size; /* Length in bytes of the data data type */ 54 | size_t size; /* size of linked list */ 55 | } list_t; 56 | 57 | list_t* create_list (compare_func cmp, free_func frd, size_t data_size); 58 | scl_error_t free_list (list_t * const __restrict__ list); 59 | 60 | uint8_t is_list_empty (const list_t * const __restrict__ list); 61 | size_t get_list_size (const list_t * const __restrict__ list); 62 | const void* get_list_head (const list_t * const __restrict__ list); 63 | const void* get_list_tail (const list_t * const __restrict__ list); 64 | 65 | scl_error_t list_swap_data (const list_t * const __restrict__ list, const void * const __restrict__ first_data, const void * const __restrict__ second_data); 66 | scl_error_t list_change_data (const list_t * const __restrict__ list, const void * const __restrict__ base_data, const void * __restrict__ new_data); 67 | scl_error_t list_insert (list_t * const __restrict__ list, const void * __restrict__ data); 68 | scl_error_t list_insert_order (list_t * const __restrict__ list, const void * __restrict__ data); 69 | scl_error_t list_insert_front (list_t * const __restrict__ list, const void * __restrict__ data); 70 | scl_error_t list_insert_index (list_t * const __restrict__ list, const void * __restrict__ data, size_t data_index); 71 | 72 | const void* list_find_index (const list_t * const __restrict__ list, size_t data_index); 73 | const void* list_find_data (const list_t * const __restrict__ list, const void * const __restrict__ data); 74 | 75 | scl_error_t list_delete_data (list_t * const __restrict__ list, const void * const __restrict__ data); 76 | scl_error_t list_delete_index (list_t * const __restrict__ list, size_t data_index); 77 | scl_error_t list_erase (list_t * const __restrict__ list, size_t left_index, size_t right_index); 78 | 79 | list_t* list_filter (const list_t * const __restrict__ list, filter_func filter); 80 | scl_error_t list_traverse (const list_t * const __restrict__ list, action_func map); 81 | 82 | #endif /* LIST_UTILS_H_ */ 83 | -------------------------------------------------------------------------------- /voidptr_data_structs/src/include/scl_priority_queue.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file scl_priority_queue.h 3 | * @author Mihai Negru (determinant289@gmail.com) 4 | * @version 1.0.0 5 | * @date 2022-06-21 6 | * 7 | * @copyright Copyright (C) 2022-2023 Mihai Negru 8 | * This file is part of C-language-Data-Structures. 9 | * 10 | * C-language-Data-Structures is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU Lesser General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * C-language-Data-Structures is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public License 21 | * along with C-language-Data-Structures. If not, see . 22 | * 23 | */ 24 | 25 | #ifndef PRIORITY_QUEUE_UTILS_H_ 26 | #define PRIORITY_QUEUE_UTILS_H_ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "scl_config.h" 34 | 35 | /** 36 | * @brief Priority Queue Node Object definition 37 | * 38 | */ 39 | typedef struct pri_node_s { 40 | void *pri; /* Priority element definition */ 41 | void *data; /* Data type element definition */ 42 | } pri_node_t; 43 | 44 | /** 45 | * @brief Priority Queue Object definition 46 | * 47 | */ 48 | typedef struct priority_queue_s { 49 | pri_node_t **nodes; /* Array of binary-heap nodes */ 50 | compare_func cmp_dt; /* Function to compare two sets of data */ 51 | compare_func cmp_pr; /* Function to compare two sets of priority */ 52 | free_func frd_dt; /* Function to free memory of a single data element */ 53 | free_func frd_pr; /* Function to free memory of a single priority element */ 54 | size_t capacity; /* Maximum capacity of the priority queue */ 55 | size_t pri_size; /* Length in bytes of the priority data type */ 56 | size_t data_size; /* Length in bytes of the data data type */ 57 | size_t size; /* Current size of the priority queue */ 58 | } priority_queue_t; 59 | 60 | priority_queue_t* create_priority_queue (size_t init_capacity, compare_func cmp_pr, compare_func cmp_dt, free_func frd_pr, free_func frd_dt, size_t pri_size, size_t data_size); 61 | scl_error_t free_priority_queue (priority_queue_t * const __restrict__ pqueue); 62 | scl_error_t heapify (priority_queue_t * const __restrict__ empty_pqueue, const void *priority, const void *data); 63 | 64 | scl_error_t change_node_priority (const priority_queue_t * const __restrict__ pqueue, size_t node_index, const void * __restrict__ new_pri); 65 | scl_error_t change_node_data (const priority_queue_t * const __restrict__ pqueue, size_t node_index, const void * __restrict__ new_data); 66 | 67 | size_t pri_find_data_index (const priority_queue_t * const __restrict__ pqueue, const void * const __restrict__ data); 68 | size_t pri_find_pri_index (const priority_queue_t * const __restrict__ pqueue, const void * const __restrict__ priority); 69 | 70 | scl_error_t pri_queue_push (priority_queue_t * const __restrict__ pqueue, const void *priority, const void *data); 71 | const void* pri_queue_top (const priority_queue_t * const __restrict__ pqueue); 72 | const void* pri_queue_top_pri (const priority_queue_t * const __restrict__ pqueue); 73 | scl_error_t pri_queue_pop (priority_queue_t * const __restrict__ pqueue); 74 | scl_error_t pri_queue_traverse (const priority_queue_t * const __restrict__ pqueue, action_func action); 75 | 76 | size_t pri_queue_size (const priority_queue_t * const __restrict__ pqueue); 77 | uint8_t is_priq_empty (const priority_queue_t * const __restrict__ pqueue); 78 | 79 | scl_error_t heap_sort (void* arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 80 | 81 | #endif /* PRIORITY_QUEUE_UTILS_H_ */ 82 | -------------------------------------------------------------------------------- /voidptr_data_structs/src/include/scl_queue.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file scl_queue.h 3 | * @author Mihai Negru (determinant289@gmail.com) 4 | * @version 1.0.0 5 | * @date 2022-06-21 6 | * 7 | * @copyright Copyright (C) 2022-2023 Mihai Negru 8 | * This file is part of C-language-Data-Structures. 9 | * 10 | * C-language-Data-Structures is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU Lesser General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * C-language-Data-Structures is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public License 21 | * along with C-language-Data-Structures. If not, see . 22 | * 23 | */ 24 | 25 | #ifndef QUEUE_UTILS_H_ 26 | #define QUEUE_UTILS_H_ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "scl_config.h" 34 | 35 | /** 36 | * @brief queue Node object definition 37 | * 38 | */ 39 | typedef struct queue_node_s { 40 | void *data; /* Pointer to data */ 41 | struct queue_node_s *next; /* Pointer to next data node */ 42 | } queue_node_t; 43 | 44 | /** 45 | * @brief queue object definition 46 | * 47 | */ 48 | typedef struct queue_s { 49 | queue_node_t *front; /* Pointer to beginning of queue */ 50 | queue_node_t *back; /* Pointer to end of queue */ 51 | free_func frd; /* Function to free one data */ 52 | size_t data_size; /* Length in bytes of the data data type */ 53 | size_t size; /* Size of the queue */ 54 | } queue_t; 55 | 56 | queue_t* create_queue (free_func frd, size_t data_size); 57 | scl_error_t free_queue (queue_t * const __restrict__ queue); 58 | scl_error_t print_queue (const queue_t * const __restrict__ queue, action_func print); 59 | 60 | uint8_t is_queue_empty (const queue_t * const __restrict__ queue); 61 | size_t get_queue_size (const queue_t * const __restrict__ queue); 62 | 63 | const void* queue_front (const queue_t * const __restrict__ queue); 64 | const void* queue_back (const queue_t * const __restrict__ queue); 65 | scl_error_t queue_push (queue_t * const __restrict__ queue, const void * __restrict__ data); 66 | scl_error_t queue_pop (queue_t * const __restrict__ queue); 67 | 68 | #endif /* QUEUE_UTILS_H_ */ 69 | -------------------------------------------------------------------------------- /voidptr_data_structs/src/include/scl_sort_algo.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file scl_sort_algo.h 3 | * @author Mihai Negru (determinant289@gmail.com) 4 | * @version 1.0.0 5 | * @date 2022-06-21 6 | * 7 | * @copyright Copyright (C) 2022-2023 Mihai Negru 8 | * This file is part of C-language-Data-Structures. 9 | * 10 | * C-language-Data-Structures is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU Lesser General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * C-language-Data-Structures is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public License 21 | * along with C-language-Data-Structures. If not, see . 22 | * 23 | */ 24 | 25 | #ifndef SORT_ALGORITHMS_H_ 26 | #define SORT_ALGORITHMS_H_ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "scl_priority_queue.h" 34 | #include "scl_config.h" 35 | 36 | scl_error_t quick_sort (void *arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 37 | scl_error_t merge_sort (void *arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 38 | scl_error_t bubble_sort (void *arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 39 | scl_error_t radix_sort (uint64_t *arr, size_t number_of_elem); 40 | scl_error_t insertion_sort (void *arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 41 | scl_error_t selection_sort (void *arr, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 42 | 43 | scl_error_t reverse_array (void *arr, size_t number_of_elem, size_t arr_elem_size); 44 | void* binary_search (void *arr, void *data, size_t number_of_elem, size_t arr_elem_size, compare_func cmp); 45 | 46 | #endif /* SORT_ALGORITHMS_H_ */ 47 | -------------------------------------------------------------------------------- /voidptr_data_structs/src/include/scl_stack.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file scl_stack.h 3 | * @author Mihai Negru (determinant289@gmail.com) 4 | * @version 1.0.0 5 | * @date 2022-06-21 6 | * 7 | * @copyright Copyright (C) 2022-2023 Mihai Negru 8 | * This file is part of C-language-Data-Structures. 9 | * 10 | * C-language-Data-Structures is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU Lesser General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * C-language-Data-Structures is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU Lesser General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU Lesser General Public License 21 | * along with C-language-Data-Structures. If not, see . 22 | * 23 | */ 24 | 25 | #ifndef STACK_UTILS_H_ 26 | #define STACK_UTILS_H_ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "scl_config.h" 34 | 35 | /** 36 | * @brief stack Node object definition 37 | * 38 | */ 39 | typedef struct stack_node_s { 40 | void *data; /* Pointer to data */ 41 | struct stack_node_s *next; /* Pointer to next data node */ 42 | } stack_node_t; 43 | 44 | /** 45 | * @brief stack object definition 46 | * 47 | */ 48 | typedef struct stack_s { 49 | stack_node_t *top; /* Pointer to top data node */ 50 | free_func frd; /* Function to free one data */ 51 | size_t data_size; /* Length in bytes of the data data type */ 52 | size_t size; /* Size of the stack */ 53 | } sstack_t; 54 | 55 | sstack_t* create_stack (free_func frd, size_t data_size); 56 | scl_error_t free_stack (sstack_t * const __restrict__ stack); 57 | scl_error_t print_stack (const sstack_t * const __restrict__ stack, action_func print); 58 | 59 | uint8_t is_stack_empty (const sstack_t * const __restrict__ stack); 60 | size_t get_stack_size (const sstack_t * const __restrict__ stack); 61 | 62 | const void* stack_top (const sstack_t * const __restrict__ stack); 63 | scl_error_t stack_push (sstack_t * const __restrict__ stack, const void * __restrict__ data); 64 | scl_error_t stack_pop (sstack_t * const __restrict__ stack); 65 | 66 | #endif /* STACK_UTILS_H_ */ 67 | --------------------------------------------------------------------------------