-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.rb
More file actions
44 lines (34 loc) · 1.04 KB
/
example.rb
File metadata and controls
44 lines (34 loc) · 1.04 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
# frozen_string_literal: true
require 'dlinked' # Add this line!
list = DLinked::List.new
list.append(2).append(3).prepend(1)
puts "After operations: #{list}"
puts "Size: #{list.size}"
# More examples...
list << 10 << 20 << 30
puts "List: #{list}"
puts "First: #{list.first}, Last: #{list.last}"
list.each { |v| puts " Value: #{v}" }
if __FILE__ == $PROGRAM_NAME
list = DLinked::List.new
# Test append and prepend
list.append(2).append(3).prepend(1)
puts "After operations: #{list}" # [1, 2, 3]
puts "Size: #{list.size}" # 3
# Test pop and shift
puts "Pop: #{list.pop}" # 3
puts "Shift: #{list.shift}" # 1
puts "After pop/shift: #{list}" # [2]
# Test iteration
list.append(4).append(6).prepend(0)
puts "\nForward iteration:"
list.each { |v| puts " #{v}" }
puts "\nReverse iteration:"
list.reverse_each { |v| puts " #{v}" }
# Test enumerable methods
puts "\nSquared values: #{list.map { |v| v * v }}"
puts "Sum: #{list.sum}"
# Test delete
list.delete(2)
puts "After deleting 2: #{list}"
end