1 Star 0 Fork 0

ljfirst/algo-go-sdk

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
DuplexList.go 2.03 KB
一键复制 编辑 原始数据 按行查看 历史
ljfirst 提交于 2023-09-13 09:41 +08:00 . feat: new add
package list
import (
C "gitee.com/ljfirst/algo-go-sdk/common/constant"
)
/**
* @author ljfirst
* @version V1.0
* @date 2023/6/28 17:03
* @author-Email ljfirst@mail.ustc.edu.cn
* @blogURL https://blog.csdn.net/ljfirst
* @description
* */
type DuplexList struct {
head *Node // 带有头节点的list
tail *Node
size int
headInsert bool // 头插法,默认false是尾插法
}
func NewDuplexList(options ...C.Options) *DuplexList {
opt := C.NewOptions(options...)
temp := &Node{}
list := &DuplexList{
head: temp,
tail: temp,
size: 0,
}
list.headInsert = opt.HeadInsert
return list
}
func (m *DuplexList) Insert(node ...*Node) { // 尾插入
if node == nil || len(node) == 0 {
return
}
if m.headInsert {
for i := 0; i < len(node); i++ {
node[i].Next = m.head.Next
m.head.Next = node[i]
}
// 链表为空时的首个元素处理
if m.head.Next == nil {
m.tail = node[0]
}
return
}
// 尾插法
for i := 0; i < len(node); i++ {
m.tail.Next = node[i]
m.tail = node[i]
}
}
func (m *DuplexList) InsertIndex(index int, nodes ...*Node) { // index 起始为0
for _, n := range nodes {
node := n
if node == nil {
return
}
// 空链表插入首个节点
if index == 0 && m.head == nil {
m.Insert(node)
return
}
// 插入首个节点
if index == 0 {
}
if index >= m.size {
return
}
count := 0
point := m.head
for count < index {
point = point.Next
count++
}
if point.Next != nil {
next := point.Next
next.Pre = node
node.Next = next
}
point.Next = node
node.Pre = point
}
}
func (m *DuplexList) Delete(node *Node) {}
func (m *DuplexList) DeleteIndex(int) {}
func (m *DuplexList) ValueSet() []interface{} {
return nil
}
func (m *DuplexList) Equals(node *Node) {}
func (m *DuplexList) Size() int { return 0 }
func (m *DuplexList) Clear() {}
func (m *DuplexList) GetAttribute() *C.Attribute {
return &C.Attribute{
Tags: []string{C.List},
Desc: &C.Desc{
Name: "DuplexList",
NameCn: "双向链表",
},
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ljfirst/algo-go-sdk.git
git@gitee.com:ljfirst/algo-go-sdk.git
ljfirst
algo-go-sdk
algo-go-sdk
v1.0.3

搜索帮助