Algorithms/README.md

67 lines
2.1 KiB
Markdown
Raw Normal View History

2017-05-22 12:13:50 +00:00
# Algorithms
A handy collection of C algorithms compiled into one header file for use anywhere. Feel free to include this file with any C project, as long as you keep the comment at the top with a little copyright notice.
2017-05-23 09:13:41 +00:00
## Table of Contents
1. [Stacks](#stack)
2. [Queues](#queue)
3. [Linked Lists](#linked-list)
4. [Heap(Priority Queue)](#heap-priority-queue)
2017-05-23 09:05:23 +00:00
2017-05-23 09:10:24 +00:00
## Stack
2017-05-23 09:10:24 +00:00
### Functions
2017-05-23 09:05:23 +00:00
```c
//Creates new stack.
void new();
//Frees stack memory.
void free(Stack *s);
//Pushes item to top of the stack.
void push(item, Stack *s);
//Removes item on top of the stack and returns it.
item pop(Stack *s);
//Prints stack size, memory usage.
void print(Stack s);
//Returns 1 if empty stack, 0 otherwise.
int is_empty(Stack s);
2017-05-23 09:05:23 +00:00
```
2017-05-23 09:10:24 +00:00
### Description
The stack is probably the most basic storage structure, using the 'first-in,
first-out' approach. To define a stack, the user must first know what type the stack
is, either a primitive type, or a **struct**. To define a stack, you must first define
the **STACK_TYPE** to *int*, *float*, *char*, or whatever type you wish. Then, include
the *handystack* header file.
2017-05-23 09:05:23 +00:00
```c
#define STACK_TYPE int
#include "handystack.h"
```
To define more than one stack, **STACK_TYPE** must first be undefined.
2017-05-23 09:05:23 +00:00
` #undef STACK_TYPE `
Then you simply define the type, and include the header again. Do not try to define two of the same type, however, as that will give all sorts of nasty errors.
2017-05-23 09:10:24 +00:00
### Example
2017-05-23 09:05:23 +00:00
The following example creates a stack of doubles, pushes 45.000 onto it, and prints the stack's characteristics.
```c
#define STACK_TYPE double
#include "handystack.h"
double_stack myStack = new_double_stack();
push_double_stack(45.000, &myStack);
print_double_stack(myStack);
```
Notice that the function names are characteristic of the **STACK_TYPE** you've defined earlier? The functions have the following basic format:
``` FUNCTION_ + STACK_TYPE + _stack ```
Where `FUNCTION` is the name of the function.
[Back to Top](#table-of-contents)
2017-05-23 09:10:24 +00:00
## Queue
[Back to Top](#table-of-contents)
2017-05-23 09:10:24 +00:00
## Linked List
[Back to Top](#table-of-contents)
2017-05-23 09:10:24 +00:00
## Heap (Priority Queue)
[Back to Top](#table-of-contents)