Implementation of the collection
function Set () {this.datastore = []; This.add = add; This.remove = remove; this.size = size; this.union = Union; This.intersect = intersect; This.subset = subset; this.difference = difference; This.show = Show; This.contains = contains;} function Add (data) {if (this.dataStore.indexOf (data) < 0) {This.dataStore.push (data); return true; } else {return false; }}function Remove (data) {Let pos = this.dataStore.indexOf (data); if (pos >-1) {This.dataStore.splice (POS, 1); return true; } else {return false; }}function Show () {return this.datastore;} function contains (data) {if (this.dataStore.indexOf (data) >-1) {return true; } else {return false; }}function Union (SET) {Let Tempset = new set (); for (Let i = 0; i < this.dataStore.length; ++i) {Tempset.add (this.datastore[i]); } for (Let i = 0; i < set.dataStore.length; ++i) { if (!tempset.contains (set.datastore[i))) {TempSet.dataStore.push (set.datastore[i]); }} return tempset;} function intersect (SET) {Let Tempset = new set (); For (let I =0; i < this.dataStore.length; ++i) {if (Set.contains (This.datastore[i])) {Tempset.add (t His.datastore[i]); }} return tempset;} function subset (SET) {if (This.size () > Set.size ()) {return false; } else {for (Let i = 0; i < this.dataStore.length; ++i) {if (!set.contains (This.datastore[i])) { return false; }}} return true; function size () {return this.dataStore.length;} function difference (set) {Let Tempset = new set (); for (Let i = 0; i < this.dataStore.length; ++i) {if (!set.contains (This.datastore[i])) {Tempset.add (This.datastore[i]); }} return tempset;}
Practice one. Modify the collection class so that the elements inside are stored sequentially. Write a test code to test your changes.
// 修改add方法function add (data) { if (this.dataStore.indexOf(data) < 0) { this.dataStore.push(data); // 排序 this.dataStore = this.dataStore.sort((a, b) => a - b); return true; } else { return false; }}// 示例let s = new Set();s.add(23);s.add(3);s.add(2);s.add(24);s.add(73);console.log(s.show()); // [2, 3, 23, 24, 73]
Two. Add a higher (element) method for the collection class that returns the smallest of the elements that are larger than the passed-in element. Write a test code to test this method.
Set.prototype.higher = function (element) { let arr = []; for (let i = 0; i < this.dataStore.length; ++i) { if (this.dataStore[i] > element) { arr.push(this.dataStore[i]); } } return arr.sort((a, b) => a - b)[0];};// 示例let s = new Set();s.add(23);s.add(3);s.add(2);s.add(24);s.add(73);console.log(s.higher(20)); // 23console.log(s.higher(60)); // 73
Three. Add a lower (element) method for the collection class that returns the largest of the elements that are smaller than the passed-in element. Write a test code to test this method.
Set.prototype.lower = function (element) { let arr = []; for (let i = 0; i < this.dataStore.length; ++i) { if (this.dataStore[i] < element) { arr.push(this.dataStore[i]); } } return arr.sort((a, b) => b - a)[0];};// 示例let s = new Set();s.add(23);s.add(3);s.add(2);s.add(24);s.add(73);console.log(s.lower(20)); // 3console.log(s.lower(60)); // 24
JavaScript data structures and algorithms-collection exercises