Doubly linked list implementation with an extensive Java-like API. If you are used to the Java LinkedList collection, you should feel at home with linkedlistpup. Linked lists are best used as queues or stacks since random access is very inefficient.
Install with npm as expected.
npm install linkedlistpup --save
You will need to import linkedlistpup.
import LinkedList from 'linkedlistpup';
Or you will need to require it.
var LinkedList = require('linkedlistpup');
Then use linkedlistpup like any other class.
import LinkedList from 'linkedlistpup';
let list = new LinkedList();
list.add("Alpha");
list.add("Beta");
list.add("Charlie");
let firstValue = list.poll();
The API for linkedlistpup will feel familiar to Java users, but there are a few differences and additions to better fit into a Javascript paradigm. The list will hold any value aside from undefined.
Creates an empty list.
Adds item to the end of the list. It will return true to indicate the item has been added. If the passed in value is undefined, then it will throw a TypeError.
Adds item to the front of the list. It will return true to indicate the item has been added. If the passed in value is undefined, then it will throw a TypeError.
Adds item to the end of the list. It will return true to indicate the item has been added. If the passed in value is undefined, then it will throw a TypeError. This behavior is identical to add.
Adds all elements from items to the end of the list, returning the number of items added. The argument must be an array or another instance of linkedlistpup or it will throw a TypeError. Adding 4,5,6 to a list that already has 1,2,3 will result in 1, 2, 3, 4, 5, 6.
Adds all elements from items to the front of the list, returning the number of items added. The argument must be an array or another instance of linkedlistpup or it will throw a TypeError. Adding 4,5,6 to a list that already has 1,2,3 will result in 4, 5, 6, 1, 2, 3.
Returns the number of items in the list.
Removes all items from the list, leaving it empty.
Performs a shallow copy of the list resulting in a new instance of linkedlistpup with the same contents as the original.
Returns true if the list contains the specified item. If the item isn't in the list, then it will return false.
Returns the item, whatever type that might be, at the specified index. If the index isn't valid for the list, then a RangeError will be thrown.
Returns the first item in the list. If the list is empty, then a RangeError will be thrown.
Returns the last item in the list. If the list is empty, then a RangeError will be thrown.
Returns the index of the first occurence of the specified item in the list. If the item isn't in the list, then it will return -1.
Returns the index of the last occurence of the specified item in the list. If the item isn't in the list, then it will return -1.
Returns true if the list has no items and false if the list has one or more items.
Returns true if the list has one or more items and false if the list is empty.
Returns the first item in the list while leaving the list unchanged. If the list is empty, then the returned value will be undefined.
Returns the first item in the list while leaving the list unchanged. If the list is empty, then the returned value will be undefined.
Returns the last item in the list while leaving the list unchanged. If the list is empty, then the returned value will be undefined.
Returns the item at the specified index in the list while leaving the list unchanged. If the index is invalid, then the returned value will be undefined.
Removes and returns the first item in the list. If the list is empty, then it will return undefined.
Removes and returns the first item in the list. If the list is empty, then it will return undefined. Same as poll.
Removes and returns the list item in the list. If the list is empty, then it will return undefined.
Removes and returns the first item in the list. If the list is empty, then it will throw a RangeError.
Removes and returns the first item in the list. If the list is empty, then it will throw a RangeError. Same as pop.
Removes and returns the first item in the list. If the list is empty, then it will throw a RangeError. Same as pop.
Removes and returns the last item in the list. If the list is empty, then it will throw a RangeError.
Removes and returns the item at the specified index. If the index is invalid, then it will throw a RangeError.
Removes the first occurrence of the specified item in the list. If an item was removed, it will return true. If the item wasn't found, then it will return false.
Removes the first occurrence of the specified item in the list. If an item was removed, it will return true. If the item wasn't found, then it will return false. Same as removeValue.
Removes the last occurrence of the specified item in the list. If an item was removed, it will return true. If the item wasn't found, then it will return false.
Returns the list as an array. If the list is empty, then an empty array will be returned.
Returns the list as an array in reverse order. If the list is empty, then an empty array will be returned.
Invokes the passed-in function on each item in the list in order.
Invokes the passed-in function on each item in the list in reverse order.
Inserts an item in the list before the item at the specified index. If the list is empty and the index is zero, then it will add the item as the only item in the list. If the item passed in is undefined then a TypeError will be thrown. If the index is out of range (with the exception of an index of 0), then a RangeError will be thrown.
Inserts an item in the list after the item at the specified index. If the list is empty and the index is zero, then it will add the item as the only item in the list. If the item passed in is undefined then a TypeError will be thrown. If the index is out of range (with the exception of an index of 0), then a RangeError will be thrown.