JS-常用的数据结构之栈和队列

具体实现

  

 1 // js栈实现  使用数组
 2 // 栈的原则先进后出(LIFO),每次操作的是栈顶元素
 3 function Stack(){
 4     var items = [];
 5     
 6     this.push = function(element){
 7         items.push(element);
 8     }; //添加一个新元素到栈顶。
 9 
10     this.pop = function(){
11         return items.pop();
12     };//移除栈顶的元素,同时返回被移除的元素。
13 
14     this.peek = function(){
15         return items[items.length - 1];
16     };//返回栈顶的元素,不对栈做任何修改。
17 
18     this.isEmpty = function(){
19         return items.length == 0;
20     };//如果栈里没有任何元素就返回true,否则返回false。
21 
22     this.clear = function(){
23         items = [];
24     };//移除栈里的所有元素。
25 
26     this.size = function(){
27         return items.length;
28     };//返回栈里的元素个数。
29 }
30 
31 var a1 = new Stack();
32 a1.push(1);
33 
34 a1.peek();
35 
36 // 使用栈实现十进制转二进制
37 //十进制整数转换为二进制整数采用"除2取余,逆序排列"法。
38 //具体做法是:用2整除十进制整数,可以得到一个商和余数;
39 //再用2去除商,又会得到一个商和余数,
40 //如此进行,直到商为小于1时为止,然后把先得到的余数作为二进制数的低位有效位,后得到的余数作为二进制数的高位有效位,依次排列起来
41 function divideBy2(decNumber){
42     var remStack = new Stack();
43     var rem = null;
44     binarString = "";
45 
46     while (decNumber > 0 ){
47         rem = Math.floor(decNumber % 2);
48         remStack.push(rem);
49         decNumber = Math.floor(decNumber / 2);
50     }
51 
52     while(!remStack.isEmpty()){
53         binarString += remStack.pop().toString();
54     }
55     return binarString;
56 }
57 
58 // 十进制转16 比转2多了一步,数值到字母的映射
59 function baseConverter (decNumber, base) {
60     var remStack = new Stack(),
61         rem,
62         baseString = "",
63         digits = "0123456789ABCDEF";
64 
65     while (decNumber > 0) {
66         rem = Math.floor(decNumber % base);
67         remStack.push(rem);
68         decNumber = Math.floor(decNumber / base);
69     }
70 
71     while (!remStack.isEmpty()) {
72         baseString += digits[remStack.pop()];
73     }
74     return baseString;
75 }
76 
77 document.write(divideBy2(12));

队列实现

参考链接  https://blog.csdn.net/weixin_38181873/article/details/78149442

  1 function Queue () {
  2     var items = [];
  3 
  4     // this.enqueue  和 enqueue的区别??? 测试发现不加this 调用不了此函数
  5     this.enqueue = function(element){
  6         items.push(element);
  7     }; //向队列尾部添加一个新的项。
  8     this.dequeue = function(){
  9         return items.shift();
 10     };//移除队列的第一项,并返回被移除的项。
 11     this.front = function(){
 12         return items[0];
 13     };//返回队列中的第一个元素,队列不做任何变动。
 14     this.isEmpty = function(){
 15         return items.length == 0;
 16     };//判断队列是否为空。
 17     this.size = function(){
 18         return items.length;
 19     };//返回队列长度。
 20     this.print = function () {
 21         console.log(items.toString());
 22     };
 23 }
 24 
 25 
 26  var aa = new Queue();
 27  aa.enqueue(1);
 28 
 29  // 优先队列
 30  function PriorityQueue () {
 31     var items = [];
 32 
 33     // priority越大,优先级越高
 34     function QueueElement(element, priority){
 35         this.element = element;
 36         this.priority = priority;
 37     }
 38 
 39     this.enqueue = function(element){
 40         var qe = new QueueElement(element, priority);
 41 
 42         if (this.isEmpty()){
 43             items.push(qe);
 44         }else{
 45             // splice() 方法可删除从 index 处开始的零个或多个元素,并且用参数列表中声明的一个或多个值来替换那些被删除的元素, 要删除的项目数量。如果设置为 0,则不会删除项目
 46 
 47             //循环比较所添加元素和队列中元素优先级
 48             //当找到比要添加元素优先级更小的项时,就把新元素插入到它之前(priority越大,优先级越高)
 49             var isAdd = false;
 50             for(let i = 0; i < items.length; i++){
 51                 if (qe.priority > item[i].priority){
 52                     items.splice(i, 0, qe);
 53                     isAdd = true;
 54                     break;
 55                 }
 56             }
 57             //优先级相同,遵循先进先出原则
 58             if (!isAdd){
 59                 items.push(qe);
 60             }
 61         }
 62     }; //向队列尾部添加一个新的项。
 63     this.dequeue = function(){
 64         return items.shift();
 65     };//移除队列的第一项,并返回被移除的项。
 66     this.front = function(){
 67         return items[0];
 68     };//返回队列中的第一个元素,队列不做任何变动。
 69     this.isEmpty = function(){
 70         return items.length == 0;
 71     };//判断队列是否为空。
 72     this.size = function(){
 73         return items.length;
 74     };//返回队列长度。
 75     this.print = function () {
 76         console.log(items.toString());
 77     };
 78  }
 79 
 80 
 81  // 击鼓传花游戏  假设每一轮数5次
 82 
 83  function hotPotato(nameList, num){
 84     var que = new Queue();
 85     var eliminated = "";
 86     // 先全部进队
 87     for(let i = 0; i < nameList.length; i++){
 88         que.enqueue(nameList[i]);
 89     }
 90 
 91     // 循环执行直到队列只剩下一个元素
 92     while (que.size() > 1){
 93         for (let j = 0; j < num; j++){
 94             // 先出队,再进队
 95             var temp = que.dequeue();
 96             que.enqueue(temp);
 97         }
 98         // 一次循环结束,队首的人被淘汰
 99         eliminated = queue.dequeue();
100         console.log(eliminated + "被淘汰");
101     }
102 
103     return queue.dequeue();
104  }
105 
106 var names = ["小明", "小花", "小黄", "小易", "小华"];
107 var winner = hotPotato(names, 5);//可以改变传入数字,模拟不同场景
108 console.log("胜利者:" + winner);
原文地址:https://www.cnblogs.com/orxx/p/10279478.html