Stack

Stack

Created
Feb 25, 2023 11:54 PM
Tag
Data structure

Let's talk about Stacks!

The stack is a highly versatile data structure used in computer science with many different uses. For example, let's say you're happily coding away at 3 PM with your trusty cup of coffee by your side when suddenly a big, red error message pops up on your screen. You start thinking, "What the heck happened here?" In this situation, the error message will likely show you the call stack of your application. The stack data structure is used in the call stack to keep track of the sequence of function calls during program execution, which is a crucial feature for many programming languages.

what does a "stack" look like?

The concept of a stack is easy to understand. Imagine a stack of books, with each book resting on top of the previous one. You can add a new book to the top of the stack, but if you want to remove a book, you must remove them one by one in order to prevent the stack from collapsing and causing injury (like falling on your foot 馃槯 馃Χ). It's similar to how a stack data structure works in programming. New items can be added to the top of the stack, and items can be removed from the top of the stack one by one in a specific order. Actually, this order has a very popular acronym called FILO (First In, Last Out) or LIFO (Last In, First Out)
notion image

Now, time to roll up your sleeves

Ok, let's create our simple stack example
export class Stack { private readonly items: any constructor () { this.items = [] } }
Stacks are similar to arrays, but we need to respect the LIFO (Last-In, First-Out) principle. So let's implement some new methods.
export class Stack { private readonly items: any constructor () { this.items = [] } push (element: any): void { this.items.push(element) } pop (): any { return this.items.pop() } peek (): any { return this.items[this.items.length - 1] } }
  • push(element: any): void: This method adds a new element to the top of the stack. It takes one argument, element, which is the value to be added to the stack. The push method uses the Array.push() method to add the new element to the end of the items array.
  • pop(): any: This method removes and returns the top element from the stack. It doesn't take any arguments. The pop method uses the Array.pop() method to remove the last element from the items array and return it.
  • peek(): any: This method returns the top element of the stack without removing it. It doesn't modify the stack in any way. The peek method returns the last element of the items array using the Array.length property to get the index of the last element.
All of these methods are designed to respect the LIFO (Last-In-First-Out) principle of a stack data structure. The push method adds new elements to the top of the stack, the pop method removes elements from the top of the stack, and the peek method allows you to look at the top element without removing it. This is a very simple stack (: I have a more complete example on my GitHub, check it :D