|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +type Node struct { |
| 9 | + Value int |
| 10 | + Next *Node |
| 11 | +} |
| 12 | + |
| 13 | +type SinglyLinkedList struct { |
| 14 | + Head *Node |
| 15 | +} |
| 16 | + |
| 17 | +func (list *SinglyLinkedList) AddLeft(value int) { |
| 18 | + node := Node{Value: value} |
| 19 | + |
| 20 | + if list.Head == nil { |
| 21 | + list.Head = &node |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + node.Next = list.Head |
| 26 | + list.Head = &node |
| 27 | +} |
| 28 | + |
| 29 | +func (list *SinglyLinkedList) AddRight(value int) { |
| 30 | + node := Node{Value: value} |
| 31 | + |
| 32 | + if list.Head == nil { |
| 33 | + list.Head = &node |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + pointer := list.Head |
| 38 | + |
| 39 | + for pointer.Next != nil { |
| 40 | + pointer = pointer.Next |
| 41 | + } |
| 42 | + |
| 43 | + pointer.Next = &node |
| 44 | +} |
| 45 | + |
| 46 | +func (list *SinglyLinkedList) RemoveLeft() { |
| 47 | + if list.Head == nil { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + list.Head = list.Head.Next |
| 52 | +} |
| 53 | + |
| 54 | +func (list *SinglyLinkedList) RemoveRight() { |
| 55 | + if list.Head == nil { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + if list.Head.Next == nil { |
| 60 | + list.Head = nil |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + pointer := list.Head |
| 65 | + |
| 66 | + for pointer.Next.Next != nil { |
| 67 | + pointer = pointer.Next |
| 68 | + } |
| 69 | + |
| 70 | + pointer.Next = nil |
| 71 | +} |
| 72 | + |
| 73 | +func (list *SinglyLinkedList) FindAt(idx int) *Node { |
| 74 | + pointer := list.Head |
| 75 | + |
| 76 | + for i := 0; i < idx; i++ { |
| 77 | + pointer = pointer.Next |
| 78 | + if pointer == nil { |
| 79 | + return nil |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return pointer |
| 84 | +} |
| 85 | + |
| 86 | +func (list *SinglyLinkedList) AddAt(idx, value int) { |
| 87 | + if idx == 0 { |
| 88 | + list.AddLeft(value) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + prevNode := list.FindAt(idx - 1) |
| 93 | + |
| 94 | + if prevNode == nil { |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + node := Node{Value: value} |
| 99 | + |
| 100 | + node.Next = prevNode.Next |
| 101 | + prevNode.Next = &node |
| 102 | +} |
| 103 | + |
| 104 | +func (list *SinglyLinkedList) RemoveAt(idx int) { |
| 105 | + if idx == 0 { |
| 106 | + list.RemoveLeft() |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + prevNode := list.FindAt(idx - 1) |
| 111 | + |
| 112 | + if prevNode == nil { |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + prevNode.Next = prevNode.Next.Next |
| 117 | +} |
| 118 | + |
| 119 | +func (list *SinglyLinkedList) ToSlice() []int { |
| 120 | + var slice []int |
| 121 | + |
| 122 | + for pointer := list.Head; pointer != nil; pointer = pointer.Next { |
| 123 | + slice = append(slice, pointer.Value) |
| 124 | + } |
| 125 | + |
| 126 | + return slice |
| 127 | +} |
| 128 | + |
| 129 | +func TestSinglyLinkedList(t *testing.T) { |
| 130 | + list := SinglyLinkedList{} |
| 131 | + |
| 132 | + // test AddLeft |
| 133 | + list.AddLeft(1) |
| 134 | + list.AddLeft(2) |
| 135 | + if !reflect.DeepEqual(list.ToSlice(), []int{2, 1}) { |
| 136 | + t.Errorf("AddLeft failed, expected %v, got %v", []int{2, 1}, list.ToSlice()) |
| 137 | + } |
| 138 | + |
| 139 | + // test AddRight |
| 140 | + list.AddRight(3) |
| 141 | + list.AddRight(4) |
| 142 | + if !reflect.DeepEqual(list.ToSlice(), []int{2, 1, 3, 4}) { |
| 143 | + t.Errorf("AddRight failed, expected %v, got %v", []int{2, 1, 3, 4}, list.ToSlice()) |
| 144 | + } |
| 145 | + |
| 146 | + //// test RemoveLeft |
| 147 | + list.RemoveLeft() |
| 148 | + if !reflect.DeepEqual(list.ToSlice(), []int{1, 3, 4}) { |
| 149 | + t.Errorf("RemoveLeft failed, expected %v, got %v", []int{1, 3, 4}, list.ToSlice()) |
| 150 | + } |
| 151 | + |
| 152 | + // test RemoveRight |
| 153 | + list.RemoveRight() |
| 154 | + if !reflect.DeepEqual(list.ToSlice(), []int{1, 3}) { |
| 155 | + t.Errorf("RemoveRight failed, expected %v, got %v", []int{1, 3}, list.ToSlice()) |
| 156 | + } |
| 157 | + |
| 158 | + // test AddAt |
| 159 | + list.AddAt(1, 5) |
| 160 | + if !reflect.DeepEqual(list.ToSlice(), []int{1, 5, 3}) { |
| 161 | + t.Errorf("AddAt failed, expected %v, got %v", []int{1, 5, 3}, list.ToSlice()) |
| 162 | + } |
| 163 | + |
| 164 | + // test RemoveAt |
| 165 | + list.RemoveAt(2) |
| 166 | + if !reflect.DeepEqual(list.ToSlice(), []int{1, 5}) { |
| 167 | + t.Errorf("RemoveAt failed, expected %v, got %v", []int{1, 5}, list.ToSlice()) |
| 168 | + } |
| 169 | + |
| 170 | + // test FindAt |
| 171 | + node := list.FindAt(0) |
| 172 | + if node == nil || node.Value != 1 { |
| 173 | + t.Errorf("FindAt failed, expected %v, got %v", 1, node.Value) |
| 174 | + } |
| 175 | +} |
0 commit comments