【AHK v2】数据结构LinkedList实现示例

AutoHotkey v2 是一个功能强大的脚本语言,它支持面向对象的编程范式。下面是一个简单的面向对象的链表(LinkedList)实现示例,使用AutoHotkey v2编写:

复制代码
#Requires AutoHotkey v2.0
; 定义节点类
class Node {
    __New(value) {  
        this.value := value
        this.next := ""
    }
}

; 定义链表类
class LinkedList {
    ; 初始化链表
    __New() {
        this.head := ""
    }

    ; 向链表尾部添加元素
    Add(value) {
        _node := Node(value)
        if (!this.head) {
            this.head := _node
        } else {
            current := this.head
            Loop {
                if (!current.next) {
                    break
                }
                current := current.next
            }
            current.next := _node
        }
    }

    ; 根据索引获取元素
    Get(index) {
        current := this.head
        ;这里采用AHK习惯第一个元素索引为1,如果想改成0为第一个的话,可改为 idx := 0
        idx := 1
        while (current) {
            if (idx == index) {
                return current.value
            }
            idx++
            current := current.next
        }
        return -1 ; 索引无效
    }

    ; 移除元素(按值)
    Remove(value) {
        prev := ""
        current := this.head
        while (current) {
            if (current.value == value) {
                if (!prev) {
                    this.head := current.next
                } else {
                    prev.next := current.next
                }
                break
            }
            prev := current
            current := current.next
        }
    }

    ; 获取链表长度
    Length() {
        count := 0
        current := this.head
        while (current) {
            count++
            current := current.next
        }
        return count
    }

    ; 打印链表
    Print() {
        current := this.head
        while (current) {
            MsgBox "Value: " current.value
            current := current.next
        }
    }
}

; 使用示例
list := LinkedList()
list.Add(1)
list.Add(2)
list.Add(3)
list.Print()
MsgBox "原链表长度:" list.Length()
indexValue := list.Get(1) ; 获取索引为1的元素值
MsgBox  "Index 1 Value: " indexValue

list.Remove(2) ; 移除值为2的元素
list.Print()
MsgBox "新链表长度:" list.Length()

这个示例中,我们定义了两个类:NodeLinkedListNode类代表链表中的一个节点,包含一个值(value)和一个指向下一个节点的引用(next)。

LinkedList类代表整个链表,包含以下方法:

  • Add(value): 向链表尾部添加一个新节点。
  • Get(index): 根据索引获取链表中的元素。
  • Remove(value): 移除链表中第一个值为指定值的节点。
  • Length(): 获取链表的长度。
  • Print(): 打印链表中的所有元素。

在使用示例中,我们创建了一个LinkedList实例,添加了几个元素,然后展示了如何获取元素、移除元素和打印链表。这个简单的链表实现可以作为学习AutoHotkey v2面向对象编程的起点。

相关推荐
zxctsclrjjjcph5 小时前
【高并发内存池】从零到一的项目之centralcache整体结构设计及核心实现
开发语言·数据结构·c++·链表
柃歌6 小时前
【LeetCode Solutions】LeetCode 176 ~ 180 题解
数据结构·数据库·sql·算法·leetcode
轮到我狗叫了10 小时前
力扣智慧思想小题,目录力扣.跳跃游戏(思想很重要)力扣.跳跃游戏II(还是思想)力扣.分发糖果力扣151.反转字符串中的单词力扣.轮转数组
数据结构·算法·leetcode
10000hours10 小时前
【SGL】Scatter-Gather List内存传输技术
linux·数据结构·网络协议·list·存储·sgl
海盐泡泡龟11 小时前
ES6新增Set、Map两种数据结构、WeakMap、WeakSet举例说明详细。(含DeepSeek讲解)
前端·数据结构·es6
少了一只鹅11 小时前
深入理解指针(3)
c语言·数据结构·算法
似水এ᭄往昔12 小时前
【数据结构】——双向链表
c语言·数据结构·c++·链表
有一个好名字12 小时前
力扣:轮转数组
数据结构·算法·leetcode
fpcc12 小时前
跟我学C++中级篇——STL容器的查找对比
数据结构·c++
小兔子25313 小时前
嵌入式培训之C语言学习完(十七)结构体、共用体、枚举、typedef关键字与位运算
数据结构