Added true generic typing for stacks.

Allowed the user to custom define types for stacks, and more to come in the future.
This commit is contained in:
Andrew Lalis 2017-05-23 10:45:31 +02:00
parent d034657bf6
commit bdfb9c9d57
5 changed files with 142 additions and 130 deletions

View File

@ -4,7 +4,16 @@ A handy collection of C algorithms compiled into one header file for use anywher
## Functionality ## Functionality
### Stack ### Stack
The stack is probably the most basic storage structure, using the 'first-in, first-out' approach. The following functions are the only ones available for stack manipulation, as it is quite a minimalistic data structure. The STACK_TYPE is a constant defined above the stack functions which can be set by the user to determine the type of items in the stack. By default it is left as an integer. 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.
``` #define STACK_TYPE int
#include "handystack.h" ```
To define more than one stack, **STACK_TYPE** must first be undefined.
``` #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.
* ``` Stack createStack() ``` * ``` Stack createStack() ```
* ``` void freeStack(Stack *s) ``` * ``` void freeStack(Stack *s) ```
* ``` void pushToStack(STACK_TYPE item, Stack *s) ``` * ``` void pushToStack(STACK_TYPE item, Stack *s) ```

View File

@ -10,116 +10,5 @@ Please contact the author regarding bugs and/or feature requests.
#ifndef HANDYALGS_H #ifndef HANDYALGS_H
#define HANDYALGS_H #define HANDYALGS_H
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
/*
STACK
The stack is a data structure in which items are stored and retrieved by the first-in, first-out basis. The stack keeps track of the index of the top item, so that the item may be taken out with pop. push will add an item to the top of the list, and increment the top variable.
*/
/*
Stack constants:
*/
// Size allocated to the stack on creation.
#define STACK_SIZE_DEFAULT 8
// Type to be used for the stack.
#define STACK_TYPE TEST
/*
Stack Structure:
Stores the top index of the stack, and a list of items in the stack.
top: index of the top of the stack (one above top item).
size: allocated size of the stack's list.
items: list of items in the stack.
*/
typedef struct {
int top;
int size;
STACK_TYPE* items;
} Stack;
/*
Stack Functions:
*/
Stack newStack();
void freeStack(Stack *s);
void pushToStack(STACK_TYPE item, Stack *s);
STACK_TYPE popFromStack(Stack *s);
void printStack(Stack s);
/*
Stack Creation:
Creates a new stack with a default size defined above.
*/
Stack newStack(){
Stack s;
s.top = 0;
s.size = STACK_SIZE_DEFAULT;
s.items = malloc(s.size*sizeof(STACK_TYPE));
assert(s.items != NULL);
return s;
}
/*
Stack Deallocation:
Safely deallocates memory assigned to the stack's list.
*/
void freeStack(Stack *s){
free(s->items);
}
/*
Stack Push:
Pushes an item onto a stack. If there is no more room, more memory will be allocated to accommodate the extra item.
*/
void pushToStack(STACK_TYPE item, Stack *s){
//It is safe to push to the stack.
if (s->top < s->size){
s->items[s->top] = item;
} else {
//There was not enough space, so we must double size.
s->size *= 2;
s->items = realloc(s->items, s->size*sizeof(STACK_TYPE));
assert(s->items != NULL);
s->items[s->top] = item;
}
s->top++;
}
/*
Stack Pop:
Pops an item from the top of the stack, if it exists. Size will be checked to see if it is needed to reduce the size of the array.
*/
STACK_TYPE popFromStack(Stack *s){
STACK_TYPE result;
//Check if at least one item exists in the list.
if (s->top > 0){
s->top--;
result = s->items[s->top];
} else {
//The stack is empty.
fprintf(stderr, "Stack empty, exiting.");
exit(-1);
}
//Check if the items array size can be reduced.
if (s->top < (s->size / 2) && (s->size > STACK_SIZE_DEFAULT)){
s->size /= 2;
s->items = realloc(s->items, s->size*sizeof(STACK_TYPE));
assert(s->items != NULL);
}
return result;
}
/*
Stack Print:
Utility function to print a stack.
*/
void printStack(Stack s){
printf("\tStack: \tItems: %d, Allocated Size: %d, Bytes used: %lu\n", s.top, s.size, s.size*sizeof(STACK_TYPE));
}
#endif #endif

117
source/handystack.h Normal file
View File

@ -0,0 +1,117 @@
/*
handystack.h
Created By:
Andrew Lalis (andrewlalisofficial@gmail.com)
22 May 2017
This file is free to use, as long as the above comment remains in the file.
Please contact the author regarding bugs and/or feature requests.
*/
#ifdef STACK_TYPE
#include "templates.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
/*
Stack
The stack is a data structure in which items are stored and retrieved by the first-in, first-out basis. The stack keeps track of the index of the top item, so that the item may be taken out with pop. push will add an item to the top of the list, and increment the top variable.
*/
/*
Stack constants:
*/
// Size allocated to the stack on creation.
#ifndef STACK_SIZE_DEFAULT
#define STACK_SIZE_DEFAULT 8
#endif
#define STACK_CONTAINER TEMPLATE(STACK_TYPE, stack)
/*
Stack Structure:
Stores the top index of the stack, and a list of items in the stack.
top: index of the top of the stack (one above top item).
size: allocated size of the stack's list.
items: list of items in the stack.
*/
typedef struct {
int top;
int size;
STACK_TYPE* items;
} STACK_CONTAINER;
/*
Stack Creation:
Creates a new stack with a default size defined above.
*/
STACK_CONTAINER TEMPLATE(new,STACK_CONTAINER)(){
STACK_CONTAINER s;
s.top = 0;
s.size = STACK_SIZE_DEFAULT;
s.items = malloc(s.size*sizeof(STACK_TYPE));
assert(s.items != NULL);
return s;
}
/*
Stack Deallocation:
Safely deallocates memory assigned to the stack's list.
*/
void TEMPLATE(free,STACK_CONTAINER)(STACK_CONTAINER *s){
free(s->items);
}
/*
Stack Push:
Pushes an item onto a stack. If there is no more room, more memory will be allocated to accommodate the extra item.
*/
void TEMPLATE(push,STACK_CONTAINER)(STACK_TYPE item, STACK_CONTAINER *s){
//It is safe to push to the stack.
if (s->top < s->size){
s->items[s->top] = item;
} else {
//There was not enough space, so we must double size.
s->size *= 2;
s->items = realloc(s->items, s->size*sizeof(STACK_TYPE));
assert(s->items != NULL);
s->items[s->top] = item;
}
s->top++;
}
/*
Stack Pop:
Pops an item from the top of the stack, if it exists. Size will be checked to see if it is needed to reduce the size of the array.
*/
STACK_TYPE TEMPLATE(pop,STACK_CONTAINER)(STACK_CONTAINER *s){
STACK_TYPE result;
//Check if at least one item exists in the list.
if (s->top > 0){
s->top--;
result = s->items[s->top];
} else {
//The stack is empty.
fprintf(stderr, "Stack empty, exiting.");
exit(-1);
}
//Check if the items array size can be reduced.
if (s->top < (s->size / 2) && (s->size > STACK_SIZE_DEFAULT)){
s->size /= 2;
s->items = realloc(s->items, s->size*sizeof(STACK_TYPE));
assert(s->items != NULL);
}
return result;
}
/*
Stack Print:
Utility function to print a stack.
*/
void TEMPLATE(print,STACK_CONTAINER)(STACK_CONTAINER s){
printf("\tStack: \tItems: %d, Allocated Size: %d, Bytes used: %lu\n", s.top, s.size, s.size*sizeof(STACK_TYPE));
}
#endif

7
source/templates.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef TEMPLATES_H
#define TEMPLATES_H
#define CONCAT(X,Y) X##_##Y
#define TEMPLATE(X,Y) CONCAT(X,Y)
#endif

26
test.c
View File

@ -1,22 +1,12 @@
typedef struct{
char c;
int v;
} TEST;
#include "source/handyalgs.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(int argc, char* argv[]){ int main(int argc, char* argv[]){
Stack s = newStack();
for (int i = 2; i < 500; i+=3){ #define STACK_TYPE double
TEST t; #include "source/handystack.h"
t.v = i; double_stack s = new_double_stack();
pushToStack(t, &s); print_double_stack(s);
}
printStack(s);
for (int i = 0; i < 100; i++){
popFromStack(&s);
}
printStack(s);
} }