Class: References::List
- Inherits:
-
Object
- Object
- References::List
- Includes:
- Enumerable
- Defined in:
- lib/references/list.rb
Overview
Is a double-linked list
Instance Method Summary (collapse)
- - (Object) each
- - (Object) head
-
- (List) initialize(*vals)
constructor
A new instance of List.
- - (Object) last
- - (Object) length
- - (Object) pushEnd(val)
- - (Object) put(val)
- - (Object) takeFirst
- - (Object) takeLast
- - (Object) to_s
Constructor Details
- (List) initialize(*vals)
Returns a new instance of List
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/references/list.rb', line 8 def initialize(*vals) if vals.length == 0 @head = nil @last = nil else for val in vals do pushEnd val end end end |
Instance Method Details
- (Object) each
19 20 21 22 23 24 25 |
# File 'lib/references/list.rb', line 19 def each aux = @head while aux != nil yield aux.value aux = aux.next end end |
- (Object) head
93 94 95 96 97 98 99 |
# File 'lib/references/list.rb', line 93 def head if @head != nil @head.value else nil end end |
- (Object) last
101 102 103 104 105 106 107 |
# File 'lib/references/list.rb', line 101 def last if @last != nil @last.value else nil end end |
- (Object) length
83 84 85 86 87 88 89 90 91 |
# File 'lib/references/list.rb', line 83 def length aux = @head count = 0 while aux!=nil count = count + 1 aux = aux.next end count end |
- (Object) pushEnd(val)
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/references/list.rb', line 72 def pushEnd(val) if @last==nil @head = @last = Node.new(val,nil,nil) else aux = Node.new(val,nil,@last) @last.next = aux @last = aux end nil end |
- (Object) put(val)
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/references/list.rb', line 61 def put(val) if @head == nil @head = @last = Node.new(val,nil,nil) else aux = Node.new(val,@head, nil) @head.back = aux @head = aux end nil end |
- (Object) takeFirst
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/references/list.rb', line 27 def takeFirst if (@head==@last) if @head == nil return nil end value = @head.value @head = nil @last = nil return value else value = @head.value @head = @head.next @head.back = nil return value end end |
- (Object) takeLast
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/references/list.rb', line 44 def takeLast if (@head==@last) if @last == nil return nil end value = @last.value @head = nil @last = nil return value else value = @last.value @last = @last.back @last.next = nil return value end end |
- (Object) to_s
109 110 111 |
# File 'lib/references/list.rb', line 109 def to_s (self.sort.map { |x| x.formatAPA }).join("\n") end |