#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Stack Example: Implementation in C

A stack is a list where you add and remove items only from one end, the top. It works on LIFO — Last In, First Out. Implementing it in C is a classic lab program and a common interview warm-up, because it tests arrays, functions, pointers and edge cases.

Stack Example Implementation in C

Stack Implementation in C: Push, Pop and Peek (Array and Linked List)

This guide shows two implementations — array and linked list — each as a complete program you can compile and run.

Stack operations

Operation What it does
push(x) Add x on top
pop() Remove and return the top item
peek() Return the top item without removing it
isEmpty() Is the stack empty?
isFull() Is the stack full? (array version only)

Two error cases to handle:

  • Overflow: pushing when the stack is full.
  • Underflow: popping when the stack is empty.

1. Stack using an array

We keep an integer top that stores the index of the top element. top = -1 means the stack is empty.

#include <stdio.h>
#define MAX 5

int stack[MAX];
int top = -1;

int isEmpty(void) { return top == -1; }
int isFull(void)  { return top == MAX - 1; }

void push(int value) {
    if (isFull()) {
        printf("Stack Overflow! Cannot push %d\n", value);
        return;
    }
    stack[++top] = value;
    printf("Pushed %d\n", value);
}

int pop(void) {
    if (isEmpty()) {
        printf("Stack Underflow! Nothing to pop\n");
        return -1;
    }
    return stack[top--];
}

int peek(void) {
    if (isEmpty()) {
        printf("Stack is empty\n");
        return -1;
    }
    return stack[top];
}

void display(void) {
    if (isEmpty()) {
        printf("Stack is empty\n");
        return;
    }
    printf("Stack (top to bottom): ");
    for (int i = top; i >= 0; i--)
        printf("%d ", stack[i]);
    printf("\n");
}

int main(void) {
    push(10);
    push(20);
    push(30);
    display();
    printf("Top element: %d\n", peek());
    printf("Popped: %d\n", pop());
    display();
    push(40);
    push(50);
    push(60);
    push(70);
    display();
    while (!isEmpty())
        printf("Popped: %d\n", pop());
    pop();
    return 0;
}

Compile and run:

gcc -Wall -Wextra -o stack stack.c
./stack

Output

Pushed 10
Pushed 20
Pushed 30
Stack (top to bottom): 30 20 10
Top element: 30
Popped: 30
Stack (top to bottom): 20 10
Pushed 40
Pushed 50
Pushed 60
Stack Overflow! Cannot push 70
Stack (top to bottom): 60 50 40 20 10
Popped: 60
Popped: 50
Popped: 40
Popped: 20
Popped: 10
Stack Underflow! Nothing to pop

Understanding stack[++top] and stack[top--]

  • stack[++top] = value; — first increase top, then store. From -1 the first push stores at index 0.
  • return stack[top--]; — first read the value at top, then decrease top.

Getting these the wrong way round (top++ in push, --top in pop) is the most common bug in this program.

Note: returning -1 on underflow is a simple teaching choice. If -1 can be a real value in your stack, return a status code and pass the value through a pointer instead.

2. Stack using a linked list

The array version has a fixed size (MAX). A linked list version grows as needed — every push creates a new node at the head of the list, and the head is the top.

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

struct Node {
    int data;
    struct Node *next;
};

struct Node *top = NULL;

void push(int value) {
    struct Node *n = malloc(sizeof(struct Node));
    if (n == NULL) {
        printf("Heap full, cannot push\n");
        return;
    }
    n->data = value;
    n->next = top;
    top = n;
}

int pop(void) {
    if (top == NULL) {
        printf("Stack Underflow!\n");
        return -1;
    }
    struct Node *temp = top;
    int value = temp->data;
    top = top->next;
    free(temp);
    return value;
}

int peek(void) {
    return top ? top->data : -1;
}

int main(void) {
    push(5);
    push(15);
    push(25);
    printf("Peek: %d\n", peek());
    printf("Pop: %d\n", pop());
    printf("Pop: %d\n", pop());
    printf("Pop: %d\n", pop());
    pop();
    return 0;
}

Output

Peek: 25
Pop: 25
Pop: 15
Pop: 5
Stack Underflow!

Always free() the node you pop, otherwise memory leaks.

Array vs linked list stack

Point Array Linked list
Size Fixed (MAX) Grows until memory runs out
Overflow Yes, when full Only if malloc() fails
Memory per item Just the value Value + one pointer
Speed Very fast, cache-friendly Slightly slower (malloc/free)
Code complexity Simpler Needs pointers

All main operations — push, pop, peek, isEmpty — are O(1) in both versions.

A common beginner scenario

Imagine a student whose program prints “Popped: 0” and then strange large numbers. Push looks fine, so he keeps staring at pop(). The real bug is in push(): he wrote stack[top++] = value, which stores the first value at index -1 — outside the array. C doesn’t stop you from doing this; it just quietly writes into memory you don’t own. Changing it to stack[++top] fixes everything. Compiling with -Wall -Wextra and testing the empty and full cases early saves a lot of time.

Common mistakes

  • Wrong increment order (top++ vs ++top), as above.
  • Not checking isFull() before push or isEmpty() before pop.
  • Starting top at 0 instead of -1 and then mixing both conventions.
  • Forgetting free() in the linked-list pop.
  • Printing the stack from bottom to top and calling it “top to bottom”.

Practice questions

  1. Add a menu (1. Push 2. Pop 3. Peek 4. Display 5. Exit) using switch.
  2. Use your stack to reverse a string.
  3. Check balanced brackets in an expression using the stack.
  4. Implement two stacks in one array.
  5. Make a stack that also returns the minimum element in O(1).

Frequently asked questions

What is stack overflow and underflow?

Overflow is pushing onto a full stack; underflow is popping from an empty stack.

Which is better: array or linked list stack?

Use an array when you know the maximum size; use a linked list when the size can grow a lot.

What is the time complexity of push and pop?

O(1) for both.

References

  • Kernighan and Ritchie, The C Programming Language, 2nd ed. — section 4.3 builds a stack for a calculator program.
  • Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms — chapter “Elementary Data Structures”.

Placed Students

Our Clients

Partners

...