js數(shù)據(jù)結(jié)構(gòu)與算法_棧

1.解決問題方法的效率,跟數(shù)據(jù)的組織方式是有關(guān)系的,比如圖書館借書,需要刷門禁,找書架,拿到書找管理員登記等等流程
2.解決問題方法的效率,和空間的利用率有關(guān)系 (遞歸打印1~100000(N), 方法一是循環(huán),方法二是遞歸,結(jié)果爆掉了直接輸出100000)
3.解決問題方法的效率,也和算法的巧妙運用程度有關(guān)系

數(shù)據(jù)結(jié)構(gòu)+算法=程序

抽象數(shù)據(jù)類型(ATD)

1.抽象具有普適性,不會因為我之前學的是C語言,換成JAVA,PHP就不適用了。
2.抽象來源于實際,高于實際,通過從一個問題抽象出普適的理論,就可以解決其他相通的問題

JavaScript基礎(chǔ)

操作符

算數(shù)操作符、賦值操作符、比較操作符、邏輯操作符、位操作符、一元操作符和其他操作符 typeof,delete

真值和假值

在大多數(shù)編程語言中,布爾值true和false僅僅表示true/false

數(shù)值類型 | 轉(zhuǎn)換成布爾值
-|-|-
undefined | false
null | false
布爾值 | true就是true,false就是false
數(shù)字 | +0,-0或NaN都是false,其他都是true
字符串 | 字符串為空(length為0)就是false,其他true
對象 | true

function testTruthy(val){ 
 return val ? console.log('truthy') : console.log('falsy'); 
} 

相等操作符

類型x | 類型y | 結(jié)果
-|-|-|-
null | undefined | true
undefined | null | true
數(shù)字 | 字符串 | x==toNumber(y)
字符串 | 數(shù)字 | toNumber(x)==y
布爾值 | 任何類型 | toNumber(x)==y
任何類型 | 布爾值 | x==toNumber(y)
字符串或數(shù)字 | 對象 | x == toPrimitive(y)
對象 | 字符串或數(shù)字 | toPrimitive(x) == y

如果x和y是相同類型,JavaScript會比較它們的值或?qū)ο笾怠F渌麤]有列在這個表格中的情況都會返回false。

條件語句

循環(huán)

do...while循環(huán)和while循環(huán)很相似。區(qū)別是

//在while循環(huán)里,先進行條件判斷再執(zhí)行循環(huán)體中的代碼,
var i = 0;
while (i < 10) {
    console.log(i);
    i++;
}


//而在do...while循環(huán)里,是先執(zhí)行循環(huán)體中的代碼再判斷循環(huán)條件
var i = 0;
do {
    console.log(i);
    i++;
} while (i < 10)

function Book(title, pages, isbn){ //{1} 
 this.title = title; 
 this.pages = pages; 
 this.isbn = isbn; 
} 
Book.prototype.printTitle = function(){ 
 console.log(this.title); 
}; 

//我們可以用ES6把語法簡化如下:
class Book { //{2} 
 constructor (title, pages, isbn) { 
 this.title = title; 
 this.pages = pages; 
 this.isbn = isbn; 
 } 
 printIsbn(){ 
 console.log(this.isbn); 
 } 
} 

class ITBook extends Book { //{1} 
 constructor (title, pages, isbn, technology) { 
 super(title, pages, isbn); //{2} 
   this.technology = technology; 
 } 
 printTechnology(){ 
  console.log(this.technology); 
 } 
} 
let jsBook = new ITBook('學習JS算法', '200', '1234567890', 'JavaScript'); 
console.log(jsBook.title); 
console.log(jsBook.printTechnology()); 


/**
 * 堆棧:是具有一定操作約束的線性表,只在一端做插入和刪除
 * 案例說明:日常使用的是中綴表達式,而后綴表達式是利用堆棧
 * 特點:后進先出 LIFO,新插入的元素在棧頂,舊元素接近棧底
 * 抽象描述:
 *  1. 生成空的堆棧,最大長度: createStack , maxSize
 *  2. 判斷堆棧是否已滿  isFull()
 *  3. 壓入堆棧  push()
 *  4. 判斷堆棧是否為空 isEmpty()
 *  5. 刪除并返回棧頂元素  Pop()
 * 
 *  以下是基于ES5實現(xiàn)
 *
 */
function Stack() {
    let items = [];  //私有變量,多個Stack實例會創(chuàng)建多個items副本
    this.size = function () {
        return items.length;
    }
    this.push = function (element) {  //向棧添加元素
        items.push(element);
    },
        this.pop = function () {  //從棧中移除元素
            return items.pop();
        },
        this.peek = function () {  //查看棧頂元素
            return items[items.length - 1];
        },
        this.isEmpty = function () {  //檢查棧是否為空
            return items.length == 0;
        },
        this.clear = function () {  //清空棧
            items = []
        },
        this.print = function () {
            console.log(items.toString())
        }
}

let stack = new Stack();
console.log(stack);
stack.push('A')
stack.push('B')
stack.push('C')
stack.pop() //移除棧頂元素 C
stack.print() // A,B
console.log(`是否為空: ${stack.isEmpty()}`);  // false
console.log(`Stack的size: ${stack.size()}`);  // 2
console.log(`棧頂元素: ${stack.peek()}`);  //此時棧頂元素 B




ES6實現(xiàn)的方式

//第一種方式Symbol
let _items = Symbol()  //利用symbol創(chuàng)建私有屬性
class Stack {
    constructor() {
        this[_items] = [];
    }
    push(element) {
         this[_items].push(element)
    }
    pop(){
        this[_items].pop()
    }
    isEmpty(){
        return this[_items].length===0
    }
}

//第二種方式利用weakMap
let Stack = (function() {
  const items = new WeakMap(); //weakMap可以存儲鍵值對,其中鍵是對象,值可以是任意數(shù)據(jù)類型
  class Stack {
    constructor() {
      items.set(this, []);
    }
    push(element) {
      let s = items.get(this);
      s.push(element);
    }
    pop() {
      let s = items.get(this);
      let r = s.pop();
      return r;
    }
    isEmpty() {
      return items.get(this).length === 0;
    }
    print() {
      console.log(items.get(this));
    }
  }
  return Stack; //返回類的一個實例
})();

案例

function divideBy2(decNumber) {
  var remStack = new Stack();
  var rem = ""; //余數(shù)
  var binaryString = ""; //轉(zhuǎn)成二進制后的
  while (decNumber > 0) {
    rem = Math.floor(decNumber % 2);
    remStack.push(rem);
    decNumber = Math.floor(decNumber / 2);
  }
  while (!remStack.isEmpty()) {
    binaryString += remStack.pop();
  }
  return binaryString;
}

console.log(divideBy2(100000));

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容