From 5ebfcf4148f6bf73e5e60f0b16cbd758482b9c7e Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Fri, 8 Dec 2017 18:00:49 +0100 Subject: [PATCH] simple modular and generic stack See the README.md --- Data Structures/stack/README.md | 39 +++++++++ Data Structures/stack/main.c | 40 ++++++++++ Data Structures/stack/stack.c | 136 ++++++++++++++++++++++++++++++++ Data Structures/stack/stack.h | 43 ++++++++++ 4 files changed, 258 insertions(+) create mode 100644 Data Structures/stack/README.md create mode 100644 Data Structures/stack/main.c create mode 100644 Data Structures/stack/stack.c create mode 100644 Data Structures/stack/stack.h diff --git a/Data Structures/stack/README.md b/Data Structures/stack/README.md new file mode 100644 index 00000000..174e9454 --- /dev/null +++ b/Data Structures/stack/README.md @@ -0,0 +1,39 @@ +# Simple generic Stack + +This is a modular generic stack data-structure. The stack is self growing. + +### Content + +* stack-Header file for import. +* stack.c implementation of the stack +* main.c framework program for testing. + +You need to only import the **stack.h** + +### Public interface + +``` void initStack(); ``` + +Initializes the stack with a capacity of 10 elements. + +``` void push(void * object); ``` + +pushs the argument onto the stack + +``` void * pop(); ``` + +pop: pops the top element of the stack from the stack. + + assumes: stack not empty. + +``` int size(); ``` + +gets the number of elements of the stack. + +``` int isEmpty(); ``` + +returns 1 if stack is empty otherwise 0. + + + + diff --git a/Data Structures/stack/main.c b/Data Structures/stack/main.c new file mode 100644 index 00000000..4d0ef5e1 --- /dev/null +++ b/Data Structures/stack/main.c @@ -0,0 +1,40 @@ +/* + author: Christian Bender + + This is the main-program for testing the stack. +*/ + +#include +#include + +#include "stack.h" + +int main() +{ + + int MAX = 55; + int field[MAX]; + int i; /* for the loop */ + int *pInt = NULL; + + initStack(); + + /* pushs some elements onto stack */ + for (i = 0; i < MAX; i++) + { + field[i] = i; + push((field + i)); // HERE + } + + /* pops the elements from the stack and print that out.*/ + for (i = 0; i < MAX; i++) + { + pInt = pop(); /* fetch next integer */ + + printf("%d\n", *pInt); /* print value */ + } + + + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/Data Structures/stack/stack.c b/Data Structures/stack/stack.c new file mode 100644 index 00000000..29c6a1de --- /dev/null +++ b/Data Structures/stack/stack.c @@ -0,0 +1,136 @@ +/* + author: Christian Bender + + This is the implementation of the (generic) stack. + The implementation uses the dynamic memory management and the principle + of data hiding. +*/ + +#include +#include +#include + +#include "stack.h" + +/* + actual stack data structure + This pointer will pointing at the actual field (of void * pointers) + that represents the stack. + */ +void **array; + +/* the current capacity of the stack */ +int max = 10; + +/* counter variable for counting the elements of the stack. */ +int counter = 0; + +/* + offset address + points at the top element of the stack. +*/ +int offset = -1; + +void initStack() +{ + + array = malloc(sizeof(void *) * max); + assert(array); /* tests whether pointer is assigned to memory. */ +} + +/* + grow: increases the stack by 10 elements. + This utility function isn't part of the public interface +*/ +void grow() +{ + max += 10; /* increases the capacity */ + + int i; // for the loop + void **tmp = malloc(sizeof(void *) * max); + + /* copies the elements from the origin array in the new one. */ + for (i = 0; i < max - 10; i++) + { + *(tmp + i) = *(array + i); + } + + array = tmp; /* setups the new one as basis */ +} + +/* push: pushs the argument onto the stack */ +void push(void *object) +{ + + assert(object); /* tests whether pointer isn't null */ + + if (counter < max) + { + + offset++; /* increases the element-pointer */ + + /* + moves pointer by the offset address + pushs the object onto stack + */ + *(array + offset) = object; + + /* increases the inner counter */ + counter++; + } + else /* stack is full */ + { + + grow(); /* lets grow stack */ + push(object); /* recursive call */ + } +} + +/* + pop: pops the top element of the stack from the stack. +*/ +void *pop() +{ + + void *top = *(array + offset); + + /* check pointers */ + assert(top); + + /* if use the pop-function, stack must not empty. */ + assert(!isEmpty()); + + /* decreases the offset address for pointing of + the new top element */ + offset--; + + /* decreases the inner counter */ + counter--; + + return top; +} + +/* + size: gets the number of elements of the stack. +*/ +int size() +{ + return counter; +} + +/* + isEmpty(): returns 1 if stack is empty otherwise 0. +*/ +int isEmpty() +{ + return counter == 0; +} + +/* + top: returns the top element from the stack without removing it. +*/ +void *top() +{ + /* offset address points to the top element */ + return array[offset]; +} \ No newline at end of file diff --git a/Data Structures/stack/stack.h b/Data Structures/stack/stack.h new file mode 100644 index 00000000..5af106a9 --- /dev/null +++ b/Data Structures/stack/stack.h @@ -0,0 +1,43 @@ +/* + author: Christian Bender + + This header represents the public stack-interface. + The stack is generic and self growing. +*/ + + +#ifndef __STACK__ +#define __STACK__ + +/* + initStack: initializes the stack with a capacity of 10 elements. +*/ +void initStack(); + +/* + push: pushs the argument onto the stack +*/ +void push(void * object); + +/* + pop: pops the top element of the stack from the stack. + assumes: stack not empty. +*/ +void * pop(); + +/* + size: gets the number of elements of the stack. +*/ +int size(); + +/* + isEmpty(): returns 1 if stack is empty otherwise 0. +*/ +int isEmpty(); + +/* + top: returns the top element from the stack without removing it. +*/ +void * top(); + +#endif \ No newline at end of file