March 6, 2021

Stack datastructure implementation in C programming Language.

Stack is an abstract data-structure. This data-structure allows user to access the contents in Last In First Out  order.
This means, the latest element which was put in the data-structure, will be the first one to be removed on remove operation.

You can watch my video, where I code this implementation.

The operation, where we put an element in the stack is called push.
The operation, where we remove an element from stack is called pop.

We can have other operations.
The operation, where we just take a look at the element present at the top of stack is called peek.
We can have other utility operations, for example, to find number of elements present in the stack and delete stack.

Here is the implementation of Stack in C programming language.

Interfaces for Stack implementation.

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>

struct Stack{
    uint_least8_t top;
    uint_least8_t capacity;
    int *data;
};

//Creation
struct Stack *createStack(int *capacity);

//Deletion
int deleteStack(struct Stack *stack);

//Status Checks
int isStackFull(struct Stack *stack);
int isStackEmpty(struct Stack *stack);

//Modification Functions
int push(struct Stack *stack, int *data);
int pop(struct Stack *stack);

In below code, I have implemented above mentioned interfaces, which operates on contents of stack.
You can browse my code at github, for better user experience.

#include "Stack.h" //My Stack Declarations

//Creation
struct Stack *createStack(int *capacity){
    if(*capacity < 1) return NULL;
    
    struct Stack *stack = (struct Stack *) malloc(sizeof(struct Stack));
    stack->top      = -1;
    stack->capacity = *capacity;
    stack->data     = (int *) malloc(sizeof(int) * *capacity);

    return stack;
}

//Deletion
int deleteStack(struct Stack *stack){
    if(stack) {
        if(stack->data) free(stack->data);
        free(stack);
    }
}

#define isStackFull(stack)     ((stack != NULL && (stack->top + 1) == stack->capacity))
#define isStackEmpty(stack)    ((stack == NULL || stack->top < 0))

//Modifications Functions
int push(struct Stack *stack, int *data){
    if(stack == NULL || isStackFull(stack)) return EOF;

    *(stack->data + ++(stack->top)) = *data;

    return *(stack->data + stack->top);
}

int pop(struct Stack *stack){
    return isStackEmpty(stack) ? EOF : *(stack->data + stack->top--);
}

void main(){
    int capacity = 3;
    struct Stack *stack = createStack(&capacity);

    if(stack != NULL) printf("Stack memory successfully allocated.\n");
    int data = 100;
    int pushStatus = push(stack, &data);
    if(EOF == pushStatus) printf("Stack can not accept %d.\n", data);

    data = 101;
    pushStatus = push(stack, &data);
    if(EOF == pushStatus) printf("Stack can not accept %d.\n", data);

    data = 102;
    pushStatus = push(stack, &data);
    if(EOF == pushStatus) printf("Stack can not accept %d.\n", data);

    data = 103;
    pushStatus = push(stack, &data);
    if(EOF == pushStatus) printf("Stack can not accept %d.\n", data);

    int value = pop(stack);
    if(EOF == value) printf("Stack is EMPTY, because top is %d.\n", stack->top);

    value = pop(stack);
    if(EOF == value) printf("Stack is EMPTY, because top is %d.\n", stack->top);

    value = pop(stack);
    if(EOF == value) printf("Stack is EMPTY, because top is %d.\n", stack->top);

    value = pop(stack);
    if(EOF == value) printf("Stack is EMPTY, because top is %d.\n", stack->top);

    printf("Stack top %d. \n", stack->top);

    deleteStack(stack);
}