Stack in C (original) (raw)

Last Updated : 31 Jan, 2026

A stack is a linear data structure that follows the **Last In, First Out (LIFO) principle, meaning the last element added is the first one to be removed.

stack-in-c

Basic Operations in C

Following are some basic operations in the stack that make it easy to manipulate the stack data structure:

Operation Description Time Complexity Space Complexity
Push Inserts an element at the top of the stack. O(1) O(1)
Pop Removes the top most element of the stack. O(1) O(1)
Peek Returns the topmost element of the stack. O(1) O(1)
IsFull Returns true is the stack is full otherwise returns false. O(1) O(1)
IsEmpty Returns true is the stack is empty otherwise returns false. O(1) O(1)

As we can see, the stack offers O(1) time complexity for all the operation but with a catch that we can only perform these operation to the topmost element. So, we need to consider our requirements to take advantage of stack data structure.

Let's see how to implement these basic operations for our stack in C.

C `

// C Program to demonstrate how to Implement a Stack #include <stdio.h> #include <stdbool.h>

// Define the maximim capacity of the stack #define MAX_SIZE 100

// Define a structure for the stack typedef struct { int arr[MAX_SIZE];
int top;
} Stack;

// Function to initialize the stack void initialize(Stack *stack) { stack->top = -1;
}

// Function to check if the stack is empty bool isEmpty(Stack *stack) { return stack->top == -1;
}

// Function to check if the stack is full bool isFull(Stack *stack) { return stack->top >= MAX_SIZE - 1;
}

// Function to push an element onto the stack void push(Stack *stack, int value) { if (isFull(stack)) { printf("Stack Overflow\n"); return; } stack->arr[++stack->top] = value; printf("Pushed %d onto the stack\n", value); }

// Function to pop an element from the stack int pop(Stack *stack) { if (isEmpty(stack)) { printf("Stack Underflow\n"); return -1; }

int popped = stack->arr[stack->top];
stack->top--;
printf("Popped %d from the stack\n", popped);
return popped;

}

// Function to peek the top element of the stack int peek(Stack *stack) { if (isEmpty(stack)) { printf("Stack is empty\n"); return -1; } return stack->arr[stack->top]; }

int main() { Stack stack; initialize(&stack);

push(&stack, 3);
printf("Top element: %d\n", peek(&stack));

push(&stack, 5);
printf("Top element: %d\n", peek(&stack));

push(&stack, 2);
printf("Top element: %d\n", peek(&stack));

push(&stack, 8);
printf("Top element: %d\n", peek(&stack));

while (!isEmpty(&stack)) {
    printf("Top element: %d\n", peek(&stack));
    printf("Popped element: %d\n", pop(&stack));
}

return 0;

}

`

Output

Pushed 3 onto the stack Top element: 3 Pushed 5 onto the stack Top element: 5 Pushed 2 onto the stack Top element: 2 Pushed 8 onto the stack Top element: 8 Top element: 8 Popped 8 from the stack Poppe...

1. isFull Function

The isFull() function provides the information about whether the stack have some space left or it is completely full. We know that the max capacity of the stack is MAX_SIZE elements. So, the max value of top can be MAX_SIZE - 1.

**Algorithm of Stack isFull:

2. isEmpty Function

The isEmpty function will check whether the stack is empty or not. We know that when the stack is empty, the top is equal to -1.

**Algorithm of Stack isEmpty:

3. Push Function

The push function will add (or push) an element to the stack. The edge case here will be when we try to add a new element when the stack is already full. It is called stack overflow and we have to check for it before inserted new element.

**Algorithm of Stack Push:

Push-Operation-in-Stack-in-c

4. Pop Function

The pop function will remove an element from the stack. One case that can occur here is when we try to remove the top using pop() function when the stack is already empty. Such condition is called stack underflow and can be easily checked.

**Algorithm of Stack Pop:

Pop-Operation-on-stack-in-c

5. top Function

The peek function will return the topmost element of the stack in constant time. If the stack is empty it returns -1.

**Algorithm for Stack Top Function