Introduction to Stack

A data structure where the last item added is the first one removed.

Introduction to Stack

Stack एक linear data structure है जिसमें data को एक विशेष क्रम में store और access किया जाता है। Stack में insertion और deletion केवल एक ही end से किया जाता है, जिसे Top कहा जाता है।

Stack LIFO (Last In, First Out) principle पर काम करता है। इसका अर्थ है कि जो element सबसे अंत में stack में insert किया जाता है, वही सबसे पहले बाहर निकलता है।

LIFO: Last In, First Out — जो element सबसे बाद में आएगा, वह सबसे पहले बाहर जाएगा।

Real-Life Example of Stack

Stack को plates के ढेर से समझा जा सकता है। यदि plates को एक के ऊपर एक रखा जाए, तो सबसे ऊपर रखी गई plate को सबसे पहले हटाया जाएगा। इसी प्रकार stack में सबसे ऊपर मौजूद element को सबसे पहले remove किया जाता है।

Basic Terminology of Stack

Term Meaning
Stack LIFO principle पर आधारित linear data structure
Top Stack का वह end जहाँ insertion और deletion होता है
Push Stack में नया element insert करना
Pop Stack से top element को remove करना
Peek Top element को बिना remove किए देखना
Overflow Full stack में नया element insert करने का प्रयास
Underflow Empty stack से element remove करने का प्रयास

Representation of a Stack

एक stack को सामान्यतः इस प्रकार represent किया जा सकता है:

Top → 30
        20
        10
        5

इस stack में 30 top element है। यदि pop operation किया जाता है, तो 30 सबसे पहले remove होगा।

Stack Operations

Stack पर मुख्य रूप से निम्न operations किए जाते हैं:

  • Push: नया element stack में insert करना।
  • Pop: top element को stack से remove करना।
  • Peek: top element की value देखना।
  • isEmpty: यह check करना कि stack खाली है या नहीं।
  • isFull: यह check करना कि stack पूरा भर चुका है या नहीं।

1. Push Operation

Push operation का उपयोग stack में नया element insert करने के लिए किया जाता है। नया element हमेशा Top पर add होता है।

उदाहरण:

Initial Stack:
Top → 20
        10

Push(30) के बाद:
Top → 30
        20
        10

2. Pop Operation

Pop operation stack के top element को remove करता है। केवल top element ही remove किया जा सकता है।

Before Pop:
Top → 30
        20
        10

After Pop:
Top → 20
        10

3. Peek Operation

Peek operation stack के top element की value बताता है, लेकिन element को stack से remove नहीं करता।

#include <iostream>
using namespace std;

int main()
{
    int stack[5] = {10, 20, 30};
    int top = 2;

    cout << "Top element = " << stack[top];

    return 0;
}
Output:
Top element = 30

Implementing Stack Using an Array

C++ में stack को array की सहायता से implement किया जा सकता है। इसमें एक variable top stack के current top element का index store करता है।

जब stack empty होता है, तो:

top = -1;

जब कोई नया element push किया जाता है, तो पहले top को increase किया जाता है और फिर element को stack में store किया जाता है।

Simple Stack Implementation

#include <iostream>
using namespace std;

int main()
{
    int stack[5];
    int top = -1;

    // Push elements
    stack[++top] = 10;
    stack[++top] = 20;
    stack[++top] = 30;

    cout << "Stack elements:" << endl;

    for (int i = top; i >= 0; i--)
    {
        cout << stack[i] << endl;
    }

    return 0;
}
Output:
Stack elements:
30
20
10

Push Operation in C++

Array-based stack में push operation के दौरान यह check करना आवश्यक है कि stack full तो नहीं है। यदि top == size - 1 है, तो stack full है और push operation नहीं किया जा सकता।

#include <iostream>
using namespace std;

int main()
{
    int stack[5];
    int top = -1;

    int value = 10;

    if (top == 4)
    {
        cout << "Stack Overflow";
    }
    else
    {
        stack[++top] = value;
        cout << value << " pushed into stack";
    }

    return 0;
}
Output:
10 pushed into stack

Pop Operation in C++

Pop operation से पहले यह check किया जाता है कि stack empty तो नहीं है। यदि top == -1 है, तो stack empty है और pop operation नहीं किया जा सकता।

#include <iostream>
using namespace std;

int main()
{
    int stack[5] = {10, 20, 30};
    int top = 2;

    if (top == -1)
    {
        cout << "Stack Underflow";
    }
    else
    {
        cout << "Popped element = " << stack[top];
        top--;
    }

    return 0;
}
Output:
Popped element = 30

Complete Stack Program Using Array

नीचे एक simple C++ program दिया गया है जिसमें push, pop और display operations को functions की सहायता से implement किया गया है।

#include <iostream>
using namespace std;

const int SIZE = 5;

int stack[SIZE];
int top = -1;

void push(int value)
{
    if (top == SIZE - 1)
    {
        cout << "Stack Overflow" << endl;
    }
    else
    {
        stack[++top] = value;
        cout << value << " pushed" << endl;
    }
}

void pop()
{
    if (top == -1)
    {
        cout << "Stack Underflow" << endl;
    }
    else
    {
        cout << stack[top] << " popped" << endl;
        top--;
    }
}

void display()
{
    if (top == -1)
    {
        cout << "Stack is empty" << endl;
    }
    else
    {
        cout << "Stack:" << endl;

        for (int i = top; i >= 0; i--)
        {
            cout << stack[i] << endl;
        }
    }
}

int main()
{
    push(10);
    push(20);
    push(30);

    display();

    pop();

    display();

    return 0;
}
Output:
10 pushed
20 pushed
30 pushed
Stack:
30
20
10
30 popped
Stack:
20
10

Stack Overflow

जब stack पूरी तरह भर चुका हो और उसमें एक नया element insert करने का प्रयास किया जाए, तो इस स्थिति को Stack Overflow कहा जाता है।

Stack Overflow: Full stack में Push operation करने का प्रयास।

Stack Underflow

जब stack खाली हो और उसमें से element remove करने का प्रयास किया जाए, तो इस स्थिति को Stack Underflow कहा जाता है।

Stack Underflow: Empty stack में Pop operation करने का प्रयास।

Applications of Stack

Stack का उपयोग computer science और programming में कई महत्वपूर्ण कार्यों के लिए किया जाता है:

  • Function calls और recursion को manage करने में।
  • Expression evaluation में।
  • Infix, Prefix और Postfix expressions के processing में।
  • Parentheses matching में।
  • Undo और Redo operations में।
  • Browser में Back operation को implement करने में।
  • Depth First Search (DFS) जैसे algorithms में।
  • Memory management में call stack के रूप में।

Stack and LIFO Principle

Stack का सबसे महत्वपूर्ण characteristic LIFO है। मान लीजिए elements को इस क्रम में push किया गया:

Push: 10 → 20 → 30 → 40

तो pop करने पर elements इस क्रम में बाहर आएँगे:

Pop: 40 → 30 → 20 → 10

Advantages of Stack

  • Stack का structure सरल और समझने में आसान होता है।
  • Insertion और deletion operations fast होते हैं।
  • Function calls और recursion को manage करने में उपयोगी है।
  • Expression evaluation में बहुत उपयोगी है।
  • Undo/Redo जैसे operations को implement किया जा सकता है।

Limitations of Stack

  • Stack में केवल top element को directly access किया जा सकता है।
  • Array-based stack की size fixed हो सकती है।
  • Full stack में push करने पर overflow हो सकता है।
  • Empty stack में pop करने पर underflow हो सकता है।

Stack vs Queue

Feature Stack Queue
Principle LIFO FIFO
Insertion Top से Rear से
Deletion Top से Front से
Example Plates का stack Ticket की queue

Important Points

  • Stack एक linear data structure है।
  • Stack LIFO (Last In, First Out) principle पर काम करता है।
  • Insertion और deletion केवल Top से होते हैं।
  • Push operation element को insert करता है।
  • Pop operation top element को remove करता है।
  • Peek operation top element को बिना remove किए दिखाता है।
  • Empty stack में pop करने पर Underflow होता है।
  • Full stack में push करने पर Overflow होता है।
  • Array-based stack में empty stack के लिए सामान्यतः top = -1 होता है।
  • Stack का उपयोग recursion, expression evaluation और undo operations में किया जाता है।

Board Focus

Exam के लिए याद रखें:
Stack → Linear Data Structure
Principle → LIFO
Insertion → Push
Deletion → Pop
Top Element → Peek
Empty Stack → top = -1
Full Stack → top = SIZE - 1
Full Stack में Push → Overflow
Empty Stack में Pop → Underflow

Board Important Questions

Very Short Answer Questions

Q1. Stack क्या है?

Answer: Stack एक linear data structure है जो LIFO principle पर काम करता है।

Q2. Stack का full form LIFO क्या है?

Answer: Last In, First Out.

Q3. Stack में insertion किस operation द्वारा किया जाता है?

Answer: Push operation द्वारा।

Q4. Stack में deletion किस operation द्वारा किया जाता है?

Answer: Pop operation द्वारा।

Q5. Stack का top क्या है?

Answer: Stack का वह end जहाँ insertion और deletion किया जाता है।

Q6. Stack Overflow क्या है?

Answer: Full stack में नया element insert करने का प्रयास Stack Overflow कहलाता है।

Q7. Stack Underflow क्या है?

Answer: Empty stack से element remove करने का प्रयास Stack Underflow कहलाता है।

Q8. Peek operation क्या करता है?

Answer: Peek operation top element को बिना remove किए देखता है।

Short Answer Questions

Q9. Stack के Push और Pop operations को समझाइए।

Answer: Push operation का उपयोग stack में नया element insert करने के लिए किया जाता है। Pop operation stack के top element को remove करता है। दोनों operations stack के top से perform किए जाते हैं।

Q10. Stack Overflow और Stack Underflow में अंतर बताइए।

Overflow Underflow
Full stack में Push करने पर होता है। Empty stack में Pop करने पर होता है।

Q11. Stack के कोई चार applications लिखिए।

Answer: Stack का उपयोग recursion, expression evaluation, undo/redo operations और browser history में किया जाता है।

Q12. Stack में top = -1 का क्या अर्थ है?

Answer: Array-based stack में top = -1 का अर्थ है कि stack अभी empty है।

Long Answer Questions

Q13. Stack को array की सहायता से implement करने का C++ program लिखिए।

#include <iostream>
using namespace std;

int main()
{
    int stack[5];
    int top = -1;

    stack[++top] = 10;
    stack[++top] = 20;
    stack[++top] = 30;

    cout << "Stack elements:" << endl;

    for (int i = top; i >= 0; i--)
    {
        cout << stack[i] << endl;
    }

    return 0;
}
Output:
Stack elements:
30
20
10

Q14. Stack में Push और Pop operations को C++ में implement कीजिए।

#include <iostream>
using namespace std;

int main()
{
    int stack[5];
    int top = -1;

    // Push
    stack[++top] = 10;
    stack[++top] = 20;
    stack[++top] = 30;

    // Pop
    cout << "Popped: " << stack[top] << endl;
    top--;

    cout << "Top element: " << stack[top];

    return 0;
}
Output:
Popped: 30
Top element: 20

Q15. Stack Overflow को check करने का C++ program लिखिए।

#include <iostream>
using namespace std;

int main()
{
    const int SIZE = 3;

    int stack[SIZE] = {10, 20, 30};
    int top = 2;

    if (top == SIZE - 1)
        cout << "Stack Overflow";
    else
        cout << "Push operation possible";

    return 0;
}
Output:
Stack Overflow

Q16. Stack Underflow को check करने का C++ program लिखिए।

#include <iostream>
using namespace std;

int main()
{
    int stack[5];
    int top = -1;

    if (top == -1)
        cout << "Stack Underflow";
    else
        cout << "Pop operation possible";

    return 0;
}
Output:
Stack Underflow

Practice Questions

  1. Stack क्या है? इसके मुख्य characteristics लिखिए।
  2. LIFO principle को उदाहरण सहित समझाइए।
  3. Stack में Push और Pop operations क्या हैं?
  4. Peek operation क्या है?
  5. Stack Overflow और Stack Underflow को समझाइए।
  6. Array की सहायता से stack implement करने का C++ program लिखिए।
  7. Stack में तीन elements push करने का program लिखिए।
  8. Stack से top element pop करने का program लिखिए।
  9. Stack के top element को display करने का program लिखिए।
  10. Stack में Overflow condition check करने का program लिखिए।
  11. Stack में Underflow condition check करने का program लिखिए।
  12. Stack और Queue में अंतर लिखिए।
  13. Stack के पाँच applications लिखिए।
  14. Array-based stack में top variable की भूमिका समझाइए।
  15. Stack में elements 10, 20, 30 और 40 push किए गए हैं। उन्हें pop करने पर किस क्रम में प्राप्त किया जाएगा?

Quick Revision

  • Stack: Linear data structure
  • Principle: LIFO
  • Insertion: Push
  • Deletion: Pop
  • Top Element: Peek द्वारा देखा जा सकता है
  • Empty Stack: top = -1
  • Overflow: Full stack में Push
  • Underflow: Empty stack में Pop
  • Main Applications: Recursion, expression evaluation, undo/redo और browser history
One-Line Revision: Stack एक LIFO-based linear data structure है जिसमें insertion और deletion केवल Top से किए जाते हैं।
Lesson 18 of 37
On This Page