資料結構與演算法 簡單棧實現及其應用

2021-10-11 07:46:41 字數 2968 閱讀 9512

棧的實現

棧的鍊錶實現

棧的陣列實現

以上測操作都是以常數時間執行,非常快速的只有乙個操作,以下我們用陣列實現自己的乙個棧:

/**

* @author liaojiamin

* @date:created in 18:07 2020/12/2

*/public

class

mystack

/*** 擴容

* */

public

void

resize()

}/**

* 新增推入資料

* */

public object push

(e e)

/** * 刪除,推出資料

* */

public e pop()

e e =

(e)elementdata[top--];

position --

;return e;

}/**

* 檢視棧頂元素

* */

public e gettop()

/** * 判斷是否為空

* */

public

boolean

isempty()

/** * 當前棧儲存資料個數

* */

public

intsize()

/** * 列印輸出

*/public

void

printmystack()

top = temp;

}public

static

void

main

(string[

] args)

integer result =0;

while

(result != null)

}}

應用

平衡符號

字尾表示式

4.99

+5.99

+6.99

*2

4.99

5.99

+6.992*

+

中綴表示式轉字尾表示式
a+b*c+

(d*e+f)

*g

123

*45*

6+7*

++

演算法分析

**展示

* 中序轉後序表示式的各種邏輯判斷

*/public

static

intflag

(string s, mystack

action)

if(s.

matches

("(\\*)|(\\/)|(\\+)|(\\-)"))

elseif(

prior

(s, action.

gettop()

))else}if

(s.matches

("\\("))

if(s.

matches

("\\)"))

return0;

}/**

* @return 優先順序

* @author: liaojiamin

* @description:s 操作符, top 棧頂操作符

*/public

static

boolean

prior

(string s, string top)

if(s.

matches

("(\\*)|(\\/)"

)&& top.

matches

("(\\+)|(\\-)"))

return

false;}

public

static double evalutepostfix

(mystack

mystack)

else

if(temp.

matches

("(\\*)|(\\/)|(\\+)|(\\-)"))

double a = double.

valueof

(newstack.

pop())

;double b = double.

valueof

(newstack.

pop())

;switch

(temp)}}

return double.

valueof

(newstack.

pop())

;}public

static

void

main

(string[

] args)

}應用三

JavaScript資料結構與演算法 棧及其應用

1.使用es6模擬棧的實現 let stack function push element pop isempty size peer clear print tostring return stack 2.棧應用之進製轉換 問題描述 將十進位制轉換為其他進製資料。function baseconv...

資料結構與演算法 棧及利用棧實現簡單括號匹配

棧 一種有次序的資料項集合,在棧中,資料項的加入和移除 都僅發生在同一端,距離棧底越近的資料項,留在棧中的時間 就越長。這種次序通常稱為 後進先出lifo last in first out 利用棧實現簡單括號匹配 遇到左括號則入棧 s.push symbol else 遇到右括號時,棧為空,則說明...

資料結構與演算法《棧》

概念 棧 stack 又名堆疊,它是一種運算受限的線性表。其限制是僅允許在表的一端進行插入和刪除運算。這一端被稱為棧頂,相對地,把另一端稱為棧底。向乙個棧插入新元素又稱作進棧 入棧或壓棧,它是把新元素放到棧頂元素的上面,使之成為新的棧頂元素 從乙個棧刪除元素又稱作出棧或退棧,它是把棧頂元素刪除掉,使...