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.
- Loading branch information
1 parent
b870907
commit e2b841f
Showing
7 changed files
with
306 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package _8_stack | ||
|
||
import "fmt" | ||
|
||
type Browser struct { | ||
forwardStack Stack | ||
backStack Stack | ||
} | ||
|
||
func NewBrowser() *Browser { | ||
return &Browser{ | ||
forwardStack: NewArrayStack(), | ||
backStack: NewLinkedListStack(), | ||
} | ||
} | ||
|
||
func (this *Browser) CanForward() bool { | ||
if this.forwardStack.IsEmpty() { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (this *Browser) CanBack() bool { | ||
if this.backStack.IsEmpty() { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (this *Browser) Open(addr string) { | ||
fmt.Printf("Open new addr %+v\n", addr) | ||
this.forwardStack.Flush() | ||
} | ||
|
||
func (this *Browser) PushBack(addr string) { | ||
this.backStack.Push(addr) | ||
} | ||
|
||
func (this *Browser) Forward() { | ||
if this.forwardStack.IsEmpty() { | ||
return | ||
} | ||
top := this.forwardStack.Pop() | ||
this.backStack.Push(top) | ||
fmt.Printf("forward to %+v\n", top) | ||
} | ||
|
||
func (this *Browser) Back() { | ||
if this.backStack.IsEmpty() { | ||
return | ||
} | ||
top := this.backStack.Pop() | ||
this.forwardStack.Push(top) | ||
fmt.Printf("back to %+v\n", top) | ||
} |
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,29 @@ | ||
package _8_stack | ||
|
||
import "testing" | ||
|
||
func TestBrowser(t *testing.T) { | ||
b := NewBrowser() | ||
b.PushBack("www.qq.com") | ||
b.PushBack("www.baidu.com") | ||
b.PushBack("www.sina.com") | ||
if b.CanBack() { | ||
b.Back() | ||
} | ||
if b.CanForward() { | ||
b.Forward() | ||
} | ||
if b.CanBack() { | ||
b.Back() | ||
} | ||
if b.CanBack() { | ||
b.Back() | ||
} | ||
if b.CanBack() { | ||
b.Back() | ||
} | ||
b.Open("www.taobao.com") | ||
if b.CanForward() { | ||
b.Forward() | ||
} | ||
} |
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,67 @@ | ||
package _8_stack | ||
|
||
import "fmt" | ||
|
||
/* | ||
基于数组实现的栈 | ||
*/ | ||
|
||
type ArrayStack struct { | ||
//数据 | ||
data []interface{} | ||
//栈顶指针 | ||
top int | ||
} | ||
|
||
func NewArrayStack() *ArrayStack { | ||
return &ArrayStack{ | ||
data: make([]interface{}, 0, 32), | ||
top: -1, | ||
} | ||
} | ||
|
||
func (this *ArrayStack) IsEmpty() bool { | ||
if this.top < 0 { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (this *ArrayStack) Push(v interface{}) { | ||
this.data = append(this.data, v) | ||
if this.top < 0 { | ||
this.top = 0 | ||
} else { | ||
this.top += 1 | ||
} | ||
} | ||
|
||
func (this *ArrayStack) Pop() interface{} { | ||
if this.IsEmpty() { | ||
return nil | ||
} | ||
v := this.data[this.top] | ||
this.top -= 1 | ||
return v | ||
} | ||
|
||
func (this *ArrayStack) Top() interface{} { | ||
if this.IsEmpty() { | ||
return nil | ||
} | ||
return this.data[this.top] | ||
} | ||
|
||
func (this *ArrayStack) Flush() { | ||
this.top = -1 | ||
} | ||
|
||
func (this *ArrayStack) Print() { | ||
if this.IsEmpty() { | ||
fmt.Println("empty statck") | ||
} else { | ||
for i := this.top; i >= 0; i-- { | ||
fmt.Println(this.data[i]) | ||
} | ||
} | ||
} |
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,41 @@ | ||
package _8_stack | ||
|
||
import "testing" | ||
|
||
func TestArrayStack_Push(t *testing.T) { | ||
s := NewArrayStack() | ||
s.Push(1) | ||
s.Push(2) | ||
s.Push(3) | ||
s.Print() | ||
} | ||
|
||
func TestArrayStack_Pop(t *testing.T) { | ||
s := NewArrayStack() | ||
s.Push(1) | ||
s.Push(2) | ||
s.Push(3) | ||
s.Print() | ||
|
||
t.Log(s.Pop()) | ||
t.Log(s.Pop()) | ||
t.Log(s.Pop()) | ||
t.Log(s.Pop()) | ||
s.Print() | ||
} | ||
|
||
func TestArrayStack_Top(t *testing.T) { | ||
s := NewArrayStack() | ||
s.Push(1) | ||
s.Push(2) | ||
s.Push(3) | ||
|
||
t.Log(s.Top()) | ||
s.Pop() | ||
t.Log(s.Top()) | ||
s.Pop() | ||
t.Log(s.Top()) | ||
s.Pop() | ||
t.Log(s.Top()) | ||
s.Pop() | ||
} |
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,63 @@ | ||
package _8_stack | ||
|
||
import "fmt" | ||
|
||
/* | ||
基于链表实现的栈 | ||
*/ | ||
type node struct { | ||
next *node | ||
val interface{} | ||
} | ||
|
||
type LinkedListStack struct { | ||
//栈顶节点 | ||
topNode *node | ||
} | ||
|
||
func NewLinkedListStack() *LinkedListStack { | ||
return &LinkedListStack{nil} | ||
} | ||
|
||
func (this *LinkedListStack) IsEmpty() bool { | ||
if this.topNode == nil { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (this *LinkedListStack) Push(v interface{}) { | ||
this.topNode = &node{next: this.topNode, val: v} | ||
} | ||
|
||
func (this *LinkedListStack) Pop() interface{} { | ||
if this.IsEmpty() { | ||
return nil | ||
} | ||
v := this.topNode.val | ||
this.topNode = this.topNode.next | ||
return v | ||
} | ||
|
||
func (this *LinkedListStack) Top() interface{} { | ||
if this.IsEmpty() { | ||
return nil | ||
} | ||
return this.topNode.val | ||
} | ||
|
||
func (this *LinkedListStack) Flush() { | ||
this.topNode = nil | ||
} | ||
|
||
func (this *LinkedListStack) Print() { | ||
if this.IsEmpty() { | ||
fmt.Println("empty stack") | ||
} else { | ||
cur := this.topNode | ||
for nil != cur { | ||
fmt.Println(cur.val) | ||
cur = cur.next | ||
} | ||
} | ||
} |
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,41 @@ | ||
package _8_stack | ||
|
||
import "testing" | ||
|
||
func TestLinkedListStack_Push(t *testing.T) { | ||
s := NewLinkedListStack() | ||
s.Push(1) | ||
s.Push(2) | ||
s.Push(3) | ||
s.Print() | ||
} | ||
|
||
func TestLinkedListStack_Pop(t *testing.T) { | ||
s := NewLinkedListStack() | ||
s.Push(1) | ||
s.Push(2) | ||
s.Push(3) | ||
s.Print() | ||
|
||
t.Log(s.Pop()) | ||
t.Log(s.Pop()) | ||
t.Log(s.Pop()) | ||
t.Log(s.Pop()) | ||
s.Print() | ||
} | ||
|
||
func TestLinkedListStack_Top(t *testing.T) { | ||
s := NewLinkedListStack() | ||
s.Push(1) | ||
s.Push(2) | ||
s.Push(3) | ||
|
||
t.Log(s.Top()) | ||
s.Pop() | ||
t.Log(s.Top()) | ||
s.Pop() | ||
t.Log(s.Top()) | ||
s.Pop() | ||
t.Log(s.Top()) | ||
s.Pop() | ||
} |
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,9 @@ | ||
package _8_stack | ||
|
||
type Stack interface { | ||
Push(v interface{}) | ||
Pop() interface{} | ||
IsEmpty() bool | ||
Top() interface{} | ||
Flush() | ||
} |