TalentJR

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:

💻 code
#include <stdio.h>
#define MAX 10
💻 code
struct Stack {
    int items[MAX];
    int top;
};
💻 code
void initStack(struct Stack* s) {
    s->top = -1;
}
💻 code
int isFull(struct Stack* s) {
    return s->top == MAX - 1;
}
💻 code
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:

💻 code
#include <stdio.h>
#define MAX 10
💻 code
struct Queue {
    int items[MAX];
    int front, rear;
};
💻 code
void initQueue(struct Queue* q) {
    q->front = -1;
    q->rear = -1;
}
💻 code
int isEmpty(struct Queue* q) {
    return q->front == -1;
}
💻 code
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

🟢 EASY

Stacks & Queues - Easy Challenge

Start with the basics. Perfect for beginners!

🟡 MEDIUM

Stacks & Queues - Medium Challenge

Get more challenging. Build your confidence!

🔴 HARD

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!