用Go语言实现LinkTable

Amor 2022-04-06 22:38:28

编码实现

    用Go语言实现LinkTable库,更新menu项目

func main() {
	list := &LinkTable{
		head:      nil,
		tail:      nil,
		SumofNode: 0,
	}
	cmd1 := createCmd("order", "show order Cmd", new(cmd_order))
	cmd2 := createCmd("version", "show version Cmd", new(cmd_version))
	cmd3 := createCmd("help", "show help Cmd", new(cmd_help))
	cmd4 := createCmd("quit", "show quit Cmd", new(cmd_quit))
	list.insertNode(cmd1)
	list.insertNode(cmd2)
	list.insertNode(cmd3)
	list.insertNode(cmd4)
	list.delectNode(cmd2)
	for {
		showAllCmd()
		var command string
		fmt.Scan(&command)
		list.execCmd(command)
		if command == "quit" {
			break
		}

	}
}
func showAllCmd() {
	fmt.Print("---------------Menu v 2.0---------------\n>")
	fmt.Print("            Enter your choice           \n>")
	fmt.Print("   1. order                2. version   \n>")
	fmt.Print("   3. help                 4. quit      \n>")
}

一、定义数据结构

1.1定义数据结构

首先定义链表的数据结构以及command的数据结构

type LinkTable struct {
	head      *CmdNode //头节点
	tail      *CmdNode //尾节点
	SumofNode int      //节点总数
}
type CmdNode struct {
	cmd         string   //指令名称
	desc        string   //指令描述
	cmd_handler handler  //执行方法
	next        *CmdNode //下个节点
}
type handler interface {
	exec()
}

二、链表方法函数定义

2.1链表创建command

func createCmd(name string, desc string, handler handler) *CmdNode {
	return &CmdNode{
		cmd:         name,
		desc:        desc,
		cmd_handler: handler,
	}
}

2.2链表执行command

func (this *LinkTable) execCmd(name string) error {
	node := this.findNode(name)
	if node == nil {
		println("no such cmd!")
		return nil
	}
	node.cmd_handler.exec()
	return nil
}

2.3链表插入结点

func (this *LinkTable) insertNode(node *CmdNode) error {
	//判断是否存在
	if this.findNode(node.cmd) != nil {
		return errors.New("The command already exists!")
	}
	//判断是否存在头节点
	if this.head == nil {
		this.head = node
		this.tail = node
		this.SumofNode = 1
		return nil
	}
	//存在头节点
	this.SumofNode++
	this.tail.next = node
	this.tail = this.tail.next
	return nil
}

2.4链表删除节点

func (this *LinkTable) delectNode(node *CmdNode) error {
	//判断是否存在
	if this.findNode(node.cmd) == nil {
		return errors.New("The command does not exist!")
	}
	var temp_head *CmdNode = this.head
	var temp_tail *CmdNode = this.tail
	temp_head.next = temp_tail
	return nil
}

2.5链表查找结点

func (this *LinkTable) findNode(cmd string) *CmdNode {
	for n := this.head; n != nil; n = n.next {
		if n.cmd == cmd {
			return n
		}
	}
	return nil
}

三、创建命令

type cmd_order struct {
}

func (this cmd_order) exec() {
	println("This is order command!")
}

type cmd_help struct {
}

func (this cmd_help) exec() {
	println("This is help command!")
}

type cmd_version struct {
}

func (this cmd_version) exec() {
	println("Menu verison = 2.0!")
}

type cmd_quit struct {
}

func (this cmd_quit) exec() {
	println("You have successfully exited")
}

运行结果如下:

 

四、上传至gitee

 

 

 作者:560

...全文
85 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

571

社区成员

发帖
与我相关
我的任务
社区描述
软件工程教学新范式,强化专项技能训练+基于项目的学习PBL。Git仓库:https://gitee.com/mengning997/se
软件工程 高校
社区管理员
  • 码农孟宁
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧