学习JavaScript数据结构与算法(四)——循环链表
来源:互联网 发布:aim聊天软件 编辑:程序博客网 时间:2024/06/05 23:49
循环链表可以向普通链表一样只有单向引用,也可以像双向链表一样有双向引用。
循环链表和普通链表的区别就在于,循环链表的最后一个元素指向下一个元素的指针(tail.next)不是引用null,而是指向第一个元素(head)。
双向循环链表有指向head元素的tail.next,和指向tail元素的head.prev。
function CircularLinkedList() { var Node = function(element){ this.element = element; this.next = null; }; var length = 0; var head = null; this.append = function(element){ var node = new Node(element), current; //向为空的列表添加一个元素 if (head === null){ head = node; } else { current = head; //循环列表,直到找到最后一项 //列表的最后一个节点的下一个元素指向head而不是null while(current.next !== head){ current = current.next; } //找到最后一项,将其next赋为node,建立链接 current.next = node; } //将插入的节点的next赋为head,建立链接 node.next = head; length++;//更新列表的长度 }; this.insert = function(position, element){ //检查越界值 if (position >= 0 && position <= length){ var node = new Node(element), current = head, previous, index = 0; //在列表的起点添加一个元素 if (position === 0){ //如果列表中没有节点 if(!head){ head = node; node.next = head; } else { node.next = current; //更新最后一个元素 while(current.next !== head){ current = current.next; } head = node; current.next = head; } } //在列表中间或尾部添加一个元素 else { while (index++ < position){ previous = current; current = current.next; } node.next = current; previous.next = node; } length++;//更新列表长度 return true; } //越界就返回false,表示没有添加项到列表中 else { return false; } }; this.removeAt = function(position){ //检查越界值 if (position > -1 && position < length){ var current = head, previous, index = 0; //移除第一项 if (position === 0){ while(current.next !== head){ current = current.next; } head = head.next; current.next = head; } else { while (index++ < position){ previous = current; current = current.next; } previous.next = current.next; } length--; return current.element; } else { return null; } }; this.remove = function(element){ var index = this.indexOf(element); return this.removeAt(index); }; this.indexOf = function(element){ var current = head, index = -1; //检查第一项 if (element == current.element){ return 0; } index++; //检查列表中的元素 while(current.next !== head){ if (element == current.element){ return index; } current = current.next; index++; } //检查最后一项 if (element == current.element){ return index; } return -1; }; this.isEmpty = function() { return length === 0; }; this.size = function() { return length; }; this.getHead = function(){ return head; }; this.toString = function(){ var current = head, s = current.element; while(current.next !== head){ current = current.next; s += ', ' + current.element; } return s.toString(); }; this.print = function(){ console.log(this.toString()); };}
测试代码如下:
var circularLinkedList = new CircularLinkedList();circularLinkedList.append(15);circularLinkedList.print();circularLinkedList.append(16);circularLinkedList.print();circularLinkedList.insert(0,14);circularLinkedList.print();circularLinkedList.insert(1,14.5);circularLinkedList.print();circularLinkedList.insert(4,17);circularLinkedList.print();circularLinkedList.removeAt(0);circularLinkedList.print();circularLinkedList.removeAt(1);circularLinkedList.print();circularLinkedList.removeAt(2);circularLinkedList.print();console.log(circularLinkedList.indexOf(14.5));console.log(circularLinkedList.indexOf(16));
在控制台上输出结果如下:
阅读全文
0 0
- 学习JavaScript数据结构与算法(四)——循环链表
- 学习JavaScript数据结构与算法(四)——链表
- 学习JavaScript数据结构与算法(四)——双向链表
- 数据结构与算法Javascript描述(五)循环链表
- 数据结构与算法—循环链表
- 数据结构与算法Javascript描述(四)双向链表
- 学习JavaScript数据结构与算法(一)——数组
- 学习JavaScript数据结构与算法(二)——栈
- 学习JavaScript数据结构与算法(三)——队列
- 学习JavaScript数据结构与算法(五)——集合
- 学习JavaScript数据结构与算法(六)——字典
- C++数据结构与算法——单向循环链表
- 数据结构(四)——循环链表与双向链表
- 数据结构与算法专题之线性表——链表(三)循环链表
- 数据结构与算法——线性表链式存储(双向循环链表)
- 算法与数据结构【四】——C语言实现循环队列
- 算法与数据结构之循环链表
- 数据结构与算法——循环队列
- CSS cursor 属性
- Hive内部表与外部表(外部表使用场景)
- POJ 1573--Robot Motion
- 【STM32小案例 06 】STM32使用按键驱动L298N电机模块控制电机正反转
- HDU1166 敌兵布阵
- 学习JavaScript数据结构与算法(四)——循环链表
- Remove Duplicates from Sorted Array II
- 看起来像它——图像搜索其实也不难
- 标准IO缓存
- js 时间-date
- 一些oracle相关的链接
- company
- 循环,数组,参数变量
- HDU1556 Color the ball