Skip to content

Commit

Permalink
08_stack
Browse files Browse the repository at this point in the history
  • Loading branch information
scissorsfeet committed Oct 9, 2018
1 parent b870907 commit e2b841f
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 0 deletions.
56 changes: 56 additions & 0 deletions go/08_stack/SimpleBrowser.go
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)
}
29 changes: 29 additions & 0 deletions go/08_stack/SimpleBrowser_test.go
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()
}
}
67 changes: 67 additions & 0 deletions go/08_stack/StackBasedOnArray.go
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])
}
}
}
41 changes: 41 additions & 0 deletions go/08_stack/StackBasedOnArray_test.go
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()
}
63 changes: 63 additions & 0 deletions go/08_stack/StackBasedOnLinkedList.go
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
}
}
}
41 changes: 41 additions & 0 deletions go/08_stack/StackBasedOnLinkedList_test.go
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()
}
9 changes: 9 additions & 0 deletions go/08_stack/StatckInterface.go
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()
}

0 comments on commit e2b841f

Please sign in to comment.