上一篇講過隊列(queue),隊列就像是居民樓裏的垃圾管道,從樓道的垃圾管道的入口處將垃圾扔進去,清潔工會從一樓垃圾管道的出口處將垃圾拿走。每一層的垃圾通道入口與一樓的垃圾管道出口之間都造成了一個隊列,先被扔進垃圾道里的垃圾會先到達垃圾通道的出口,即:先進先出。數組
棧是一種更簡單的數據結構,若是隊列是垃圾通道,那棧就是垃圾桶,先扔進垃圾桶的垃圾都被壓桶底了,反而是後仍進去的垃圾會先被倒出來。這種後進先出的數據結構,就是——棧(stack)。數據結構
隊列的三要素是隊伍,隊頭,隊尾。dom
在PowerShell中能夠用一個動態數組表示隊伍,用兩個變量來表示隊頭和隊尾之於數組中的位置。ui
隊列(垃圾道)有兩頭,數據從一頭進,另外一頭出。進的那頭叫隊尾,出的那頭叫隊頭。this
棧(垃圾桶)只有一頭,從哪頭進就從哪頭出,進的那頭叫頂(垃圾桶都是立着放)。spa
在PowerShell中能夠用一個動態數組來表示棧,棧頂之於數組的位置就用一個變量來記錄。3d
$stack = New-Object System.Collections.ArrayList #Create a stack of numbers. $flag = $true while($flag) { $input = Read-Host "Put a number into the stack" $stack.Add([int] $input) $choice = Read-Host "Press 'c' to continue, any other key to quit" if($choice -ne 'c') { $flag = $false } } $top = $stack.count-1 $stack_top = $stack[$top] #Show the top number of this stack. Write-Host "The top of the stack is:" -ForegroundColor green Write-Host $stack_top -ForegroundColor green
以上這段代碼簡述了棧的建立和棧頂元素的查看方法。
如今來舉個現實世界中的例子,你馬上就會明白什麼是棧——發撲克牌。code
首先建立一套撲克牌:blog
#Create a set of pokers. $pokers = New-Object System.Collections.ArrayList $pokerModel = @{"pokers"="2,3,4,5,6,7,8,9,10,J,Q,K,A,SmallKing,BigKing";"colors"="spade,heart,club,diamond"} $pokerEles = $pokerModel.Item("pokers").Split(",") $pokerColors = $pokerModel.Item("colors").Split(",") foreach($pokerEle in $pokerEles) { if(($pokerEle -eq "SmallKing") -or ($pokerEle -eq "BigKing")) { $pokers.Add($pokerEle) } else { foreach($color in $pokerColors) { $newPoker = $color + "_" + $pokerEle $pokers.Add($newPoker) } } }
而後對這幅撲克進行洗牌(將第一張牌隨機放入整副牌中,重複10000次):隊列
#Shuffle. for($i=0;$i -le 10000;$i++) { $randomPlace = Get-Random -min 0 -max $pokers.count $selectedPoker = $pokers[0] $pokers.RemoveAt(0) $pokers.Insert($randomPlace, $selectedPoker) }
而後咱們把洗好的牌放進「棧」中(發牌器就是棧),像下圖這個樣子。而後開始發牌(每次發牌都是從棧頂pop出一張)——
#Push the cards into the stack. $myStack = new-object System.Collections.Stack foreach($poker in $pokers) { $myStack.Push($poker) } #Start the deal. Write-Host "---Start dealing---" -ForegroundColor green $flag = $true while($flag) { $myStack.Pop() $choice = Read-Host "Press 'c' to continue, any other key to quit" if($choice -ne 'c') { $flag = $false } else { Write-Host "---Continue dealing---" -ForegroundColor yellow } }
運行結果以下:
到此,一個發紙牌的過程就完成了,而這個發牌器就是棧,裝牌的過程就是入棧的過程,發牌的過程就是出棧的過程。