Merge pull request #41 from liutianyi1989/master

fix bugs of array stack & 09_queue by golang
This commit is contained in:
wangzheng0822 2018-10-12 17:47:19 +08:00 committed by GitHub
commit 982801c002
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 286 additions and 1 deletions

View File

@ -28,12 +28,17 @@ func (this *ArrayStack) IsEmpty() bool {
}
func (this *ArrayStack) Push(v interface{}) {
this.data = append(this.data, v)
if this.top < 0 {
this.top = 0
} else {
this.top += 1
}
if this.top > len(this.data)-1 {
this.data = append(this.data, v)
} else {
this.data[this.top] = v
}
}
func (this *ArrayStack) Pop() interface{} {

View File

@ -6,7 +6,12 @@ func TestArrayStack_Push(t *testing.T) {
s := NewArrayStack()
s.Push(1)
s.Push(2)
t.Log(s.Pop())
s.Push(3)
t.Log(s.Pop())
t.Log(s.Pop())
s.Push(4)
t.Log(s.Pop())
s.Print()
}

View File

@ -0,0 +1,72 @@
package _9_queue
import "fmt"
type CircularQueue struct {
q []interface{}
capacity int
head int
tail int
}
func NewCircularQueue(n int) *CircularQueue {
if n == 0 {
return nil
}
return &CircularQueue{make([]interface{}, n), n, 0, 0}
}
/*
栈空条件head==tail为true
*/
func (this *CircularQueue) IsEmpty() bool {
if this.head == this.tail {
return true
}
return false
}
/*
栈满条件(tail+1)%capacity==head为true
*/
func (this *CircularQueue) IsFull() bool {
if this.head == (this.tail+1)%this.capacity {
return true
}
return false
}
func (this *CircularQueue) EnQueue(v interface{}) bool {
if this.IsFull() {
return false
}
this.q[this.tail] = v
this.tail = (this.tail + 1) % this.capacity
return true
}
func (this *CircularQueue) DeQueue() interface{} {
if this.IsEmpty() {
return nil
}
v := this.q[this.head]
this.head = (this.head + 1) % this.capacity
return v
}
func (this *CircularQueue) String() string {
if this.IsEmpty() {
return "empty queue"
}
result := "head"
var i = this.head
for true {
result += fmt.Sprintf("<-%+v", this.q[i])
i = (i + 1) % this.capacity
if i == this.tail {
break
}
}
result += "<-tail"
return result
}

View File

@ -0,0 +1,37 @@
package _9_queue
import "testing"
func TestCircularQueue_EnQueue(t *testing.T) {
q := NewCircularQueue(5)
q.EnQueue(1)
q.EnQueue(2)
q.EnQueue(3)
q.EnQueue(4)
q.EnQueue(5)
q.EnQueue(6)
t.Log(q)
}
func TestCircularQueue_DeQueue(t *testing.T) {
q := NewCircularQueue(5)
q.EnQueue(1)
q.EnQueue(2)
q.EnQueue(3)
q.EnQueue(4)
q.EnQueue(5)
q.EnQueue(6)
t.Log(q)
t.Log(q.DeQueue())
t.Log(q)
q.EnQueue(5)
t.Log(q)
q.DeQueue()
t.Log(q)
q.DeQueue()
t.Log(q)
q.DeQueue()
t.Log(q)
q.DeQueue()
t.Log(q)
}

View File

@ -0,0 +1,44 @@
package _9_queue
import "fmt"
type ArrayQueue struct {
q []interface{}
capacity int
head int
tail int
}
func NewArrayQueue(n int) *ArrayQueue {
return &ArrayQueue{make([]interface{}, n), n, 0, 0}
}
func (this *ArrayQueue) EnQueue(v interface{}) bool {
if this.tail == this.capacity {
return false
}
this.q[this.tail] = v
this.tail++
return true
}
func (this *ArrayQueue) DeQueue() interface{} {
if this.head == this.tail {
return nil
}
v := this.q[this.head]
this.head++
return v
}
func (this *ArrayQueue) String() string {
if this.head == this.tail {
return "empty queue"
}
result := "head"
for i := this.head; i <= this.tail-1; i++ {
result += fmt.Sprintf("<-%+v", this.q[i])
}
result += "<-tail"
return result
}

View File

@ -0,0 +1,35 @@
package _9_queue
import "testing"
func TestArrayQueue_EnQueue(t *testing.T) {
q := NewArrayQueue(5)
q.EnQueue(1)
q.EnQueue(2)
q.EnQueue(3)
q.EnQueue(4)
q.EnQueue(5)
q.EnQueue(6)
t.Log(q)
}
func TestArrayQueue_DeQueue(t *testing.T) {
q := NewArrayQueue(5)
q.EnQueue(1)
q.EnQueue(2)
q.EnQueue(3)
q.EnQueue(4)
q.EnQueue(5)
q.EnQueue(6)
t.Log(q)
q.DeQueue()
t.Log(q)
q.DeQueue()
t.Log(q)
q.DeQueue()
t.Log(q)
q.DeQueue()
t.Log(q)
q.DeQueue()
t.Log(q)
}

View File

@ -0,0 +1,52 @@
package _9_queue
import "fmt"
type ListNode struct {
val interface{}
next *ListNode
}
type LinkedListQueue struct {
head *ListNode
tail *ListNode
length int
}
func NewLinkedListQueue() *LinkedListQueue {
return &LinkedListQueue{nil, nil, 0}
}
func (this *LinkedListQueue) EnQueue(v interface{}) {
node := &ListNode{v, nil}
if nil == this.tail {
this.tail = node
this.head = node
} else {
this.tail.next = node
this.tail = node
}
this.length++
}
func (this *LinkedListQueue) DeQueue() interface{} {
if this.head == nil {
return nil
}
v := this.head.val
this.head = this.head.next
this.length--
return v
}
func (this *LinkedListQueue) String() string {
if this.head == nil {
return "empty queue"
}
result := "head<-"
for cur := this.head; cur != nil; cur = cur.next {
result += fmt.Sprintf("<-%+v", cur.val)
}
result += "<-tail"
return result
}

View File

@ -0,0 +1,35 @@
package _9_queue
import "testing"
func TestListQueue_EnQueue(t *testing.T) {
q := NewLinkedListQueue()
q.EnQueue(1)
q.EnQueue(2)
q.EnQueue(3)
q.EnQueue(4)
q.EnQueue(5)
q.EnQueue(6)
t.Log(q)
}
func TestListQueue_DeQueue(t *testing.T) {
q := NewLinkedListQueue()
q.EnQueue(1)
q.EnQueue(2)
q.EnQueue(3)
q.EnQueue(4)
q.EnQueue(5)
q.EnQueue(6)
t.Log(q)
q.DeQueue()
t.Log(q)
q.DeQueue()
t.Log(q)
q.DeQueue()
t.Log(q)
q.DeQueue()
t.Log(q)
q.DeQueue()
t.Log(q)
}