首页 > 代码库 > js:数据结构笔记8--集合
js:数据结构笔记8--集合
集合:唯一性,无序性;
基本结构:
function Set () { this.dataStore = []; this.add = add; this.remove = remove; this.contains =contains; this.show = show; } function contains(data) { var pos = this.dataStore.indexOf(data); if(pos > -1) { return true; } else { return false; } } function add(data) { var pos = this.dataStore.indexOf(data); if( pos < 0) { this.dataStore.push(data); return true; } else { return false; } } function remove(data) { var pos = this.dataStore.indexOf(data); if(pos > -1) { this.dataStore.splice(pos,1); return true; } else { return false; } } function show() { console.log(this.dataStore); }
操作:demo
集合的基本操作:并集,交集,补集;
并集:
新增: function union(set) { var tempSet = new Set(); for(var i = 0; i < this.dataStore.length; ++i) { tempSet.add(this.dataStore[i]); } for(var i = 0; i < set.dataStore.length; ++i) { var setData = http://www.mamicode.com/set.dataStore[i];>
操作: demo;
交集:
新增: function intersect(set) { var tempSet = new Set(); for(var i = 0; i < this.dataStore.length; ++i) { var thisData = http://www.mamicode.com/this.dataStore[i];>
操作: demo;
补集:
新增: function difference(set) { var tempSet = new Set(); for(var i = 0; i < this.dataStore.length; ++i) { var thisData = http://www.mamicode.com/this.dataStore[i];>
父集判断:
新增: function size() { return this.dataStore.length; } function subset(set) { if(this.size() > set.size()) { return false; } else { for(var i = 0; i < this.dataStore.length; ++i) { var thisData = http://www.mamicode.com/this.dataStore[i];>
操作:demo
js:数据结构笔记8--集合
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。