JavaScript数据结构与算法1

92 阅读1分钟

参考

前言

  • 什么是数据结构?

    • 计算机中组织、存储数据的方式
  • 图书馆大量的图书应该如何组织,才能更方便的去找到某本指定的书?

    • 有空位置就将新书放入,但找书时要一本本找,很累的呀!
    • 先将书分类别,同一类书再按书名的拼音字母排序,找的时候先找类别,再找相应书名的字母,轻松! 结论:需要根据不同的应用情景,选择合适的数据结构来提高效率!
  • 什么是算法(Algorithm)?

    • 解决问题的方法
      • 按照书名的拼音字母顺序组织的图书,可以通过二分查找去找到相应书名的拼音字母,从而找到这本书
      • 从广州到上海,假设有1000km,中间有0.1km出了问题,如果我们0.1km的一段段查找,虽然能解决问题,但是效率太慢;此时用二分查找就能提高效率

栈(Stack)

  • 后进先出(先进后出)
  • 函数A调用B,B调用C,C调用D(函数调用栈)
    • 栈底 D->C->B->A 栈顶
function Stack(){
  // push(Element)
  this.items = [];
  Stack.prototype.push = function(Element){
    this.items.push(Element);
  }
  // pop()
  Stack.prototype.pop = function(){
    return this.items.pop();
  }
  // peek()
  Stack.prototype.peek = function(){
    return this.items[this.items.length - 1];
  }
  // isEmpty()
  Stack.prototype.isEmpty = function(){
    return this.items.length === 0;
  }
  // size()
  Stack.prototype.size = function(){
    return this.items.length;
  }
  // toString()
  Stack.prototype.toString = function(){
    var result = '';
    for(var i = 0; i < this.items.length; i ++){
      result += this.items[i];
    }
    return items;
  }
}

队列(Queue)

普通队列

  • 先进先出(First In First Out)
    • 只允许队头出,队尾进
// [ queue_tail...queue_head ]
function Queue(){
  this.items = [];
  // enqueue(element)
  Queue.prototype.enqueue = function(element){
    this.items.unshift(element);
  }
  // dequeue()
  Queue.prototype.dequeue = function(){
    return this.items.pop();
  }
  // front()
  Queue.prototype.front = function(){
    return this.items[this.items.length - 1];
  }
  // size()
  Queue.prototype.size = function(){
    return this.items.length;
  }
  // isEmpty()
  Queue.prototype.isEmpty = function(){
    return this.items.length === 0;
  }
  // toString():把数组元素连接成字符串
  // [1,2,3] -> "1,2,3"
  Queue.prototype.toString = function(){
    var resultString = '';
    for(var i = 0; i < this.items.length; i ++){
      resultString += this.items[i];
    }
    return resultString;
  }
}
  • 击鼓传花
    • 将数组的元素围成一个圈,从某个元素开始,数到第n个将此元素移除,然后从它的下一个元素继续数到第n个将此元素移除,直到数组中的元素只剩下一个,再将此元素的下标返回
1、将数组arr的元素加入到队列中
2、when 队列元素大于1个
       循环n - 1次
           每次将队头元素移到队尾
       将第n个元素从队列中移除
3、遍历arr数组与队头元素进行比较,返回此元素在数组arr的下标 
function passGame(arr,n){
  var len = arr.length;
  var queue = new Queue();
  for(var i = 0; i < len; i ++){
      queue.enqueue(arr[i]);
  } 
  
  while(queue.size() > 1){
    for(var i = 0; i < n - 1; i ++){
      queue.enqueue(queue.dequeue());
    }
    queue.dequeue();
  } 
  
  var temp = queue.front();
  for(var i = 0; i < len; i ++){
    if(arr[i] == temp){
      console.log(i);
      return i;
    }
  }
}
passGame(['moon','sun','moonlight','top','god'],3);
  • 'top'元素在原arr数组的下标是3

优先级队列(Priority Queue)

  • 元素除了携带数据,还携带优先级
    • 如:进程的动态优先级调度
    • 入队操作需要根据队列每个元素的优先级进行插入
      • 队列按优先级进行插入元素(enqueue)
function PriorityQueue(){
  this.items = [];
  // 内部类
  function QueueElement(element,priority){
    this.element = element;
    this.priority = priority;
  }
  
1if 队列没有元素
      then 队列尾部插入此元素
   else
      遍历队列,若待插入元素的优先级高于队列中的一个元素,将其插入到它的前面
      否则,将待插入元素从队尾插入队列  
  Queue.prototype.enqueue = function(element,priority){
    var queueElement = new QueueElement(element,priority);
    var len = this.items.length;
    
    if(this.items.length === 0){
      this.items.unshift(queueElement);
    }else{
      var flag = false;
      for(var i = 0; i < len; i ++){
        if(queueElement.priority > this.items[i]){
          this.items.splice(i + 1,0,queueElement);
          flag = true;
          break;
        }
      } 
      if(!flag){
        this.items.unshift(queueElement);
      }
    }
  }
  
  // 其它的方法跟普通队列一样
  ......
}