程序员社区

Go实现一个简单的双向链表

定义:双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。

包含操作

  • 创建双向链表
  • 向链表添加元素
  • 单步向前遍历节点
  • 单步向后遍历节点
  • 打印链表所有元素
package main

import "fmt"

// a bidirectional list node that holds a single int
type list struct {
    number int
    next   *list
    prev   *list
}

// newList initializes the list with a new number
func newList(number int) *list {
    return &list{
        number: number,
    }
}

// pushNumber appends numbers to the end of the list with each call
func (numbers *list) pushNumber(number int) error {
    currentNode := numbers
    for currentNode.next != nil {
        currentNode = currentNode.next
    }
    currentNode.next = newList(number)
    currentNode.next.prev = currentNode
    return nil
}

// goBack traverses backwards by 1 node step
func goBack(numbers *list) *list {
    if numbers.prev == nil {
        fmt.Println("Can't go back")
    } else {
        numbers = numbers.prev
    }
    return numbers
}

// goForward traverses forward with by 1 node step.
func goForward(numbers *list) *list {
    if numbers.next == nil {
        fmt.Println("Can't go forward")
    } else {
        numbers = numbers.next
    }
    return numbers
}

// printList prints out each number at each node
func (numbers *list) printList() {
    currentNode := numbers
    for currentNode != nil {
        fmt.Printf("%+v ", currentNode.number)
        currentNode = currentNode.next
    }
}

// testing it all out
func main() {
    listing := newList(1)
    listing.pushNumber(2)
    listing.pushNumber(3)
    listing.pushNumber(4)
    listing.pushNumber(5)
    listing = goForward(listing)
    listing = goForward(listing)
    listing = goBack(listing)
    listing.printList()
}

根据以上双向链表的特点,实现一个虚拟文件系统,包含操作:

  • 创建一个目录
  • 在目录中添加文件
  • 添加子目录

这里使用map来存放目录中的文件key是文件名,value是文件内容

package main
import "fmt"
// Dummy payload that represents a directory example
type list struct {
 path string             // represents the directory name.
 files map[string]string // a map that represents the files in this directory
 directories []list      // a sub list containing all the directories in this directory
}
// newNumber creates a directory - this function is poorly named
func newNumber(name string) *list {
 return &list{
  path: name,
 }
}
// addDirectory adds a new directory to the current directory
func (fs *list) addDirectory(dir_name string) error {
 new_dir := list{path: dir_name}
 fs.directories = append(fs.directories, new_dir) 
 return nil
}

// createFile creates a file and its contents in a directory
func createFile(file string, content string, value      map[string]string) map[string]string {
 value[file] = content
 return value
}
// A few implementation tests
func main() {
 fs := newNumber("./")
 placeholder := make(map[string]string)
 fs.files = placeholder
 fs.files = createFile("hello.txt", "hello world", fs.files)
 fs.files = createFile("gone", fs.files)
 fmt.Println(fs.files)
 fs.addDirectory("Desktop")
 fs.addDirectory("Users")
 fmt.Println(fs.path)
 fmt.Println(fs.directories[1])
 fs.directories[0].addDirectory("NewWorkFolder")
 fmt.Println(fs.directories[0])
}
赞(0) 打赏
未经允许不得转载:IDEA激活码 » Go实现一个简单的双向链表

一个分享Java & Python知识的社区