Added custom type to the stack

User can define a different type if they wish, and the stack will store those items.
This commit is contained in:
Andrew Lalis 2017-05-22 15:52:30 +02:00
parent 76e80dbd9a
commit d034657bf6
3 changed files with 29 additions and 21 deletions

View File

@ -4,11 +4,11 @@ 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 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.
* ``` Stack createStack() ``` * ``` Stack createStack() ```
* ``` void freeStack(Stack *s) ``` * ``` void freeStack(Stack *s) ```
* ``` void pushToStack(int item, Stack *s) ``` * ``` void pushToStack(STACK_TYPE item, Stack *s) ```
* ``` int popFromStack(Stack *s) ``` * ``` STACK_TYPE popFromStack(Stack *s) ```
* ``` void printStack(Stack s) ``` * ``` void printStack(Stack s) ```
### Queue ### Queue

View File

@ -24,6 +24,8 @@ Stack constants:
*/ */
// Size allocated to the stack on creation. // Size allocated to the stack on creation.
#define STACK_SIZE_DEFAULT 8 #define STACK_SIZE_DEFAULT 8
// Type to be used for the stack.
#define STACK_TYPE TEST
/* /*
Stack Structure: Stack Structure:
@ -35,7 +37,7 @@ Stack Structure:
typedef struct { typedef struct {
int top; int top;
int size; int size;
int* items; STACK_TYPE* items;
} Stack; } Stack;
/* /*
@ -43,8 +45,8 @@ Stack Functions:
*/ */
Stack newStack(); Stack newStack();
void freeStack(Stack *s); void freeStack(Stack *s);
void pushToStack(int item, Stack *s); void pushToStack(STACK_TYPE item, Stack *s);
int popFromStack(Stack *s); STACK_TYPE popFromStack(Stack *s);
void printStack(Stack s); void printStack(Stack s);
/* /*
@ -55,7 +57,7 @@ Stack newStack(){
Stack s; Stack s;
s.top = 0; s.top = 0;
s.size = STACK_SIZE_DEFAULT; s.size = STACK_SIZE_DEFAULT;
s.items = malloc(s.size*sizeof(int)); s.items = malloc(s.size*sizeof(STACK_TYPE));
assert(s.items != NULL); assert(s.items != NULL);
return s; return s;
} }
@ -72,14 +74,14 @@ void freeStack(Stack *s){
Stack Push: Stack Push:
Pushes an item onto a stack. If there is no more room, more memory will be allocated to accommodate the extra item. Pushes an item onto a stack. If there is no more room, more memory will be allocated to accommodate the extra item.
*/ */
void pushToStack(int item, Stack *s){ void pushToStack(STACK_TYPE item, Stack *s){
//It is safe to push to the stack. //It is safe to push to the stack.
if (s->top < s->size){ if (s->top < s->size){
s->items[s->top] = item; s->items[s->top] = item;
} else { } else {
//There was not enough space, so we must double size. //There was not enough space, so we must double size.
s->size *= 2; s->size *= 2;
s->items = realloc(s->items, s->size*sizeof(int)); s->items = realloc(s->items, s->size*sizeof(STACK_TYPE));
assert(s->items != NULL); assert(s->items != NULL);
s->items[s->top] = item; s->items[s->top] = item;
} }
@ -88,23 +90,23 @@ void pushToStack(int item, Stack *s){
/* /*
Stack Pop: Stack Pop:
Pops an item from the top of the stack, if it exists, or zero otherwise. Size will be checked to see if it is needed to reduce the size of the array. 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.
*/ */
int popFromStack(Stack *s){ STACK_TYPE popFromStack(Stack *s){
int result; STACK_TYPE result;
//Check if at least one item exists in the list. //Check if at least one item exists in the list.
if (s->top > 0){ if (s->top > 0){
s->top--; s->top--;
result = s->items[s->top]; result = s->items[s->top];
} else { } else {
//The stack is empty. //The stack is empty.
result = 0; fprintf(stderr, "Stack empty, exiting.");
fprintf(stderr, "STACK EMPTY"); exit(-1);
} }
//Check if the items array size can be reduced. //Check if the items array size can be reduced.
if (s->top < (s->size / 2) && (s->size > STACK_SIZE_DEFAULT)){ if (s->top < (s->size / 2) && (s->size > STACK_SIZE_DEFAULT)){
s->size /= 2; s->size /= 2;
s->items = realloc(s->items, s->size*sizeof(int)); s->items = realloc(s->items, s->size*sizeof(STACK_TYPE));
assert(s->items != NULL); assert(s->items != NULL);
} }
return result; return result;
@ -112,13 +114,12 @@ int popFromStack(Stack *s){
/* /*
Stack Print: Stack Print:
Utility function to print the formatted contents of a stack. Utility function to print a stack.
*/ */
void printStack(Stack s){ void printStack(Stack s){
printf("\tStack: \tItems: %d, Allocated Size: %d\n", s.top, s.size); printf("\tStack: \tItems: %d, Allocated Size: %d, Bytes used: %lu\n", s.top, s.size, s.size*sizeof(STACK_TYPE));
for (int i = s.top-1; i >= 0; i--){
printf("\t\tItem %d: %d\n", i+1, s.items[i]);
}
} }
#endif #endif

9
test.c
View File

@ -1,3 +1,8 @@
typedef struct{
char c;
int v;
} TEST;
#include "source/handyalgs.h" #include "source/handyalgs.h"
#include <stdio.h> #include <stdio.h>
@ -5,7 +10,9 @@
int main(int argc, char* argv[]){ int main(int argc, char* argv[]){
Stack s = newStack(); Stack s = newStack();
for (int i = 2; i < 500; i+=3){ for (int i = 2; i < 500; i+=3){
pushToStack(i, &s); TEST t;
t.v = i;
pushToStack(t, &s);
} }
printStack(s); printStack(s);
for (int i = 0; i < 100; i++){ for (int i = 0; i < 100; i++){