-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorted_list.rb
66 lines (58 loc) · 1.23 KB
/
sorted_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
require_relative 'generic_list'
##
# This is effectively an implementation of O(n**2) insertion sort.
# Not efficient for actually sorting values.
class SortedList < GenericList
##
# @yield a, b => -1, 0, 1
def initialize &comparator
@proc = comparator
super()
end
##
# Add new node to the list.
def add value
if @head.nil?
@length = 1
@head = GenericList::Node.new(value)
else
lst = @head
ptr = @head.nxt
while ptr && @proc.call(value, lst.value) <= 0
lst = ptr
ptr = ptr.nxt
end
@length += 1
lst.insert value
end
end
##
# Remove value from list.
#
# @param value value to delete
# @param n max number to remove (<1 means "all")
# @return value or nil
def delete value, n=0
return if @head.nil?
x = n # remember value (see end of function)
while @head && @head.value == value
@length -= 1
@head = @head.remove
n -= 1
return value if n == 0
any = true
end
ptr = @head
while ptr
if ptr.value == value
@length -= 1
ptr = ptr.remove
n -= 1
return value if n == 0
else
ptr = ptr.nxt
end
end
value if x != n
end
end