|
| 1 | +=begin |
| 2 | +File: my_list.rb |
| 3 | +Created Time: 2024-03-18 |
| 4 | +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) |
| 5 | +=end |
| 6 | + |
| 7 | +# ### List class ### |
| 8 | +class MyList |
| 9 | + attr_reader :size # Get list length (current number of elements) |
| 10 | + attr_reader :capacity # Get list capacity |
| 11 | + |
| 12 | + # ### Constructor ### |
| 13 | + def initialize |
| 14 | + @capacity = 10 |
| 15 | + @size = 0 |
| 16 | + @extend_ratio = 2 |
| 17 | + @arr = Array.new(capacity) |
| 18 | + end |
| 19 | + |
| 20 | + # ### Access element ### |
| 21 | + def get(index) |
| 22 | + # If the index is out of bounds, throw an exception, as below |
| 23 | + raise IndexError, "Index out of bounds" if index < 0 || index >= size |
| 24 | + @arr[index] |
| 25 | + end |
| 26 | + |
| 27 | + # ### Access element ### |
| 28 | + def set(index, num) |
| 29 | + raise IndexError, "Index out of bounds" if index < 0 || index >= size |
| 30 | + @arr[index] = num |
| 31 | + end |
| 32 | + |
| 33 | + # ### Add element at end ### |
| 34 | + def add(num) |
| 35 | + # When the number of elements exceeds capacity, trigger the extension mechanism |
| 36 | + extend_capacity if size == capacity |
| 37 | + @arr[size] = num |
| 38 | + |
| 39 | + # Update the number of elements |
| 40 | + @size += 1 |
| 41 | + end |
| 42 | + |
| 43 | + # ### Insert element in middle ### |
| 44 | + def insert(index, num) |
| 45 | + raise IndexError, "Index out of bounds" if index < 0 || index >= size |
| 46 | + |
| 47 | + # When the number of elements exceeds capacity, trigger the extension mechanism |
| 48 | + extend_capacity if size == capacity |
| 49 | + |
| 50 | + # Move all elements after index index forward by one position |
| 51 | + for j in (size - 1).downto(index) |
| 52 | + @arr[j + 1] = @arr[j] |
| 53 | + end |
| 54 | + @arr[index] = num |
| 55 | + |
| 56 | + # Update the number of elements |
| 57 | + @size += 1 |
| 58 | + end |
| 59 | + |
| 60 | + # ### Delete element ### |
| 61 | + def remove(index) |
| 62 | + raise IndexError, "Index out of bounds" if index < 0 || index >= size |
| 63 | + num = @arr[index] |
| 64 | + |
| 65 | + # Move all elements after index forward by one position |
| 66 | + for j in index...size |
| 67 | + @arr[j] = @arr[j + 1] |
| 68 | + end |
| 69 | + |
| 70 | + # Update the number of elements |
| 71 | + @size -= 1 |
| 72 | + |
| 73 | + # Return the removed element |
| 74 | + num |
| 75 | + end |
| 76 | + |
| 77 | + # ### Expand list capacity ### |
| 78 | + def extend_capacity |
| 79 | + # Create new array with length extend_ratio times original, copy original array to new array |
| 80 | + arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1)) |
| 81 | + # Add elements at the end |
| 82 | + @capacity = arr.length |
| 83 | + end |
| 84 | + |
| 85 | + # ### Convert list to array ### |
| 86 | + def to_array |
| 87 | + sz = size |
| 88 | + # Elements enqueue |
| 89 | + arr = Array.new(sz) |
| 90 | + for i in 0...sz |
| 91 | + arr[i] = get(i) |
| 92 | + end |
| 93 | + arr |
| 94 | + end |
| 95 | +end |
| 96 | + |
| 97 | +### Driver Code ### |
| 98 | +if __FILE__ == $0 |
| 99 | + # Initialize list |
| 100 | + nums = MyList.new |
| 101 | + |
| 102 | + # Direct traversal of list elements |
| 103 | + nums.add(1) |
| 104 | + nums.add(3) |
| 105 | + nums.add(2) |
| 106 | + nums.add(5) |
| 107 | + nums.add(4) |
| 108 | + puts "List nums = #{nums.to_array}, capacity = #{nums.capacity}, length = #{nums.size}" |
| 109 | + |
| 110 | + # Sort list |
| 111 | + nums.insert(3, 6) |
| 112 | + puts "Insert number 6 at index 3, get nums = #{nums.to_array}" |
| 113 | + |
| 114 | + # Remove element |
| 115 | + nums.remove(3) |
| 116 | + puts "Delete element at index 3, get nums = #{nums.to_array}" |
| 117 | + |
| 118 | + # Update element |
| 119 | + num = nums.get(1) |
| 120 | + puts "Access element at index 1, get num = #{num}" |
| 121 | + |
| 122 | + # Add elements at the end |
| 123 | + nums.set(1, 0) |
| 124 | + puts "Update element at index 1 to 0, get nums = #{nums.to_array}" |
| 125 | + |
| 126 | + # Test capacity expansion mechanism |
| 127 | + for i in 0...10 |
| 128 | + # At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism |
| 129 | + nums.add(i) |
| 130 | + end |
| 131 | + puts "After expansion, list nums = #{nums.to_array}, capacity = #{nums.capacity}, length = #{nums.size}" |
| 132 | +end |
0 commit comments