JavaScript Data Structure-list

Source: Internet
Author: User


Application Scenario: Shopping list


A list that does not contain any elements is called an empty list


The complete abstract data type definition for the list:

Listsize (attributes) Number of elements in the list
POS (properties) The current position of the list
Length (property) Returns the number of elements in a list
Clear (method) Empty all the elements in the list
ToString (method) Returns the string form of the list
GetElement (method) Returns the element at the current position
Insert (method) Inserting a new element after an existing element
Append (method) Add a new element at the end of the list
Remove (method) Remove an element from the list
Front (method) Moves the current position of a list to the first element
End (method) Move the current position of the list to the last element
Prev (method) Move the current position back one
Next (method) Move the current position forward one
Currpos (method) Returns the current position of the list
MoveTo (method) Moves the current position to the specified position


Function list ()  {        this.listSize = 0;         this.append = append;         this.remove = remove;        this.pos =  0;        this.dataStore = [];         this.find = find;         this.tostring = tostring;        this.length =  length;        this.clear = clear;         this.insert = insert;         this.front = front;        this.end = end;         this.prev = prev;        this.next = next;         this.currPos = currPos;         this.moveTo = moveTo;         this.getelement = getelement;        this.contains =  contains;      }      function append ( Element)  {        this.dataStore[this.listSize++] =  Element;      }      function find (Element)  {        var dataStore = this.dataStore;         for (var i = 0, len = datastore.length;  i < len; i++) &nbsP {          if (datastore[i] == element)  {             return i;           }        }         return -1;      }       function remove (Element)  {        var fountat  = this.find (Element);         if (fountat > -1)  {          this.datastore.splice (fountAt, 1);           --this.listSize;           return true;        }         return false;      }      function  Length ()  {        return this.listSize;       }      function tostring ()  {         return this.dataStore;      }       function clear ()  {        delete  this.datastore;        this.datastore = [];         this.listSize = this.pos = 0;       }      function insert (Element, after)  {         var insertpos = this.find (after);         if (InseRTPOS&NBSP;&GT;&NBSP;-1)  {           This.dataStore.splice (insertpos + 1, 0, element); ++this.listsize;           return true;        }         return false;      }       function front ()  {         this.pos = 0;      }      function  End ()  {        this.pos = this.listSize - 1;       }      function prev ()  {         if (this.pos > 0)  {--this.pos;         }  &nBsp;   }      function next ()  {         if (this.pos < this.listsize - 1)  {++this.pos;         }      }       Function currpos ()  {        return this.pos;       }      function moveto (position)  {         this.pos = position;      }       function getelement ()  {         return this.dataStore[this.pos];      }       function contains (Element)  {        var  Datastore = thiS.datastore;        for (var i = 0, len =  datastore.length; i < len; i++)  {           if (datastore[i] == element)  {             return true;          }         }        return false;       }      var names = new  List ();       names.append ("1");       names.append ( "2");       names.append ("3");       names.append ("4 ");       names.append (" 5 ");       names.append (" 6 ") ;    &Nbsp;  names.insert ("7",  "6");       names.prev ();       console.log (Names.contains ("7"));



JavaScript Data Structure-list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.