Algorithm in C – Stack Data Structure

Stack Data Structure


In this tutorial, you will learn about the stack data structure and it’s implementation in  C.

A stack is a useful data structure in programming. It is just like a pile of plates kept on top of each other.

elements on stack are added on top and removed from top just like a pile of plate
Stack representation similar to a pile of plate

Think about the things you can do with such a pile of plates

  • Put a new plate on top
  • Remove the top plate


If you want the plate at the bottom, you have to first remove all the plates on top. Such an arrangement is called Last In First Out – the last item that was placed is the first item to go out.

LIFO Principle of Stack

In programming terms, putting an item on top of the stack is called “push” and removing an item is called “pop”.

represent the lifo principle by using push and pop operation
Stack Push and Pop Operations

In the above image, although item 2 was kept last, it was removed first – so it follows the Last In First Out(LIFO) principle.

We can implement a stack in any programming language like C, C++, Java, Python or C#, but the specification is pretty much the same.

Basic Operations of Stack

A stack is an object or more specifically an abstract data structure(ADT) that allows the following operations:

  • Push: Add an element to the top of a stack
  • Pop: Remove an element from the top of a stack
  • IsEmpty: Check if the stack is empty
  • IsFull: Check if the stack is full
  • Peek: Get the value of the top element without removing it


Working of Stack Data Structure

The operations work as follows:

  1. A pointer called TOP is used to keep track of the top element in the stack.
  2. When initializing the stack, we set its value to -1 so that we can check if the stack is empty by comparing TOP == -1.
  3. On pushing an element, we increase the value of TOP and place the new element in the position pointed to by TOP.
  4. On popping an element, we return the element pointed to by TOP and reduce its value.
  5. Before pushing, we check if the stack is already full
  6. Before popping, we check if the stack is already empty


adding elements to the top of stack and removing elements from the top of stack
Working of Stack Data Structure

Stack Implementations in C

The most common stack implementation is using arrays, but it can also be implemented using lists.

// Stack implementation in C

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

#define MAX 10

int count = 0;

// Creating a stack
struct stack {
  int items[MAX];
  int top;
typedef struct stack st;

void createEmptyStack(st *s){
  s->top = -1;

// Check if the stack is full
int isfull(st *s){
  if (s->top == MAX - 1)
    return 1;
    return 0;

// Check if the stack is empty
int isempty(st *s){
  if (s->top == -1)
    return 1;
    return 0;

// Add elements into stack
void push(st *s, int newitem){
  if (isfull(s)) {
    printf("STACK FULL");
  } else {
    s->items[s->top] = newitem;

// Remove element from stack
void pop(st *s){
  if (isempty(s)) {
    printf("n STACK EMPTY n");
  } else {
    printf("Item popped= %d", s->items[s->top]);

// Print elements of stack
void printStack(st *s){
  printf("Stack: ");
  for (int i = 0; i < count; i++) {
    printf("%d ", s->items[i]);

// Driver code
int main(){
  int ch;
  st *s = (st *)malloc(sizeof(st));


  push(s, 1);
  push(s, 2);
  push(s, 3);
  push(s, 4);



  printf("nAfter popping outn");

Stack Time Complexity

For the array-based implementation of a stack, the push and pop operations take constant time i.e. O(1) because there is only a movement of the pointer in both the cases.

Applications of Stack Data Structure

Although stack is a simple data structure to implement, it is very powerful. The most common uses of a stack are:

  • To reverse a word – Put all the letters in a stack and pop them out. Because of the LIFO order of stack, you will get the letters in reverse order.
  • In compilers – Compilers use the stack to calculate the value of expressions like 2 + 4 / 5 * (7 - 9) by converting the expression to prefix or postfix form.
  • In browsers – The back button in a browser saves all the URLs you have visited previously in a stack. Each time you visit a new page, it is added on top of the stack. When you press the back button, the current URL is removed from the stack and the previous URL is accessed.


Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars