Logo

dev-resources.site

for different kinds of informations.

Stacks in Action: A Dive into the Concept and Implementation

Published at
5/12/2024
Categories
datastructures
stack
programming
Author
guihbc
Categories
3 categories in total
datastructures
open
stack
open
programming
open
Author
6 person written this
guihbc
open
Stacks in Action: A Dive into the Concept and Implementation

A stack is a data structure that allows the addition and removal of elements with a specific rule: the last element to be added is the first one to be removed. This rule follows the LIFO (Last-In, First-Out) order, which means that the last item to enter the stack is the first to leave. Stacks are similar to a real-life stack of plates, where the plate placed on top of the stack is the first to be removed.

Now, let's take a look at a Java code example representing a simple stack implementation:

import java.util.List;
import java.util.ArrayList;
import java.util.function.Consumer;

public class Stack<T> {
    private int top;
    private int length;
    private List<T> data;

    public Stack() {
        this.top = -1;
        this.length = 0;
        this.data = new ArrayList<T>();
    }

    public boolean isEmpty() {
        return this.length == 0;
    }

    public void push(T element) {
        this.top++;
        this.length++;
        this.data.add(element);
    }

    public T getTop() {
        if (this.isEmpty()) {
            throw new RuntimeException("empty stack");
        }

        return this.data.get(top);
    }

    public T pop() {
        if (this.isEmpty()) {
            throw new RuntimeException("empty stack");
        }

        T element = this.data.remove(top);

        this.top--;
        this.length--;
        return element;
    }

    public void forEach(Consumer<T> consumer) {
        for (int i = this.top; i >= 0; i--) {
            consumer.accept(this.data.get(i));
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

In the code above we have a simple stack structure with 3 properties:

  • top: The index of the top element.
  • length: The length.
  • data: A list of a generic type to store the data.

And we have 3 main methods:

  • push: The top position will be the next one, the length will be incremented by 1 and the new element will be added to the top of the stack.

  • pop: This method checks if the stack is empty. If it is, an exception is thrown indicating that the stack is empty. If it is not empty, it retrieves the top element, decrements the top index and the length by 1, and then returns the removed top element.

push usage example:

Stack<Integer> stack = new Stack();
stack.push(10); // index 0
stack.push(20); // index 1
stack.push(30); // index 2
// the top is 2
Enter fullscreen mode Exit fullscreen mode

pop usage example:

Stack<Integer> stack = new Stack();
stack.push(10); // index 0
stack.push(20); // index 1
stack.push(30); // index 2
// the top is 2
Integer removed = stack.pop();
// after pop top is 1
Enter fullscreen mode Exit fullscreen mode
stack Article's
30 articles in total
Favicon
Stack Developer Web
Favicon
Whats your TECH stack ?? How did you get into that??
Favicon
Building a Stack Implementation in Java: Mastering Data Structure Fundamentals.
Favicon
Pattern 7: Stack
Favicon
Stacks: 50 Leetcode Questions
Favicon
Why Is Stack Memory Faster Than Heap Memory? Hereโ€™s What You Need to Know!
Favicon
Stack: Concepts and Applications โ€” Java
Favicon
Top 7 Reasons to Hire a MERN Stack Development Company for Scalable Web Solutions
Favicon
Maximum swap
Favicon
Comprehensive ๐—š๐˜‚๐—ถ๐—ฑ๐—ฒ ๐˜๐—ผ ๐—ฆ๐˜๐—ฎ๐—ฐ๐—ธ ๐——๐—ฎ๐˜๐—ฎ ๐—ฆ๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐˜‚๐—ฟ๐—ฒ: ๐—œ๐—บ๐—ฝ๐—น๐—ฒ๐—บ๐—ฒ๐—ป๐˜๐—ฎ๐˜๐—ถ๐—ผ๐—ป, ๐—ข๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐˜€, ๐—ฎ๐—ป๐—ฑ ๐—ฃ๐—ฟ๐—ผ๐—ฏ๐—น๐—ฒ๐—บ-๐—ฆ๐—ผ๐—น๐˜ƒ๐—ถ๐—ป๐—ด
Favicon
Understanding Stack as an Abstract Data Type
Favicon
Heap vs Stack: como o Java gerencia o que deve ser lembrado ou esquecido
Favicon
Factors to consider in choosing a tech stack for a project
Favicon
stack in PyTorch
Favicon
Becoming a Full Stack Developer: A Step-by-Step Guide
Favicon
Full Stack Development: A Comprehensive Guide
Favicon
The Complete Guide to Full Stack Development: Essential Skills and Strategies
Favicon
Stack & Heap
Favicon
Stacks in Action: A Dive into the Concept and Implementation
Favicon
An ode to Stacks and Pointers in Go!
Favicon
Queue and Stack Essentials: A Python Programmer's Guide
Favicon
Navigating the Seas of Web Development
Favicon
Stack are not Queue
Favicon
Solving DSA Problems. HackerRank: Balanced Brackets
Favicon
Understanding Stack Memory and Heap Space inย Java.
Favicon
Join Our Stack Programming Community!
Favicon
Understanding the Stack Data Structure: A Java Implementation
Favicon
A Brief Look At Spotify's Tech Stack
Favicon
Implementation of Stack Using Array
Favicon
Detect what calls your Node.js code

Featured ones: