Skip to content

Commit dc9fd98

Browse files
committed
queue
1 parent 18962e9 commit dc9fd98

File tree

17 files changed

+196
-84
lines changed

17 files changed

+196
-84
lines changed

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
3+
.PHONY : build
4+
build : fmt test
5+
@echo "可以提交"
6+
7+
.PHONY : fmt
8+
fmt :
9+
@echo "格式化代码"
10+
@gofmt -l -w ./
11+
12+
13+
.PHONY : test
14+
test :
15+
@echo "测试代码"
16+
go test -v ./...

data-structures/binaryTree/bst.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package binaryTree
22

33
//二叉树
44
//二叉树是每个节点最多有两个子树的树结构
5-
type ElementType int//节点数据
5+
type ElementType int //节点数据
66
//结点
77
type Node struct {
8-
Value ElementType
8+
Value ElementType
99
Parent *Node
10-
Left *Node
11-
Right *Node
10+
Left *Node
11+
Right *Node
1212
}
1313

1414
//创建节点
1515
func NewNode(v ElementType) *Node {
16-
return &Node{Value:v}
16+
return &Node{Value: v}
1717
}
1818

1919
/*
@@ -41,13 +41,13 @@ func NewTree(n *Node) *Tree {
4141
if n == nil {
4242
return &Tree{}
4343
}
44-
return &Tree{Head:n,Size:1}
44+
return &Tree{Head: n, Size: 1}
4545
}
4646

4747
//插入,相同的节点值,放到右子树
4848
func (t *Tree) Insert(i ElementType) {
49-
n := NewNode(i)//创建节点
50-
if t.Head == nil {//判断树的根节点
49+
n := NewNode(i) //创建节点
50+
if t.Head == nil { //判断树的根节点
5151
t.Head = n
5252
t.Size++
5353
return
@@ -56,15 +56,15 @@ func (t *Tree) Insert(i ElementType) {
5656
h := t.Head
5757

5858
for {
59-
if n.Compare(h) == -1 {//小于parent,到左子节点
60-
if h.Left == nil {//无左子节点
59+
if n.Compare(h) == -1 { //小于parent,到左子节点
60+
if h.Left == nil { //无左子节点
6161
h.Left = n
6262
n.Parent = h
6363
break
6464
} else {
6565
h = h.Left
6666
}
67-
} else {//大于parent
67+
} else { //大于parent
6868
if h.Right == nil {
6969
h.Right = n
7070
n.Parent = h
@@ -166,4 +166,4 @@ func IterOnTree(n *Node, f func(*Node)) bool {
166166
f(n)
167167

168168
return IterOnTree(n.Right, f)
169-
}
169+
}

data-structures/binaryTree/bst_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ import (
55
"testing"
66
)
77

8-
98
func TestNode_Compare(t *testing.T) {
109
n := NewNode(1)
1110
m := NewNode(2)
1211
k := NewNode(1)
13-
assert.Equal(t,-1,n.Compare(m))
14-
assert.Equal(t,0,n.Compare(k))
15-
assert.Equal(t,1,m.Compare(k))
12+
13+
assert.Equal(t, -1, n.Compare(m))
14+
assert.Equal(t, 0, n.Compare(k))
15+
assert.Equal(t, 1, m.Compare(k))
1616
}
1717

1818
func TestTree(t *testing.T) {
1919
max_size := 10
2020
tree := NewTree(nil)
21-
for i := 0; i < max_size ; i++ {
21+
for i := 0; i < max_size; i++ {
2222
tree.Insert(ElementType(i))
2323
}
24-
assert.Equal(t,10,tree.Size)
25-
assert.Equal(t,ElementType(5),tree.Search(5).Value)
26-
assert.Equal(t,true,tree.Delete(5))
27-
assert.Equal(t,9,tree.Size)
24+
assert.Equal(t, 10, tree.Size)
25+
assert.Equal(t, ElementType(5), tree.Search(5).Value)
26+
assert.Equal(t, true, tree.Delete(5))
27+
assert.Equal(t, 9, tree.Size)
2828

2929
}

data-structures/queue/list_queue.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package queue
2+
3+
import "container/list"
4+
5+
//使用go标准库的list实现
6+
type ListQueue struct {
7+
l *list.List
8+
}
9+
10+
func NewListQueue() *ListQueue {
11+
l := list.New()
12+
return &ListQueue{l}
13+
}
14+
15+
func (lq *ListQueue) Shift() (el interface{}) {
16+
e := lq.l.Front()
17+
el = e.Value
18+
lq.l.Remove(e)
19+
return
20+
}
21+
22+
func (lq *ListQueue) Push(el interface{}) {
23+
lq.l.PushBack(el)
24+
return
25+
}
26+
27+
func (lq *ListQueue) Len() int {
28+
return lq.l.Len()
29+
}

data-structures/queue/queue.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package queue
2+
3+
import "sync"
4+
5+
type Queue struct {
6+
queue []interface{}
7+
len int
8+
lock *sync.Mutex
9+
}
10+
11+
func New() *Queue {
12+
q := &Queue{}
13+
q.queue = make([]interface{}, 0)
14+
q.len = 0
15+
q.lock = new(sync.Mutex)
16+
return q
17+
}
18+
19+
func (q *Queue) Len() int {
20+
return q.len
21+
}
22+
23+
func (q *Queue) isEmpty() bool {
24+
q.lock.Lock()
25+
defer q.lock.Unlock()
26+
return q.len == 0
27+
}
28+
29+
func (q *Queue) Shift() (el interface{}) {
30+
el, q.queue = q.queue[0], q.queue[1:]
31+
q.len--
32+
return
33+
}
34+
35+
func (q *Queue) Push(el interface{}) {
36+
q.queue = append(q.queue, el)
37+
q.len++
38+
return
39+
}
40+
41+
func (q *Queue) Peek() (el interface{}) {
42+
q.lock.Lock()
43+
defer q.lock.Unlock()
44+
return q.queue[0]
45+
}

data-structures/queue/queue_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package queue
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestNew(t *testing.T) {
9+
queue := New()
10+
assert.Equal(t, true, queue.isEmpty())
11+
queue.Push(1)
12+
queue.Push(2)
13+
queue.Push(3)
14+
assert.Equal(t, 3, queue.Len())
15+
assert.Equal(t, 1, queue.Shift().(int))
16+
assert.Equal(t, 2, queue.Shift().(int))
17+
assert.Equal(t, 3, queue.Peek().(int))
18+
assert.Equal(t, 3, queue.Shift().(int))
19+
20+
}
21+
22+
func TestNewListQueue(t *testing.T) {
23+
queue := NewListQueue()
24+
queue.Push(1)
25+
queue.Push(2)
26+
queue.Push(3)
27+
assert.Equal(t, 3, queue.Len())
28+
assert.Equal(t, 1, queue.Shift().(int))
29+
assert.Equal(t, 2, queue.Shift().(int))
30+
assert.Equal(t, 3, queue.Shift().(int))
31+
assert.Equal(t, 0, queue.Len())
32+
}

data-structures/stack/stack.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package stack

data-structures/stack/stack_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package stack

sort/bubble/bubble_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,28 @@ package bubble
22

33
import (
44
"github.com/stretchr/testify/assert"
5-
"testing"
65
"github.com/xiaomeng79/go-algorithm/sort/testdata"
76
"github.com/xiaomeng79/go-algorithm/sort/utils"
7+
"testing"
88
)
99

10-
1110
func TestBubbleSort(t *testing.T) {
1211
for _, v := range testdata.Values {
1312
assert.Exactly(t, v.Sort, BubbleSort(v.Nosort), "no eq")
1413
}
1514
}
1615

17-
func benchmarkBubbleSort(n int,b *testing.B) {
16+
func benchmarkBubbleSort(n int, b *testing.B) {
1817
b.StopTimer()
1918
list := utils.GetArrayOfSize(n)
2019
b.ReportAllocs()
2120
b.StartTimer()
22-
for i:=0;i < b.N;i++ {
21+
for i := 0; i < b.N; i++ {
2322
BubbleSort(list)
2423
}
2524
}
2625

27-
func BenchmarkBubbleSort100(b *testing.B) { benchmarkBubbleSort(100, b) }
26+
func BenchmarkBubbleSort100(b *testing.B) { benchmarkBubbleSort(100, b) }
2827
func BenchmarkBubbleSort1000(b *testing.B) { benchmarkBubbleSort(1000, b) }
2928
func BenchmarkBubbleSort10000(b *testing.B) { benchmarkBubbleSort(10000, b) }
3029
func BenchmarkBubbleSort100000(b *testing.B) { benchmarkBubbleSort(100000, b) }

sort/insertion/insertion.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ func InsertionSort2(arr []int) []int {
2929

3030
for i := 1; i < length; i++ {
3131
backup := arr[i]
32-
j := i -1;
32+
j := i - 1
3333
//将选出的被排数比较后插入左边有序区
34-
for j >= 0 && backup < arr[j] {//注意j >= 0必须在前边,否则会数组越界
35-
arr[j+1] = arr[j]//移动有序数组
36-
j -- //反向移动下标
34+
for j >= 0 && backup < arr[j] { //注意j >= 0必须在前边,否则会数组越界
35+
arr[j+1] = arr[j] //移动有序数组
36+
j-- //反向移动下标
3737
}
38-
arr[j + 1] = backup //插队插入移动后的空位
38+
arr[j+1] = backup //插队插入移动后的空位
3939
}
4040
return arr
4141
}

sort/insertion/insertion_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,28 @@ package insertion
22

33
import (
44
"github.com/stretchr/testify/assert"
5-
"testing"
65
"github.com/xiaomeng79/go-algorithm/sort/testdata"
76
"github.com/xiaomeng79/go-algorithm/sort/utils"
7+
"testing"
88
)
99

10-
1110
func TestInsertionSort(t *testing.T) {
1211
for _, v := range testdata.Values {
1312
assert.Exactly(t, v.Sort, InsertionSort(v.Nosort), "no eq")
1413
}
1514
}
1615

17-
func benchmarkInsertionSort(n int,b *testing.B) {
16+
func benchmarkInsertionSort(n int, b *testing.B) {
1817
b.StopTimer()
1918
list := utils.GetArrayOfSize(n)
2019
b.ReportAllocs()
2120
b.StartTimer()
22-
for i:=0;i < b.N;i++ {
21+
for i := 0; i < b.N; i++ {
2322
InsertionSort(list)
2423
}
2524
}
2625

27-
func BenchmarkInsertionSort100(b *testing.B) { benchmarkInsertionSort(100, b) }
26+
func BenchmarkInsertionSort100(b *testing.B) { benchmarkInsertionSort(100, b) }
2827
func BenchmarkInsertionSort1000(b *testing.B) { benchmarkInsertionSort(1000, b) }
2928
func BenchmarkInsertionSort10000(b *testing.B) { benchmarkInsertionSort(10000, b) }
3029
func BenchmarkInsertionSort100000(b *testing.B) { benchmarkInsertionSort(100000, b) }
31-

sort/selection/selection.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ package selection
1111

1212
func SelectionSort(arr []int) []int {
1313
length := len(arr)
14-
for i:= 0;i < length-1;i++ {//注意这里使用length-1少一次,
14+
for i := 0; i < length-1; i++ { //注意这里使用length-1少一次,
1515
minIndex := i
16-
for j:= i+1;j<length;j++ {
16+
for j := i + 1; j < length; j++ {
1717
if arr[j] < arr[minIndex] {
1818
minIndex = j
1919
}
2020
}
2121
//
22-
arr[minIndex],arr[i] = arr[i],arr[minIndex]
22+
arr[minIndex], arr[i] = arr[i], arr[minIndex]
2323
}
2424
return arr
2525
}
26-
27-
28-

sort/selection/selection_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package selection
22

33
import (
4-
"testing"
54
"github.com/stretchr/testify/assert"
65
"github.com/xiaomeng79/go-algorithm/sort/testdata"
76
"github.com/xiaomeng79/go-algorithm/sort/utils"
7+
"testing"
88
)
99

1010
func TestSelectionSort(t *testing.T) {
@@ -13,20 +13,17 @@ func TestSelectionSort(t *testing.T) {
1313
}
1414
}
1515

16-
17-
func benchmarkSelectionSort(n int,b *testing.B) {
16+
func benchmarkSelectionSort(n int, b *testing.B) {
1817
b.StopTimer()
1918
list := utils.GetArrayOfSize(n)
2019
b.ReportAllocs()
2120
b.StartTimer()
22-
for i:=0;i < b.N;i++ {
21+
for i := 0; i < b.N; i++ {
2322
SelectionSort(list)
2423
}
2524
}
2625

27-
28-
func BenchmarkSelectionSort100(b *testing.B) { benchmarkSelectionSort(100, b) }
26+
func BenchmarkSelectionSort100(b *testing.B) { benchmarkSelectionSort(100, b) }
2927
func BenchmarkSelectionSort1000(b *testing.B) { benchmarkSelectionSort(1000, b) }
3028
func BenchmarkSelectionSort10000(b *testing.B) { benchmarkSelectionSort(10000, b) }
3129
func BenchmarkSelectionSort100000(b *testing.B) { benchmarkSelectionSort(100000, b) }
32-

0 commit comments

Comments
 (0)