class LinkedList constructor: -> # 首节点 @_head = null # 添加节点 add: (data) -> # 创建新节点 node = data: data, next:null # 如果首节点为空, 则进行赋值; 否则设置遍历的起点为首节点 current = @_head or = node # 判断是否只有一个节点 if @_head isnt node # 遍历至最后一个节点 current = current.next while current.next # 添加新节点至队列尾部 current.next = node # 返回队列 this # 查找节点 item: (index) -> # 负数处理 return null if index < 0 # 第一个节点 current = @_head or null i = -1 # 遍历, 直至节点不会空且序号为index current = current.next while current and index > ++i # 返回节点的data值 current and current.data # 删除节点 remove: (index) -> # 负数处理 return null if index < 0 current = @_head or null i = -1 # 当序号为0时 if index is 0 # 则将首节点的下一个节点提升为子节点 @_head = current.next else # 序号>0时, 遍历获取目标节点以及其上一个节点 [previous, current] = [current, current.next] while index > ++i # 将目标节点的上下级节点关联, 从而达到删除目标节点的效果 previous.next = current.next # 返回被删除的目标节点的data值 current and current.data # 计算队列大小 size: -> current = @_head count = 0 while current count += 1 current = current.next count # 转化为Array toArray: -> result = [] current = @_head while current result.push current.data current = current.next result # 转化为字符串 toString: -> @toArray().toString()
相关推荐
模拟实现 LinkedList 测试类
链表类LinkedList的完全c++实现,根据数据结构与算法课堂总结。
C语言实现LinkedList
实现一个链表LinkedList,要求使用链表机构实现,并提供相应的add(Object o),remove(Object o)这两个方法.
数据结构,LinkedList在c++的实现,一共有三个头文件:Link.h 、List.h 、LinkedList。
自定义实现的ArrayList数据结构,有大量注释以及编写思路,帮助新手用Java语言来实现数据结构
阶段练习:实现链表(LinkedList) 简介:写一个链表的数据结构,要求实现IList接口。 具体要求: 1、 使用代码规范。 2、 至少对IList中的Add,Remove,Insert,Indexer,IEnumerator进行单元测试。 3、 对上述每个...
复习内容
LinkedList实现栈。对LinkedList添加同步,可以多线程操作。
java中LinkedList集合类实现栈和队列.docjava中LinkedList集合类实现栈和队列.docjava中LinkedList集合类实现栈和队列.docjava中LinkedList集合类实现栈和队列.docjava中LinkedList集合类实现栈和队列.doc
源码解析jdk7.0集合:LinkedList的底层实现原理.pdf
List<String> l1=new LinkedList();//使用泛型 //List l1= new LinkedList(); int index=0; //插入元素 l1.add("firstElement"); l1.add("secondElement"); l1.add("thirdElement"); //访问索引 ...
栈是先进先出的原则,该类实现了栈的移入移除
List-LinkedList 单链表就地反转 的代码的一种实现。
LinkedList一个简单的双链表实现,提供了Append、Prepend、Remove、ForEach、ForEachRev、Map、Filter、Reduce
使用LinkedList模拟堆栈操作,包括进栈、出栈,入队、出队
javascript 集合类 LinkedList代码实现
相信大家都明白 LinkedList 是基于双向链表而实现的,本篇文章主要讲解一下双向链表的实现,并且我们参考 LinkedList 自己实现一个单链表尝试一下。 什么是链表? 简单的来讲一下什么是链表:首先链表是一种线性的...
使用LinkedList类编写程序,用某种集合接口的实现类作存储,实现具有自定义排序功能的包含姓名、年龄、身高、职称等内容的人事信息输入和打印。