实现双向链表计算运势

本文涉及的产品
实时数仓Hologres,5000CU*H 100GB 3个月
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
智能开放搜索 OpenSearch行业算法版,1GB 20LCU 1个月
简介: 【5月更文挑战第4天】本文提供链表构造、判断空链表、头部添加和尾部添加节点的方法,以及显示链表值的功能。这些将在后续的运势解读部分发挥作用

占卜算卦是一个古老的方法,人们无把握又希望应对未来时,经常通过这个方法获得一些确定性的心理效应。

其社会影响深远而广泛,虽然它并不是一种完全准确的科学,但我们可以通过go实现 一个简易的占卜程序,用以了解我们的古人如何处理不确定性的。 并尝试了解其中的变化规则。

本文介绍了双向链表的实现,用于存储卦象的爻信息。定义了包含numberyaobianprevnextnode结构体,以及具有lensheadtaildlist结构体。

1 双向链表的实现

我们可以通过双向链表 存储卦象的6个爻,这样我们可以知道前后顺序,并且可以在每个节点存储变爻的值,为此实现其结构体 和 链表

定义链表结构体

type node struct {
        number  int    //爻值
        yaobian [][]int   //三次爻变的 具体算子
        prev    *node   //前一个爻 节点
        next    *node   //后一个爻 节点
    }

定义双向链表

type dlist struct {
        lens int
        head *node
        tail *node
    }

链表构造函数

func makeDlist() *dlist {
        return &dlist{}
    }

判断是否空链表

func (this *dlist) newNodeList(n *node) bool { 

    if this.lens == 0 {
        this.head = n
        this.tail = n
        n.prev = nil
        n.next = nil
        this.lens += 1
        return true
    } else {
        Logg.Panic("not empty node list.")
    }
    return false
}

头部添加 节点

func (this *dlist) pushHead(n *node) bool {

    if this.lens == 0 {
        return this.newNodeList(n)
    } else {
        this.head.prev = n
        n.prev = nil
        n.next = this.head
        this.head = n
        this.lens += 1
        return true
    }
}

添加尾部节点,我们主要使用此方法,用以保持爻的相对位置

func (this *dlist) append(n *node) bool {

    if this.lens == 0 {
        return this.newNodeList(n)
    } else {
        this.tail.next = n
        n.prev = this.tail
        n.next = nil
        this.tail = n
        this.lens += 1
        return true
    }
}

显示并返回链表的值

func (this *dlist) display() []int {

    numbs := []int{}
    node := this.head
    t := 0 
    for node != nil {

        Logg.Println(node.number, node.yaobian)
        numbs = append(numbs, node.number)
        t += 1
        if t >= this.lens {
            break
        }

        node = node.next
    }

    fmt.Println("length:", this.lens)
    return numbs
}

2 结语

本小节我们实现了 一个简单的双向链表,包括其链表节点 和 链表管理,添加和查询操作,这将在第三节运势解读中得到运用。

目录
相关文章
|
4月前
|
Go
实现双向链表的运势解读
【5月更文挑战第4天】我们实现了用Go语言处理周易卦象的功能。 这个项目结合了双向链表和周易知识,有助于理解两者并提供运势占卜功能。
27 0
|
4月前
|
算法
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
|
4月前
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
|
3月前
|
存储 SQL 算法
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
|
3月前
|
存储 SQL 算法
LeetCode 题目 86:分隔链表
LeetCode 题目 86:分隔链表
|
3月前
|
存储 算法 Java
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
27 2
|
4月前
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点.
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点
43 1
|
3月前
|
算法
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
|
3月前
|
存储 SQL 算法
LeetCode 83题:删除排序链表中的重复元素【面试】
LeetCode 83题:删除排序链表中的重复元素【面试】
|
3月前
|
存储 SQL 算法
LeetCode 题目 82:删除排序链表中的重复元素 II
LeetCode 题目 82:删除排序链表中的重复元素 II