Skip to content

Commit

Permalink
Merge pull request wangzheng0822#41 from liutianyi1989/master
Browse files Browse the repository at this point in the history
fix bugs of array stack & 09_queue by golang
  • Loading branch information
wangzheng0822 authored Oct 12, 2018
2 parents a7d5144 + 2029a4d commit 982801c
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 1 deletion.
7 changes: 6 additions & 1 deletion go/08_stack/StackBasedOnArray.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down
5 changes: 5 additions & 0 deletions go/08_stack/StackBasedOnArray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
72 changes: 72 additions & 0 deletions go/09_queue/CircularQueue.go
Original file line number Diff line number Diff line change
@@ -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
}
37 changes: 37 additions & 0 deletions go/09_queue/CircularQueue_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
44 changes: 44 additions & 0 deletions go/09_queue/QueueBasedOnArray.go
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions go/09_queue/QueueBasedOnArray_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
52 changes: 52 additions & 0 deletions go/09_queue/QueueBasedOnLinkedList.go
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions go/09_queue/QueueBasedOnLinkedList_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 982801c

Please sign in to comment.