JS数据结构与算法(二)
一些数据结构的封装,未完待续
/**
* 栈的封装
*/
function Stack() {
var items = [];
this.push = function (element) {
items.push(element);
};
this.pop = function (element) {
return items.pop(element);
};
this.peek = function () {
return items[items.length - 1];
};
this.size = function () {
return items.length;
};
this.isEmpty = function () {
return (!items.length);
};
this.clear = function () {
items = [];
};
this.print = function () {
console.log(items.toString);
};
}
/**
* 十进制数转换为二进制数
* @param {Number} decNumber 十进制数
* @return {String} 二进制数
*/
function divideBy2(decNumber) {
var binaryString = '',
tmp,
myStack = new Stack();
if (!myStack.isEmpty()) {
myStack.clear();
}
while (decNumber > 0) {
tmp = decNumber & 1;
myStack.push(tmp);
decNumber >>= 1;
}
while (!myStack.isEmpty()) {
binaryString += myStack.pop().toString();
}
return binaryString;
}
// console.log(divideBy2(233));
/**
* 十进制数转换为n(n >= 1 && n <= 16) 进制数
* @param {Number} decNumber 十进制数
* @param {Number} base n进制
* @return {String} n进制数
*/
function baseConverter(decNumber, base) {
var resString = '',
tmp,
myStack = new Stack(),
hashArr = '0123456789ABCDEF';
if (!myStack.isEmpty()) {
myStack.clear();
}
while (decNumber > 0) {
tmp = Math.floor(decNumber % base);
myStack.push(tmp);
decNumber = Math.floor(decNumber / base);
}
while (!myStack.isEmpty()) {
resString += hashArr[myStack.pop()];
}
return resString;
}
// console.log(baseConverter(100345, 8));
/**
* 队列分装
*/
function Queue() {
var items = [];
this.enQueue = function (element) {
items.push(element);
};
this.deQueue = function (element) {
return items.shift();
};
this.front = function () {
return items[0];
};
this.size = function () {
return items.length();
};
this.isEmpty = function () {
return (!items.length);
};
this.clear = function () {
items = [];
};
this.print = function () {
console.log(items.toString());
};
}
var myQueue = new Queue();
// console.log(myQueue.isEmpty());
/**
* 优先队列,权重越小越优先
*/
function PriorityQueue() {
var items = [];
function QueueElement(element, priority) {
this.element = element;
this.priority = priority;
}
this.enQueue = function (element) {
items.push(element);
};
this.deQueue = function (element) {
return items.shift();
};
this.front = function () {
return items[0];
};
this.size = function () {
return items.length();
};
this.isEmpty = function () {
return (!items.length);
};
this.clear = function () {
items = [];
};
this.print = function () {
console.log(items);
};
this.enQueue = function (element, priority) {
var queueElemnet = new QueueElement(element, priority),
i,
len,
isAdd = false;
if (this.isEmpty()) {
items.push(queueElemnet);
} else {
for (i = 0, len = items.length; i < len; i++) {
if (queueElemnet.priority < items[i].priority) {
items.splice(i, 0, queueElemnet);
isAdd = true;
break;
}
}
if (!isAdd) {
items.push(queueElemnet);
}
}
};
}
// var priorityQueue = new PriorityQueue();
// priorityQueue.enQueue('John', 2);
// priorityQueue.enQueue('Jack', 1);
// priorityQueue.enQueue('Cam', 1);
// priorityQueue.print();
//
/**
* 集合封装
*/
function Set() {
var items = {},
length = 0;
this.has = function ( ) {
return items.hasOwnProperty(value);
};
this.add = function (value) {
if (!this.has(value)) {
items[value] = value;
length++;
return true;
} else {
return false;
}
};
this.remove = function (value) {
if (!this.has(value)) {
delete items[value];
length--;
return true;
} else {
return false;
}
};
this.clear = function () {
items = {};
};
this.size = function () {
return length;
};
this.values = function () {
var keys = [],
key;
for (key in items) {
keys.push(key);
}
return keys;
};
this.print = function () {
console.log(this.values());
};
this.getUnion = function (otherSet) {
var unionSet = new Set(),
values = null,
i,
len;
values = this.values().concat(otherSet.values());
for (i = 0, len = values.length; i < len; i++) {
unionSet.add(values[i]);
}
return unionSet;
};
this.getIntersection = function (otherSet) {
var intersectionSet = new Set(),
values = null,
i,
len;
values = this.values();
for (i = 0, len = values.length; i < len; i++) {
if (otherSet.has(values[i])) {
intersectionSet.add(values[i]);
}
}
return intersectionSet;
};
this.getDifference = function (otherSet) {
var differenceSet = new Set(),
values = null,
i,
len;
values = this.values();
for (i = 0, len = values.length; i < len; i++) {
if (!otherSet.has(values[i])) {
differenceSet.add(values[i]);
}
}
return differenceSet;
};
this.isSubset = function (otherSet) {
var subsetFlag = true,
values = null,
i,
len;
values = this.values();
for (i = 0, len = values.length; i < len; i++) {
if (!otherSet.has(values[i])) {
subsetFlag = false;
break;
}
}
return subsetFlag ? true : false;
};
}
// var setA = new Set();
// setA.add(1);
// setA.add(2);
// setA.add(3);
// var setB = new Set();
// setB.add(4);
// setB.add(2);
// setB.add(3);
// var setUnionAB = setA.getUnion(setB);
// setUnionAB.print();
// var setInsersectionAB = setA.getIntersection(setB);
// setInsersectionAB.print();
// var setDifferenceAB = setA.getDifference(setB);
// setDifferenceAB.print();
// console.log(setB.isSubset(setA));
/**
* Map封装
*/
function Map() {
var items = {},
length = 0;
this.has = function (key) {
return key in items;
};
this.set = function (key, valus) {
items[key] = value;
};
this.get = function (key) {
return this.has(key) ? items[key] : false;
};
this.values = function () {
var values = [],
k;
for (k in items) {
if (this.has(k)) {
values.push(items[k]);
}
}
return values;
};
this.keys = function () {
var keys = [],
k;
for (k in items) {
if (this.has(k)) {
keys.push(k);
}
}
return keys;
};
this.remove = function (key) {
if (this.has(key)) {
delete items[key];
length--;
return true;
} else {
return false;
}
};
this.clear = function () {
items = {};
};
this.size = function () {
return length;
};
this.getItems = function () {
return items;
};
this.print = function () {
console.log(this.getItems());
};
}