Go编写一个Linktable的库

锦衣夜行孙大侠 2022-04-05 17:30:01

1. LinkNode 实现

LinkNode作为一个“单纯”的节点数据结构,内容很简单,如下:

package main

type LinkTableNode struct {
	pNext *LinkTableNode
}

2. LinkTable 实现

LinkTable数据结构本身有3个对象(头指针,尾指针,链表内节点数量),5个方法(增、删、查、获取头结点、获取指定节点的下一个节点),实现方式如下:

2.1 LinkTable struct

三个对象(头指针,尾指针,链表内节点数量)

type LinkTable struct {
	pHead     *LinkTableNode
	pTail     *LinkTableNode
	SumOfNode int
}

五个方法(增、删、查、获取头结点、获取指定节点的下一个节点)


// 链表组合函数-为链表添加节点(尾插法)
func (pLinkTable *LinkTable) AddLinkTableNode(pNode *LinkTableNode) *LinkTable {
	if pLinkTable == nil {
		fmt.Println("Can't add the Node to the LinkTable, because the LinkTable is nil!")
		return pLinkTable
	}
	if pNode == nil {
		fmt.Println("Can't add the Node to the LinkTable, because the Node is nil!")
		return pLinkTable
	}
	// 检查是否当前列表为空,若为空,应以当前节点作为链表的第一个节点
	if pLinkTable.pHead == nil && pLinkTable.pTail == nil {
		pLinkTable.pHead = pNode
		pLinkTable.pTail = pNode
	} else {
		pLinkTable.pTail.pNext = pNode
		pLinkTable.pTail = pNode
	}
	pLinkTable.pTail.pNext = nil
	pLinkTable.SumOfNode += 1
	return nil
}

//链表组合函数-为链表删除指定节点
func (pLinkTable *LinkTable) DelLinkTableNode(pNode *LinkTableNode) *LinkTable {
	if pLinkTable == nil {
		fmt.Println("Can't delete the Node from the LinkTable, because the LinkTable is nil!")
		return pLinkTable
	}
	if pNode == nil {
		fmt.Println("Can't delete the Node from the LinkTable, because the Node is nil!")
		return pLinkTable
	}
	if pLinkTable.pHead == nil {
		fmt.Println("Can't delete the Node from the LinkTable, because the LinkTable doesn't have any Node!")
		return pLinkTable
	}
	var pWork *LinkTableNode = pLinkTable.pHead
	var pre *LinkTableNode = pWork
	if pLinkTable.pHead == pNode {
		pLinkTable.pHead = pWork.pNext
		pLinkTable.SumOfNode--
		return nil
	}
	pWork = pWork.pNext
	for pWork != nil {
		if pWork == pNode {
			pre.pNext = pWork.pNext
			pLinkTable.SumOfNode--
			return nil
		}
		pre = pWork
		pWork = pWork.pNext
	}
	fmt.Println("Can't delete the Node from the LinkTable, because the Node doesn't exist!")
	return pLinkTable
}

//链表组合函数-在链表中查询指定节点
func (pLinkTable *LinkTable) SearchLinkTableNode(pNode *LinkTableNode) (li *LinkTableNode) {
	if pLinkTable == nil {
		fmt.Println("Can't search for the Node in the LinkTable, because the LinkTable is nil!")
		return nil
	}
	if pNode == nil {
		fmt.Println("Can't search for the Node in the LinkTable, because the Node is nil!")
		return nil
	}
	var tmpNode *LinkTableNode = pLinkTable.pHead
	for pNode != nil {
		if tmpNode == pNode {
			return tmpNode
		}
		pNode = pNode.pNext
	}
	fmt.Println("Can't search for the Node in the LinkTable, because the Node doesn't exist!")
	return nil
}

//链表组合函数-获取LinkTable的头节点
func (pLinkTable *LinkTable) getLinkTableHead() *LinkTableNode {
	if pLinkTable == nil {
		fmt.Println("Can't get the Head of the LinkTable, because the LinkTable is nil!")
		return nil
	}
	return pLinkTable.pHead
}

//链表组合函数-获取指定节点的下一个节点
func (pLinkTable *LinkTable) getNextLinkTableNode(pNode *LinkTableNode) *LinkTableNode {
	if pLinkTable == nil {
		fmt.Println("Can't get the Next Node of the LinkTable, because the LinkTable is nil!")
		return nil
	}
	if pNode == nil {
		fmt.Println("Can't get the Next Node of the LinkTable, because the Node is nil!")
		return nil
	}
	var pWork *LinkTableNode = pLinkTable.pHead
	for pWork != nil {
		if pWork == pNode {
			return pWork.pNext
		}
		pWork = pWork.pNext
	}
	fmt.Println("Can't get the Next Node of the LinkTable, because the Node doesn't exist!")
	return nil
}

2.2 创建、删除链表

为更好的封装,提供了创建和删除链表的函数


// 创建链表
func CreateLinkTable() *LinkTable {
	var pLinkTable *LinkTable = new(LinkTable)
	if pLinkTable == nil {
		return nil
	}
	pLinkTable.pHead = nil
	pLinkTable.pTail = nil
	pLinkTable.SumOfNode = 0
	return pLinkTable
}

// 删除链表
func DeleteLinkTable(pLinkTable *LinkTable) *LinkTable {
	if pLinkTable == nil {
		fmt.Println("Can't delete the LinkTable, because the LinkTable is nil!")
		return pLinkTable
	}
	pLinkTable.pHead = nil
	pLinkTable.pTail = nil
	pLinkTable.SumOfNode = 0
	return nil
}

3. 编写测试函数

package main

import (
	"fmt"
)

func main() {
	tLinkTable := CreateLinkTable()
	if tLinkTable == nil {
		fmt.Println("Can't create a LinkTable, please try again!")
		return
	}

	tLinkTable.SearchLinkTableNode(nil)

	tmpLinkTableNode1 := LinkTableNode{nil}
	tLinkTable.DelLinkTableNode(&tmpLinkTableNode1)
	tLinkTable.AddLinkTableNode(&tmpLinkTableNode1)

	tmpLinkTableNode2 := LinkTableNode{nil}
	tLinkTable.AddLinkTableNode(&tmpLinkTableNode2)

	if tLinkTable.getNextLinkTableNode(&tmpLinkTableNode1) != nil {
		fmt.Println("Sucess get the next Node!")
	}

	if tLinkTable.SearchLinkTableNode(&tmpLinkTableNode1) != nil {
		fmt.Println("Sucess search the Node!")
	}

	if tLinkTable.getLinkTableHead() != nil {
		fmt.Println("Sucess get the Head Node!")
	}

}

运行效果如下图所示:

 4. 更新代码到版本库

git status

 

git add .
git status

 

 git commit -m "Sun commit dir:LinkTable"

git push

 

 

 

 

 

代码的版本库链接

 学号:389

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

571

社区成员

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

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