Stack vs Queue in C: Key Differences and Usage
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.
| Feature | Stack | Queue |
|---|---|---|
| Order | Last In First Out (LIFO) | First In First Out (FIFO) |
| Insertion Point | Top of the stack | Rear (end) of the queue |
| Removal Point | Top of the stack | Front (start) of the queue |
| Common Operations | push, pop, peek | enqueue, dequeue, peek |
| Use Cases | Undo actions, expression evaluation | Task scheduling, buffering |
| Implementation | Array or linked list | Array 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.
#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; }
Queue Equivalent
Here is a simple queue implementation in C using an array to enqueue and dequeue integers.
#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; }
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.