July 11, 2020

Stack Implementation using C language

You can watch me live code the Stack Datastructure on youtube.

You can get the entire source code from github

I have put all the function declaration in a header file.

struct Stack{
    int top;
    int 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);

The function names are self explanatory. They do not need explanations.

Then I created a c file to implement all the function declared in the Stack Header file.

In the isStackFull function I check if the current top-index variable is equal to capacity minus 1. If it is true then the stack is full, we have already used all the allocated memory.

In the isStackEmpty function I check if the current top-index is less than 0 or not. If it is truely less than 0, then that is not a valid index inside an array, because of that all allocated memory is unused.

In the pop  function, I read a value from the stack, after reading is complete, I decrement the top-index by 1, because we do not need that data any more.

In the push function, I increment the top-index to go to a memory block where I can put the current data, If I do not increment the top-index I will overwrite the value present at the current index.

The push and pop operations does validation check before adding values or removing values. We can not add any data if the stack is full, the same way we can not remove anything from a data if stack is empty.

#include "Stack.h" //My Stack Declarations
#include<stdio.h>  //Input Output Operations
#include<stdlib.h> //malloc and free functions

//Creation
extern 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 == NULL) return EOF;

    if(stack->data != NULL) free(stack->data);

    free(stack);
}

//Status Check
int isStackFull(struct Stack *stack){
    if(stack == NULL) return EOF;
    return stack->top + 1 == stack->capacity ? 1 : 0;
}

int isStackEmpty(struct Stack *stack){
    if(stack == NULL) return EOF;
    return stack->top < 0 ? 1 : 0;
}

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

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

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

int pop(struct Stack *stack){
    return stack == NULL || isStackEmpty(stack) == 1 ? 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);
}

 

 

Tagged on: