Stacks and Queues
Understand the stack (LIFO) and queue (FIFO) data structures and their applications.
Stacks and Queues is a free C Academy lesson on CoddyKit. This is lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.
Stacks and Queues in C
Stacks and queues are linear data structures used for storing and managing data.
In this lesson, you will learn:
- How stacks work (LIFO - Last In, First Out).
- How queues work (FIFO - First In, First Out).
- How to implement stacks and queues in C.

What is a Stack?
A stack follows the Last In, First Out (LIFO) principle.
Operations:
- Push - Add an element to the top.
- Pop - Remove the top element.
- Peek - View the top element without removing it.
Example: Implementing a Stack
This program demonstrates stack operations using an array.
#include <stdio.h>
#define MAX 5
int stack[MAX], top = -1;
void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = value;
}
}
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
} else {
return stack[top--];
}
}
int main() {
push(10);
push(20);
printf("Popped: %d\n", pop());
return 0;
}What is a Queue?
A queue follows the First In, First Out (FIFO) principle.
Operations:
- Enqueue - Add an element to the rear.
- Dequeue - Remove an element from the front.
- Peek - View the front element without removing it.
Example: Implementing a Queue
This program demonstrates queue operations using an array.
#include <stdio.h>
#define MAX 5
int queue[MAX], front = -1, rear = -1;
void enqueue(int value) {
if (rear == MAX - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1) front = 0;
queue[++rear] = value;
}
}
int dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return -1;
} else {
return queue[front++];
}
}
int main() {
enqueue(10);
enqueue(20);
printf("Dequeued: %d\n", dequeue());
return 0;
}Circular Queue
A circular queue overcomes the limitations of a normal queue by reusing empty spaces.
Operations remain the same but wrap around the array.
Example: Circular Queue
This program demonstrates a circular queue implementation.
#include <stdio.h>
#define MAX 5
int queue[MAX], front = -1, rear = -1;
void enqueue(int value) {
if ((rear + 1) % MAX == front) {
printf("Queue Overflow\n");
} else {
if (front == -1) front = 0;
rear = (rear + 1) % MAX;
queue[rear] = value;
}
}
int dequeue() {
if (front == -1) {
printf("Queue Underflow\n");
return -1;
}
int data = queue[front];
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
return data;
}
int main() {
enqueue(10);
enqueue(20);
printf("Dequeued: %d\n", dequeue());
return 0;
}Applications of Stacks and Queues
Stacks are used in:
- Function call management (recursion).
- Undo/redo operations.
- Expression evaluation.
Queues are used in:
- Scheduling tasks in operating systems.
- Managing requests in web servers.
- Data transfer in networking.
Summary
In this lesson, you learned:
- How stacks and queues work.
- How to implement stack and queue operations.
- The applications of stacks and queues.
Next, we will explore trees and graphs in C!

Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “Stacks and Queues” lesson free?
Yes — the full text of “Stacks and Queues” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 3 lessons in total.
What will I learn in “Stacks and Queues”?
Understand the stack (LIFO) and queue (FIFO) data structures and their applications. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.
How long does the “Stacks and Queues” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Linked Lists
- Stacks and Queues
- Trees and Graphs