diff --git a/datastructure/doublylink.go b/datastructure/link/doublylink.go similarity index 93% rename from datastructure/doublylink.go rename to datastructure/link/doublylink.go index 5b46ae4..09b40fd 100644 --- a/datastructure/doublylink.go +++ b/datastructure/link/doublylink.go @@ -3,11 +3,13 @@ package datastructure import ( "errors" "fmt" + + "github.com/duke-git/lancet/datastructure" ) // DoublyLink is a linked list. Whose node has a generic Value, Pre pointer points to a previous node of the link, Next pointer points to a next node of the link. type DoublyLink[T any] struct { - Head *LinkNode[T] + Head *datastructure.LinkNode[T] length int } @@ -18,7 +20,7 @@ func NewDoublyLink[T any]() *DoublyLink[T] { // InsertAtHead insert value into doubly linklist at head index func (link *DoublyLink[T]) InsertAtHead(value T) { - newNode := NewLinkNode(value) + newNode := datastructure.NewLinkNode(value) size := link.Size() if size == 0 { @@ -48,7 +50,7 @@ func (link *DoublyLink[T]) InsertAtTail(value T) { current = current.Next } - newNode := NewLinkNode(value) + newNode := datastructure.NewLinkNode(value) newNode.Next = nil newNode.Pre = current current.Next = newNode @@ -78,7 +80,7 @@ func (link *DoublyLink[T]) InsertAt(index int, value T) error { for current != nil { if i == index-1 { - newNode := NewLinkNode(value) + newNode := datastructure.NewLinkNode(value) newNode.Next = current.Next newNode.Pre = current @@ -161,7 +163,7 @@ func (link *DoublyLink[T]) DeleteAt(index int) error { // Reverse the linked list func (link *DoublyLink[T]) Reverse() { current := link.Head - var temp *LinkNode[T] + var temp *datastructure.LinkNode[T] for current != nil { temp = current.Pre @@ -176,7 +178,7 @@ func (link *DoublyLink[T]) Reverse() { } // GetMiddleNode return node at middle index of linked list -func (link *DoublyLink[T]) GetMiddleNode() *LinkNode[T] { +func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] { if link.Head == nil { return nil } diff --git a/datastructure/doublylink_test.go b/datastructure/link/doublylink_test.go similarity index 100% rename from datastructure/doublylink_test.go rename to datastructure/link/doublylink_test.go diff --git a/datastructure/singlylink.go b/datastructure/link/singlylink.go similarity index 92% rename from datastructure/singlylink.go rename to datastructure/link/singlylink.go index ffb7718..69723d1 100644 --- a/datastructure/singlylink.go +++ b/datastructure/link/singlylink.go @@ -3,11 +3,13 @@ package datastructure import ( "errors" "fmt" + + "github.com/duke-git/lancet/datastructure" ) // SinglyLink is a linked list. Whose node has a Value generics and Next pointer points to a next node of the link. type SinglyLink[T any] struct { - Head *LinkNode[T] + Head *datastructure.LinkNode[T] length int } @@ -18,7 +20,7 @@ func NewSinglyLink[T any]() *SinglyLink[T] { // InsertAtHead insert value into singly linklist at head index func (link *SinglyLink[T]) InsertAtHead(value T) { - newNode := NewLinkNode(value) + newNode := datastructure.NewLinkNode(value) newNode.Next = link.Head link.Head = newNode link.length++ @@ -36,7 +38,7 @@ func (link *SinglyLink[T]) InsertAtTail(value T) { current = current.Next } - newNode := NewLinkNode(value) + newNode := datastructure.NewLinkNode(value) newNode.Next = nil current.Next = newNode @@ -65,7 +67,7 @@ func (link *SinglyLink[T]) InsertAt(index int, value T) error { for current != nil { if i == index-1 { - newNode := NewLinkNode(value) + newNode := datastructure.NewLinkNode(value) newNode.Next = current.Next current.Next = newNode link.length++ @@ -144,7 +146,7 @@ func (link *SinglyLink[T]) DeleteAt(index int) error { // Reverse the linked list func (link *SinglyLink[T]) Reverse() { - var pre, next *LinkNode[T] + var pre, next *datastructure.LinkNode[T] current := link.Head @@ -159,7 +161,7 @@ func (link *SinglyLink[T]) Reverse() { } // GetMiddleNode return node at middle index of linked list -func (link *SinglyLink[T]) GetMiddleNode() *LinkNode[T] { +func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] { if link.Head == nil { return nil } diff --git a/datastructure/singlylink_test.go b/datastructure/link/singlylink_test.go similarity index 100% rename from datastructure/singlylink_test.go rename to datastructure/link/singlylink_test.go diff --git a/datastructure/list.go b/datastructure/list/list.go similarity index 100% rename from datastructure/list.go rename to datastructure/list/list.go diff --git a/datastructure/list_test.go b/datastructure/list/list_test.go similarity index 100% rename from datastructure/list_test.go rename to datastructure/list/list_test.go diff --git a/datastructure/arrayqueue.go b/datastructure/queue/arrayqueue.go similarity index 100% rename from datastructure/arrayqueue.go rename to datastructure/queue/arrayqueue.go diff --git a/datastructure/arrayqueue_test.go b/datastructure/queue/arrayqueue_test.go similarity index 100% rename from datastructure/arrayqueue_test.go rename to datastructure/queue/arrayqueue_test.go diff --git a/datastructure/set.go b/datastructure/set/set.go similarity index 100% rename from datastructure/set.go rename to datastructure/set/set.go diff --git a/datastructure/set_test.go b/datastructure/set/set_test.go similarity index 100% rename from datastructure/set_test.go rename to datastructure/set/set_test.go diff --git a/datastructure/arraystack.go b/datastructure/stack/arraystack.go similarity index 100% rename from datastructure/arraystack.go rename to datastructure/stack/arraystack.go diff --git a/datastructure/arraystack_test.go b/datastructure/stack/arraystack_test.go similarity index 100% rename from datastructure/arraystack_test.go rename to datastructure/stack/arraystack_test.go diff --git a/datastructure/linkedstack.go b/datastructure/stack/linkedstack.go similarity index 92% rename from datastructure/linkedstack.go rename to datastructure/stack/linkedstack.go index 966b8e7..8e0a1c7 100644 --- a/datastructure/linkedstack.go +++ b/datastructure/stack/linkedstack.go @@ -3,11 +3,13 @@ package datastructure import ( "errors" "fmt" + + "github.com/duke-git/lancet/datastructure" ) // LinkedStack implements stack with link list type LinkedStack[T any] struct { - top *StackNode[T] + top *datastructure.StackNode[T] length int } @@ -40,7 +42,7 @@ func (s *LinkedStack[T]) IsEmpty() bool { // Push element into stack func (s *LinkedStack[T]) Push(value T) { - newNode := NewStackNode(value) + newNode := datastructure.NewStackNode(value) top := s.top if top == nil { s.top = newNode diff --git a/datastructure/linkedstack_test.go b/datastructure/stack/linkedstack_test.go similarity index 100% rename from datastructure/linkedstack_test.go rename to datastructure/stack/linkedstack_test.go