• 栈实现
  • 递归遍历文件夹
  • 栈模拟文件夹递归
  • 递归遍历文件夹并打印层级1
  • 递归遍历文件夹并打印层级2
  • 链表栈

文件遍历 数据量轻量级: 数组栈 深度遍历; 数组队列,广度遍历。 数据量重量级: 链表栈 深度遍历; 链表队列,广度遍历。

栈实现

 1package stack
 2
 3type StackArray interface {
 4	Clear()                //清空
 5	Size() int             //大小
 6	Pop() interface{}      //弹出
 7	Push(data interface{}) //压入
 8	IsFull() bool          //是否满了
 9	IsEmpty() bool         //是否为空
10}
11type Stack struct {
12	dataSource  []interface{}
13	capsize     int //最大范围
14	currentsize int //实际使用大小
15}
16
17func NewStack() *Stack {
18	mystack := new(Stack)
19	mystack.dataSource = make([]interface{}, 0, 1000) //数组
20	mystack.capsize = 1000                            //空间
21	mystack.currentsize = 0
22	return mystack
23}
24func (mystack *Stack) Clear() {
25	mystack.dataSource = make([]interface{}, 0, 1000) //数组
26	mystack.currentsize = 0
27	mystack.capsize = 1000 //空间
28}
29func (mystack *Stack) Size() int {
30	return mystack.currentsize
31}
32func (mystack *Stack) Pop() interface{} {
33	if !mystack.IsEmpty() {
34		last := mystack.dataSource[mystack.currentsize-1]               //最后一个数据
35		mystack.dataSource = mystack.dataSource[:mystack.currentsize-1] //删除最后一个
36		mystack.currentsize--                                           //删除
37		return last
38	}
39	return nil
40}
41func (mystack *Stack) Push(data interface{}) {
42	if !mystack.IsFull() {
43		mystack.dataSource = append(mystack.dataSource, data) //叠加数据,压入
44		mystack.currentsize++
45	}
46}
47func (mystack *Stack) IsFull() bool { //判断满了
48	if mystack.currentsize >= mystack.capsize {
49		return true
50	} else {
51		return false
52	}
53}
54func (mystack *Stack) IsEmpty() bool { //判断为空
55	if mystack.currentsize == 0 {
56		return true
57	} else {
58		return false
59	}
60}
 1package stack
 2
 3import (
 4	"github.com/stretchr/testify/assert"
 5	"testing"
 6)
 7
 8func TestArrayStack(t *testing.T) {
 9	arraystack := NewStack()
10	arraystack.Push(1)
11	arraystack.Push(2)
12	v := arraystack.Pop()
13	t.Log(v)
14	assert.Equal(t, 2, v)
15	v = arraystack.Pop()
16	t.Log(v)
17	assert.Equal(t, 1, v)
18}

递归遍历文件夹

 1func GetALL(path string, files []string) ([]string, error) {
 2	read, err := ioutil.ReadDir(path) //读取文件夹
 3	if err != nil {
 4		return files, errors.New("文件加不可读取")
 5	}
 6	for _, fi := range read { //循环每个文件或者文件夹
 7		if fi.IsDir() { //判断是否文件夹
 8			fulldir := path + "/" + fi.Name() //构造新的路径
 9			files = append(files, fulldir)    //追加路径
10			files, _ = GetALL(fulldir, files) //文件夹递归处理
11		} else {
12			fulldir := path + "/" + fi.Name() //构造新的路径
13			files = append(files, fulldir)    //追加路径
14		}
15	}
16	return files, nil
17}
18
19
20func main() {
21	path := "/Users/mac/code/golangproject/src/code"
22	files := []string{}            //数组字符串
23	files, _ = GetALL(path, files) //抓取所有文件
24	for i := 0; i < len(files); i++ { //打印路径
25		fmt.Println(files[i])
26	}
27}
28
29
30/**
31/Users/mac/code/golangproject/src/code/code_sub
32/Users/mac/code/golangproject/src/code/code_sub/11
33/Users/mac/code/golangproject/src/code/code_sub/11/11.txt
34/Users/mac/code/golangproject/src/code/code_sub/sub1.txt
35/Users/mac/code/golangproject/src/code/datastruct
36/Users/mac/code/golangproject/src/code/datastruct/data.go
37/Users/mac/code/golangproject/src/code/datastruct/digui.go
38/Users/mac/code/golangproject/src/code/test1
39/Users/mac/code/golangproject/src/code/test1/11.txt
40/Users/mac/code/golangproject/src/code/yy
41/Users/mac/code/golangproject/src/code/yy/1.txt
42
43**/

栈模拟文件夹递归

 1func main() {
 2	path := "/Users/mac/code/golangproject/src/code"
 3	files := []string{} //数组字符串
 4	mystack := StackArray.NewStack()
 5	mystack.Push(path)
 6	for !mystack.IsEmpty() {
 7		path := mystack.Pop().(string)
 8		files = append(files, path)     //加入列表
 9		read, _ := ioutil.ReadDir(path) //读取文件夹下面所有的路径
10		for _, fi := range read {
11			if fi.IsDir() {
12				fulldir := path + "/" + fi.Name() //构造新的路径
13				mystack.Push(fulldir)
14
15			} else {
16				fulldir := path + "/" + fi.Name() //构造新的路径
17				files = append(files, fulldir)    //追加路径
18			}
19		}
20
21	}
22	for i := 0; i < len(files); i++ { //打印
23		fmt.Println(files[i])
24	}
25}
26/**
27	/Users/mac/code/golangproject/src/code
28/Users/mac/code/golangproject/src/code/yy
29/Users/mac/code/golangproject/src/code/yy/1.txt
30/Users/mac/code/golangproject/src/code/test1
31/Users/mac/code/golangproject/src/code/test1/11.txt
32/Users/mac/code/golangproject/src/code/datastruct
33/Users/mac/code/golangproject/src/code/datastruct/data.go
34/Users/mac/code/golangproject/src/code/datastruct/digui.go
35/Users/mac/code/golangproject/src/code/code_sub
36/Users/mac/code/golangproject/src/code/code_sub/sub1.txt
37/Users/mac/code/golangproject/src/code/code_sub/11
38/Users/mac/code/golangproject/src/code/code_sub/11/11.txt
39**/

递归遍历文件夹并打印层级1

 1//打印层级
 2func GetALLX(path string, level int) {
 3	levelstr := ""
 4	realLevel := level
 5	if level == 1 {
 6		levelstr = "|--"
 7	} else {
 8		for ; level > 1; level-- {
 9			levelstr += "|--"
10		}
11		levelstr += "|--"
12	}
13	read, err := ioutil.ReadDir(path) //读取文件夹
14	if err != nil {
15		return
16	}
17	for _, fi := range read { //循环每个文件或者文件夹
18		if fi.IsDir() { //判断是否文件夹
19			fulldir := path + "/" + fi.Name() //构造新的路径
20			fmt.Println(strconv.Itoa(realLevel) + levelstr + fulldir)
21			GetALLX(fulldir, realLevel+1) //文件夹递归处理
22
23		} else {
24			fulldir := path + "/" + fi.Name() //构造新的路径
25			fmt.Println(strconv.Itoa(realLevel) + levelstr + fulldir)
26		}
27	}
28}
29func main() {
30	path := "/Users/mac/code/golangproject/src/code"
31	GetALLX(path, 1)
32}
33/**
341|--/Users/mac/code/golangproject/src/code/code_sub
352|--|--/Users/mac/code/golangproject/src/code/code_sub/11
363|--|--|--/Users/mac/code/golangproject/src/code/code_sub/11/11.txt
372|--|--/Users/mac/code/golangproject/src/code/code_sub/sub1.txt
381|--/Users/mac/code/golangproject/src/code/datastruct
392|--|--/Users/mac/code/golangproject/src/code/datastruct/data.go
402|--|--/Users/mac/code/golangproject/src/code/datastruct/digui.go
411|--/Users/mac/code/golangproject/src/code/test1
422|--|--/Users/mac/code/golangproject/src/code/test1/11.txt
431|--/Users/mac/code/golangproject/src/code/yy
442|--|--/Users/mac/code/golangproject/src/code/yy/1.txt
45**/

递归遍历文件夹并打印层级2

 1func GetALLY(path string, files []string,level int) ([]string, error) {
 2	read, err := ioutil.ReadDir(path) //读取文件夹
 3	if err != nil {
 4		return files, errors.New("文件加不可读取")
 5	}
 6	levelstr := ""
 7	realLevel := level
 8	if level == 1 {
 9		levelstr = "|--"
10	} else {
11		for ; level > 1; level-- {
12			levelstr += "|--"
13		}
14		levelstr += "|--"
15	}
16
17
18	for _, fi := range read { //循环每个文件或者文件夹
19		if fi.IsDir() { //判断是否文件夹
20			fulldir := path + "/" + fi.Name() //构造新的路径
21			files = append(files, strconv.Itoa(realLevel)+levelstr+fulldir)    //追加路径
22			fmt.Println(fulldir)
23			files, _ = GetALLY(fulldir, files,realLevel+1) //文件夹递归处理
24		} else {
25			fulldir := path + "/" + fi.Name() //构造新的路径
26			files = append(files, strconv.Itoa(realLevel)+levelstr+fulldir)    //追加路径
27		}
28	}
29	return files, nil
30}
31
32func main() {
33	path := "/Users/mac/code/golangproject/src/code"
34	files := []string{}            //数组字符串
35	files, _ = GetALLY(path, files,1) //抓取所有文件
36	for i := 0; i < len(files); i++ { //打印路径
37		fmt.Println(files[i])
38	}
39}

链表栈

 1package stack
 2
 3type Node struct {
 4	data  interface{}
 5	pNext *Node
 6}
 7
 8type LinkStack interface {
 9	IsEmpty() bool
10	Push(data interface{})
11	Pop() interface{}
12	Length() int
13}
14
15func NewLinkStack() *Node {
16	return &Node{} //返回一个节点指针
17}
18
19func (n *Node) IsEmpty() bool {
20	if n.pNext == nil {
21		return true
22	} else {
23		return false
24	}
25}
26func (n *Node) Push(data interface{}) {
27	newnode := &Node{data, nil}
28	newnode.pNext = n.pNext
29	n.pNext = newnode //头部插入
30}
31
32//栈操作方向,
33func (n *Node) Pop() interface{} {
34	if n.IsEmpty() == true {
35		return nil
36	}
37	value := n.pNext.data   //要弹出的数据
38	n.pNext = n.pNext.pNext //删除
39	return value
40}
41
42func (n *Node) Length() int {
43	pnext := n
44	length := 0
45	for pnext.pNext != nil {
46		pnext = pnext.pNext //节点循环跳跃
47		length++            //追加
48	}
49	return length //返回长度
50}
 1package stack
 2
 3import (
 4	"fmt"
 5	"github.com/stretchr/testify/assert"
 6	"testing"
 7)
 8
 9func TestLinkStack(t *testing.T) {
10	arraystack := NewLinkStack()
11	arraystack.Push(1)
12	arraystack.Push(2)
13	v := arraystack.Pop()
14	t.Log(v)
15	assert.Equal(t, 2, v)
16	v = arraystack.Pop()
17	t.Log(v)
18	assert.Equal(t, 1, v)
19
20	for i := 0; i < 10000000; i++ {
21		arraystack.Push(i)
22	}
23
24	for data := arraystack.Pop(); data != nil; data = arraystack.Pop() {
25		fmt.Println(data)
26	}
27}