Logo

dev-resources.site

for different kinds of informations.

Stack are not Queue

Published at
3/25/2024
Categories
javascript
algorithms
stack
datastructures
Author
ajithr116
Author
9 person written this
ajithr116
open
Stack are not Queue

A stack is a linear data structure that follows the Last In, First Out (LIFO) principle, meaning that the last element added to the stack is the first one to be removed. It can be envisioned as a stack of plates, where you can only add or remove plates from the top.

Operations:

  1. Push: Adds an element to the top of the stack.
  2. Pop: Removes and returns the top element of the stack.
  3. Peek/Top: Returns the top element of the stack without removing it.
  4. isEmpty: Checks if the stack is empty.
  5. Size: Returns the number of elements in the stack.

JavaScript Implementation:

class Stack {
    constructor() {
        this.data = [];
    }

    push(data) {
        this.data[this.data.length] = data;
    }

    printStack() {
        return this.data;
    }

    pop() {
        if (this.isEmpty()) {
            return "Stack is empty";
        }
        const poppedElement = this.data[this.data.length - 1];
        this.data.length -= 1;
        return poppedElement;
    }

    peek() {
        if (this.isEmpty()) {
            return "Stack is empty";
        }
        return this.data[this.data.length - 1];
    }

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

    size() {
        return this.data.length;
    }

    clear() {
        this.data = [];
    }

    haveOrNot(data) {
        for (let i = 0; i < this.data.length; i++) {
            if (this.data[i] === data) {
                return true;
            }
        }
        return false;
    }

    middleValue() {
        return this.data[Math.floor(this.data.length / 2)];
    }

    locate(data) {
        for (let i = 0; i < this.data.length; i++) {
            if (this.data[i] === data) {
                return i + 1;
            }
        }
        return -1;
    }

    reverse() {
        const reversedStack = new Stack();
        for (let i = this.data.length - 1; i >= 0; i--) {
            reversedStack.push(this.data[i]);
        }
        return reversedStack;
    }
}

const stack = new Stack();

stack.push(1);
stack.push(2);
stack.push(3);

console.log(stack.printStack());

stack.pop();

console.log(stack.printStack());

console.log(stack.peek());

console.log(stack.isEmpty());

console.log(stack.size());

stack.clear();

console.log(stack.printStack());

stack.push(11);
stack.push(22);
stack.push(33);
stack.push(44);

console.log(stack.printStack());

console.log(stack.haveOrNot(12));

console.log(stack.haveOrNot(11));

console.log(stack.middleValue());

console.log(stack.locate(22));
Enter fullscreen mode Exit fullscreen mode

Advantages of Stack:

  1. Simple Implementation: Stacks are easy to implement and understand, making them suitable for a wide range of applications.

  2. Efficiency: Adding or removing elements from the top of the stack (push and pop operations) has a time complexity of O(1), making stacks efficient for certain operations.

  3. Memory Management: Stacks are often used for managing function calls and recursion in programming languages, as well as for undo mechanisms in applications.

Disadvantages of stack:

  1. Limited Access: Unlike arrays, stacks provide limited access to elements. You can only access the top element without removing it, and accessing other elements requires popping elements off the stack.

  2. Fixed Size: In some implementations, the size of a stack may be fixed, leading to potential overflow errors if the stack exceeds its capacity.

  3. Not Suitable for All Use Cases: While stacks are useful for certain scenarios like function call management and parsing expressions, they may not be the best choice for all applications.

Conclusion:

https://github.com/ajithr116/Data-Structures/blob/main/08-stack/arrayStacj2.js are fundamental data structures with simple yet powerful operations. They find applications in various domains, including programming languages, algorithms, and computer science concepts like recursion and backtracking. Understanding stacks and their operations is essential for any programmer or computer science enthusiast.


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: