Stack Data Structure in Typescript

  1. push: Add a new element on top of the stack.
  2. pop : Remove the last added element.
  3. peek: Retrieve the top element without removing it.
  4. size: Total number of elements in the stack.
  5. isEmpty: Checks if the stack has no elements.
export class Stack<T> { 
private _data: T[] = [];
push(value: T): void {} pop(): T {} peek(): T {} size(): number {}

isEmpty(): boolean {}
}
  1. If it is an open parenthesis push that in the stack.
  2. If it is a closing parenthesis we perform the following checks:
  3. If the stack is empty we return false because it does not find the opening parenthesis.
  4. If the open parenthesis we found in stack does not match the closing parenthesis we return false because the string is not balanced.
  5. Otherwise if opening and closing parenthesis match, we pop the last parenthesis.
  6. In the end we return true only if the stack is empty because the string was balanced: for each opening parenthesis we found a closing one successfully.

--

--

--

Senior Front End Software Engineer focused on Angular. Passionate about learning new skills and sharing my knowledge. Blog agdev.tech in progress.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using metro-bundler on a web application

Angular rookie mistakes — How to cache bust an Angular 2+ application in production

External Tables for Nested JSON with Trino

JavaScript Algorithm: First Reverse

Understanding Event Loop, Call Stack, Event & Job Queue in Javascript

Setting up User Profiles and Authentication on Webflow

React With A Little Help From My Node Package Friends

Learning JavaScript As A Complete Beginner.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
AG Dev

AG Dev

Senior Front End Software Engineer focused on Angular. Passionate about learning new skills and sharing my knowledge. Blog agdev.tech in progress.

More from Medium

Java Script , Version Control and NoSQL

Raspberry PI + React JS + Java

Working with Filter in Spring Boot And Angular

JavaScript, NoSQL and MongoDB