1 function Stack() { 2 //存储元素数组 3 var aElement = new Array(); 4 /* 5 * @brief: 元素入栈 6 * @param: 入栈元素列表 7 * @return: 堆栈元素个数 8 * @remark: 1.Push方法参数可以多个 9 * 2.参数为空时返回-1 10 */ 11 Stack.prototype.Push = function (vElement) { 12 if (arguments.length == 0) 13 return -1; 14 //元素入栈 15 for (var i = 0; i < arguments.length; i++) { 16 aElement.push(arguments[i]); 17 } 18 return aElement.length; 19 } 20 /* 21 * @brief: 元素出栈 22 * @return: vElement 23 * @remark: 当堆栈元素为空时,返回null 24 */ 25 Stack.prototype.Pop = function () { 26 if (aElement.length == 0) 27 return null; 28 else 29 return aElement.pop(); 30 } 31 /* 32 * @brief: 获取堆栈元素个数 33 * @return: 元素个数 34 */ 35 Stack.prototype.GetSize = function () { 36 return aElement.length; 37 } 38 /* 39 * @brief: 返回栈顶元素值 40 * @return: vElement 41 * @remark: 若堆栈为空则返回null 42 */ 43 Stack.prototype.GetTop = function () { 44 if (aElement.length == 0) 45 return null; 46 else 47 return aElement[aElement.length - 1]; 48 } 49 /* 50 * @brief: 将堆栈置空 51 */ 52 Stack.prototype.MakeEmpty = function () { 53 aElement.length = 0; 54 } 55 /* 56 * @brief: 判断堆栈是否为空 57 * @return: 堆栈为空返回true,否则返回false 58 */ 59 Stack.prototype.IsEmpty = function () { 60 if (aElement.length == 0) 61 return true; 62 else 63 return false; 64 } 65 /* 66 * @brief: 将堆栈元素转化为字符串 67 * @return: 堆栈元素字符串 68 */ 69 Stack.prototype.toString = function () { 70 var sResult = (aElement.reverse()).toString(); 71 aElement.reverse() 72 return sResult; 73 } 74 }
1 function Queue() { 2 //存储元素数组 3 var aElement = new Array(); 4 /* 5 * @brief: 元素入队 6 * @param: vElement元素列表 7 * @return: 返回当前队列元素个数 8 * @remark: 1.EnQueue方法参数可以多个 9 * 2.参数为空时返回-1 10 */ 11 Queue.prototype.EnQueue = function (vElement) { 12 if (arguments.length == 0) 13 return -1; 14 //元素入队 15 for (var i = 0; i < arguments.length; i++) { 16 aElement.push(arguments[i]); 17 } 18 return aElement.length; 19 } 20 /* 21 * @brief: 元素出队 22 * @return: vElement 23 * @remark: 当队列元素为空时,返回null 24 */ 25 Queue.prototype.DeQueue = function () { 26 if (aElement.length == 0) 27 return null; 28 else 29 return aElement.shift(); 30 31 } 32 /* 33 * @brief: 获取队列元素个数 34 * @return: 元素个数 35 */ 36 Queue.prototype.GetSize = function () { 37 return aElement.length; 38 } 39 /* 40 * @brief: 返回队头素值 41 * @return: vElement 42 * @remark: 若队列为空则返回null 43 */ 44 Queue.prototype.GetHead = function () { 45 if (aElement.length == 0) 46 return null; 47 else 48 return aElement[0]; 49 } 50 /* 51 * @brief: 返回队尾素值 52 * @return: vElement 53 * @remark: 若队列为空则返回null 54 */ 55 Queue.prototype.GetEnd = function () { 56 if (aElement.length == 0) 57 return null; 58 else 59 return aElement[aElement.length - 1]; 60 } 61 /* 62 * @brief: 将队列置空 63 */ 64 Queue.prototype.MakeEmpty = function () { 65 aElement.length = 0; 66 } 67 /* 68 * @brief: 判断队列是否为空 69 * @return: 队列为空返回true,否则返回false 70 */ 71 Queue.prototype.IsEmpty = function () { 72 if (aElement.length == 0) 73 return true; 74 else 75 return false; 76 } 77 /* 78 * @brief: 将队列元素转化为字符串 79 * @return: 队列元素字符串 80 */ 81 Queue.prototype.toString = function () { 82 var sResult = (aElement.reverse()).toString(); 83 aElement.reverse() 84 return sResult; 85 } 86 }
1 function List() { 2 //存储元素数组 3 var aElement = new Array(); 4 5 //列表的大小 6 List.prototype.size = function () { 7 return aElement.length; 8 } 9 //向列表添加一个元素 10 List.prototype.add = function (element) { 11 aElement.push(element); 12 } 13 //批量添加元素到列表 14 List.prototype.addAll = function (list) { 15 if (list && isList(list)) { 16 var size = list.size(); 17 var element; 18 for (var i = 0; i < size; i++) { 19 element = list.get(i); 20 aElement.push(element); 21 } 22 } 23 } 24 //转化成数组 25 List.prototype.toArray = function () { 26 return aElement; 27 } 28 //移除数组(根据下标) 29 List.prototype.remove = function (index) { 30 if (index >= this.size()) { 31 index = this.size() - 1; 32 } 33 aElement.splice(index, 1); 34 } 35 List.prototype.get = function (index) { 36 return aElement[index]; 37 } 38 //清除列表原色 39 List.prototype.clear = function () { 40 array = new Array(); 41 } 42 //是否为空列表 43 List.prototype.isEmpty = function () { 44 return this.size() === 0; 45 } 46 //是否包含某个元素 47 List.prototype.contains = function (element) { 48 return this.indexOf(element) >= 0; 49 } 50 //判断列表的某个元素的第一个元素在列表的下标位置 51 //-1则表示没有这个元素 52 List.prototype.indexOf = function (element) { 53 var size = this.size(); 54 if (null === element) { 55 for (var i = 0; i < size; i++) { 56 if (aElement[i] === null) { 57 return i; 58 } 59 } 60 } else { 61 for (var i = 0; i < size; i++) { 62 if (aElement[i] === element) { 63 return i; 64 } 65 } 66 } 67 return -1; 68 } 69 var isList = function (list) { 70 return list instanceof List; 71 } 72 }
1 function StringBuilder() { 2 //存储元素数组 3 var aElement = new Array(); 4 5 //追加数据到StringBuilder类 6 StringBuilder.prototype.append = function (element) { 7 aElement.push(element); 8 } 9 //转换成String 10 StringBuilder.prototype.toString = function () { 11 return aElement.join(""); 12 } 13 }
function Map() { //初始化map集合 var aElement = new Array(); var _size = 0; var _keySet = new Array(); //map的大小 Map.prototype.size = function () { return this._size; } //将数据放入map中 Map.prototype.put = function (key, value) { if (!this.containsKey(key)) { aElement[key] = value; this._size++; } } //根据key获得map的其中一个value Map.prototype.get = function (key) { return aElement[key]; } //map是否为空集合 Map.prototype.isEmpty = function () { return this._size === 0; } //map集合是否包含key Map.prototype.containsKey = function (key) { return aElement[key] !== undefined ? true : false; } //移除指定的key Map.prototype.remove = function (key) { if (this.containsKey(key)) { delete aElement[key]; this._size--; } } //移除所有集合 Map.prototype.clear = function () { //初始化map集合 aElement = new Array(); this._size = 0; this._keySet = new Array(); } //map集合转化成Array Map.prototype.toArray = function () { return aElement; } }