Class: LinkedList::List
- Inherits:
-
Object
- Object
- LinkedList::List
- Includes:
- Enumerable
- Defined in:
- lib/linkedList/linkedList.rb
Instance Attribute Summary collapse
-
#First ⇒ Object
First = Tail || Last = Head.
-
#Last ⇒ Object
First = Tail || Last = Head.
-
#Size ⇒ Object
readonly
Returns the value of attribute Size.
Instance Method Summary collapse
- #[](i) ⇒ Object
- #at(i) ⇒ Object
- #each ⇒ Object
- #empty ⇒ Object
-
#initialize ⇒ List
constructor
A new instance of List.
- #pop_back ⇒ Object
- #pop_front ⇒ Object
- #push_back(v) ⇒ Object
- #push_front(v) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ List
Returns a new instance of List.
20 21 22 23 |
# File 'lib/linkedList/linkedList.rb', line 20 def initialize @Last = @First = nil @Size = 0 end |
Instance Attribute Details
#First ⇒ Object
First = Tail || Last = Head
18 19 20 |
# File 'lib/linkedList/linkedList.rb', line 18 def First @First end |
#Last ⇒ Object
First = Tail || Last = Head
18 19 20 |
# File 'lib/linkedList/linkedList.rb', line 18 def Last @Last end |
#Size ⇒ Object (readonly)
Returns the value of attribute Size.
19 20 21 |
# File 'lib/linkedList/linkedList.rb', line 19 def Size @Size end |
Instance Method Details
#[](i) ⇒ Object
81 82 83 |
# File 'lib/linkedList/linkedList.rb', line 81 def [] (i) at(i) end |
#at(i) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/linkedList/linkedList.rb', line 71 def at(i) aux = @First j = 0 while(j < i) do aux = aux.next j = j + 1 end aux end |
#each ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/linkedList/linkedList.rb', line 10 def each aux = @First while aux != nil yield aux.value aux = aux.next end end |
#empty ⇒ Object
86 87 88 |
# File 'lib/linkedList/linkedList.rb', line 86 def empty @Size == 0 end |
#pop_back ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/linkedList/linkedList.rb', line 57 def pop_back if(@Size == 1) aux = @Last @First = @Last = nil else aux = @Last @Last = @Last.back @Last.next = nil end @Size = @Size - 1 aux end |
#pop_front ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/linkedList/linkedList.rb', line 44 def pop_front if(@Size == 1) aux = @First @First = @Last = nil else aux = @First @First = @First.next end @Size = @Size - 1 aux end |
#push_back(v) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/linkedList/linkedList.rb', line 25 def push_back(v) if(@Size == 0) @Last = @First = Node.new(v,nil,nil) else @Last.next = Node.new(v,nil,@Last) @Last = @Last.next end @Size = @Size + 1 end |
#push_front(v) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/linkedList/linkedList.rb', line 35 def push_front(v) if(@Size == 0) @Last = @First = Node.new(v,nil,nil) else @First = Node.new(v,@First, nil) end @Size = @Size + 1 end |
#to_s ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/linkedList/linkedList.rb', line 90 def to_s aux = [] aux = sort # if (aux.each aux2 = '' for i in 0..aux.size-1 aux2 += aux[i].get_APA end aux2 end |