Skip to content

Commit

Permalink
添加单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy-boo committed Oct 22, 2020
1 parent 5038a0c commit 172ea2b
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 172 deletions.
130 changes: 0 additions & 130 deletions container/linked_list.go

This file was deleted.

12 changes: 0 additions & 12 deletions container/linked_list_test.go

This file was deleted.

25 changes: 0 additions & 25 deletions container/types.go

This file was deleted.

15 changes: 15 additions & 0 deletions struct/linkedlist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Golang重学数据结构之链表

> 链表主要分为单链表、双链表和循环链表
### Part 1 单链表的实现

> 结构内容
```
D:\WORKSPACE\GOLANG\SRC\GO-UTIL-TOUR
├─middlerware
│ └─logger
├─struct
│ ├─arraylist
│ └─linkedlist // 链表
```
110 changes: 105 additions & 5 deletions struct/linkedlist/single_linkedlist.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package linkedlist

import "errors"

// Object singly linked list value
type Object interface{}

Expand All @@ -16,11 +18,12 @@ type LinkedList interface {
PushFront(value Object)
PushEnd(value Object)
Add(value Object)
Insert(value Object, index int)
RemoveFront() Node
RemoveEnd() Node
Remove(index int) Node
FindByIndex(index int) Node
Insert(value Object, index int) (bool, error)
RemoveFront() bool
RemoveEnd() bool
RemoveAll() bool
Remove(index int) (bool, error)
FindByIndex(index int) (Object, error)
}

// SingleLinkedList Single list
Expand Down Expand Up @@ -92,3 +95,100 @@ func (list *SingleLinkedList) PushEnd(value Object) {
list.tail = node
list.len++
}

// Insert Insert data at the specified position in the linked list.
func (list *SingleLinkedList) Insert(value Object, index int) (bool, error) {
if index > list.Len()-1 || index < 0 {
return false, errors.New("index is not available")
}
// add to head
if index == 0 {
list.PushFront(value)
}
prev := list.head
node := &Node{
value: value,
next: nil,
}
for i := 1; i < list.Len(); i++ {
if index == i {
node.next = prev.next
prev.next = node
list.len++
break
}
prev = prev.next
}
return true, nil
}

// RemoveFront Delete head node.
func (list *SingleLinkedList) RemoveFront() bool {
if list.head == nil {
return false
}
list.head = list.head.next
list.len--
return true
}

// RemoveEnd Delete tail node.
func (list *SingleLinkedList) RemoveEnd() bool {
if list.tail == nil {
return false
}
if list.Len() == 1 {
list.RemoveFront()
}
prev := list.head
for i := 0; i < list.Len(); i++ {
if i == list.Len()-2 {
prev.next = nil
list.tail = prev
list.len--
break
}
prev = prev.next
}
return true
}

// Remove Delete the node at the specified position.
func (list *SingleLinkedList) Remove(index int) (bool, error) {
if index < 0 || index > list.Len()-1 {
return false, errors.New("index is not available")
}
if index == 0 {
list.RemoveFront()
}
if index == list.Len()-1 {
list.RemoveEnd()
}
prev := list.head
for i := 1; i < list.Len()-1; i++ {
if index == i {
prev.next = prev.next.next
list.len--
break
}
prev = prev.next
}
return true, nil
}

// FindByIndex Find the value of an element based on the index.
func (list *SingleLinkedList) FindByIndex(index int) (Object, error) {
if index < 0 || index > list.Len()-1 {
return nil, errors.New("index is not available")
}
var val Object
prev := list.head
for i := 0; i < list.Len(); i++ {
if index == i {
val = prev.value
break
}
prev = prev.next
}
return val, nil
}
Loading

0 comments on commit 172ea2b

Please sign in to comment.