-
Notifications
You must be signed in to change notification settings - Fork 1
Home
This is a C Static library project with included test code.
The Stack_t data structure has the following attributes:-
- DataType ( The type of data which the stack will be handling)
- Ptr (Pointer to the stack memory)
- MaxNumberOfElements (The maximum number of elements the stack can accommodate)
- Current (The current number of elements in the stack, also used as stack index)
/*! \struct Stack_t
\brief Stack Data Structure Definition.
Stack Data Structure Definition
used for all stack operations.
*/
struct Stack_t
{
//! Datatype.
/*!
The Data Type used in the stack.
*/
DataType_t Datatype;
//! Ptr.
/*!
Pointer to the Stack Data.
*/
void *Ptr;
//! MaxNumberOfElements.
/*!
total number of elements in the stack.
*/
size_t MaxNumberOfElements;
//! Current.
/*!
stack element count and current position.
*/
size_t Current;
}__ATTRIBUTE__(packed);
typedef struct Stack_t Stack_t;
typedef struct Stack_t * PStack_t;
The operations allowed on the Stack DS are:-
- Initialize Stack
- Push Element
- Pop Element
- Stack Empty Check
- Stack Full Check
- De-initialize Stack
This function takes a pointer to the Stack DS as input parameter.
- Performs all necessary NULL checks.
- Determines size of memory required to be initialized based on the data type used.
- In case of Dynamic memory Version, uses
malloc()to allocate memory. In case of static, it simply assumes that the memory will be preallocated to the Stack Pointer input Parameter. - Sets the Stack to 0s.
This function takes a pointer to the Stack DS as input parameter.
- Performs all necessary NULL checks.
- Frees Stack Memory.
This function takes a pointer to the Stack DS and the element to be pushed as input parameters.
- Performs all necessary NULL checks.
- Checks if the Stack is Full.
- Depending upon the type of data used in the Stack init, the new element is inserted.
This function takes a pointer to the Stack DS and the container for the element to be popped as input parameters.
- Performs all necessary NULL checks.
- Checks if the Stack is Empty.
- Depending upon the type of data used in the Stack init, the new element is popped and copied into the container.
This function takes a pointer to the Stack DS as input parameter.
- Performs all necessary NULL checks.
- Checks if the Stack is Empty by comparing the Current Index with 0.
This function takes a pointer to the Stack DS as input parameter.
- Performs all necessary NULL checks.
- Checks if the Stack is Full by comparing the Current Index with the Number of Elements.
To use this library in your project:-
-
First build the project using
makecommand. -
Copy the
libStack.aand header files (stack.h&cmn_header.h) from thelibandincdirectory respectively to your project directory. -
#includethestack.hfile in your source file. -
Modify the
stack.hfile contents if you would like to use Dynamic memory allocations by uncommenting the#define __USE_DYNAMIC_MEMORYline -
Create a
Stack_tinstance. For example, look into the test.c file.Stack_t myStack; -
Initialize the Stack. For example, look into the test.c file.
myStack.Datatype = DT_INT32; myStack.MaxNumberOfElements = STACK_SIZE;#ifndef __USE_DYNAMIC_MEMORYmyStack.Ptr = myStackData;#endif`Initialize(&myStack);`Compare the return value with enum value
Stk_OKto confirm if Initialization was successful. -
Check for Stack Empty by calling
Empty(&myStack)and comparing its return value with enum valueStk_EMPTY. -
Check for Stack Full by calling
Full(&myStack)and comparing its return value with enum valueStk_FULL. -
Call
DeInitialize(&myStack)at the end of your program. -
Compile & link your program with -lStack flag.
I hope you will find it useful.
Any comments, suggestions are welcome.
Thank you.