1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

refactor: reconstructure souce file into the corresponding folder

This commit is contained in:
dudaodong
2022-02-10 17:57:53 +08:00
parent 85e1f711f5
commit f551c56921
14 changed files with 20 additions and 14 deletions

View File

@@ -3,11 +3,13 @@ package datastructure
import ( import (
"errors" "errors"
"fmt" "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. // 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 { type DoublyLink[T any] struct {
Head *LinkNode[T] Head *datastructure.LinkNode[T]
length int length int
} }
@@ -18,7 +20,7 @@ func NewDoublyLink[T any]() *DoublyLink[T] {
// InsertAtHead insert value into doubly linklist at head index // InsertAtHead insert value into doubly linklist at head index
func (link *DoublyLink[T]) InsertAtHead(value T) { func (link *DoublyLink[T]) InsertAtHead(value T) {
newNode := NewLinkNode(value) newNode := datastructure.NewLinkNode(value)
size := link.Size() size := link.Size()
if size == 0 { if size == 0 {
@@ -48,7 +50,7 @@ func (link *DoublyLink[T]) InsertAtTail(value T) {
current = current.Next current = current.Next
} }
newNode := NewLinkNode(value) newNode := datastructure.NewLinkNode(value)
newNode.Next = nil newNode.Next = nil
newNode.Pre = current newNode.Pre = current
current.Next = newNode current.Next = newNode
@@ -78,7 +80,7 @@ func (link *DoublyLink[T]) InsertAt(index int, value T) error {
for current != nil { for current != nil {
if i == index-1 { if i == index-1 {
newNode := NewLinkNode(value) newNode := datastructure.NewLinkNode(value)
newNode.Next = current.Next newNode.Next = current.Next
newNode.Pre = current newNode.Pre = current
@@ -161,7 +163,7 @@ func (link *DoublyLink[T]) DeleteAt(index int) error {
// Reverse the linked list // Reverse the linked list
func (link *DoublyLink[T]) Reverse() { func (link *DoublyLink[T]) Reverse() {
current := link.Head current := link.Head
var temp *LinkNode[T] var temp *datastructure.LinkNode[T]
for current != nil { for current != nil {
temp = current.Pre temp = current.Pre
@@ -176,7 +178,7 @@ func (link *DoublyLink[T]) Reverse() {
} }
// GetMiddleNode return node at middle index of linked list // 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 { if link.Head == nil {
return nil return nil
} }

View File

@@ -3,11 +3,13 @@ package datastructure
import ( import (
"errors" "errors"
"fmt" "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. // 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 { type SinglyLink[T any] struct {
Head *LinkNode[T] Head *datastructure.LinkNode[T]
length int length int
} }
@@ -18,7 +20,7 @@ func NewSinglyLink[T any]() *SinglyLink[T] {
// InsertAtHead insert value into singly linklist at head index // InsertAtHead insert value into singly linklist at head index
func (link *SinglyLink[T]) InsertAtHead(value T) { func (link *SinglyLink[T]) InsertAtHead(value T) {
newNode := NewLinkNode(value) newNode := datastructure.NewLinkNode(value)
newNode.Next = link.Head newNode.Next = link.Head
link.Head = newNode link.Head = newNode
link.length++ link.length++
@@ -36,7 +38,7 @@ func (link *SinglyLink[T]) InsertAtTail(value T) {
current = current.Next current = current.Next
} }
newNode := NewLinkNode(value) newNode := datastructure.NewLinkNode(value)
newNode.Next = nil newNode.Next = nil
current.Next = newNode current.Next = newNode
@@ -65,7 +67,7 @@ func (link *SinglyLink[T]) InsertAt(index int, value T) error {
for current != nil { for current != nil {
if i == index-1 { if i == index-1 {
newNode := NewLinkNode(value) newNode := datastructure.NewLinkNode(value)
newNode.Next = current.Next newNode.Next = current.Next
current.Next = newNode current.Next = newNode
link.length++ link.length++
@@ -144,7 +146,7 @@ func (link *SinglyLink[T]) DeleteAt(index int) error {
// Reverse the linked list // Reverse the linked list
func (link *SinglyLink[T]) Reverse() { func (link *SinglyLink[T]) Reverse() {
var pre, next *LinkNode[T] var pre, next *datastructure.LinkNode[T]
current := link.Head current := link.Head
@@ -159,7 +161,7 @@ func (link *SinglyLink[T]) Reverse() {
} }
// GetMiddleNode return node at middle index of linked list // 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 { if link.Head == nil {
return nil return nil
} }

View File

@@ -3,11 +3,13 @@ package datastructure
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/duke-git/lancet/datastructure"
) )
// LinkedStack implements stack with link list // LinkedStack implements stack with link list
type LinkedStack[T any] struct { type LinkedStack[T any] struct {
top *StackNode[T] top *datastructure.StackNode[T]
length int length int
} }
@@ -40,7 +42,7 @@ func (s *LinkedStack[T]) IsEmpty() bool {
// Push element into stack // Push element into stack
func (s *LinkedStack[T]) Push(value T) { func (s *LinkedStack[T]) Push(value T) {
newNode := NewStackNode(value) newNode := datastructure.NewStackNode(value)
top := s.top top := s.top
if top == nil { if top == nil {
s.top = newNode s.top = newNode