How to Implement Stack in C, C++, and Java - Beginner's Guide

How to Implement Stack in C, C++, and Java - Beginner's Guide

A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. Also known as the Stack Data Structure, it ensures that the last item inserted is the first to be removed. This means that the last element added to the stack is the first one to be removed. Stacks are widely used in programming for tasks like expression evaluation, backtracking, and function call management. In this blog post, we'll explore how to implement a stack in C, C++, and Java.


Key Operations of a Stack

  1. Push: Add an element to the top of the stack.
  2. Pop: Remove the top element from the stack.
  3. Peek/Top: Retrieve the top element without removing it.
  4. isEmpty: Check if the stack is empty.

1. Stack Implementation in C

In C, we typically use arrays for stack implementation. Here's how:

#include <stdio.h>
#define MAX 100

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

void push(int value) {
    if (top == MAX - 1) {
        printf("Stack Overflow!\n");
    } else {
        stack[++top] = value;
        printf("Pushed %d onto the stack.\n", value);
    }
}

int pop() {
    if (top == -1) {
        printf("Stack Underflow!\n");
        return -1;
    } else {
        return stack[top--];
    }
}

int peek() {
    if (top == -1) {
        printf("Stack is empty!\n");
        return -1;
    } else {
        return stack[top];
    }
}

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

int main() {
    push(10);
    push(20);
    printf("Top element: %d\n", peek());
    printf("Popped: %d\n", pop());
    printf("Stack empty: %s\n", isEmpty() ? "Yes" : "No");
    return 0;
}

2. Stack Implementation in C++

In C++, we can use the built-in stack class from the Standard Template Library (STL). Here's an example:

#include <iostream>
#include <stack>

int main() {
    std::stack<int> s;

    s.push(10);
    s.push(20);
    std::cout << "Top element: " << s.top() << "\n";
    s.pop();
    std::cout << "Popped an element.\n";
    std::cout << "Stack empty: " << (s.empty() ? "Yes" : "No") << "\n";

    return 0;
}

3. Stack Implementation in Java

In Java, we can use the built-in Stack class from the java.util package. Here's an example:

import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();

        stack.push(10);
        stack.push(20);
        System.out.println("Top element: " + stack.peek());
        stack.pop();
        System.out.println("Popped an element.");
        System.out.println("Stack empty: " + (stack.isEmpty() ? "Yes" : "No"));
    }
}

Conclusion

In this blog post, we covered how to implement a stack in C, C++, and Java. While C requires manual implementation using arrays, C++ and Java offer built-in classes (std::stack and java.util.Stack, respectively) that simplify stack operations. Understanding these implementations will help you grasp the fundamental concepts and applications of stacks.

Happy coding!

Post a Comment (0)
Previous Post Next Post