Lesson 14
Stacks & Queues
Implement classic data structures
1What is a Stack?✨
A stack is a linear data structure that follows the Last In First Out (LIFO) principle. This means the last item added is the first one to be removed. Think of it as a stack of plates!
2Why Learn Stacks?🎯
- ✓ Useful for function calls and memory management
- ✓ Essential for algorithms like Depth-First Search
- ✓ Helps in undo mechanisms in software applications
3Your First Stack Program💡
Let's implement a simple stack in C:
#include <stdio.h>
#define MAX 10struct Stack {
int items[MAX];
int top;
};void initStack(struct Stack* s) {
s->top = -1;
}int isFull(struct Stack* s) {
return s->top == MAX - 1;
}void push(struct Stack* s, int item) {
if (!isFull(s)) {
s->items[++(s->top)] = item;
}
}4What is a Queue?🚀
A queue is another linear data structure that follows the First In First Out (FIFO) principle. Here, the first item added is the first one to be removed. Imagine a line of people waiting!
5Why Learn Queues?🔍
- ✓ Great for scheduling processes in operating systems
- ✓ Used in breadth-first search algorithms
- ✓ Perfect for handling requests in web servers
6Your First Queue Program⭐
Now, let’s create a simple queue in C:
#include <stdio.h>
#define MAX 10struct Queue {
int items[MAX];
int front, rear;
};void initQueue(struct Queue* q) {
q->front = -1;
q->rear = -1;
}int isEmpty(struct Queue* q) {
return q->front == -1;
}void enqueue(struct Queue* q, int item) {
if (q->rear < MAX - 1) {
if (q->front == -1) q->front = 0;
q->items[++(q->rear)] = item;
}
}7What You’ll Build This Grade🧩
- ✓ A mini text editor using stacks for undo/redo functionality
- ✓ A task scheduler using queues for managing tasks
- ✓ A simulation of customer service with both stacks and queues
- ✓ A game that uses stacks for managing game states
Now you're ready to dive into implementing stacks and queues in your projects! Happy coding!
🎯 Practice Challenges
Master Stacks & Queues by completing all 3 difficulty levels
Stacks & Queues - Easy Challenge
Start with the basics. Perfect for beginners!
Stacks & Queues - Medium Challenge
Get more challenging. Build your confidence!
Stacks & Queues - Hard Challenge
Master this concept. Prove your skills!
🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →
Ready to Master Stacks & Queues? 🚀
Complete the challenges, take the quiz and keep your learning streak going!
Keep learning
- ← Previous lessonLesson 13: Linked ListsBuild your first dynamic data structure
- Next lesson →Lesson 15: Sorting AlgorithmsBubble sort and selection sort
- Grade 11 courseC++ OOP - Advanced ProgrammingC++ coding course for kids.
- Student ToolsFree calculators for studentsPercentage, fraction, ratio and marks calculators with steps.
- CBSEClass 10 CBSE & NCERT study materialMaths, Science and Computer chapters with solutions.
- Future SkillsExplore future skillsAI, robotics and new-age skills explained for students.