Interview Prep
Interview questions, roadmaps and practice sets
Tutorials
Free tutorials on 30+ topics
Blog
Career guides, skills and interview tips
Free PDFs
Notes, cheat sheets and question banks
Certificates
Free skill tests and course certificates
Tools & Whiteboard
Free tools you can use right in the browser
Practice Tests
Mock tests, MCQs and quick Q&A
Free LMS
Learn on the Uncodemy LMS
Choose the best training program according to your career.
Improve the performance of your business with our innovative Industrial Training Program.
Read More →
Our instructor-led training classes are designed to make you an expert.
Read More →
Real-life case studies are a great way to demonstrate your practical knowledge.
Read More →
Explore blogs, latest technologies and industry updates with practical examples.
Read More →Browse all available training programs and choose the right one for your career.
Read More →Know more about who we are and what we stand for.
Discover our mission to equip individuals with essential IT skills and industry expertise.
Read More →
Free tutorials, interview questions, tools and calculators to help you learn.
Explore →Learn, practice and plan your career — bilkul free.
Book a free video counselling session and pick the right course for you.
Read More →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.

This guide shows two implementations — array and linked list — each as a complete program you can compile and run.
| 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:
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
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.
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.
| 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.
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.
top++ vs ++top), as above.isFull() before push or isEmpty() before pop.top at 0 instead of -1 and then mixing both conventions.free() in the linked-list pop.switch.Overflow is pushing onto a full stack; underflow is popping from an empty stack.
Use an array when you know the maximum size; use a linked list when the size can grow a lot.
O(1) for both.