0
0
CHow-ToBeginner · 4 min read

How to Implement Stack in C: Simple Guide with Example

To implement a stack in C, use an array and an integer to track the top position. Implement push to add elements, pop to remove elements, and peek to view the top element without removing it.
📐

Syntax

A stack in C can be implemented using an array and an integer variable to track the top index. The main operations are:

  • push: Add an element to the top.
  • pop: Remove the top element.
  • peek: View the top element without removing it.
  • isEmpty: Check if the stack is empty.
  • isFull: Check if the stack is full.
c
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;

void push(int value) {
    if (top < MAX_SIZE - 1) {
        stack[++top] = value;
    }
}

int pop() {
    if (top >= 0) {
        return stack[top--];
    }
    return -1; // or error
}

int peek() {
    if (top >= 0) {
        return stack[top];
    }
    return -1; // or error
}
💻

Example

This example shows a complete stack implementation using an array with push, pop, and peek operations, including checks for overflow and underflow.

c
#include <stdio.h>
#define MAX_SIZE 5

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

int isFull() {
    return top == MAX_SIZE - 1;
}

int isEmpty() {
    return top == -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() {
    if (isEmpty()) {
        printf("Stack underflow! Cannot pop\n");
        return -1;
    }
    int val = stack[top--];
    printf("Popped %d\n", val);
    return val;
}

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

int main() {
    push(10);
    push(20);
    push(30);
    printf("Top element is %d\n", peek());
    pop();
    pop();
    pop();
    pop(); // extra pop to show underflow
    return 0;
}
Output
Pushed 10 Pushed 20 Pushed 30 Top element is 30 Popped 30 Popped 20 Popped 10 Stack underflow! Cannot pop
⚠️

Common Pitfalls

Common mistakes when implementing a stack in C include:

  • Not checking for stack overflow before pushing, which can overwrite memory.
  • Not checking for stack underflow before popping, which can cause errors or invalid data.
  • Incorrectly updating the top index, causing off-by-one errors.
  • Using an uninitialized top variable.
c
/* Wrong: No overflow check */
void push_wrong(int value) {
    stack[++top] = value; // May overflow
}

/* Correct: With overflow check */
void push_right(int value) {
    if (top < MAX_SIZE - 1) {
        stack[++top] = value;
    } else {
        printf("Stack overflow!\n");
    }
}
📊

Quick Reference

Remember these key points when working with stacks in C:

  • Use an array and a top index to track the stack.
  • Initialize top to -1 to indicate an empty stack.
  • Check for overflow before pushing and underflow before popping.
  • Push increments top, pop decrements it.

Key Takeaways

Use an array and a top index to implement a stack in C.
Always check for overflow before pushing and underflow before popping.
Initialize top to -1 to represent an empty stack.
Push operation increments top and adds the element; pop decrements top and returns the element.
Properly handle edge cases to avoid memory errors.