Repository navigation
Expand file tree
/
Copy pathlinkedList.rb
More file actions
111 lines (83 loc) · 1.46 KB
/
Copy pathlinkedList.rb
File metadata and controls
111 lines (83 loc) · 1.46 KB
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
101
102
103
104
105
106
107
108
109
110
111
# Fibonacci Series
class LinkedList
attr_accessor :size, :head, :tail
def initialize
@head = nil
@tail = nil
@size = 0
end
def append(node)
if (@size == 0)
@head = node
@tail = node
@size += 1
elsif (@size > 0)
@tail.next_node = node
@tail = node
@size += 1
end
end
def prepend(node)
if (@size == 0)
@head = node
@tail = node
@size += 1
else
node.next_node = @head
@head = node
@size += 1
end
end
def to_s
outputStr = "";
i = 0
node = @head
while i < @size do
outputStr = outputStr + "( "+ node.value.to_s + " ) -> ";
i +=1
node = node.next_node
end
outputStr = outputStr + "nil"
puts outputStr;
end
def at(index)
counter = index;
tempNode = @head;
while counter > 0 do
outputVal = tempNode.value
counter = counter - 1;
tempNode = tempNode.next_node
end
puts outputVal.to_s;
end
def pop
counter = @size;
tempNode = @head;
while counter - 1 > 0 do
tempNode = tempNode.next_node;
counter = counter - 1;
end
tempNode.next_node = nil
@size = @size - 1;
end
def contains? (varCheck)
result = false;
list.each do |listVar|
if (listVar.value == varCheck)
result = true;
end
end
return result;
end
def find(data)
result = nil;
count = 0;
list.each do |listVar|
if (listVar.value == varCheck)
result = count;
end
count = count+1;
end
return result;
end
end