掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
本文轉(zhuǎn)載自微信公眾號「泰斗賢若如」,作者AhuntSun 。轉(zhuǎn)載本文請聯(lián)系泰斗賢若如公眾號。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比鐘樓網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式鐘樓網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋鐘樓地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。
一、前言
1.1.什么是數(shù)據(jù)結(jié)構(gòu)?
數(shù)據(jù)結(jié)構(gòu)就是在計算機中,存儲和組織數(shù)據(jù)的方式。
例如:圖書管理,怎樣擺放圖書才能既能放很多書,也方便取?
主要需要考慮兩個問題:
「常見的數(shù)據(jù)結(jié)構(gòu):」
「注意」:數(shù)據(jù)結(jié)構(gòu)與算法與語言無關(guān),常見的編程語言都有「直接或間接」的使用上述常見的數(shù)據(jù)結(jié)構(gòu)。
1.2.什么是算法?
算法(Algorithm)的定義
算法通俗理解:解決問題的辦法/步驟邏輯。數(shù)據(jù)結(jié)構(gòu)的實現(xiàn),離不開算法。
二、棧結(jié)構(gòu)(Stack)
2.1.簡介
數(shù)組是一個線性結(jié)構(gòu),并且可以在數(shù)組的「任意位置」插入和刪除元素。而「棧和隊列」就是比較常見的「受限的線性結(jié)構(gòu)」。如下圖所示:
image-20200226131817102
棧的特點為「先進后出,后進先出」(LIFO:last in first out)。
「程序中的棧結(jié)構(gòu):」
3.練習:題目:有6個元素6,5,4,3,2,1按順序進棧,問下列哪一個不是合法的出棧順序?
題目所說的按順序進棧指的不是一次性全部進棧,而是有進有出,進棧順序為6 -> 5 -> 4 -> 3 -> 2 -> 1。
解析:
「棧常見的操作:」
2.2.封裝棧類
「代碼實現(xiàn):」
- // 封裝棧類
- function Stack(){
- // 棧中的屬性
- this.items =[]
- // 棧的相關(guān)操作
- // 1.push():將元素壓入棧
- //方式一(不推薦):給對象添加方法,其他對象不能復(fù)用
- // this.push = () => {
- // }
- //方式二(推薦):給Stack類添加方法,能夠多對象復(fù)用
- Stack.prototype.push = function(element) {
- // 利用數(shù)組item的push方法實現(xiàn)Stack類的pop方法
- this.items.push(element)
- }
- // 2.pop():從棧中取出元素
- Stack.prototype.pop = () => {
- // 利用數(shù)組item的pop方法實現(xiàn)Stack類的pop方法
- return this.items.pop()
- }
- // 3.peek():查看一下棧頂元素
- Stack.prototype.peek = () => {
- return this.items[this.items.length - 1]
- }
- // 4.isEmpty():判斷棧是否為空
- Stack.prototype.isEmpty = () => {
- // 兩個小時的教訓啊不是this.length(不是Stack對象的length,Stack類沒有l(wèi)ength屬性啊),而是 Stack類中定義的數(shù)組items才有l(wèi)ength屬性呀
- return this.items.length == 0
- }
- // 5.size():獲取棧中元素的個數(shù)
- Stack.prototype.size = () => {
- return this.items.length
- }
- // 6.toString():以字符串形式輸出棧內(nèi)數(shù)據(jù)
- Stack.prototype.toString = () => {
- //希望輸出的形式:20 10 12 8 7
- let resultString = ''
- for (let i of this.items){
- resultString += i + ' '
- }
- return resultString
- }
- }
「測試代碼:」
- // 棧的使用
- let s = new Stack()
- s.push(20)
- s.push(10)
- s.push(100)
- s.push(77)
- console.log(s) //65
- console.log(s.pop()); //68
- console.log(s.pop()); //69
- console.log(s.peek()); //71
- console.log(s.isEmpty()); //72
- console.log(s.size()); //74
- console.log(s.toString()); //75
「測試結(jié)果:」
image-20200305205050816
「棧結(jié)構(gòu)的簡單應(yīng)用:」
利用棧結(jié)構(gòu)的特點封裝十進至轉(zhuǎn)換為二進至的函數(shù):
- //簡單應(yīng)用:
- //封裝函數(shù):將十進制轉(zhuǎn)成二進制(十轉(zhuǎn)二的運算最后倒敘取余的特點符合棧'先進后出')
- let dec2bin = decNumber => {
- //1.定義一個棧對象,保存余數(shù)
- var stack = new Stack()
- // 2.循環(huán)操作
- while(decNumber > 0){
- // 2.1.獲取余數(shù)并放入棧中
- stack.push(decNumber % 2)
- // 2.2.獲取整除后的結(jié)果作為下一次運算的數(shù)字(floor:向下取整)
- decNumber = Math.floor(decNumber / 2)
- }
- // 3.從棧中取出0和1
- let binaryString = '';
- let a = stack.items.length
- while(stack.items.length != 0){
- binaryString += stack.pop();
- }
- return binaryString;
- }
- //測試代碼
- console.log(dec2bin(10)); //103
- console.log(dec2bin(100)); //104
- console.log(dec2bin(1000)); //105
「測試結(jié)果:」
image-20200305205547226

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流