A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. It can be implemented using an array by treating the end of the array as the top of the stack.
A stack can be implemented using an array where we maintain:
- An integer array to store elements.
- A variable capacity to represent the maximum size of the stack.
- A variable top to track the index of the top element. Initially, top = -1 to indicate an empty stack.
class myStack {
// array to store elements
int *arr;
// maximum size of stack
int capacity;
// index of top element
int top;
public:
// constructor
myStack(int cap) {
capacity = cap;
arr = new int[capacity];
top = -1;
}
};
typedef struct
{
int *arr; // array to store elements
int capacity; // maximum size of stack
int top; // index of top element
} Stack;
// Function to create a stack (constructor equivalent)
Stack *createStack(int capacity)
{
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->capacity = capacity;
stack->arr = (int *)malloc(capacity * sizeof(int));
stack->top = -1;
return stack;
}
// Function to free stack memory
void freeStack(Stack *stack)
{
free(stack->arr);
free(stack);
}
class myStack {
// array to store elements
private int[] arr;
// maximum size of stack
private int capacity;
// index of top element
private int top;
// constructor
public myStack(int cap) {
capacity = cap;
arr = new int[capacity];
top = -1;
}
}
class myStack:
def __init__(self, cap):
# array to store elements
self.arr = [0] * cap
# maximum size of stack
self.capacity = cap
# index of top element
self.top = -1
class myStack {
// array to store elements
private int[] arr;
// maximum size of stack
private int capacity;
// index of top element
private int top;
// constructor
public myStack(int cap) {
capacity = cap;
arr = new int[capacity];
top = -1;
}
}
class myStack {
// constructor
constructor(cap) {
// array to store elements
this.arr = new Array(cap);
// maximum size of stack
this.capacity = cap;
// index of top element
this.top = -1;
}
}
Operations On Stack
Push Operation:
Adds an item to the stack. If the stack is full, then it is said to be an Overflow condition.
- Before pushing the element to the stack, we check if the stack is full.
- If the stack is full (top == capacity-1) , then Stack Overflows and we cannot insert the element to the stack.
- Otherwise, we increment the value of top by 1 (top = top + 1) and the new value is inserted at top position .
- The elements can be pushed into the stack till we reach the capacity of the stack.
void push(int x) {
if (top == capacity - 1) {
cout << "Stack Overflow\n";
return;
}
arr[++top] = x;
}
typedef struct {
int* arr;
int capacity;
int top;
} Stack;
void push(Stack* stack, int x) {
if (stack->top == stack->capacity - 1) {
printf("Stack Overflow\n");
return;
}
stack->arr[++stack->top] = x;
}
void push(int x) {
if (top == capacity - 1) {
System.out.println("Stack Overflow");
return;
}
arr[++top] = x;
}
def push(self, x):
if self.top == self.capacity - 1:
print("Stack Overflow")
return
self.top += 1
self.arr[self.top] = x
void push(int x) {
if (top == capacity - 1) {
Console.WriteLine("Stack Overflow");
return;
}
arr[++top] = x;
}
function push(x) {
if (top === capacity - 1) {
console.log('Stack Overflow');
return;
}
arr[++top] = x;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Pop Operation:
Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
- Before popping the element from the stack, we check if the stack is empty .
- If the stack is empty (top == -1), then Stack Underflows and we cannot remove any element from the stack.
- Otherwise, we store the value at top, decrement the value of top by 1 (top = top – 1) and return the stored top value.
int pop() {
if (top == -1) {
cout << "Stack Underflow\n";
return -1;
}
return arr[top--];
}
int pop()
{
if (top == -1)
{
cout << "Stack Underflow\n";
return -1;
}
return arr[top--];
}
int pop() {
if (top == -1) {
System.out.println("Stack Underflow");
return -1;
}
return arr[top--];
}
def pop(self):
if self.top == -1:
print("Stack Underflow")
return -1
value = self.arr[self.top]
self.top -= 1
return value
int pop() {
if (top == -1) {
Console.WriteLine("Stack Underflow");
return -1;
}
return arr[top--];
}
function pop() {
if (top == -1) {
console.log('Stack Underflow');
return -1;
}
return arr[top--];
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Top or Peek Operation in Stack:
Returns the top element of the stack.
- Before returning the top element from the stack, we check if the stack is empty.
- If the stack is empty (top == -1), we simply print “Stack is empty”.
- Otherwise, we return the element stored at index = top.

int peek() {
if (top == -1) {
cout << "Stack is Empty\n";
return -1;
}
return arr[top];
}
int peek()
{
if (top == -1)
{
cout << "Stack is Empty\n";
return -1;
}
return arr[top];
}
int peek() {
if (top == -1) {
System.out.println("Stack is Empty");
return -1;
}
return arr[top];
}
def peek(self):
if self.top == -1:
print("Stack is Empty")
return -1
return self.arr[self.top]
int peek() {
if (top == -1) {
Console.WriteLine("Stack is Empty");
return -1;
}
return arr[top];
}
function peek() {
if (top == -1) {
console.log('Stack is Empty');
return -1;
}
return arr[top];
}
Time Complexity: O(1)
Auxiliary Space: O(1)
isEmpty Operation in Stack:
Returns true if the stack is empty, else false.
- Check for the value of top in stack.
- If (top == -1) , then the stack is empty so return true.
- Otherwise, the stack is not empty so return false.

bool isEmpty() {
return top == -1;
}
bool isEmpty() {
return top == -1;
}
boolean isEmpty() {
return top == -1;
}
def isEmpty(self):
return self.top == -1
bool isEmpty() {
return top == -1;
}
isEmpty() {
return this.top === -1;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
isFull Operation in Stack :
Returns true if the stack is full, else false.
- Check for the value of top in stack.
- If (top == capacity-1), then the stack is full so return true.
- Otherwise, the stack is not full so return false.

Time Complexity: O(1)
Auxiliary Space: O(1)
Full Implementation of Stack using Array
#include <iostream>
using namespace std;
class myStack {
// array to store elements
int *arr;
// maximum size of stack
int capacity;
// index of top element
int top;
public:
// constructor
myStack(int cap) {
capacity = cap;
arr = new int[capacity];
top = -1;
}
// push operation
void push(int x) {
if (top == capacity - 1) {
cout << "Stack Overflow\n";
return;
}
arr[++top] = x;
}
// pop operation
int pop() {
if (top == -1) {
cout << "Stack Underflow\n";
return -1;
}
return arr[top--];
}
// peek (or top) operation
int peek() {
if (top == -1) {
cout << "Stack is Empty\n";
return -1;
}
return arr[top];
}
// check if stack is empty
bool isEmpty() {
return top == -1;
}
// check if stack is full
bool isFull() {
return top == capacity - 1;
}
};
int main() {
myStack st(4);
// pushing elements
st.push(1);
st.push(2);
st.push(3);
st.push(4);
// popping one element
cout << "Popped: " << st.pop() << "\n";
// checking top element
cout << "Top element: " << st.peek() << "\n";
// checking if stack is empty
cout << "Is stack empty: " << (st.isEmpty() ? "Yes" : "No") << "\n";
// checking if stack is full
cout << "Is stack full: " << (st.isFull() ? "Yes" : "No") << "\n";
return 0;
}
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// Define stack structure
typedef struct
{
int *arr; // array to store elements
int capacity; // maximum size of stack
int top; // index of top element
} Stack;
// Function to create a stack
Stack *createStack(int capacity)
{
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->capacity = capacity;
stack->arr = (int *)malloc(capacity * sizeof(int));
stack->top = -1;
return stack;
}
// Push operation
void push(Stack *stack, int x)
{
if (stack->top == stack->capacity - 1)
{
printf("Stack Overflow\n");
return;
}
stack->arr[++stack->top] = x;
}
// Pop operation
int pop(Stack *stack)
{
if (stack->top == -1)
{
printf("Stack Underflow\n");
return -1;
}
return stack->arr[stack->top--];
}
// Peek operation
int peek(Stack *stack)
{
if (stack->top == -1)
{
printf("Stack is Empty\n");
return -1;
}
return stack->arr[stack->top];
}
// Check if stack is empty
bool isEmpty(Stack *stack)
{
return stack->top == -1;
}
// Check if stack is full
bool isFull(Stack *stack)
{
return stack->top == stack->capacity - 1;
}
// Free stack memory
void freeStack(Stack *stack)
{
free(stack->arr);
free(stack);
}
int main()
{
Stack *st = createStack(4);
// Push elements
push(st, 1);
push(st, 2);
push(st, 3);
push(st, 4);
// Pop one element
printf("Popped: %d\n", pop(st));
// Peek top element
printf("Top element: %d\n", peek(st));
// Check if stack is empty
printf("Is stack empty: %s\n", isEmpty(st) ? "Yes" : "No");
// Check if stack is full
printf("Is stack full: %s\n", isFull(st) ? "Yes" : "No");
freeStack(st); // free memory
return 0;
}
import java.util.Arrays;
class myStack {
// array to store elements
private int[] arr;
// maximum size of stack
private int capacity;
// index of top element
private int top;
// constructor
public myStack(int cap) {
capacity = cap;
arr = new int[capacity];
top = -1;
}
// push operation
public void push(int x) {
if (top == capacity - 1) {
System.out.println("Stack Overflow");
return;
}
arr[++top] = x;
}
// pop operation
public int pop() {
if (top == -1) {
System.out.println("Stack Underflow");
return -1