Object buckets.LinkedList
A linked list is a sequence of items arranged one after another. The size is not fixed and it can grow or shrink on demand. One of the main benefits of a linked list is that you can add or remove elements at both ends in constant time. One disadvantage of a linked list against an array is that it doesn’t provide constant time random access.
Constructor Name and Description |
---|
Creates an empty Linked List.
|
Method Name and Description |
---|
buckets.LinkedList.add(item, index)
Adds an element to the list.
|
buckets.LinkedList.clear()
Removes all the elements from the list.
|
buckets.LinkedList.contains(item, equalsFunction)
Returns true if the list contains the specified element.
|
buckets.LinkedList.elementAtIndex(index)
Returns the element at the specified position in the list.
|
buckets.LinkedList.equals(other, equalsFunction)
Returns true if the list is equal to another list.
|
buckets.LinkedList.first()
Returns the first element in the list.
|
buckets.LinkedList.forEach(callback)
Executes the provided function once per element present in the list in order.
|
buckets.LinkedList.indexOf(item, equalsFunction)
Returns the index of the first occurrence of the
specified element, or -1 if the list does not contain the element.
|
buckets.LinkedList.isEmpty()
Returns true if the list contains no elements.
|
buckets.LinkedList.last()
Returns the last element in the list.
|
buckets.LinkedList.remove(item, equalsFunction)
Removes the first occurrence of the specified element in the list.
|
buckets.LinkedList.removeElementAtIndex(index)
Removes the element at the specified position in the list.
|
buckets.LinkedList.reverse()
Reverses the order of the elements in the linked list (makes the last
element first, and the first element last).
|
buckets.LinkedList.size()
Returns the number of elements in the list.
|
buckets.LinkedList.toArray()
Returns an array containing all the elements in the list in proper
sequence.
|
- Parameters:
- {Object} item
- Element to be added.
- {number=} index
- Optional index to add the element. If no index is specified the element is added to the end of the list.
- Returns:
- {boolean} True if the element was added or false if the index is invalid or if the element is undefined.
If the elements inside the list are not comparable with the === operator, a custom equals function should be provided to perform searches, that function must receive two arguments and return true if they are equal, false otherwise. Example:
var petsAreEqualByName = function(pet1, pet2) { return pet1.name === pet2.name; }
- Parameters:
- {Object} item
- Element to search for.
- {function(Object|Object):boolean=} equalsFunction
- Optional function used to check if two elements are equal.
- Returns:
- {boolean} True if the list contains the specified element, false otherwise.
- Parameters:
- {number} index
- Desired index.
- Returns:
- {*} The element at the given index or undefined if the index is out of bounds.
- Parameters:
- {buckets.LinkedList} other
- The other list.
- {function(Object|Object):boolean=} equalsFunction
- Optional function to check if two elements are equal. If the elements in the lists are custom objects you should provide a custom equals function, otherwise the === operator is used to check equality between elements.
- Returns:
- {boolean} true if the list is equal to the given list.
- Returns:
- {*} The first element in the list or undefined if the list is empty.
- Parameters:
- {function(Object):*} callback
- Function to execute, it is invoked with one argument: the element value, to break the iteration you can optionally return false inside the callback.
If the elements inside the list are not comparable with the === operator, a custom equals function should be provided to perform searches, that function must receive two arguments and return true if they are equal, false otherwise. Example:
var petsAreEqualByName = function(pet1, pet2) { return pet1.name === pet2.name; }
- Parameters:
- {Object} item
- Element to search for.
- {function(Object|Object):boolean=} equalsFunction
- Optional function used to check if two elements are equal.
- Returns:
- {number} The index in the list of the first occurrence of the specified element, or -1 if the list does not contain the element.
- Returns:
- {boolean} true if the list contains no elements.
- Returns:
- {*} The last element in the list or undefined if the list is empty.
If the elements inside the list are not comparable with the === operator, a custom equals function should be provided to perform searches, that function must receive two arguments and return true if they are equal, false otherwise. Example:
var petsAreEqualByName = function(pet1, pet2) { return pet1.name === pet2.name; }
- Parameters:
- {Object} item
- Element to be removed from the list, if present.
- equalsFunction
- Returns:
- {boolean} True if the list contained the specified element.
- Parameters:
- {number} index
- Given index.
- Returns:
- {*} Removed element or undefined if the index is out of bounds.
- Returns:
- {number} The number of elements in the list.
- Returns:
- {Array.<*>} An array containing all the elements in the list, in proper sequence.