listish

0.0.9 • Public • Published

Listish

Basic data structures implemented efficiently in JavaScript. Don't just use an array for everything, use the right tool for the job!

Linked lists are superior to arrays for data where items need to be removed, inserted, concatenated, or split. They are inferior to arrays for indexed lookup and require more memory per item. They are equivalent for iteration.

Doubly linked lists and less space efficient but provide more capabilities than a singly linked list.

A queue is much better than an array when a first-in first-out structure is needed.

A stack is similar to an array but gives direct access to the top of the stack at all times.

Install

npm install listish

Or include in browser (IE8 and modern browsers, potentially IE6 and 7 but untested).

Common properties and functions

All of the below data structures share the following:

  • iterator() Creates an iterator to cycle through the items
  • empty() Clear all the items
  • size Number of items contained

LinkedList

  • append(value) Insert a new item at end of the list
  • pop() Remove item from end of the list and return it
  • insert(value, search) Find a value and insert a new one after it
  • remove(search) Find and remove a value
  • replace(value, search) Find and replace a value with another one
  • has(search) Check if an item is in the list
  • clone() Create a new list and copy every item to it

DoublyLinkedList

  • first() Peak at the first item in the list
  • last() Peak at the last item in the list
  • prepend(value) Add an item to the beginning of the list
  • append(value) Add an item to the end of the list
  • insertAfter(value, search) Add an item to the list directly after the given item
  • insertBefore(value, search) Add an item to the list directly before the given item
  • replace(value, search) Replace an value with another value
  • pop() Remove the end of the list of and return it
  • shift() Remove the beginning of the list of and return it
  • remove(search) Remove an item from the list
  • has(search) Check if a value is in the list
  • clone() Create a new list and add all the items from the current list into it

Stack

FILO (first in, last out) data structure.

push          pop
\__\         /__/
   --> top --^   _
      |___|       |
      |___|     length
      |___|      _|
  • top Top value which will be removed by pop or pushed down by push
  • push(value) Add a new value top the top of the stack
  • pop() Remove a value from the top of the stack and return it

Queue

FIFO (First in, first out) data structure.

dequeue
 \___\    front
      ^--|____|____|____|
                         ^-- \___\
                            enqueue
         |-----length----|
  • front Front value of the list which will be removed and returned by dequeue
  • enqueue(value) Add an item to the end of the queue
  • dequeue() Remove an item from the front of the queue and return it

Readme

Keywords

none

Package Sidebar

Install

npm i listish

Weekly Downloads

0

Version

0.0.9

License

none

Last publish

Collaborators

  • benvie