TheHingineer

  • Data Structures & Algorithms


  • DSA Part-1

  • DSA Part-2

  • DSA Part-3

  • DSA Part-4

  • DSA Part-5

  • Stack Data Structure: Array Implementation

    Stack ek linear data structure hai jo LIFO (Last In First Out) principle follow karta hai. Iska matlab yeh hai ki jo element sabse aakhir me insert hota hai, wahi sabse pehle remove hota hai. Real-life example: Kitchen me rakhi hui plates ka dher (stack of plates) ya CD rack.

    Stack Operations and Boundary Conditions

    • PUSH: Stack ke top par naya element add karna. Condition check: TOP == CAPACITY - 1 ho toh Stack Overflow hota hai.
    • POP: Stack ke top se element ko remove aur return karna. Condition check: TOP == -1 ho toh Stack Underflow hota hai.
    • PEEK / TOP: Stack ke topmost element ko dekhta hai bina use remove kiye.
    • isEmpty: Check karta hai ki kya TOP == -1 hai.
        +-------------+
    3   |     40      |  <--- TOP (After Push 40)
        +-------------+
    2   |     30      |
        +-------------+
    1   |     20      |
        +-------------+
    0   |     10      |
        +-------------+
           STACK[4]
    

    C Implementation of Stack using Array

    #define MAX 100
    int stack[MAX];
    int top = -1;
    
    void push(int val) {
        if (top == MAX - 1) {
            printf("Stack Overflow!\n");
            return;
        }
        stack[++top] = val;
    }
    
    int pop() {
        if (top == -1) {
            printf("Stack Underflow!\n");
            return -1;
        }
        return stack[top--];
    }

    Complexity: Push, Pop, Peek sabhi O(1) constant time aur O(1) auxiliary space me execute hote hain.

    Agli post me hum cover karenge Stack Implementation using Linked List.

    Scroll to Top