List

DataStructure. List

List pattern as representation of an ordered sequence of values where the same value may appear many times. Because Lists have an order it is possible to insert elements at the start, middle or at the end.

  • Push - Add value to the end
  • Pop - Remove value from the end
  • Unshift - Add value to the start
  • Shift - Remove value from the start

Constructor

new List()

Start with an empty array and store the length of the "list". Note that we to store the length separately because the "memory" doesn't have a length that can be read.
Source:

Methods

get(position) → {null|*}

Getter to retrieve list elements.
Parameters:
Name Type Description
position Number Position of element in List
Source:
Returns:
Type
null | *

pop() → {*}

Pop items off of the end of our list. Similar to push all we need to do is remove the value at the address at the end of our list. Then just decrement length.
Source:
Returns:
Type
*

push(value)

Add and item to the end of the list. Adding a value after the end of our list. Just add the value and increment the length.
Parameters:
Name Type Description
value * Value that should be added to the list
Source:

shift() → {*}

Shift function to move in the opposite direction. Delete the first value and slide through every single item in the ist to move it down one address. [x, a, b, c, d, e] 1 2 3 4 5 0 1 2 3 4 [a, b, c, d, e]
Source:
Returns:
Type
*

unshift(value)

In order to add a new item at the beginning of the list, we need to make room for the value at the start by sliding all of the values over by one. [a, b, c, d, e] 0 1 2 3 4 1 2 3 4 5 [x, a, b, c, d, e] In order to slide all of the items over we need to iterate over each one moving the prev value over.
Parameters:
Name Type Description
value *
Source: