📚 Stacks & Queues in Data Structures

**Stacks and Queues** are **linear data structures** that organize elements in a specific order for **efficient insertion, deletion, and retrieval**.

⚡ Why Use Stacks & Queues?

  • 🚀 **Faster Operations** - Efficient **O(1) insertion & deletion**.
  • 📌 **Used in Recursion & Function Calls** - Stacks manage **function execution.**
  • 🔄 **FIFO & LIFO Access** - Queues process **tasks in order**, while stacks **reverse order.**
  • 📂 **Used in Algorithms** - Important for **DFS, BFS, Backtracking, Scheduling.**

📌 1. Stack (LIFO - Last In First Out)

A **stack** follows the **LIFO (Last In, First Out)** principle, meaning the **last inserted element is removed first.**

🛠️ Stack Operations

  • 🔼 **Push** - Insert an element.
  • 🔽 **Pop** - Remove the top element.
  • 🔍 **Peek** - Retrieve the top element without removing it.
  • ❓ **isEmpty** - Check if the stack is empty.

⚙️ Stack Implementation (C++)


                class Stack {
                    private:
                        int arr[1000];  
                        int top;
                    public:
                        Stack() { top = -1; }
                        void push(int x) { arr[++top] = x; }
                        void pop() { if (top >= 0) top--; }
                        int peek() { return arr[top]; }
                        bool isEmpty() { return top == -1; }
                };
                

📌 2. Queue (FIFO - First In First Out)

A **Queue** follows the **FIFO (First In, First Out)** principle, meaning the **first inserted element is removed first.**

🛠️ Queue Operations

  • ➡️ **Enqueue** - Insert an element at the rear.
  • ⬅️ **Dequeue** - Remove the front element.
  • 🔍 **Front** - Retrieve the front element without removing it.
  • ❓ **isEmpty** - Check if the queue is empty.

⚙️ Queue Implementation (C++)


                class Queue {
                    private:
                        int arr[1000];
                        int front, rear;
                    public:
                        Queue() { front = 0; rear = -1; }
                        void enqueue(int x) { arr[++rear] = x; }
                        void dequeue() { if (front <= rear) front++; }
                        int getFront() { return arr[front]; }
                        bool isEmpty() { return front > rear; }
                };
                

🔄 Types of Queues

🔸 1. Circular Queue

Rear connects to the front to utilize empty space efficiently.

🔹 2. Double-Ended Queue (Deque)

Insertion & deletion allowed at **both ends**.

⏳ Time Complexity Analysis

Operation Stack Queue
Push/Enqueue O(1) O(1)
Pop/Dequeue O(1) O(1)
Access O(1) O(1)

🌍 Real-World Applications

  • 🖥️ **Function Calls (Stack)** - Manages **recursion, function execution.**
  • 📞 **Call Center Queues** - First user in queue **gets served first**.
  • 🔄 **Undo/Redo in Text Editors (Stack)** - Stores previous changes.
  • 🎮 **Game AI Decision Making** - Uses stacks for **backtracking moves.**

✅ Advantages & ❌ Disadvantages

  • ✅ **Efficient Operations (O(1))** - Insertion & deletion are constant time.
  • ✅ **Uses Less Memory** - Stacks & Queues use minimal overhead.
  • ❌ **Limited Access** - Cannot access elements randomly.
  • ❌ **Queue Requires Extra Space** - For **rear & front pointers.**

🔗 Next Topics