Stack Using Array

Complete implementation of a stack using array and its functions.

Stack Structure :

struct Stack {
    int size;
    int top;
    int *data;
};

Creating a Stack Dynamically :

struct Stack *createStack(int size) {

    // Allocating memory for stack
    struct Stack *stack;
    stack = (struct Stack *)malloc(sizeof(struct Stack));

    // Setting size and top
    stack -> size = size;
    stack -> top = -1;

    // Allocating memory for data array
    stack -> data = (int *)malloc(size * sizeof(int));

    return stack;
    
}

Function to Check if Stack is Empty or Full :

Stack Push Function :

Stack Pop Function :

Stack Peek Function :

Stack Display Function :

Contributed by Nitin Ranganath

Last updated

Was this helpful?