JavaScript data structures and algorithms-list exercises

Source: Internet
Author: User
Tags prev

Implementing a list Class
List class function lists () {this.listsize = 0;//List of elements This.pos = 0;//The current position of the list this.datastore = [];//Initialize an empty array to Save list element this.clear = clear;    Clears all the elements in the list this.find = find;    this.tostring = toString;    This.insert = insert;    This.append = append;    This.remove = remove;    This.front = Front;    This.end = end;    This.prev = prev;    This.next = Next;    This.hasnext = Hasnext;    This.hasprev = Hasprev;    this.length = length; This.currpos = Currpos;    Returns the current position of the list this.moveto = MoveTo; This.getelement = getelement; Returns the current position of the element this.contains = contains;} Append: add element function append to list {this.datastore[this.listsize++] = element;} Find: Finds an element in the list indexof?function find (Element) {for (Let i = 0; i < this.dataStore.length; i++) {if (t        His.datastore[i] = = = Element) {return i; }} return-1;}    Remove: Remove the element from the list function remove (element) {Let Foundat = This.find (Element);  if (Foundat >-1) {      This.dataStore.splice (Foundat, 1);        this.listsize--;    return true; } return false;} Length: How many elements in a list are different from listsize? Function length () {return this.listsize;} ToString: Displays the elements in the list function toString () {return this.datastore;}    Insert: Inserts an element into the list function insert (element, after) {Let Insertpos = This.find (after);        if (Insertpos >-1) {This.dataStore.splice (Insertpos + 1, 0, Element);        this.listsize++;    return true; } return false;}    Clear: Clears all elements in the list function clear () {delete this.datastore;    this.dataStore.length = 0; this.listsize = This.pos = 0;}        Contains: Determines whether the given value is find?function in the list contains (element) {for (Let i = 0; i < this.dataStore.length; i++) {        if (this.datastore[i] = = = Element) {return true; }} return false;} Traversing list function front () {this.pos = 0;} Function End () {this.pos = this.listsize-1;} Function prev () {--this.pos;} function Next () {if (tHis.pos < this.listsize) {++this.pos; }}function Currpos () {return this.pos;} function MoveTo (position) {this.pos = position;} function GetElement () {return this.datastore[this.pos];} function Hasnext () {return this.pos < this.listsize;} function Hasprev () {return this.pos >= 0;}
Practice one. Adds a method to insert an element into a list that performs an insert operation only if the element being inserted is greater than all elements in the list. Here the greater than there are multiple meanings, for numbers, it refers to the size of the number, for the letter, it refers to the alphabet in the order of occurrence.
List.prototype.insertThen = function (Element) {Let type = typeof element;        if (type = = = ' number ') {//debugger; for (Let i = 0; i < this.dataStore.length; i++) {if (typeof this.datastore[i] = = = ' Number ' && eleme                NT > This.datastore[i]) {//satisfies both the number and then the inserted element is greater than the element within the array this.append (elements);            return true;        }}} else {let NewArr = This.dataStore.filter (val) = {return typeof val!== ' number ';        }). Concat ([element]). sort ();            if (newarr.indexof (element) = = = (newarr.length-1)) {this.append (element);        return true; }} return false;};/ /Example Let Datathen = new List ();D atathen.append (' Mazey ');D atathen.append (' Cherrie ');D atathen.append (' Luna ');D Atathen.append (' John ');D atathen.append (' July ');D Atathen.append (;D atathen.append); Console.log ( Datathen.tostring ()); ["Mazey", "Cherrie", "Luna", "John", "July", 73]datathen.insertthen (;D Atathen.insertthen (), Console.log (Datathen.tostring ()); ["Mazey", "Cherrie", "Luna", "John", "July", all, 99]datathen.insertthen (' Jay ');D atathen.insertthen (' Zero '); Console.log (Datathen.tostring ()); ["Mazey", "Cherrie", "Luna", "John", "July", "Zero")
Two. Add a method that inserts an element into the list, which performs the insert operation only if the element to be inserted is smaller than all elements in the list.
List.prototype.insertThen = function (Element) {Let type = typeof element;        if (type = = = ' number ') {//debugger; for (Let i = 0; i < this.dataStore.length; i++) {if (typeof this.datastore[i] = = = ' Number ' && eleme                NT < This.datastore[i]) {//satisfies both the number and then the inserted element is greater than the element within the array this.append (elements);            return true;        }}} else {let NewArr = This.dataStore.filter (val) = {return typeof val!== ' number ';        }). Concat ([element]). sort ();            if (newarr.indexof (element) = = = 0) {this.append (element);        return true; }} return false;};/ /Example Let Datathen = new List ();D atathen.append (' Mazey ');D atathen.append (' Cherrie ');D atathen.append (' Luna ');D Atathen.append (' John ');D atathen.append (' July ');D Atathen.append (;D atathen.append); Console.log ( Datathen.tostring ()); ["Mazey", "Cherrie", "Luna", "John", "July", 73]datathen.insertthen (;D) atathen.inserTthen (n); Console.log (datathen.tostring ()); ["Mazey", "Cherrie", "Luna", "John", "July", all, 12]datathen.insertthen (' Jay ');D atathen.insertthen (' Zero ');D Atathen.insertthen (' Ada '); Console.log (datathen.tostring ()); ["Mazey", "Cherrie", "Luna", "John", "July", "the Ada")
Three. Create the person class, which is used to hold people's names and gender information. Create a list that contains at least 10 person objects. Write a function to display all people with the same gender in the list.
 function Person () {this.list = []; This.save = save; This.showsamegender = Showsamegender;} Save name and gender function save (name, gender) {Let littlecase = {name, gender}; This.list.push (littlecase);} Human function Showsamegender (gender) {let RET = [] showing the same gender; Let len = this.list.length; while (len--) {if (This.list[len].gender = = = Gender) {Ret.push (this.list[len].name); }} return ret;} Example let people = new person ();p eople.save (' Mazey ', ' Male ');p eople.save (' John ', ' Male ');p eople.save (' Zero ', ' Male '); People.save (' July ', ' Male ');p eople.save (' Bob ', ' Male ');p eople.save (' Ada ', ' female ');p eople.save (' Cherrie ', ' female ');p eople.save (' Luna ', ' female ');p eople.save (' Lucy ', ' female ');p eople.save (' June ', ' female '); Console.log ( people.list); Console.log (People.showsamegender (' Male ')); ["Bob", "July", "Zero", "John", "Mazey"]console.log (People.showsamegender (' female ')); ["June", "Lucy", "Luna", "Cherrie", "Ada"] 

JavaScript data structures and algorithms-list exercises

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.