forked from Ada-C5/list-implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked-list.rb
100 lines (84 loc) · 2.48 KB
/
linked-list.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Quick Example of a Self Referential Data Structure in Ruby
# NODE -> contains a value and a pointer to (next_node)
# LinkedList -> This class holds the linked list functions - adding a node, traversing and displaying the linked list
class Node
attr_accessor :value, :next_node
def initialize(val,next_in_line=null)
@value = val
@next_nodex = next_in_line
puts "Initialized a Node with value: " + value.to_s
end
end
class LinkedList
def initialize(val)
# Initialize a new node at the head
@head = Node.new(val,nil)
end
def add(value)
# Traverse to the end of the list
# And insert a new node over there with the specified value
current = @head
while current.next_node != nil
current = current.next_node
end
current.next_node = Node.new(value,nil)
self
end
def delete(val)
current = @head
if current.value == val
# If the head is the element to be delete, the head needs to be updated
@head = @head.next_node
else
# ... x -> y -> z
# Suppose y is the value to be deleted, you need to reshape the above list to :
# ... x->z
# ( and z is basically y.next_node )
current = @head
while (current != nil) && (current.next_node != nil) && ((current.next_node).value != val)
current = current.next_node
end
if (current != nil) && (current.next_node != nil)
current.next_node = (current.next_node).next_node
end
end
end
def display
# Traverse through the list till you hit the "nil" at the end
current = @head
full_list = []
while current.next_node != nil
full_list += [current.value.to_s]
current = current.next_node
end
full_list += [current.value.to_s]
puts full_list.join("->")
end
def include?(key)
end
def size
end
def max
end
end
# Initializing a Linked List with a node containing value (5)
ll = LinkedList.new(5)
# Adding Elements to Linked List
ll.add(10)
ll.add(20)
# Display the Linked List
puts "Displaying Linked List:"
ll.display
puts "Delete 10 and then display the linked list:"
ll.delete(10)
ll.display
=begin
Output:
Initialized a Node with value: 5
Initialized a Node with value: 10
Initialized a Node with value: 20
Displaying Linked List:
5->10->20
Delete 10 and then display the linked list:
5->20
=end