1.0.0 • Published 3 years ago

@amirho/ds-stack v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Stack data Structure

In computer science, a stack is an abstract data type that serves as a collection of elements, with two main principal operations:

Push, which adds an element to the collection, and Pop, which removes the most recently added element that was not yet removed.

The order in which elements come off a stack gives rise to its alternative name, LIFO (last in, first out). Additionally, a peek operation may give access to the top without modifying the stack.1 The name "stack" for this type of structure comes from the analogy to a set of physical items stacked on top of each other. This structure makes it easy to take an item off the top of the stack, while getting to an item deeper in the stack may require taking off multiple other items first.

Table of Content

  1. Usage
  2. Methods
  3. Test
  4. Licence
  5. Author

Usage

installation

npm install stack
import Stack from "stack";

const stack = new Stack();

stack.push("element");

also u can add elements in Constructor

const stack = new Stack("element");

both Constructor and push method has the ability to get many elements

import Stack from "stack";

const stack = new Stack("element1", "element2");

stack.peek(); // returns "element2"

stack.push("element3", "element4");

stack.peek(); //returns "element4"

the important thing is developer doesn't have access to _stack method list

stack.#_list; // syntax Error
stack._list; // undefined

Methods

  1. push() Add new element to the end of stack.
  2. pop() Remove the last element and will return it.
  3. peek() This will return the top element from stack.
  4. isEmpty() it will return True if its empty other wise false
  5. size it returns the number of element that stack has I implement it with getter so you don't have use () brackets
  6. clear() it will remove all elements from the stack

Test

npm test

Licence

MIT

Author

AmirHossein Salighedar

1.0.0

3 years ago