#16.Journey to DSA in C/C++

ยท

5 min read

"Stacks in Data Structures: Unveiling the Power of LIFO"

Greetings, dear readers! Today, I'm setting off on a journey through the intricate world of data structures, the unsung champions that form the foundation of efficient algorithms. My spotlight for today is on stacks, a seemingly straightforward yet profoundly influential data structure that adheres to the Last-In-First-Out (LIFO) principle. In this blog post, I'll dive deep into the concept of stacks, uncover their real-world applications with more example coding , and explore why they stand as pillars in the realm of computer science. Whether you're a curious novice or a seasoned developer revisiting the basics, join me as I reveal the immense power and significance of stacks in data structures.

FOR METHOD 1 check out : #15.Journey to DS in C++ (hashnode.dev)

STACK IMPLEMENTATION METHOD 2:

#include <stdio.h>
#include <stdlib.h>

#define size 20

int stack[size],top_of_stack;

void Push(int [],int); //to insert elements in the stack

void Pop (int []);//to delete elements from the stack

void display(int []); //to display the elements

void Peek(int stack[]); // to peek into the stack top


int main()
{
    int num=0;
    int option=0;
    top_of_stack=-1;

    while(1)
    {
        printf("\n1: Push \n2: Pop \n3: Display \n4: Peek \n5: Exit\n Enter one of your choices:");
        scanf("%d",&option);

        switch(option)
        {
            case 1:
                printf("Enter the item you want to insert :");
                scanf("%d",&num);
                Push(stack,num);
            break;

            case 2:
                Pop(stack);
            break;

            case 3:
                display(stack);
            break;

            case 4:
                Peek(stack);
            break;
            case 5:
                exit(0);

            default:
                printf("\nPlease enter a correct option\n");
            break;
        }
    }

}

void Push(int stack[],int num)
{
    if(top_of_stack==size-1)
    {
        printf("\nCannot add element, Stack is full\n");
        return;
    }
    top_of_stack++;
    stack[top_of_stack]=num;
}

void Pop(int stack[])
{
    int del_num;
    if(top_of_stack==-1)
    {
        printf("Empty Stack.\n");
        return;
    }

    del_num=stack[top_of_stack];
    top_of_stack--;
    printf("Element deleted: %d \n",del_num);
    return;
}

void Peek(int stack[]){
        if(top_of_stack==-1)
    {
        printf("Empty Stack.\n");
        return;
    }
    printf("Top of stack is: \n%d ",stack[top_of_stack]);

}

void display(int stack[])
{
    int i=0;
    if(top_of_stack==-1)
    {
        printf("Empty Stack .\n");
        return;
    }

    for(i = top_of_stack ; i >=0 ; i--)
    {
        printf(" %d ",stack[i]);
    }
    printf("\n\n");


}

STACK IMPLEMENTATION METHOD 3:

In C++:

#include <bits/stdc++.h>

using namespace std;

class Stack {
    int top;
    int MAX;
    int* a;

public:

    Stack(int size){
        top = -1;
        MAX = size;
        a = new int [MAX];
    }
    bool push(int x);
    int pop();
    int peek();
    bool isEmpty();
    bool isFull();
    void display();
};

bool Stack::push(int x)
{
    if (top >= (MAX - 1)) {
        cout << "Stack Overflow";
        return false;
    }
    else {
        a[++top] = x;
        cout << x << " pushed into stack\n";
        return true;
    }
}

int Stack::pop()
{
    if (top < 0) {
        cout << "Stack Underflow";
        return 0;
    }
    else {
        int x = a[top--];
        return x;
    }
}
int Stack::peek()
{
    if (top < 0) {
        cout << "Stack is Empty";
        return 0;
    }
    else {
        int x = a[top];
        return x;
    }
}

bool Stack::isEmpty()
{
    return (top < 0);
}

bool Stack::isFull()
{
    return (top == MAX - 1);
}

void Stack::display(){

    int i=0;
    if(top==-1){
        printf("The stack is empty .\n");
        return;
    }

    for(i = top ; i >=0 ; i--){
        printf("%d ",a[i]);
    }
    printf("\n\n");

}

int main()
{
    class Stack s(5);
    s.push(5);
    s.push(10);
    s.push(15);
    s.push(20);
    cout << s.isEmpty() << endl;
    cout << s.isFull() << endl;
    cout << s.pop() << " element popped from the stack\n";
    s.display();

    return 0;
}

STACK IMPLEMENTATION METHOD 4:

#include <bits/stdc++.h>
using namespace std;

struct Stack {
    int *arr;
    int capacity;
    int top;
    Stack(int cap) {
        capacity = cap;
        arr = new int [capacity];
        top = -1;
    }

    void push(int x) {
        if (top == capacity - 1) {cout << "Full Stack" << endl; return;}
        top++;
        arr[top] = x;
    }

    int pop() {
        if (top == -1) {cout << "Empty Stack" << endl; return INT_MIN;}
        int temp = arr[top];
        top--;
        return temp;
    }

    int peek() {
        if (top == -1) {cout << "Empty Stack" << endl; return INT_MIN;}
        return arr[top];
    }

    int size() {
        return (top + 1);
    }

    bool isEmpty() {
        return top == -1;
    }

    bool isFull() {
        return top == capacity - 1;
    }

    void display() {
        int i = 0;
        for (i = top ; i >= 0 ; i--) printf("%d ", arr[i]);
    }

};

int main()
{
    Stack s(5);
    s.push(5);
    s.push(10);
    s.push(15);
    s.push(20);
    cout << s.pop() << endl;
    cout << s.size() << endl;
    cout << s.peek() << endl;
    cout << s.isEmpty() << endl;
    cout << s.isFull() << endl;
    s.display();

    return 0;
}

STACK IMPLEMENTATION METHOD 5:

#include <bits/stdc++.h>
using namespace std;

struct Stack {
    vector<int> vect;

    void push(int val) {
        vect.push_back(val);
    }

    int pop() {
        if (isEmpty()) {
            return INT_MIN;
        }
        int temp = vect.back();
        vect.pop_back();
        return temp;
    }

    int peek() {
        if (isEmpty()) {
            cout << "The Stack is Empty" << endl;
            return INT_MIN;
        }
        return vect.back();
    }

    bool isEmpty() {
        return vect.empty();
    }

    int size() {
        return vect.size();
    }

    void display() {
        vector<int>::iterator it;
        for (it = vect.end() - 1; it >= vect.begin(); it--)
            cout << *it << " ";
    }
};

int main()
{
    Stack s;
    s.push(5);
    s.push(10);
    s.push(15);
    s.push(20);
    cout << s.peek() << endl;
    cout << s.pop() << endl;
    cout << s.size() << endl;
    cout << s.peek() << endl;
    cout << s.isEmpty() << endl;
    s.display();
    return 0;
}

STACK IMPLEMENTATION METHOD 6:

In C++ (STL):

#include <iostream>
#include <stack>
using namespace std;

int main()
{
    stack<int> s;

    s.push(5);
    s.push(10);
    s.push(15);
    s.push(20);

    cout << s.top() << endl;
    cout << s.size() << endl;
    s.pop();
    cout << s.top() << endl;
    cout << s.size() << endl;

    while (!s.empty()) {
        cout << s.top() << endl;
        s.pop();
    }
    return 0;
}

In conclusion, I've ventured into the world of data structures and delved deep into the fascinating realm of stacks. These unassuming LIFO-based structures may seem simple on the surface, but their significance and applications are profound. From everyday examples like the "back" button in web browsers to critical use cases in computer science and software development, stacks play a pivotal role. They are the unsung heroes that empower algorithms and make complex processes efficient.

Understanding the stack data structure is not just a theoretical exercise; it's a practical skill that can enhance your problem-solving abilities. Whether you're an aspiring programmer or an experienced developer, grasping the fundamentals of stacks can open doors to more efficient coding and problem-solving. So, I encourage you to explore this fundamental data structure further and consider how it can elevate your programming journey. ๐Ÿš€ Happy coding! ๐Ÿค–๐Ÿ’ป

ย