-
Notifications
You must be signed in to change notification settings - Fork 15
/
stack.rb
35 lines (29 loc) · 1.32 KB
/
stack.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
## A stack - last-in first out (LIFO) data structure
require 'pp' ## pp = pretty print
pp stack = [] #=> []
pp stack.empty? #=> true
pp stack.push( 1 ) #=> [1]
pp stack.empty? #=> false
pp stack.push( 2 ) #=> [1, 2]
pp stack.push( 3 ) #=> [1, 2, 3]
pp stack.push( "<signature>" ) #=> [1, 2, 3, "<signature>"]
pp stack.push( "<pubkey>") #=> [1, 2, 3, "<signature>", "<pubkey>"]
pp stack.pop #=> "<pubkey>"
pp stack #=> [1, 2, 3, "<signature>"]
pp stack.pop #=> "<signature>"
pp stack #=> [1, 2, 3]
pp stack.push( 4 ) #=> [1, 2, 3, 4]
pp stack.push( 5 ) #=> [1, 2, 3, 4, 5]
pp stack.pop #=> 5
pp stack #=> [1, 2, 3, 4]
pp stack.pop #=> 4
pp stack #=> [1, 2, 3]
pp stack.pop #=> 3
pp stack #=> [1, 2]
pp stack.pop #=> 2
pp stack #=> [1]
pp stack.empty? #=> false
pp stack.pop #=> 1
pp stack #=> []
pp stack.empty? #=> true
pp stack.pop #=> nil