diff --git a/DoublyLinkedList/DoublyLinkedList.go b/DoublyLinkedList/DoublyLinkedList.go index e6e39f9..5310ae2 100644 --- a/DoublyLinkedList/DoublyLinkedList.go +++ b/DoublyLinkedList/DoublyLinkedList.go @@ -18,20 +18,23 @@ func (list *LinkedList) InsertFirst(i int) { data.next = list.head } list.head = data + + if list.tail == nil{ + list.tail = data + } } func (list *LinkedList) InsertLast(i int) { data := &Node{data: i} - if list.head == nil { - list.head = data - list.tail = data - return - } if list.tail != nil { list.tail.next = data data.prev = list.tail } list.tail = data + + if list.head == nil { + list.head = data + } } func (list *LinkedList) RemoveByValue(i int) bool { @@ -40,7 +43,11 @@ func (list *LinkedList) RemoveByValue(i int) bool { } if list.head.data == i { list.head = list.head.next - list.head.prev = nil + if list.head != nil { + list.head.prev = nil + } else { + list.tail = nil + } return true } if list.tail.data == i { diff --git a/DoublyLinkedList/DoublyLinkedList_test.go b/DoublyLinkedList/DoublyLinkedList_test.go new file mode 100644 index 0000000..24bcc38 --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList_test.go @@ -0,0 +1,221 @@ +package DoublyLinkedList + +import "testing" + +func TestLinkedList_InsertFirst(t *testing.T) { + ll := LinkedList{} + + var f, l int + + for i := 1; i <= 1000; i++ { + ll.InsertFirst(i) + if f, _ = ll.GetFirst(); f != i { + t.Fatalf("GetFirst Error want:%d but return:%d", i, f) + } + if l, _ = ll.GetLast(); l != 1 { + t.Fatalf("GetFirst Error want:%d but return:%d", 1, l) + } + } + + if ll.GetSize() != 1000{ + t.Fatal() + } +} + +func TestLinkedList_InsertLast(t *testing.T) { + ll := LinkedList{} + + var f, l int + + for i := 1; i <= 1000; i++ { + ll.InsertLast(i) + if f, _ = ll.GetLast(); f != i { + t.Fatalf("GetFirst Error want:%d but return:%d", i, f) + } + if l, _ = ll.GetFirst(); l != 1 { + t.Fatalf("GetFirst Error want:%d but return:%d", 1, l) + } + } + + if ll.GetSize() != 1000{ + t.Fatal() + } +} + +func TestLinkedList_GetSize(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + if ll.GetSize() != i { + t.Fatal() + } + } +} + +func TestLinkedList_GetSize2(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertLast(i) + if ll.GetSize() != i { + t.Fatal() + } + } +} + +func TestLinkedList_GetFirst(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + if v, _ := ll.GetFirst(); v != count { + t.Fatal() + } +} + +func TestLinkedList_GetLast(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertLast(i) + } + + if v, _ := ll.GetLast(); v != count { + t.Fatal() + } +} + +func TestLinkedList_GetItemsFromStart(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + //from 1000 to 1 + for _, v := range ll.GetItemsFromStart() { + if v != count { + t.Fatal() + } + count-- + } +} + +func TestLinkedList_GetItemsFromEnd(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + //from 1 to 1000 + index := 1 + for _, v := range ll.GetItemsFromEnd() { + if v != index { + t.Fatal() + } + index++ + } +} + +func TestLinkedList_SearchValue(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + for i := 1; i <= count; i++ { + if !ll.SearchValue(i) { + t.Fatal() + } + } + + if ll.SearchValue(1001) { + t.Fatal() + } + if ll.SearchValue(0) { + t.Fatal() + } +} + +func TestLinkedList_RemoveByValue(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + for i := 1; i <= count; i++ { + ll.RemoveByValue(i) + if ll.SearchValue(i) { + t.Fatal() + } + } + + if ll.GetSize() > 0 { + t.Fatal() + } +} + +func TestLinkedList_RemoveByValue1(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertLast(i) + } + + for i := 1; i <= count; i++ { + ll.RemoveByValue(i) + if ll.SearchValue(i) { + t.Fatal() + } + } + + if ll.GetSize() > 0 { + t.Fatal() + } +} + +func TestLinkedList_RemoveByIndex(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertLast(i) + } + + //from 1 to 1000 + for i := 1; i <= count; i++ { + ll.RemoveByIndex(0) + if ll.SearchValue(i) { + t.Fatal() + } + } + + if ll.GetSize() > 0 { + t.Fatal() + } +} + +func TestLinkedList_RemoveByIndex2(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + //from 1 to 1000 + for i := 1; i <= count; i++ { + ll.RemoveByIndex(ll.GetSize() - 1) + if ll.SearchValue(i) { + t.Fatal() + } + } + + if ll.GetSize() > 0 { + t.Fatal() + } +}