Stack are not Queue
Tags: javascript, algorithms, stack, datastructures
Published: 2024-03-25 04:54:45

A [stack](https://github.com/ajithr116/Data-Structures/blob/main/08-stack/arrayStacj2.js) 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:

```javascript
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));
```

### Advantages of [Stack](https://github.com/ajithr116/Data-Structures/blob/main/08-stack/arrayStacj2.js):

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](https://github.com/ajithr116/Data-Structures/blob/main/08-stack/arrayStacj2.js):

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.

---