Initial Commit

This commit is contained in:
Marc
2025-09-13 14:40:16 +02:00
commit ded01301c2
383 changed files with 71046 additions and 0 deletions

150
OU3/include/array_1d.h Normal file
View File

@@ -0,0 +1,150 @@
#ifndef __ARRAY_1D_H
#define __ARRAY_1D_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of a generic 1D array for the "Datastructures and
* algorithms" courses at the Department of Computing Science, Umea
* University. The array stores void pointers, so it can be used to
* store all types of values. After use, the function array_kill must
* be called to de-allocate the dynamic memory used by the array
* itself. The de-allocation of any dynamic memory allocated for the
* element values is the responsibility of the user of the array,
* unless a kill_function is registered in array_create.
*
* An element value of NULL is considered to be "no" value.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
*
* Based on earlier code by: Johan Eliasson (johane@cs.umu.se).
*
* Version information:
* v1.0 2018-01-28: First public version.
* v1.5 2024-03-13: Renamed free_* stuff to kill_*. Converted to 4-tabs.
* v2.0 2024-05-10: Added print_internal.
*/
// ==========PUBLIC DATA TYPES============
// List type.
typedef struct array_1d array_1d;
// ==========DATA STRUCTURE INTERFACE==========
/**
* array_1d_create() - Create an array without values.
* @lo: low index limit.
* @hi: high index limit.
* @kill_func: A pointer to a function (or NULL) to be called to
* de-allocate memory on remove/kill.
*
* The index limits are inclusive, i.e. all indices i such that low <=
* i <= high are defined.
*
* Returns: A pointer to the new array, or NULL if not enough memory
* was available.
*/
array_1d *array_1d_create(int lo, int hi, kill_function kill_func);
/**
* array_1d_low() - Return the low index limit for the array.
* @a: array to inspect.
*
* Returns: The low index limit.
*/
int array_1d_low(const array_1d *a);
/**
* array_1d_high() - Return the high index limit for the array.
* @a: array to inspect.
*
* Returns: The high index limit.
*/
int array_1d_high(const array_1d *a);
/**
* array_1d_inspect_value() - Inspect a value at a given array position.
* @a: array to inspect.
* @i: index of position to inspect.
*
* Returns: The element value at the specified position. The result is
* undefined if no value are stored at that position.
*/
void *array_1d_inspect_value(const array_1d *a, int i);
/**
* array_1d_has_value() - Check if a value is set at a given array position.
* @a: array to inspect.
* @i: index of position to inspect.
*
* Returns: True if a value is set at the specified position, otherwise false.
*/
bool array_1d_has_value(const array_1d *a, int i);
/**
* array_1d_set_value() - Set a value at a given array position.
* @a: array to modify.
* @v: value to set element to, or NULL to clear value.
* @i: index of position to modify.
*
* If the old element value is non-NULL, calls kill_func if it was
* specified at array creation.
*
* Returns: Nothing.
*/
void array_1d_set_value(array_1d *a, void *v, int i);
/**
* array_1d_kill() - Return memory allocated by array.
* @a: array to kill.
*
* Iterates over all elements. If kill_func was specified at array
* creation, calls it for every non-NULL element value.
*
* Returns: Nothing.
*/
void array_1d_kill(array_1d *a);
/**
* array_1d_print() - Iterate over the array element and print their values.
* @a: Array to inspect.
* @print_func: Function called for each non-NULL element.
*
* Iterates over each position in the array. Calls print_func for each
* non-NULL value.
*
* Returns: Nothing.
*/
void array_1d_print(const array_1d * l, inspect_callback print_func);
/**
* array_1d_print_internal() - Print the internal structure of the array in dot format.
* @a: Array to inspect.
* @print_func: Function called for each element value.
* @desc: String with a description/state of the array, or NULL for no description.
* @indent_level: Indentation level, 0 for outermost
*
* Iterates over the array and outputs dot code that shows the internal
* structure of the array. The dot code can be visualized by
* Graphviz.
*
* On linux system, the output can be parsed by the dot program, e.g.
*
* <array_program> | dot -Tsvg > /tmp/dot.svg; firefox /tmp/dot.svg
*
* where <array_program> is the name of the executable
*
* The output may also be possible to visualize online on
* https://dreampuf.github.io/GraphvizOnline/ or google "graphviz
* online".
*
* For documention of the dot language, see graphviz.org.
*
* Returns: Nothing.
*/
void array_1d_print_internal(const array_1d *a, inspect_callback print_func, const char *desc,
int indent_level);
#endif

158
OU3/include/array_2d.h Normal file
View File

@@ -0,0 +1,158 @@
#ifndef __ARRAY_2D_H
#define __ARRAY_2D_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of a generic 2D array for the "Datastructures and
* algorithms" courses at the Department of Computing Science, Umea
* University. The array stores void pointers, so it can be used to
* store all types of values. After use, the function array_kill must
* be called to de-allocate the dynamic memory used by the array
* itself. The de-allocation of any dynamic memory allocated for the
* element values is the responsibility of the user of the array,
* unless a kill_function is registered in array_create.
*
* An element value of NULL is considered to be "no" value.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
*
* Based on earlier code by: Johan Eliasson (johane@cs.umu.se).
*
* Version information:
* v1.0 2018-01-28: First public version.
* v1.1 2018-04-03: Moved freehandler to last in create parameter list.
* v1.2 2024-05-10: Added print_internal.
*/
// ==========PUBLIC DATA TYPES============
// List type.
typedef struct array_2d array_2d;
// ==========DATA STRUCTURE INTERFACE==========
/**
* array_2d_create() - Create an array without values.
* @lo1: low index limit for first dimension.
* @hi1: high index limit for first dimension.
* @lo2: low index limit for second dimension.
* @hi2: high index limit for second dimension.
* @kill_func: A pointer to a function (or NULL) to be called to
* de-allocate memory on remove/kill.
*
* The index limits are inclusive, i.e. all indices i such that low <=
* i <= high are defined.
*
* Returns: A pointer to the new array, or NULL if not enough memory
* was available.
*/
array_2d *array_2d_create(int lo1, int hi1, int lo2, int hi2,
kill_function kill_func);
/**
* array_2d_low() - Return the low index limit for the array.
* @a: array to inspect.
* @d: dimension number, 1 or 2.
*
* Returns: The low index limit for dimension number d.
*/
int array_2d_low(const array_2d *a, int d);
/**
* array_2d_high() - Return the high index limit for the array.
* @a: array to inspect.
* @d: dimension number, 1 or 2.
*
* Returns: The high index limit for dimension number d.
*/
int array_2d_high(const array_2d *a, int d);
/**
* array_2d_inspect_value() - Inspect a value at a given array position.
* @a: array to inspect.
* @i: first index of position to inspect.
* @j: second index of position to inspect.
*
* Returns: The element value at the specified position. The result is
* undefined if no value are stored at that position.
*/
void *array_2d_inspect_value(const array_2d *a, int i, int j);
/**
* array_2d_has_value() - Check if a value is set at a given array position.
* @a: array to inspect.
* @i: first index of position to inspect.
* @j: second index of position to inspect.
*
* Returns: True if a value is set at the specified position, otherwise false.
*/
bool array_2d_has_value(const array_2d *a, int i, int j);
/**
* array_2d_set_value() - Set a value at a given array position.
* @a: array to modify.
* @v: value to set element to, or NULL to clear value.
* @i: first index of position to inspect.
* @j: second index of position to inspect.
*
* If the old element value is non-NULL, calls kill_func if it was
* specified at array creation.
*
* Returns: Nothing.
*/
void array_2d_set_value(array_2d *a, void *v, int i, int j);
/**
* array_2d_kill() - Return memory allocated by array.
* @a: array to kill.
*
* Iterates over all elements. If kill_func was specified at array
* creation, calls it for every non-NULL element value.
*
* Returns: Nothing.
*/
void array_2d_kill(array_2d *a);
/**
* array_2d_print() - Iterate over the array element and print their values.
* @a: Array to inspect.
* @print_func: Function called for each non-NULL element.
*
* Iterates over each position in the array. Calls print_func for each
* non-NULL value.
*
* Returns: Nothing.
*/
void array_2d_print(const array_2d * l, inspect_callback print_func);
/**
* array_2d_print_internal() - Print the arrays internal structure.
* @a: Array to inspect.
* @print_func: Function called for each element value.
* @desc: String with a description/state of the array, or NULL for no description.
* @indent_level: Indentation level, 0 for outermost
*
* Iterates over the array and outputs dot code that shows the internal
* structure of the array. The dot code can be visualized by
* Graphviz.
*
* On linux system, the output can be parsed by the dot program, e.g.
*
* <array_program> | dot -Tsvg > /tmp/dot.svg; firefox /tmp/dot.svg
*
* where <array_program> is the name of the executable
*
* The output may also be possible to visualize online on
* https://dreampuf.github.io/GraphvizOnline/ or google "graphviz
* online".
*
* For documention of the dot language, see graphviz.org.
*
* Returns: Nothing.
*/
void array_2d_print_internal(const array_2d *a, inspect_callback print_func, const char *desc,
int indent_level);
#endif

199
OU3/include/dlist.h Normal file
View File

@@ -0,0 +1,199 @@
#ifndef __DLIST_H
#define __DLIST_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of a generic, directed list for the "Datastructures
* and algorithms" courses at the Department of Computing Science,
* Umea University. The list stores void pointers, so it can be used
* to store all types of values. After use, the function list_kill
* must be called to de-allocate the dynamic memory used by the list
* itself. The de-allocation of any dynamic memory allocated for the
* element values is the responsibility of the user of the list,
* unless a kill_function is registered in list_empty.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
* Adam Dahlgren Lindstrom (dali@cs.umu.se)
* Lars Karlsson (larsk@cs.umu.se)
*
* Based on earlier code by: Johan Eliasson (johane@cs.umu.se).
*
* Version information:
* v1.0 2018-01-28: First public version.
* v1.1 2023-01-19: Added dlist_pos_equal and dlist_pos_is_valid functions.
* v1.2 2023-01-20: Renamed dlist_pos_equal to dlist_pos_are_equal.
* v1.3 2023-03-23: Renamed dlist_pos_are_equal to dlist_pos_is_equal.
* v2.0 2024-03-14: Added dlist_print_internal to output dot code for visualization.
* Renamed free_* stuff to kill_*. Converted to 4-tabs.
* v2.1 2024-05-10: updated print_internal to enhance encapsulation.
*/
// ==========PUBLIC DATA TYPES============
// List type.
typedef struct dlist dlist;
// List position type.
typedef struct cell *dlist_pos;
// ==========DATA STRUCTURE INTERFACE==========
/**
* dlist_empty() - Create an empty dlist.
* @kill_func: A pointer to a function (or NULL) to be called to
* de-allocate memory on remove/kill.
*
* Returns: A pointer to the new list.
*/
dlist *dlist_empty(kill_function kill_func);
/**
* dlist_is_empty() - Check if a dlist is empty.
* @l: List to check.
*
* Returns: True if the list is empty, otherwise false.
*/
bool dlist_is_empty(const dlist *l);
/**
* dlist_first() - Return the first position of a dlist, i.e. the
* position of the first element in the list.
* @l: List to inspect.
*
* Returns: The first position in the given list.
*/
dlist_pos dlist_first(const dlist *l);
/**
* dlist_next() - Return the next position in a dlist.
* @l: List to inspect.
* @p: Any valid position except the last in the list.
*
* Returns: The position in the list after the given position.
* NOTE: The return value is undefined for the last position.
*/
dlist_pos dlist_next(const dlist *l, const dlist_pos p);
/**
* dlist_is_end() - Check if a given position is at the end of a dlist.
* @l: List to inspect.
* @p: Any valid position in the list.
*
* Returns: True if p is at the end of the list.
*/
bool dlist_is_end(const dlist *l, const dlist_pos p);
/**
* dlist_inspect() - Return the value of the element at a given
* position in a list.
* @l: List to inspect.
* @p: Any valid position in the list, except the last.
*
* Returns: Returns the value at the given position as a void pointer.
* NOTE: The return value is undefined for the last position.
*/
void *dlist_inspect(const dlist *l, const dlist_pos p);
/**
* dlist_insert() - Insert a new element with a given value into a dlist.
* @l: List to manipulate.
* @v: Value (pointer) to be inserted into the list.
* @p: Position in the list before which the value should be inserted.
*
* Creates a new element and inserts it into the list before p.
* Stores data in the new element.
*
* Returns: The position of the newly created element.
*/
dlist_pos dlist_insert(dlist *l, void *v, const dlist_pos p);
/**
* dlist_remove() - Remove an element from a dlist.
* @l: List to manipulate.
* @p: Position in the list of the element to remove.
*
* Removes the element at position p from the list. If a kill_func
* was registered at list creation, calls it to deallocate the memory
* held by the element value.
*
* Returns: The position after the removed element.
*/
dlist_pos dlist_remove(dlist *l, const dlist_pos p);
/**
* dlist_kill() - Destroy a given dlist.
* @l: List to destroy.
*
* Returns all dynamic memory used by the list and its elements. If a
* kill_func was registered at list creation, also calls it for each
* element to return any user-allocated memory occupied by the element values.
*
* Returns: Nothing.
*/
void dlist_kill(dlist *l);
/**
* dlist_print() - Iterate over the list elements and print their values.
* @l: List to inspect.
* @print_func: Function called for each element.
*
* Iterates over the list and calls print_func with the value stored
* in each element.
*
* Returns: Nothing.
*/
void dlist_print(const dlist *l, inspect_callback print_func);
/**
* dlist_pos_is_valid() - Return true for a valid position in a dlist
* @l: List to inspect.
* @p: Any position.
*
* Returns: True if p is a valid position in the list, otherwise false.
*/
bool dlist_pos_is_valid(const dlist *l, const dlist_pos p);
/**
* dlist_pos_is_equal() - Return true if two positions in a dlist are equal.
* @l: List to inspect.
* @p1: First position to compare.
* @p2: Second position to compare.
*
* Returns: True if p1 and p2 refer to the same position in l, otherwise False.
* NOTE: The result is defined only if p1 and p2 are valid positions in l.
* p1 and p2 are assumed to be valid positions in l, no check is performed.
*/
bool dlist_pos_is_equal(const dlist *l, const dlist_pos p1, const dlist_pos p2);
/**
* dlist_print_internal() - Print the lists internal structure in dot format.
* @l: List to inspect.
* @print_func: Function called for each element value.
* @desc: String with a description/state of the list, or NULL for no description.
* @indent_level: Indentation level, 0 for outermost
*
* Iterates over the list and outputs dot code that shows the internal
* structure of the list. The dot code can be visualized by
* Graphviz.
*
* On linux system, the output can be parsed by the dot program, e.g.
*
* <list_program> | dot -Tsvg > /tmp/dot.svg; firefox /tmp/dot.svg
*
* where <list_program> is the name of the executable
*
* The output may also be possible to visualize online on
* https://dreampuf.github.io/GraphvizOnline/ or google "graphviz
* online".
*
* For documention of the dot language, see graphviz.org.
*
* Returns: Nothing.
*/
void dlist_print_internal(const dlist *l, inspect_callback print_func, const char *desc,
int indent_level);
#endif

136
OU3/include/int_array_1d.h Normal file
View File

@@ -0,0 +1,136 @@
#ifndef __INT_ARRAY_1D_H
#define __INT_ARRAY_1D_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of an integer 1D array for the "Datastructures and
* algorithms" courses at the Department of Computing Science, Umea
* University. After use, the function array_kill must be called to
* de-allocate the dynamic memory used by the array itself.
*
* An element value of 0 is considered to be "no" value.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
*
* Based on earlier code by: Johan Eliasson (johane@cs.umu.se).
*
* Version information:
* v1.0 2018-01-28: First public version.
* v1.2 2024-05-10: Added print_internal.
*/
// ==========PUBLIC DATA TYPES============
// List type.
typedef struct int_array_1d int_array_1d;
// ==========DATA STRUCTURE INTERFACE==========
/**
* int_array_1d_create() - Create an array without values.
* @lo: low index limit.
* @hi: high index limit.
*
* The index limits are inclusive, i.e. all indices i such that low <=
* i <= high are defined.
*
* Returns: A pointer to the new array, or NULL if not enough memory
* was available.
*/
int_array_1d *int_array_1d_create(int lo, int hi);
/**
* int_array_1d_low() - Return the low index limit for the array.
* @a: array to inspect.
*
* Returns: The low index limit.
*/
int int_array_1d_low(const int_array_1d *a);
/**
* int_array_1d_high() - Return the high index limit for the array.
* @a: array to inspect.
*
* Returns: The high index limit.
*/
int int_array_1d_high(const int_array_1d *a);
/**
* int_array_1d_inspect_value() - Inspect a value at a given array position.
* @a: array to inspect.
* @i: index of position to inspect.
*
* Returns: The element value at the specified position, or 0 if no
* value is stored at that position.
*/
int int_array_1d_inspect_value(const int_array_1d *a, int i);
/**
* int_array_1d_has_value() - Check if a value is set at a given array position.
* @a: array to inspect.
* @i: index of position to inspect.
*
* Returns: True if a value is set at the specified position, otherwise false.
*/
bool int_array_1d_has_value(const int_array_1d *a, int i);
/**
* int_array_1d_set_value() - Set a value at a given array position.
* @a: array to modify.
* @v: value to set element to, or 0 to clear value.
* @i: index of position to modify.
*
* Returns: Nothing.
*/
void int_array_1d_set_value(int_array_1d *a, int v, int i);
/**
* int_array_1d_kill() - Return memory allocated by array.
* @a: array to kill.
*
* Iterates over all elements. If free_func was specified at array
* creation, calls it for every non-NULL element value.
*
* Returns: Nothing.
*/
void int_array_1d_kill(int_array_1d *a);
/**
* int_array_1d_print() - Iterate over the array element and print their values.
* @a: Array to inspect.
*
* Iterates over each position in the array. Prints each non-zero element.
*
* Returns: Nothing.
*/
void int_array_1d_print(const int_array_1d *a);
/**
* int_array_1d_print_internal() - Print the arrays internal structure in dot format.
* @a: Array to inspect.
* @desc: String with a description/state of the array, or NULL for no description.
* @indent_level: Indentation level, 0 for outermost
*
* Iterates over the array and outputs dot code that shows the internal
* structure of the array. The dot code can be visualized by
* Graphviz.
*
* On linux system, the output can be parsed by the dot program, e.g.
*
* <array_program> | dot -Tsvg > /tmp/dot.svg; firefox /tmp/dot.svg
*
* where <array_program> is the name of the executable
*
* The output may also be possible to visualize online on
* https://dreampuf.github.io/GraphvizOnline/ or google "graphviz
* online".
*
* For documention of the dot language, see graphviz.org.
*
* Returns: Nothing.
*/
void int_array_1d_print_internal(const int_array_1d *a, const char *desc, int indent_level);
#endif

192
OU3/include/int_list.h Normal file
View File

@@ -0,0 +1,192 @@
#ifndef __INT_LIST_H
#define __INT_LIST_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of a undirected list for storing integers for the
* "Datastructures and algorithms" courses at the Department of
* Computing Science, Umea University. The implementation uses a
* dynamic, linked structure. After use, the function list_kill must
* be called to de-allocate the dynamic memory used by the list
* itself. The implementation is a written a code copy specialization
* of the generic list to provide a simpler starting data structure
* than the generic list.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
*
* Version information:
* v1.0 2018-01-28: First public version.
* v1.1 2023-01-19: Added list_pos_equal and list_pos_is_valid functions.
* v1.2 2023-01-20: Renamed list_pos_equal to list_pos_are_equal.
* v1.3 2023-03-23: Renamed list_pos_are_equal to list_pos_is_equal.
* v1.4 2024-05-10: Added print_internal.
*/
// ==========PUBLIC DATA TYPES============
// List type.
typedef struct list list;
// List position type.
typedef struct cell *list_pos;
// ==========DATA STRUCTURE INTERFACE==========
/**
* list_empty() - Create an empty list.
*
* Returns: A pointer to the new list.
*/
list *list_empty(void);
/**
* list_is_empty() - Check if a list is empty.
* @l: List to check.
*
* Returns: True if the list is empty, otherwise false.
*/
bool list_is_empty(const list *l);
/**
* list_first() - Return the first position of a list, i.e. the
* position of the first element in the list.
* @l: List to inspect.
*
* Returns: The first position in the given list.
*/
list_pos list_first(const list *l);
/**
* list_end() - Return the last position of a list, i.e. the position
* after the last element in the list.
* @l: List to inspect.
*
* Returns: The last position in the given list.
*/
list_pos list_end(const list *l);
/**
* list_next() - Return the next position in a list.
* @l: List to inspect.
* @p: Any valid position except the last in the list.
*
* Returns: The position in the list after the given position.
* NOTE: The return value is undefined for the last position.
*/
list_pos list_next(const list *l, const list_pos p);
/**
* list_prev() - Return the previous position in a list.
* @l: List to inspect.
* @p: Any valid position except the first in the list.
*
* Returns: The position in the list before the given position.
* NOTE: The return value is undefined for the first position.
*/
list_pos list_prev(const list *l, const list_pos p);
/**
* list_inspect() - Return the value of the element at a given
* position in a list.
* @l: List to inspect.
* @p: Any valid position in the list, except the last.
*
* Returns: The integer value stored in the element at postiion pos.
* NOTE: The return value is undefined for the last position.
*/
int list_inspect(const list *l, const list_pos p);
/**
* list_insert() - Insert a new element with a given value into a list.
* @l: List to manipulate.
* @data: Integer value to be inserted into the list.
* @p: Position in the list before which the value should be inserted.
*
* Creates a new element and inserts it into the list before p.
* Stores data in the new element.
*
* Returns: The position of the newly created element.
*/
list_pos list_insert(list *l, int data, const list_pos p);
/**
* list_remove() - Remove an element from a list.
* @l: List to manipulate.
* @p: Position in the list of the element to remove.
*
* Removes the element at position p from the list.
*
* Returns: The position after the removed element.
*/
list_pos list_remove(list *l, const list_pos p);
/**
* list_kill() - Destroy a given list.
* @l: List to destroy.
*
* Returns all dynamic memory used by the list.
*
* Returns: Nothing.
*/
void list_kill(list *l);
/**
* list_print() - Iterate over the list elements and print their values.
* @l: List to inspect.
*
* Iterates over the list and prints each stored integer.
*
* Returns: Nothing.
*/
void list_print(const list *l);
/**
* list_pos_is_valid() - Return true for a valid position in a list
* @l: List to inspect.
* @p: Any position.
*
* Returns: True if p is a valid position in the list, otherwise false.
*/
bool list_pos_is_valid(const list *l, const list_pos p);
/**
* list_pos_is_equal() - Return true if two positions in a list are equal.
* @l: List to inspect.
* @p1: First position to compare.
* @p2: Second position to compare.
*
* Returns: True if p1 and p2 refer to the same position in l, otherwise False.
* NOTE: The result is defined only if p1 and p2 are valid positions in l.
* p1 and p2 are assumed to be valid positions in l, no check is performed.
*/
bool list_pos_is_equal(const list *l, const list_pos p1, const list_pos p2);
/**
* list_print_internal() - Print the lists internal structure in dot format.
* @l: List to inspect.
* @desc: String with a description/state of the list, or NULL for no description.
* @indent_level: Indentation level, 0 for outermost
*
* Iterates over the list and outputs dot code that shows the internal
* structure of the list. The dot code can be visualized by
* Graphviz.
*
* On linux system, the output can be parsed by the dot program, e.g.
*
* <list_program> | dot -Tsvg > /tmp/dot.svg; firefox /tmp/dot.svg
*
* where <list_program> is the name of the executable
*
* The output may also be possible to visualize online on
* https://dreampuf.github.io/GraphvizOnline/ or google "graphviz
* online".
*
* For documention of the dot language, see graphviz.org.
*
* Returns: Nothing.
*/
void list_print_internal(const list *l, const char *desc, int indent_level);
#endif

View File

@@ -0,0 +1,193 @@
#ifndef __INT_LIST_ARRAY_H
#define __INT_LIST_ARRAY_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of a undirected list for storing integers for the
* "Datastructures and algorithms" courses at the Department of
* Computing Science, Umea University. The implementation uses a
* static array. After use, the function list_kill must be called to
* de-allocate the dynamic memory used by the list itself. The
* implementation is a written a code copy specialization of the
* generic list to provide a simpler starting data structure than the
* generic list.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
*
* Version information:
* v1.0 2018-03-26: First public version.
* v1.1 2023-01-19: Added list_pos_equal and list_pos_is_valid functions.
* Bugfix in list_remove.
* v1.2 2023-01-20: Renamed list_pos_equal to list_pos_are_equal.
* v1.3 2023-03-23: Renamed list_pos_are_equal to list_pos_is_equal.
* v1.4 2024-05-10: Added print_internal.
*/
// ==========PUBLIC DATA TYPES============
// List type.
typedef struct list list;
// List position type.
typedef int list_pos;
// ==========DATA STRUCTURE INTERFACE==========
/**
* list_empty() - Create an empty list.
*
* Returns: A pointer to the new list.
*/
list *list_empty(void);
/**
* list_is_empty() - Check if a list is empty.
* @l: List to check.
*
* Returns: True if the list is empty, otherwise false.
*/
bool list_is_empty(const list *l);
/**
* list_first() - Return the first position of a list, i.e. the
* position of the first element in the list.
* @l: List to inspect.
*
* Returns: The first position in the given list.
*/
list_pos list_first(const list *l);
/**
* list_end() - Return the last position of a list, i.e. the position
* after the last element in the list.
* @l: List to inspect.
*
* Returns: The last position in the given list.
*/
list_pos list_end(const list *l);
/**
* list_next() - Return the next position in a list.
* @l: List to inspect.
* @p: Any valid position except the last in the list.
*
* Returns: The position in the list after the given position.
* NOTE: The return value is undefined for the last position.
*/
list_pos list_next(const list *l, const list_pos p);
/**
* list_prev() - Return the previous position in a list.
* @l: List to inspect.
* @p: Any valid position except the first in the list.
*
* Returns: The position in the list before the given position.
* NOTE: The return value is undefined for the first position.
*/
list_pos list_prev(const list *l, const list_pos p);
/**
* list_inspect() - Return the value of the element at a given
* position in a list.
* @l: List to inspect.
* @p: Any valid position in the list, except the last.
*
* Returns: The integer value stored in the element at postiion pos.
* NOTE: The return value is undefined for the last position.
*/
int list_inspect(const list *l, const list_pos p);
/**
* list_insert() - Insert a new element with a given value into a list.
* @l: List to manipulate.
* @data: Integer value to be inserted into the list.
* @p: Position in the list before which the value should be inserted.
*
* Creates a new element and inserts it into the list before p.
* Stores data in the new element.
*
* Returns: The position of the newly created element.
*/
list_pos list_insert(list *l, int data, const list_pos p);
/**
* list_remove() - Remove an element from a list.
* @l: List to manipulate.
* @p: Position in the list of the element to remove.
*
* Removes the element at position p from the list.
*
* Returns: The position after the removed element.
*/
list_pos list_remove(list *l, const list_pos p);
/**
* list_kill() - Destroy a given list.
* @l: List to destroy.
*
* Returns all dynamic memory used by the list.
*
* Returns: Nothing.
*/
void list_kill(list *l);
/**
* list_print() - Iterate over the list elements and print their values.
* @l: List to inspect.
*
* Iterates over the list and prints each stored integer.
*
* Returns: Nothing.
*/
void list_print(const list *l);
/**
* list_pos_is_valid() - Return true for a valid position in a list
* @l: List to inspect.
* @p: Any position.
*
* Returns: True if p is a valid position in the list, otherwise false.
*/
bool list_pos_is_valid(const list *l, const list_pos p);
/**
* list_pos_is_equal() - Return true if two positions in a list are equal.
* @l: List to inspect.
* @p1: First position to compare.
* @p2: Second position to compare.
*
* Returns: True if p1 and p2 refer to the same position in l, otherwise False.
* NOTE: The result is defined only if p1 and p2 are valid positions in l.
* p1 and p2 are assumed to be valid positions in l, no check is performed.
*/
bool list_pos_is_equal(const list *l, const list_pos p1, const list_pos p2);
/**
* list_print_internal() - Print the lists internal structure in dot format.
* @l: List to inspect.
* @desc: String with a description/state of the list, or NULL for no description.
* @indent_level: Indentation level, 0 for outermost
*
* Iterates over the list and outputs dot code that shows the internal
* structure of the list. The dot code can be visualized by
* Graphviz.
*
* On linux system, the output can be parsed by the dot program, e.g.
*
* <list_program> | dot -Tsvg > /tmp/dot.svg; firefox /tmp/dot.svg
*
* where <list_program> is the name of the executable
*
* The output may also be possible to visualize online on
* https://dreampuf.github.io/GraphvizOnline/ or google "graphviz
* online".
*
* For documention of the dot language, see graphviz.org.
*
* Returns: Nothing.
*/
void list_print_internal(const list *l, const char *desc, int indent_level);
#endif

126
OU3/include/int_queue.h Normal file
View File

@@ -0,0 +1,126 @@
#ifndef __INT_QUEUE_H
#define __INT_QUEUE_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of an integer queue for the "Datastructures and
* algorithms" courses at the Department of Computing Science, Umea
* University. The queue stores integers directly and does not use
* dynamic memory. Thus, the clean-up function queue_kill is strictly
* not necessary, but recommended to maintain symmetry with untyped,
* generic queue implementations.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
*
* Version information:
* v1.0 2025-01-10: First public version.
*/
// ==========PUBLIC CONSTANTS AND DATA TYPES============
// Maximum size of the queue.
#define MAX_QUEUE_SIZE 100
// We must define the struct publically for the compiler to know its
// size for copying, etc.
typedef struct queue {
int first_free_pos;
int elements[MAX_QUEUE_SIZE];
} queue;
// ==========DATA STRUCTURE INTERFACE==========
/**
* queue_empty() - Create an empty queue.
*
* Returns: A new, empty, queue.
*/
queue queue_empty(void);
/**
* queue_is_empty() - Check if a queue is empty.
* @q: Queue to check.
*
* Returns: True if queue is empty, otherwise false.
*/
bool queue_is_empty(const queue q);
/**
* queue_enqueue() - Put a value at the end of a queue.
* @q: Queue to manipulate.
* @v: Value (integer) to be put in the queue.
*
* Returns: The modified queue.
*/
queue queue_enqueue(queue q, int v);
/**
* queue_dequeue() - Remove the element at the beginning of a queue.
* @q: Queue to manipulate.
*
* NOTE: Undefined for an empty queue.
*
* Returns: The modified queue.
*/
queue queue_dequeue(queue q);
/**
* queue_front() - Inspect the value at the front of the queue.
* @q: Queue to inspect.
*
* Returns: The value at the front of the queue.
* NOTE: The return value is undefined for an empty queue.
*/
int queue_front(const queue q);
/**
* queue_kill() - Destroy a given queue.
* @q: Queue to destroy.
*
* Does nothing since the queue does not use any dynamic
* memory. Included for symmetry with generic queue.h.
*
* Returns: Nothing.
*/
void queue_kill(queue q);
/**
* queue_print() - Iterate over the queue elements and print their values.
* @q: Queue to inspect.
*
* Iterates over the queue and prints each integer.
*
* Returns: Nothing.
*/
void queue_print(const queue q);
/**
* queue_print_internal() - Print the internal structure of the queue in dot format.
* @q: Queue to inspect.
* @desc: String with a description/state of the queue, or NULL for no description.
* @indent_level: Indentation level, 0 for outermost.
* @max_elems: Maximum number of elements to print.
*
* Iterates over the queue and outputs dot code that shows the
* internal structure of the queue. The dot code can be visualized by
* Graphviz.
*
* On linux system, the output can be parsed by the dot program, e.g.
*
* <queue_program> | dot -Tsvg > /tmp/dot.svg; firefox /tmp/dot.svg
*
* where <queue_program> is the name of the executable
*
* The output may also be possible to visualize online on
* https://dreampuf.github.io/GraphvizOnline/ or google "graphviz
* online".
*
* For documention of the dot language, see graphviz.org.
*
* Returns: Nothing.
*/
void queue_print_internal(const queue q, const char *desc, int indent_level, int max_elems);
#endif

134
OU3/include/int_stack.h Normal file
View File

@@ -0,0 +1,134 @@
#ifndef __INT_STACK_H
#define __INT_STACK_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of an integer stack for the "Datastructures and
* algorithms" courses at the Department of Computing Science, Umea
* University. The stack stores integers directly and does not use
* dynamic memory. Thus, the clean-up function stack_kill is strictly
* not necessary, but recommended to maintain symmetry with untyped,
* generic stack implementations.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
* Adam Dahlgren Lindstrom (dali@cs.umu.se)
*
* Based on earlier code by: Johan Eliasson (johane@cs.umu.se).
*
* Version information:
* v1.0 2018-01-28: First public version.
* v1.1 2024-04-15: Reduced default stack size to 15 to make output
* from print_internal readable.
* v1.2 2024-05-10: Updated print_internal to enhance encapsulation.
*/
// ==========PUBLIC DATA TYPES============
// Stack type.
#define MAX_STACK_SIZE 100
// We must define the struct publically for the compiler to know its
// size for copying, etc.
typedef struct stack {
int first_free_pos;
int elements[MAX_STACK_SIZE];
} stack;
// ==========DATA STRUCTURE INTERFACE==========
/**
* stack_empty() - Create an empty stack.
*
* Returns: A new stack.
*/
stack stack_empty(void);
/**
* stack_is_empty() - Check if a stack is empty.
* @s: Stack to check.
*
* Returns: True if stack is empty, otherwise false.
*/
bool stack_is_empty(const stack s);
/**
* stack_push() - Push a value on top of a stack.
* @s: Stack to manipulate.
* @v: Value (integer) to be put on the stack.
*
* Returns: The modified stack.
* NOTE: After the call, the input stack should be considered invalid.
*/
stack stack_push(stack s, int v);
/**
* stack_pop() - Remove the element at the top of a stack.
* @s: Stack to manipulate.
*
* NOTE: Undefined for an empty stack.
*
* Returns: The modified stack.
* NOTE: After the call, the input stack should be considered invalid.
*/
stack stack_pop(stack s);
/**
* stack_top() - Inspect the value at the top of the stack.
* @s: Stack to inspect.
*
* Returns: The value at the top of the stack.
* NOTE: The return value is undefined for an empty stack.
*/
int stack_top(const stack s);
/**
* stack_kill() - Destroy a given stack.
* @s: Stack to destroy.
*
* Does nothing since the stack does not use any dynamic
* memory. Included for symmetry with generic stack.h.
*
* Returns: Nothing.
*/
void stack_kill(stack s);
/**
* stack_print() - Iterate over the stack elements and print their values.
* @s: Stack to inspect.
*
* Iterates over the stack and prints each integer.
*
* Returns: Nothing.
*/
void stack_print(const stack s);
/**
* stack_print_internal() - Print the internal structure of the stack in dot format.
* @s: Stack to inspect.
* @desc: String with a description/state of the stack, or NULL for no description.
* @indent_level: Indentation level, 0 for outermost.
* @max_elems: Maximum number of elements to print.
*
* Iterates over the stack and outputs dot code that shows the
* internal structure of the stack. The dot code can be visualized by
* Graphviz.
*
* On linux system, the output can be parsed by the dot program, e.g.
*
* <stack_program> | dot -Tsvg > /tmp/dot.svg; firefox /tmp/dot.svg
*
* where <stack_program> is the name of the executable
*
* The output may also be possible to visualize online on
* https://dreampuf.github.io/GraphvizOnline/ or google "graphviz
* online".
*
* For documention of the dot language, see graphviz.org.
*
* Returns: Nothing.
*/
void stack_print_internal(const stack s, const char *desc, int indent_level, int max_elems);
#endif

207
OU3/include/list.h Normal file
View File

@@ -0,0 +1,207 @@
#ifndef __LIST_H
#define __LIST_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of a generic, undirected list for the "Datastructures
* and algorithms" courses at the Department of Computing Science,
* Umea University. The list stores void pointers, so it can be used
* to store all types of values. After use, the function list_kill
* must be called to de-allocate the dynamic memory used by the list
* itself. The de-allocation of any dynamic memory allocated for the
* element values is the responsibility of the user of the list,
* unless a kill_function is registered in list_empty.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
* Adam Dahlgren Lindstrom (dali@cs.umu.se)
* Lars Karlsson (larsk@cs.umu.se)
*
* Based on earlier code by: Johan Eliasson (johane@cs.umu.se).
*
* Version information:
* v1.0 2018-01-28: First public version.
* v1.1 2023-01-19: Added list_pos_equal and list_pos_is_valid functions.
* v1.2 2023-01-20: Renamed list_pos_equal to list_pos_are_equal.
* v1.3 2023-03-23: Renamed list_pos_are_equal to list_pos_is_equal.
* v2.0 2024-03-14: Added list_print_internal to output dot code for visualization.
* v2.1 2024-05-10: Updated print_internal to enhance encapsulation.
*/
// ==========PUBLIC DATA TYPES============
// List type.
typedef struct list list;
// List position type.
typedef struct cell *list_pos;
// ==========DATA STRUCTURE INTERFACE==========
/**
* list_empty() - Create an empty list.
* @kill_func: A pointer to a function (or NULL) to be called to
* de-allocate memory on remove/kill.
*
* Returns: A pointer to the new list.
*/
list *list_empty(kill_function kill_func);
/**
* list_is_empty() - Check if a list is empty.
* @l: List to check.
*
* Returns: True if the list is empty, otherwise false.
*/
bool list_is_empty(const list *l);
/**
* list_first() - Return the first position of a list, i.e. the
* position of the first element in the list.
* @l: List to inspect.
*
* Returns: The first position in the given list.
*/
list_pos list_first(const list *l);
/**
* list_end() - Return the last position of a list, i.e. the position
* after the last element in the list.
* @l: List to inspect.
*
* Returns: The last position in the given list.
*/
list_pos list_end(const list *l);
/**
* list_next() - Return the next position in a list.
* @l: List to inspect.
* @p: Any valid position except the last in the list.
*
* Returns: The position in the list after the given position.
* NOTE: The return value is undefined for the last position.
*/
list_pos list_next(const list *l, const list_pos p);
/**
* list_prev() - Return the previous position in a list.
* @l: List to inspect.
* @p: Any valid position except the first in the list.
*
* Returns: The position in the list before the given position.
* NOTE: The return value is undefined for the first position.
*/
list_pos list_prev(const list *l, const list_pos p);
/**
* list_inspect() - Return the value of the element at a given
* position in a list.
* @l: List to inspect.
* @p: Any valid position in the list, except the last.
*
* Returns: Returns the value at the given position as a void pointer.
* NOTE: The return value is undefined for the last position.
*/
void *list_inspect(const list *l, const list_pos p);
/**
* list_insert() - Insert a new element with a given value into a list.
* @l: List to manipulate.
* @v: Value (pointer) to be inserted into the list.
* @p: Position in the list before which the value should be inserted.
*
* Creates a new element and inserts it into the list before p.
* Stores data in the new element.
*
* Returns: The position of the newly created element.
*/
list_pos list_insert(list *l, void *v, const list_pos p);
/**
* list_remove() - Remove an element from a list.
* @l: List to manipulate.
* @p: Position in the list of the element to remove.
*
* Removes the element at position p from the list. If a kill_func
* was registered at list creation, calls it to deallocate the memory
* held by the element value.
*
* Returns: The position after the removed element.
*/
list_pos list_remove(list *l, const list_pos p);
/**
* list_kill() - Destroy a given list.
* @l: List to destroy.
*
* Returns all dynamic memory used by the list and its elements. If a
* kill_func was registered at list creation, also calls it for each
* element to return any user-allocated memory occupied by the element values.
*
* Returns: Nothing.
*/
void list_kill(list *l);
/**
* list_print() - Iterate over the list elements and print their values.
* @l: List to inspect.
* @print_func: Function called for each element.
*
* Iterates over the list and calls print_func with the value stored
* in each element.
*
* Returns: Nothing.
*/
void list_print(const list *l, inspect_callback print_func);
/**
* list_pos_is_valid() - Return true for a valid position in a list
* @l: List to inspect.
* @p: Any position.
*
* Returns: True if p is a valid position in the list, otherwise false.
*/
bool list_pos_is_valid(const list *l, const list_pos p);
/**
* list_pos_is_equal() - Return true if two positions in a list are equal.
* @l: List to inspect.
* @p1: First position to compare.
* @p2: Second position to compare.
*
* Returns: True if p1 and p2 refer to the same position in l, otherwise False.
* NOTE: The result is defined only if p1 and p2 are valid positions in l.
* p1 and p2 are assumed to be valid positions in l, no check is performed.
*/
bool list_pos_is_equal(const list *l, const list_pos p1, const list_pos p2);
/**
* list_print_internal() - Print the lists internal structure in dot format.
* @l: List to inspect.
* @print_func: Function called for each element value.
* @desc: String with a description/state of the list, or NULL for no description.
* @indent_level: Indentation level, 0 for outermost
*
* Iterates over the list and outputs dot code that shows the internal
* structure of the list. The dot code can be visualized by
* Graphviz.
*
* On linux system, the output can be parsed by the dot program, e.g.
*
* <list_program> | dot -Tsvg > /tmp/dot.svg; firefox /tmp/dot.svg
*
* where <list_program> is the name of the executable
*
* The output may also be possible to visualize online on
* https://dreampuf.github.io/GraphvizOnline/ or google "graphviz
* online".
*
* For documention of the dot language, see graphviz.org.
*
* Returns: Nothing.
*/
void list_print_internal(const list *l, inspect_callback print_func, const char *desc,
int indent_level);
#endif

131
OU3/include/queue.h Normal file
View File

@@ -0,0 +1,131 @@
#ifndef __QUEUE_H
#define __QUEUE_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of a generic queue for the "Datastructures and
* algorithms" courses at the Department of Computing Science, Umea
* University. The queue stores void pointers, so it can be used to
* store all types of values. After use, the function queue_kill must
* be called to de-allocate the dynamic memory used by the queue
* itself. The de-allocation of any dynamic memory allocated for the
* element values is the responsibility of the user of the queue,
* unless a kill_function is registered in queue_empty.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
* Adam Dahlgren Lindstrom (dali@cs.umu.se)
*
* Based on earlier code by: Johan Eliasson (johane@cs.umu.se).
*
* Version information:
* v1.0 2018-01-28: First public version.
* v1.1 2024-05-10: Added/updated print_internal to enhance encapsulation.
*/
// ==========PUBLIC DATA TYPES============
// Queue type.
typedef struct queue queue;
// ==========DATA STRUCTURE INTERFACE==========
/**
* queue_empty() - Create an empty queue.
* @kill_func: A pointer to a function (or NULL) to be called to
* de-allocate memory on remove/kill.
*
* Returns: A pointer to the new queue.
*/
queue *queue_empty(kill_function kill_func);
/**
* queue_is_empty() - Check if a queue is empty.
* @q: Queue to check.
*
* Returns: True if queue is empty, otherwise false.
*/
bool queue_is_empty(const queue *q);
/**
* queue_enqueue() - Put a value at the end of the queue.
* @q: Queue to manipulate.
* @v: Value (pointer) to be put in the queue.
*
* Returns: The modified queue.
*/
queue *queue_enqueue(queue *q, void *v);
/**
* queue_dequeue() - Remove the element at the front of a queue.
* @q: Queue to manipulate.
*
* NOTE: Undefined for an empty queue.
*
* Returns: The modified queue.
*/
queue *queue_dequeue(queue *q);
/**
* queue_front() - Inspect the value at the front of the queue.
* @q: Queue to inspect.
*
* Returns: The value at the top of the queue.
* NOTE: The return value is undefined for an empty queue.
*/
void *queue_front(const queue *q);
/**
* queue_kill() - Destroy a given queue.
* @q: Queue to destroy.
*
* Return all dynamic memory used by the queue and its elements. If a
* kill_func was registered at queue creation, also calls it for each
* element to kill any user-allocated memory occupied by the element values.
*
* Returns: Nothing.
*/
void queue_kill(queue *q);
/**
* queue_print() - Iterate over the queue elements and print their values.
* @q: Queue to inspect.
* @print_func: Function called for each element.
*
* Iterates over the queue and calls print_func with the value stored
* in each element.
*
* Returns: Nothing.
*/
void queue_print(const queue *q, inspect_callback print_func);
/**
* queue_print_internal() - Print the internal structure of the queue in dot format.
* @q: Queue to inspect.
* @print_func: Function called for each element value.
* @desc: String with a description/state of the queue, or NULL for no description.
* @indent_level: Indentation level, 0 for outermost
*
* Iterates over the queue and outputs dot code that shows the internal
* structure of the queue. The dot code can be visualized by
* Graphviz.
*
* On linux system, the output can be parsed by the dot program, e.g.
*
* <queue_program> | dot -Tsvg > /tmp/dot.svg; firefox /tmp/dot.svg
*
* where <queue_program> is the name of the executable
*
* The output may also be possible to visualize online on
* https://dreampuf.github.io/GraphvizOnline/ or google "graphviz
* online".
*
* For documention of the dot language, see graphviz.org.
*
* Returns: Nothing.
*/
void queue_print_internal(const queue *q, inspect_callback print_func, const char *desc,
int indent_level);
#endif

133
OU3/include/stack.h Normal file
View File

@@ -0,0 +1,133 @@
#ifndef __STACK_H
#define __STACK_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of a generic stack for the "Datastructures and
* algorithms" courses at the Department of Computing Science, Umea
* University. The stack stores void pointers, so it can be used to
* store all types of values. After use, the function stack_kill must
* be called to de-allocate the dynamic memory used by the stack
* itself. The de-allocation of any dynamic memory allocated for the
* element values is the responsibility of the user of the stack,
* unless a kill_function is registered in stack_empty.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
* Adam Dahlgren Lindstrom (dali@cs.umu.se)
*
* Based on earlier code by: Johan Eliasson (johane@cs.umu.se).
*
* Version information:
* v1.0 2018-01-28: First public version.
* v1.1 2024-05-10: Added/updated print_internal to enhance encapsulation.
*/
// ==========PUBLIC DATA TYPES============
// Stack type.
typedef struct stack stack;
// ==========DATA STRUCTURE INTERFACE==========
/**
* stack_empty() - Create an empty stack.
* @kill_func: A pointer to a function (or NULL) to be called to
* de-allocate memory on remove/kill.
*
* Returns: A pointer to the new stack.
*/
stack *stack_empty(kill_function kill_func);
/**
* stack_is_empty() - Check if a stack is empty.
* @s: Stack to check.
*
* Returns: True if stack is empty, otherwise false.
*/
bool stack_is_empty(const stack *s);
/**
* stack_push() - Push a value on top of a stack.
* @s: Stack to manipulate.
* @v: Value (pointer) to be put on the stack.
*
* Returns: The modified stack.
* NOTE: After the call, the input stack should be considered invalid.
*/
stack *stack_push(stack *s, void *v);
/**
* stack_pop() - Remove the element at the top of a stack.
* @s: Stack to manipulate.
*
* NOTE: Undefined for an empty stack.
*
* Returns: The modified stack.
* NOTE: After the call, the input stack should be considered invalid.
*/
stack *stack_pop(stack *s);
/**
* stack_top() - Inspect the value at the top of the stack.
* @s: Stack to inspect.
*
* Returns: The value at the top of the stack.
* NOTE: The return value is undefined for an empty stack.
*/
void *stack_top(const stack *s);
/**
* stack_kill() - Destroy a given stack.
* @s: Stack to destroy.
*
* Return all dynamic memory used by the stack and its elements. If a
* kill_func was registered at stack creation, also calls it for each
* element to kill any user-allocated memory occupied by the element values.
*
* Returns: Nothing.
*/
void stack_kill(stack *s);
/**
* stack_print() - Iterate over the stack elements and print their values.
* @s: Stack to inspect.
* @print_func: Function called for each element.
*
* Iterates over the stack and calls print_func with the value stored
* in each element.
*
* Returns: Nothing.
*/
void stack_print(const stack *s, inspect_callback print_func);
/**
* stack_print_internal() - Print the stacks internal structure in dot format.
* @l: Stack to inspect.
* @print_func: Function called for each element value.
* @desc: String with a description/state of the stack, or NULL for no description.
* @indent_level: Indentation level, 0 for outermost
*
* Iterates over the stack and outputs dot code that shows the internal
* structure of the stack. The dot code can be visualized by
* Graphviz.
*
* On linux system, the output can be parsed by the dot program, e.g.
*
* <stack_program> | dot -Tsvg > /tmp/dot.svg; firefox /tmp/dot.svg
*
* where <stack_program> is the name of the executable
*
* The output may also be possible to visualize online on
* https://dreampuf.github.io/GraphvizOnline/ or google "graphviz
* online".
*
* For documention of the dot language, see graphviz.org.
*
* Returns: Nothing.
*/
void stack_print_internal(const stack *s, inspect_callback print_func, const char *desc,
int indent_level);
#endif

157
OU3/include/table.h Normal file
View File

@@ -0,0 +1,157 @@
#ifndef TABLE_H
#define TABLE_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of a generic table for the "Datastructures and
* algorithms" courses at the Department of Computing Science, Umea
* University. The table stores void pointers, so it can be used to
* store all types of values. After use, the function table_kill must
* be called to de-allocate the dynamic memory used by the table
* itself. The de-allocation of any dynamic memory allocated for the
* key and/or value values is the responsibility of the user of the
* table, unless a corresponding kill_function is registered in
* table_empty.
*
* The internal handling of duplicates are transparent to the user of
* the table. Depending on the table design, a duplicate may or may
* not be stored internally. The user of the table should not make any
* assumption about which solution is implemented.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
* Adam Dahlgren Lindstrom (dali@cs.umu.se)
*
* Based on earlier code by: Johan Eliasson (johane@cs.umu.se).
*
* Version information:
* v1.0 2018-02-06: First public version.
* v1.1 2024-04-15: Changed the comments to leave room for different
* internal handling of duplicates.
* v1.2 2024-05-10: Added/updated print_internal to enhance encapsulation.
*/
// ==========PUBLIC DATA TYPES============
// Table type.
typedef struct table table;
// ==========DATA STRUCTURE INTERFACE==========
/**
* table_empty() - Create an empty table.
* @key_cmp_func: A pointer to a function to be used to compare keys. See
* util.h for the definition of compare_function.
* @key_kill_func: A pointer to a function (or NULL) to be called to
* de-allocate memory for keys on remove/kill.
* @value_kill_func: A pointer to a function (or NULL) to be called to
* de-allocate memory for values on remove/kill.
*
* Returns: Pointer to a new table.
*/
table *table_empty(compare_function key_cmp_func,
kill_function key_kill_func,
kill_function value_kill_func);
/**
* table_is_empty() - Check if a table is empty.
* @t: Table to check.
*
* Returns: True if table contains no key/value pairs, false otherwise.
*/
bool table_is_empty(const table *t);
/**
* table_insert() - Add a key/value pair to a table.
* @t: Table to manipulate.
* @key: A pointer to the key value.
* @value: A pointer to the value value.
*
* Insert the key/value pair into the table. If the key already
* exists, the key/value overwrites the existing pair. The technical
* details are internal to the datatype. At any rate, table_lookup()
* will return the latest added value for a duplicate
* key and table_remove() will remove all duplicates for a given key.
*
* Returns: Nothing.
*/
void table_insert(table *t, void *key, void *value);
/**
* table_lookup() - Look up a given key in a table.
* @t: Table to inspect.
* @key: Key to look up.
*
* Returns: The value corresponding to a given key, or NULL if the key
* is not found in the table. If the table contains duplicate keys,
* the value that was latest inserted will be returned.
*/
void *table_lookup(const table *t, const void *key);
/**
* table_choose_key() - Return an arbitrary key.
* @t: Table to inspect.
*
* Return an arbitrary key stored in the table. Can be used together
* with table_remove() to deconstruct the table. Undefined for an
* empty table.
*
* Returns: An arbitrary key stored in the table.
*/
void *table_choose_key(const table *t);
/**
* table_remove() - Remove a key/value pair in the table.
* @t: Table to manipulate.
* @key: Key for which to remove pair.
*
* Any matching duplicates will be removed. Will call any kill
* functions set for keys/values. Does nothing if key is not found in
* the table.
*
* Returns: Nothing.
*/
void table_remove(table *t, const void *key);
/**
* table_kill() - Destroy a table.
* @t: Table to destroy.
*
* Return all dynamic memory used by the table and its elements. If a
* kill_func was registered for keys and/or values at table creation,
* it is called each element to free any user-allocated memory
* occupied by the element values.
*
* Returns: Nothing.
*/
void table_kill(table *t);
/**
* table_print() - Print the given table.
* @t: Table to print.
* @print_func: Function called for each key/value pair in the table.
*
* Iterates over the key/value pairs in the table and prints them.
* Will print all stored elements, including any duplicates.
*
* Returns: Nothing.
*/
void table_print(const table *t, inspect_callback_pair print_func);
/**
* table_print_internal() - Output the internal structure of the table.
* @t: Table to print.
* @key_print_func: Function called for each key in the table.
* @value_print_func: Function called for each value in the table.
* @desc: String with a description/state of the list.
* @indent_level: Indentation level, 0 for outermost
*
* Iterates over the list and prints code that shows its' internal structure.
*
* Returns: Nothing.
*/
void table_print_internal(const table *t, inspect_callback key_print_func,
inspect_callback value_print_func, const char *desc,
int indent_level);
#endif

83
OU3/include/util.h Normal file
View File

@@ -0,0 +1,83 @@
#ifndef __UTIL_H
#define __UTIL_H
/*
* Utility function types for deallocating, printing and comparing
* values stored by various data types.
*
* Authors: Niclas Borlin (niclas@cs.umu.se)
* Adam Dahlgren Lindstrom (dali@cs.umu.se)
*
* Based on earlier code by: Johan Eliasson (johane@cs.umu.se).
*
* Version information:
* v1.0 2018-01-28: First public version.
* v1.1 2018-02-06: Updated explanation for the compare_function.
* v1.2 2023-01-14: Added version define constants and strings.
* v1.3 2024-03-13: Added PTR2ADDR macro.
*/
// Macros to create a version string out of version constants
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
// Version constants
#define CODE_BASE_MAJOR_VERSION 2
#define CODE_BASE_MINOR_VERSION 2
#define CODE_BASE_REVISION 2
#define CODE_BASE_PATCH 2
#define CODE_BASE_RELEASE_DATE "2025-01-24"
// Create a short version string
#define CODE_BASE_VERSION "v" \
STR(CODE_BASE_MAJOR_VERSION) \
"." \
STR(CODE_BASE_MINOR_VERSION) \
"." \
STR(CODE_BASE_REVISION) \
"." \
STR(CODE_BASE_PATCH)
// Create a version string
#define CODE_BASE_LONG_VERSION "Version: " \
STR(CODE_BASE_MAJOR_VERSION) \
"." \
STR(CODE_BASE_MINOR_VERSION) \
"." \
STR(CODE_BASE_REVISION) \
"." \
STR(CODE_BASE_PATCH)
// Type definition for de-allocator function, e.g. free().
typedef void (*kill_function)(void *);
// For backwards compatibility with pre-v2.0 code.
typedef kill_function free_function;
// Type definition for read-only callback for single-value containers,
// used by e.g. print functions.
typedef void (*inspect_callback)(const void *);
// Ditto for dual-value containers.
typedef void (*inspect_callback_pair)(const void *, const void *);
// Type definition for comparison function, used by e.g. table.
//
// Comparison functions should return values that indicate the order
// of the arguments. If the first argument is considered less/lower
// than the second, a negative value should be returned. If the first
// argument is considered more/higher than the second, a positive value
// should be returned. If the arguments are considered equal, a zero
// value should be returned.
typedef int compare_function(const void *,const void *);
// Constant used by ptr2addr, used by various print_internal functions.
#define PTR2ADDR_MASK 0xffff
// Macro that acts as a function to convert the address in the pointer
// p into an unsigned long value, keeping only the part indicated by
// the mask.
#define PTR2ADDR(p) (((unsigned long)p) & PTR2ADDR_MASK)
#endif