forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wangzheng0822#41 from liutianyi1989/master
fix bugs of array stack & 09_queue by golang
- Loading branch information
Showing
8 changed files
with
286 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |