Stack Algorithm

The Stack Using Array Algorithm is an efficient data structure that implements the stack concept using an array as its underlying storage mechanism. The stack is a linear data structure that follows the Last-In-First-Out (LIFO) order, where the last element added to the stack is the first one to be removed. It consists of two main operations: pushing (adding) an element onto the stack and popping (removing) the element from the top of the stack. The array-based implementation of this algorithm allows for fast and easy access to the stack's top element, making it a popular choice for various applications such as parsing, expression evaluation, and function call management. In the array-based stack implementation, an integer variable called "top" is used to keep track of the index of the topmost element in the stack. When a new element is pushed onto the stack, the "top" index is incremented, and the element is placed at that index in the array. Conversely, when an element is popped from the stack, the "top" index is decremented, effectively removing the element from the stack. One of the key considerations for this algorithm is the stack's capacity, which is determined by the size of the underlying array. When the stack becomes full, it may require resizing (either by doubling or shrinking) to accommodate additional elements or to conserve memory. This dynamic resizing can be implemented using a dynamic array or by allocating and deallocating memory as needed. Overall, the Stack Using Array Algorithm offers a simple and efficient method for implementing a stack data structure with constant time complexity for both push and pop operations, making it an attractive choice for a wide range of applications.
/* Stack!!
* A stack is exactly what it sounds like. An element gets added to the top of
* the stack and only the element on the top may be removed. This is an example
* of an array implementation of a Stack. So an element can only be added/removed
* from the end of the array.
*/

// Functions: push, pop, peek, view, length

// Creates a stack constructor
var Stack = (function () {
  function Stack () {
    // The top of the Stack
    this.top = 0
    // The array representation of the stack
    this.stack = []
  }

  // Adds a value onto the end of the stack
  Stack.prototype.push = function (value) {
    this.stack[this.top] = value
    this.top++
  }

  // Removes and returns the value at the end of the stack
  Stack.prototype.pop = function () {
    if (this.top === 0) {
      return 'Stack is Empty'
    }

    this.top--
    var result = this.stack[this.top]
    delete this.stack[this.top]
    return result
  }

  // Returns the size of the stack
  Stack.prototype.size = function () {
    return this.top
  }

  // Returns the value at the end of the stack
  Stack.prototype.peek = function () {
    return this.stack[this.top - 1]
  }

  // To see all the elements in the stack
  Stack.prototype.view = function () {
    for (var i = 0; i < this.top; i++) { console.log(this.stack[i]) }
  }

  return Stack
}())

// Implementation
var myStack = new Stack()

myStack.push(1)
myStack.push(5)
myStack.push(76)
myStack.push(69)
myStack.push(32)
myStack.push(54)
console.log(myStack.size())
console.log(myStack.peek())
console.log(myStack.pop())
console.log(myStack.peek())
console.log(myStack.pop())
console.log(myStack.peek())
myStack.push(55)
console.log(myStack.peek())
myStack.view()

LANGUAGE:

DARK MODE: