0
0
CComparisonBeginner · 4 min read

Stack vs Queue in C: Key Differences and Usage

A stack in C is a data structure that follows Last In First Out (LIFO) order, meaning the last element added is the first to be removed. A queue follows First In First Out (FIFO) order, where the first element added is the first to be removed. Both can be implemented using arrays or linked lists but serve different use cases based on their order of access.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of stack and queue in C.

FeatureStackQueue
OrderLast In First Out (LIFO)First In First Out (FIFO)
Insertion PointTop of the stackRear (end) of the queue
Removal PointTop of the stackFront (start) of the queue
Common Operationspush, pop, peekenqueue, dequeue, peek
Use CasesUndo actions, expression evaluationTask scheduling, buffering
ImplementationArray or linked listArray or linked list
⚖️

Key Differences

A stack stores elements so that the last element added is the first one removed. This is like a stack of plates where you add and remove from the top only. The main operations are push to add and pop to remove the top element.

A queue stores elements so that the first element added is the first one removed, similar to a line of people waiting. You add elements at the rear using enqueue and remove from the front using dequeue.

In C, both can be implemented using arrays or linked lists, but the logic for insertion and removal differs due to their order rules. Stacks are simpler because insertion and removal happen at the same end, while queues require managing two ends.

⚖️

Code Comparison

Here is a simple stack implementation in C using an array to push and pop integers.

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

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

void push(int val) {
    if (top == MAX - 1) {
        printf("Stack Overflow\n");
        return;
    }
    stack[++top] = val;
}

int pop() {
    if (top == -1) {
        printf("Stack Underflow\n");
        return -1;
    }
    return stack[top--];
}

int main() {
    push(10);
    push(20);
    push(30);
    printf("Popped: %d\n", pop());
    printf("Popped: %d\n", pop());
    return 0;
}
Output
Popped: 30 Popped: 20
↔️

Queue Equivalent

Here is a simple queue implementation in C using an array to enqueue and dequeue integers.

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

int queue[MAX];
int front = -1, rear = -1;

void enqueue(int val) {
    if (rear == MAX - 1) {
        printf("Queue Overflow\n");
        return;
    }
    if (front == -1) front = 0;
    queue[++rear] = val;
}

int dequeue() {
    if (front == -1 || front > rear) {
        printf("Queue Underflow\n");
        return -1;
    }
    return queue[front++];
}

int main() {
    enqueue(10);
    enqueue(20);
    enqueue(30);
    printf("Dequeued: %d\n", dequeue());
    printf("Dequeued: %d\n", dequeue());
    return 0;
}
Output
Dequeued: 10 Dequeued: 20
🎯

When to Use Which

Choose a stack when you need to reverse order or track recent actions, such as undo features or parsing expressions. Stacks are great when you only need to access the most recent item.

Choose a queue when order matters and you want to process items in the order they arrive, like task scheduling, buffering data streams, or handling requests.

Understanding the order of access (LIFO vs FIFO) helps decide which structure fits your problem best.

Key Takeaways

Stacks use LIFO order: last added, first removed.
Queues use FIFO order: first added, first removed.
Stacks add/remove from the same end; queues add at rear and remove from front.
Use stacks for undo, expression parsing; use queues for scheduling and buffering.
Both can be implemented with arrays or linked lists in C.