Class: DList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dsl/doubly_list.rb

Direct Known Subclasses

RList

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = nil) ⇒ DList

Returns a new instance of DList.



6
7
8
9
10
11
12
13
# File 'lib/dsl/doubly_list.rb', line 6

def initialize(content = nil)
    if (content != nil)
        insert_h(content)
else
    @head=nil
@end_=nil
end
end

Instance Attribute Details

#end_Object

Returns the value of attribute end_.



5
6
7
# File 'lib/dsl/doubly_list.rb', line 5

def end_
  @end_
end

#headObject

Returns the value of attribute head.



5
6
7
# File 'lib/dsl/doubly_list.rb', line 5

def head
  @head
end

Instance Method Details

#each(&block) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dsl/doubly_list.rb', line 135

def each(&block)
  aux=@head
  nodes = []
  while aux!=nil
    nodes << aux
    aux=aux["next"]
end
nodes.each do |member|
      block.call(member)
    end
  
  
end

#extract_allObject



99
100
101
102
103
104
# File 'lib/dsl/doubly_list.rb', line 99

def extract_all
    aux=@head
    while aux!=nil
    aux=extract_head
end
end

#extract_endObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dsl/doubly_list.rb', line 81

def extract_end
if @head==@end_
   nodo=@head
   @head=nil
   @end_=nil
   nodo
else
    nodo=@head
    while nodo["next"]!=@end_
    nodo=nodo["next"]
end
    @end_=nodo
    nodo=nodo["next"]
    @end_["next"]=nil
    nodo
   end 
end

#extract_headObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dsl/doubly_list.rb', line 66

def extract_head
if @head==@end_
        nodo=@head
        @head=nil
        @end_=nil
        nodo
else
    nodo=@head
    @head = @head["next"]
    @head["before"]=nil
    nodo
end

end

#insert_e(content) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/dsl/doubly_list.rb', line 56

def insert_e(content)
    if content.class.to_s == "Array"
        for i in 0..content.count-1
        insert_end(content[i])
    end
else
    insert_end(content)
end
end

#insert_empty(content) ⇒ Object



15
16
17
18
19
# File 'lib/dsl/doubly_list.rb', line 15

def insert_empty(content)
        node = Node.new(content)
        @head = node
        @end_ = node
end

#insert_end(content) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dsl/doubly_list.rb', line 43

def insert_end(content)
    if @head == nil
        insert_empty(content)
    else
        node =  Node.new(content)
        before=@end_
        @end_["next"]=node
        @end_=node
        @end_["before"]=before
    end
    
end

#insert_h(content) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/dsl/doubly_list.rb', line 33

def insert_h(content)
      if content.class.to_s == "Array"
          for i in 0..content.count-1
          insert_head(content[i])
      end
  else
      insert_head(content)
  end
end

#insert_head(content) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dsl/doubly_list.rb', line 21

def insert_head(content)
    if @head == nil
        insert_empty(content)
    else
        node = Node.new(content)
        node["next"] = @head
        @head = node
        @head["next"]["before"]=@head
    end
    
end

#reverseObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dsl/doubly_list.rb', line 121

def reverse
     aux = @end_
    cadena=""
while aux!=nil
    if aux["before"]!=nil
    cadena= cadena+"[#{aux["value"]}]<-->"
else
    cadena=cadena= cadena+"[#{aux["value"]}]"
end
    aux=aux["before"]
end
cadena
end

#to_sObject



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dsl/doubly_list.rb', line 106

def to_s
    aux = @head
    cadena=""
while aux!=nil
    if aux["next"]!=nil
    cadena= cadena+"[#{aux["value"]}]<-->"
else
    cadena=cadena= cadena+"[#{aux["value"]}]"
end
    aux=aux["next"]
end
cadena
end