Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

4 Commits

Repository files navigation

Stack Data Structure in JavaScript 🚀

A simple implementation of the Stack data structure in JavaScript. This repository demonstrates how to create a stack class with essential methods and explains its functionality with practical examples.


What is a Stack?

A Stack is a data structure that follows the LIFO (Last In, First Out) principle. The last element added to the stack is the first one to be removed. Think of it as a stack of plates: you take the top plate first when serving.


Features

  • Push: Add an item to the stack.
  • Pop: Remove and return the top item.
  • Peek: View the top item without removing it.
  • isEmpty: Check if the stack is empty.
  • Size: Get the number of items in the stack.

Code Implementation

Here’s the JavaScript implementation of the stack:

classStack{constructor(){this.items=[];// Initialize an empty array}// Add an item to the stackpush(element){this.items.push(element);}// Remove and return the top itempop(){if(this.isEmpty()){return"Stack is empty!";}returnthis.items.pop();}// Check if the stack is emptyisEmpty(){returnthis.items.length===0;}// Peek at the top item without removing itpeek(){if(this.isEmpty()){return"Stack is empty!";}returnthis.items[this.items.length-1];}// Get the size of the stacksize(){returnthis.items.length;}}

Example Usage

// Initialize the stackconststack=newStack();// Add items to the stackstack.push("Plate 1");stack.push("Plate 2");stack.push("Plate 3");// Peek at the top itemconsole.log(stack.peek());// Output: Plate 3// Remove items from the stackconsole.log(stack.pop());// Output: Plate 3console.log(stack.pop());// Output: Plate 2// Check if the stack is emptyconsole.log(stack.isEmpty());// Output: false// Get the size of the stackconsole.log(stack.size());// Output: 1

Real-World Applications

  1. Undo/Redo Operations: In text editors and IDEs.
  2. Backtracking: In algorithms like depth-first search (DFS).
  3. Expression Evaluation: Managing operands and operators in mathematical expressions.
  4. Function Call Stack: Handling nested function calls in programming.

TikTok Tutorial 🎥

Want to see a quick tutorial on how to build this? Check out this TikTok video:
https://www.tiktok.com/@jsmentoring/video/7461022091421273376


How to Run the Code

  1. Clone the repository:
    git clone https://github.com/your-username/stack-data-structure.git
    cd stack-data-structure
  2. Open the file stack.js in your favorite code editor.
  3. Run the file using Node.js:
    node stack.js

Contributing

Contributions are welcome! If you have suggestions or want to add new features, feel free to create a pull request.


License

This project is licensed under the MIT License.


Connect with Me:

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages