//文件遍历
//轻量级 数组栈 深度遍历 数组队列,广度遍历
//重量级 链表栈 深度遍历 链表队列,广度遍历
package main
import "fmt"
//链表实现堆栈,头部插入头部删除
type Node struct {
data interface{}
pNext *Node
}
type LinkStack interface {
IsEmpty() bool
Push(data interface{})
Pop() interface{}
Length() int
}
func NewStack() *Node{
return &Node{}
}
func (n *Node)IsEmpty() bool{
if n.pNext == nil{
return true
}else{
return false
}
}
func (n *Node)Push(data interface{}){
newnode := &Node{
data:data,
pNext:nil,
}
newnode.pNext=n.pNext
n.pNext=newnode
}
func (n *Node)Pop() interface{}{
if n.IsEmpty() == true{
return nil
}
value := n.pNext.data
n.pNext = n.pNext.pNext
return value
}
func (n *Node)Length() int{
nodenext := n
nodelength := 0
for nodenext.pNext != nil{
nodenext=nodenext.pNext
nodelength++
}
return nodelength
}
func main(){
mystack := NewStack()
for i:=0;i<10000000;i++{
mystack.Push(i)
}
for data := mystack.Pop();data != nil;data=mystack.Pop(){
fmt.Println(data)
}
}
网友评论