List of JS Data Structures

1. Concept

A list is a collection of ordered data, each of which is called an element. In js, elements in the list can be of any data type. Any number of elements can be saved in the list (which is limited by program memory in actual use).

2. Attributes and Methods

There are some common attributes and methods in the list. The following are listed one by one:

  • listSize: Number of elements in a list
  • position (attribute): the current location of the list
  • clear (method): clear all elements in the list
  • GetElement (Method): Get the element at the current location
  • Find (method): Find an element in the list
  • Insert (method): insert a new element after an existing element
  • Appnd: Insert new elements at the end of the list
  • remove (method): delete elements from the list
  • front: Move the current position of the list to the first element
  • end (method): Move the current position of the list to the last element
  • Prev: Move the current position one bit forward
  • next: move the current position one bit backwards
  • MoeTo: Move the current location to the specified location

3.JS code implements a list class

Let's first define a list class and wrap the attributes and methods of the list in the class.

function List(){
    this.dataSet = []//Initialize an empty array to save list elements
    this.pos = 0//Initialize an attribute to represent the current location of the list
    this.listSize = 0 //Initialize an attribute to represent the number of elements in the list
    this.clear = clear;//Clear out the elements in the list
    this.find = find;//Find an element on the list
    this.getElement = getElement;//Returns the element of the current location
    this.toString = toString;//Show lists as strings
    this.insert = insert;//Insert a new element after the current element
    this.append = append;//Insert new elements at the end of the list
    this.remove = remove;//Delete elements from the list
    this.front = front;//Move the current position to the head of the list
    this.end = end;//Move the current position to the last element
    this.prev = prev; = next
    this.currPos = currPos;
    this.moveTo = moveTo;
    this.contains = contains;

With this class, we can write out the complete constructor step by step.

3.1 Implementation of Clean Method

function clear(){
    delete this.dataSet;//Delete the original dataSet
    this.dataSet = [];//Assign a new array to dataSet
    this.pos = this.listSize = 0;//Initialize the current position and length of the array to 0

3.2 Implementation of find Method

function find(element){
    for(var i=0;i<this.dataSet.length;i++){
            return i //If an element is found, the location of the element is returned.
    return -1 //If no element is found in the dataSet, return - 1


3.3 Implementation of getElement Method

function getElement(pos){
    return this.dataSet[this.pos]

3.4 Implementation of toString Method

function toString(){
    return this.dataSet

3.5 Implementing insert method

function insert(element){

3.6 append Method

function append(element){

3.7 Implementation of remote method

function remove(element){
    var nowPos = this.find(element);
        this.listSize—-; //Delete the element and subtract listSize by one
        return true //Delete successfully, return true
    return false //If deletion is unsuccessful, return false

3.8 Method of Implementing Six Operational Positions

function front(){
    this.pos = 0;

function end(){
    this.pos = this.listSize-1
function prev(){

function next(){

function currPos(){
    return this.pos

function moveTo(newPos){
        this.pos = newPos

3.9 Implementation of contains Method

function contains(element){
    var res = this.find(element);
        return true
        return false

Tags: Javascript Attribute

Posted on Tue, 03 Sep 2019 08:25:09 -0700 by Ayien