571
社区成员
发帖
与我相关
我的任务
分享LinkNode作为一个“单纯”的节点数据结构,内容很简单,如下:
package main
type LinkTableNode struct {
pNext *LinkTableNode
}
LinkTable数据结构本身有3个对象(头指针,尾指针,链表内节点数量),5个方法(增、删、查、获取头结点、获取指定节点的下一个节点),实现方式如下:
三个对象(头指针,尾指针,链表内节点数量)
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
}
为更好的封装,提供了创建和删除链表的函数
// 创建链表
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
}
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!")
}
}
运行效果如下图所示:

git status

git add .
git status

git commit -m "Sun commit dir:LinkTable"

git push


学号:389