Skip to content
Mrunmoy Samal edited this page May 3, 2016 · 7 revisions

Welcome to the Stack wiki!

This is an implementation of a Stack data structure in C.

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)

Stack data structure


/*! \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;

Stack Operations

The operations allowed on the Stack DS are:-

  • Initialize Stack
  • Push Element
  • Pop Element
  • Stack Empty Check
  • Stack Full Check
  • De-initialize Stack

Initialize() Function

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.

DeInitialize() Function

This function takes a pointer to the Stack DS as input parameter.

  • Performs all necessary NULL checks.
  • Frees Stack Memory.

Push() Function

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.

Pop() Function

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.

Empty() Function

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.

Full() Function

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.

Stack Library Usage

To use this library in your project:-

  • First build the project using make command.

  • Copy the libStack.a and header files (stack.h & cmn_header.h) from the lib and inc directory respectively to your project directory.

  • #include the stack.h file in your source file.

  • Modify the stack.h file contents if you would like to use Dynamic memory allocations by uncommenting the #define __USE_DYNAMIC_MEMORY line

  • Create a Stack_t instance. 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_MEMORY

      myStack.Ptr 		      = myStackData;
    

    #endif

     `Initialize(&myStack);`
    

    Compare the return value with enum value Stk_OK to confirm if Initialization was successful.

  • Check for Stack Empty by calling Empty(&myStack) and comparing its return value with enum value Stk_EMPTY.

  • Check for Stack Full by calling Full(&myStack) and comparing its return value with enum value Stk_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.