Skip to content

Commit ef0b119

Browse files
committed
Inserido algoritmo de lista duplamente encadeada em ruby e modificaçao do README.
1 parent f261b45 commit ef0b119

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
4040
| [Grafo][20] | [C/C++](/src/c/Grafos.c) | Java | Python | Go | Ruby | Javascript | Pascal |
4141
| [Lista Circular Ligada][52] | [C/C++](/src/c/ListaCircularLigada.c) | Java | [Python](/src/python/lista_encadeada_circular.py) | Go | Ruby | Javascript | Pascal |
4242
| [Lista Encadeada][22] | C/C++ | Java | [Python](/src/python/lista_encadeada.py) | Go | [Ruby](/src/ruby/Lista_encadeada.rb) | [Javascript](/src/javascript/ListaSimplesmenteEncadeada.js) | Pascal |
43-
| [Lista Duplamente Encadeada][23] | [C/C++](/src/c/ListaDuplamenteEncadeada.c) | [Java](/src/java/ListaDuplamenteEncadeada.java) | [Python](/src/python/lista_duplamente_encadeada.py) | Go | Ruby | [Javascript](/src/javascript/ListaDumplamenteEncadeada.js) | Pascal |
43+
| [Lista Duplamente Encadeada][23] | [C/C++](/src/c/ListaDuplamenteEncadeada.c) | [Java](/src/java/ListaDuplamenteEncadeada.java) | [Python](/src/python/lista_duplamente_encadeada.py) | Go | [Ruby](/src/ruby/Lista_duplamente_encadeada.rb)| [Javascript](/src/javascript/ListaDumplamenteEncadeada.js) | Pascal |
4444
| [Lista Ligada Não Ordenada][24] | [C/C++](/src/c/ListaLigadaNaoOrdenada.c) | Java | Python | Go | Ruby | Javascript | Pascal |
4545
| Lista Sequencial Ordenada | [C/C++](/src/c/ListaSequencialOrdenada.c) | Java | [Python](/src/python/lista_sequencial_ordenada.py) | Go | Ruby | Javascript | Pascal |
4646
| [Pilha][31] | [C/C++](/src/c/Pilha.c) | [Java](/src/java/Pilha.java) | [Python](/src/python/pilha.py) | Go | [Ruby](/src/ruby/Pilha.rb) | [Javascript](/src/javascript/Pilha.js) | [Pascal](/src/pascal/pilha.pas) |
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
class Node
2+
attr_accessor :data, :next, :previous
3+
def initialize(data)
4+
@data = data
5+
@next = nil
6+
@previous = nil
7+
end
8+
end
9+
10+
class DoublyLinkedList
11+
def initialize
12+
@head = nil
13+
end
14+
15+
def append(data)
16+
if @head.nil?
17+
@head = Node.new(data)
18+
else
19+
tail_node = find_tail
20+
new_node = Node.new(data)
21+
tail_node.next = new_node
22+
new_node.previous = tail_node
23+
end
24+
end
25+
26+
def find_tail
27+
current_tail = @head
28+
until current_tail.next.nil?
29+
current_tail = current_tail.next
30+
end
31+
return current_tail
32+
end
33+
34+
35+
def delete(value)
36+
if @head.data == value
37+
@head = @head.next
38+
@head.previous = nil
39+
else
40+
target = find(value)
41+
if target.next.nil?
42+
target.previous.next = nil
43+
else
44+
target.previous.next = target.next
45+
target.next.previous = target.previous
46+
end
47+
end
48+
end
49+
50+
def find(value)
51+
current = @head
52+
until current.data == value
53+
current = current.next
54+
end
55+
current
56+
end
57+
58+
def insert_after(target, value)
59+
current = find(target)
60+
temp = current.next
61+
current.next = Node.new(value)
62+
current.next.next = temp
63+
current.next.previous = current
64+
current.next.next.previous = current.next
65+
end
66+
67+
def print_list
68+
current = @head
69+
puts "List: "
70+
until current.nil?
71+
print "#{current.data} "
72+
current = current.next
73+
end
74+
end
75+
end
76+
77+
list = DoublyLinkedList.new
78+
list.append(10)
79+
list.append(20)
80+
list.append(30)
81+
list.insert_after(20, 22)
82+
list.print_list
83+
list.delete(30)
84+
list.print_list
85+
list.delete(10)
86+
list.print_list

0 commit comments

Comments
 (0)