← Back to DSA Course | Chapter 5: Stacks | Lesson 1 of 5

Stack परिचय

एक stack plates के एक pile जैसा है: आप plates सिर्फ top से जोड़ते और लेते हैं, इसलिए सबसे आखिर में रखी plate सबसे पहले ली जाती है।
Syntax
markup
stack = []
stack.append(item)    # push
item = stack.pop()    # pop
top = stack[-1]       # peek
empty = len(stack) == 0

What is a Stack

एक stack एक data structure है जहां elements उसी end से जोड़े और हटाए जाते हैं, और डाला गया आखिरी item हमेशा सबसे पहले निकाला जाता है, एक rule जिसे Last In First Out (LIFO) कहते हैं।

उदाहरण: What is a Stack

#include <iostream>
#include <stack>
using namespace std;
int main() {
    stack<int> s;
    s.push(1); s.push(2); s.push(3);
    cout << "last in, first out -- top is " << s.top() << endl;
    return 0;
}
import java.util.Stack;
public class Main {
    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>();
        s.push(1); s.push(2); s.push(3);
        System.out.println("last in, first out -- top is " + s.peek());
    }
}
s = []
s.append(1)
s.append(2)
s.append(3)
print("last in, first out -- top is", s[-1])
#include <stdio.h>
int main() {
    int s[10], top = -1;
    s[++top] = 1; s[++top] = 2; s[++top] = 3;
    printf("last in, first out -- top is %d\n", s[top]);
    return 0;
}

LIFO Principle

LIFO बिल्कुल plates के एक physical pile जैसे behave करता है: आप सिर्फ top में एक plate जोड़ सकते हैं या top से एक ले सकते हैं, कभी बीच में हाथ नहीं डाल सकते, जो बिल्कुल वह है जो एक stack को simple और predictable बनाता है reason करने के लिए।

उदाहरण: LIFO Principle

#include <iostream>
#include <stack>
using namespace std;
int main() {
    stack<string> plates;
    plates.push("plate1"); plates.push("plate2"); plates.push("plate3");
    cout << "take one off the top: " << plates.top() << endl;
    plates.pop();
    cout << "next one off the top: " << plates.top() << endl;
    return 0;
}
import java.util.Stack;
public class Main {
    public static void main(String[] args) {
        Stack<String> plates = new Stack<>();
        plates.push("plate1"); plates.push("plate2"); plates.push("plate3");
        System.out.println("take one off the top: " + plates.pop());
        System.out.println("next one off the top: " + plates.peek());
    }
}
plates = ["plate1", "plate2", "plate3"]
print("take one off the top:", plates.pop())
print("next one off the top:", plates[-1])
#include <stdio.h>
int main() {
    char *plates[3] = {"plate1", "plate2", "plate3"};
    int top = 2;
    printf("take one off the top: %s\n", plates[top--]);
    printf("next one off the top: %s\n", plates[top]);
    return 0;
}

Stack Operations

Core stack operations हैं push (top में जोड़ना), pop (top से हटाना), peek (top item को हटाए बिना देखना), और यह check करना कि stack अभी खाली है या नहीं।

उदाहरण: Stack Operations

#include <iostream>
#include <stack>
using namespace std;
int main() {
    stack<int> s;
    s.push(10);
    s.push(20);
    cout << "peek: " << s.top() << endl;
    s.pop();
    cout << "after pop, empty? " << s.empty() << endl;
    return 0;
}
import java.util.Stack;
public class Main {
    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>();
        s.push(10); s.push(20);
        System.out.println("peek: " + s.peek());
        s.pop();
        System.out.println("after pop, empty? " + s.isEmpty());
    }
}
s = []
s.append(10)
s.append(20)
print("peek:", s[-1])
s.pop()
print("after pop, empty?", len(s) == 0)
#include <stdio.h>
int main() {
    int s[10], top = -1;
    s[++top] = 10; s[++top] = 20;
    printf("peek: %d\n", s[top]);
    top--;
    printf("after pop, empty? %d\n", top == -1);
    return 0;
}

Stack Applications

Stacks उन features को power देते हैं जो आप हर रोज़ उपयोग करते हैं: एक editor में undo history, एक browser का back button, code में matching brackets, और function call stack जो हर function call खत्म होने के बाद कहां return करना है इसका track रखता है।

उदाहरण: Stack Applications

#include <iostream>
#include <stack>
using namespace std;
int main() {
    stack<char> undoHistory;
    undoHistory.push('A'); undoHistory.push('B'); undoHistory.push('C');
    cout << "undo: reverts " << undoHistory.top() << endl;
    undoHistory.pop();
    cout << "undo again: reverts " << undoHistory.top() << endl;
    return 0;
}
import java.util.Stack;
public class Main {
    public static void main(String[] args) {
        Stack<Character> undoHistory = new Stack<>();
        undoHistory.push('A'); undoHistory.push('B'); undoHistory.push('C');
        System.out.println("undo: reverts " + undoHistory.pop());
        System.out.println("undo again: reverts " + undoHistory.peek());
    }
}
undo_history = ['A', 'B', 'C']
print("undo: reverts", undo_history.pop())
print("undo again: reverts", undo_history[-1])
#include <stdio.h>
int main() {
    char s[10]; int top = -1;
    s[++top] = 'A'; s[++top] = 'B'; s[++top] = 'C';
    printf("undo: reverts %c\n", s[top--]);
    printf("undo again: reverts %c\n", s[top]);
    return 0;
}

Basic Stack Practice

एक stack उपयोग करके एक sequence reverse करना, या push/pop operations की एक series का output predict करना जैसे छोटे exercises, LIFO order कैसे असल में काम करता है इसके लिए एक solid intuition बनाने का एक तेज़ तरीका है।

उदाहरण: Basic Stack Practice

#include <iostream>
#include <stack>
using namespace std;
int main() {
    stack<int> s;
    int arr[] = {1, 2, 3, 4};
    for (int x : arr) s.push(x);
    cout << "reversed: ";
    while (!s.empty()) { cout << s.top() << " "; s.pop(); }
    return 0;
}
import java.util.Stack;
public class Main {
    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>();
        int[] arr = {1, 2, 3, 4};
        for (int x : arr) s.push(x);
        System.out.print("reversed: ");
        while (!s.isEmpty()) System.out.print(s.pop() + " ");
    }
}
s = []
for x in [1, 2, 3, 4]:
    s.append(x)
print("reversed:", end=" ")
while s:
    print(s.pop(), end=" ")
#include <stdio.h>
int main() {
    int s[10], top = -1;
    int arr[] = {1, 2, 3, 4};
    for (int i = 0; i < 4; i++) s[++top] = arr[i];
    printf("reversed: ");
    while (top >= 0) printf("%d ", s[top--]);
    return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक खाली std::stack पर pop() या top() call करना, जो undefined behavior है।
  2. यह उम्मीद करना कि pop() हटाई गई value return करेगा, जब C++ में यह कुछ नहीं return करता और आपको पहले top() पढ़ना चाहिए।
  3. एक stack को एक queue की तरह treat करना और सबसे पुराने item के पहले निकलने की उम्मीद करना।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.