This commit is contained in:
leo 2018-10-09 23:54:01 +08:00
parent b870907d0c
commit e2b841f984
7 changed files with 306 additions and 0 deletions

View File

@ -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)
}

View File

@ -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()
}
}

View File

@ -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])
}
}
}

View File

@ -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()
}

View File

@ -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
}
}
}

View File

@ -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()
}

View File

@ -0,0 +1,9 @@
package _8_stack
type Stack interface {
Push(v interface{})
Pop() interface{}
IsEmpty() bool
Top() interface{}
Flush()
}