链表的实现

一. 单向链表

  1. // Node类
  2. function Node (element) {
  3. this.element = element;
  4. this.next = null;
  5. }
  6. // LinkedList类
  7. function LList () {
  8. this.head = new Node('head');
  9. this.find = find;
  10. this.insert = insert;
  11. this.findPrevious = findPrevious;
  12. this.remove = remove;
  13. this.display = display;
  14. }
  15. // 查找
  16. function find (item) {
  17. let currNode = this.head;
  18. while (currNode.element !== item) {
  19. currNode = currNode.next;
  20. }
  21. return currNode;
  22. }
  23. // 插入
  24. function insert (newElement, item) {
  25. let newNode = new Node(newElement);
  26. let current = this.find(item);
  27. newNode.next = current.next;
  28. current.next = newNode;
  29. }
  30. // 显示
  31. function display () {
  32. let currNode = this.head;
  33. while (currNode.next !== null) {
  34. currNode = currNode.next;
  35. console.log(currNode.element);
  36. }
  37. }
  38. // 检查下一个节点
  39. function findPrevious (item) {
  40. let currNode = this.head;
  41. while (currNode.next !== null && currNode.next.element !== item) {
  42. currNode = currNode.next;
  43. }
  44. return currNode;
  45. }
  46. // 删除
  47. function remove (item) {
  48. let prevNode = this.findPrevious(item);
  49. if (prevNode.next !== null) {
  50. prevNode.next = prevNode.next.next;
  51. }
  52. }

二. 双向链表

  1. function Node (element) {
  2. this.element = element;
  3. this.next = null;
  4. this.previous = null;
  5. }
  6. function DList () {
  7. this.head = new Node('head');
  8. this.find = find;
  9. this.insert = insert;
  10. this.display = display;
  11. this.remove = remove;
  12. this.findLast = findLast;
  13. this.dispReverse = dispReverse;
  14. }
  15. function dispReverse () {
  16. let currNode = this.head;
  17. currNode = this.findLast();
  18. while (currNode !== null && currNode.element !== 'head') {
  19. console.log(currNode.element);
  20. currNode = currNode.previous;
  21. }
  22. }
  23. function findLast () {
  24. let currNode = this.head;
  25. while (currNode.next !== null) {
  26. currNode = currNode.next;
  27. }
  28. return currNode;
  29. }
  30. function remove (item) {
  31. let currNode = this.find(item);
  32. if (currNode.next !== null) {
  33. currNode.previous.next = currNode.next;
  34. currNode.next.previous = currNode.previous;
  35. currNode.next = null;
  36. currNode.previous = null;
  37. }
  38. }
  39. function display () {
  40. let currNode = this.head;
  41. while (currNode.next !== null) {
  42. console.log(currNode.next.element);
  43. currNode = currNode.next;
  44. }
  45. }
  46. function find (item) {
  47. let currNode = this.head;
  48. while (currNode.element !== item) {
  49. currNode = currNode.next;
  50. }
  51. return currNode;
  52. }
  53. function insert (newElement, item) {
  54. let newNode = new Node(newElement);
  55. let currNode = this.find(item);
  56. newNode.next = currNode.next;
  57. newNode.previous = currNode;
  58. currNode.next = newNode;
  59. }

三. 循环链表

  1. // Node类
  2. function Node (element) {
  3. this.element = element;
  4. this.next = null;
  5. }
  6. // LinkedList类
  7. function CList () {
  8. this.head = new Node('head');
  9. // 修改
  10. this.head.next = this.head;
  11. this.find = find;
  12. this.insert = insert;
  13. this.findPrevious = findPrevious;
  14. this.remove = remove;
  15. this.display = display;
  16. }
  17. // 其他
  18. // ...

练习

一. 实现advance(n)方法,使当前节点向前移动n个节点。

  1. // 向前移动一位
  2. DList.prototype.goPrevious = function (thisNode) {
  3. let node1, node2, node3, node4;
  4. if (thisNode.previous.element !== 'head') {
  5. node1 = thisNode.previous.previous;
  6. node2 = thisNode.previous;
  7. node3 = thisNode;
  8. node4 = thisNode.next;
  9. // 位置调整
  10. node1.next = node3;
  11. node3.previous = node1;
  12. node3.next = node2;
  13. node2.previous = node3;
  14. node2.next = node4;
  15. node4.previous = node2;
  16. }
  17. };
  18. DList.prototype.advance = function (n, item) {
  19. let currNode = this.find(item);
  20. while (n--) {
  21. this.goPrevious(currNode);
  22. }
  23. };
  24. // 示例
  25. let names = new DList();
  26. names.insert('Mazey', 'head');
  27. names.insert('Cherrie', 'Mazey');
  28. names.insert('John', 'Cherrie');
  29. names.insert('Luna', 'John');
  30. names.insert('Ada', 'Luna');
  31. names.display();
  32. console.log('---');
  33. names.advance(2, 'Luna');
  34. names.display(); // Mazey, Luna, Cherrie, John, Ada

二. 实现back(n)方法,使当前节点向后移动n个节点。

  1. // 向前移动一位
  2. DList.prototype.goNext = function (thisNode) {
  3. let node1, node2, node3, node4;
  4. if (thisNode.next.element !== null) {
  5. node1 = thisNode.previous;
  6. node2 = thisNode;
  7. node3 = thisNode.next;
  8. node4 = thisNode.next.next;
  9. // 位置调整
  10. node1.next = node3;
  11. node3.previous = node1;
  12. node3.next = node2;
  13. node2.previous = node3;
  14. node2.next = node4;
  15. node4.previous = node2;
  16. }
  17. };
  18. DList.prototype.back = function (n, item) {
  19. let currNode = this.find(item);
  20. while (n--) {
  21. this.goNext(currNode);
  22. }
  23. };
  24. // 示例
  25. let names = new DList();
  26. names.insert('Mazey', 'head');
  27. names.insert('Cherrie', 'Mazey');
  28. names.insert('John', 'Cherrie');
  29. names.insert('Luna', 'John');
  30. names.insert('Ada', 'Luna');
  31. names.display();
  32. console.log('---');
  33. names.back(2, 'Cherrie');
  34. names.display(); // Mazey, John, Luna, Cherrie, Ada

JavaScript数据结构与算法-链表练习

JavaScript数据结构与算法-链表练习的更多相关文章

  1. javascript数据结构与算法--链表

    链表与数组的区别?  1. 定义: 数组又叫做顺序表,顺序表是在内存中开辟一段连续的空间来存储数据,数组可以处理一组数据类型相同的数据,但不允许动态定义数组的大小,即在使用数组之前必须确定数组的大小. ...

  2. 为什么我要放弃javaScript数据结构与算法(第五章)—— 链表

    这一章你将会学会如何实现和使用链表这种动态的数据结构,这意味着我们可以从中任意添加或移除项,它会按需进行扩张. 本章内容 链表数据结构 向链表添加元素 从链表移除元素 使用 LinkedList 类 ...

  3. 重读《学习JavaScript数据结构与算法-第三版》- 第6章 链表(一)

    定场诗 伤情最是晚凉天,憔悴厮人不堪言: 邀酒摧肠三杯醉.寻香惊梦五更寒. 钗头凤斜卿有泪,荼蘼花了我无缘: 小楼寂寞新雨月.也难如钩也难圆. 前言 本章为重读<学习JavaScript数据结构 ...

  4. JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)

    前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...

  5. 为什么我要放弃javaScript数据结构与算法(第九章)—— 图

    本章中,将学习另外一种非线性数据结构--图.这是学习的最后一种数据结构,后面将学习排序和搜索算法. 第九章 图 图的相关术语 图是网络结构的抽象模型.图是一组由边连接的节点(或顶点).学习图是重要的, ...

  6. 为什么我要放弃javaScript数据结构与算法(第八章)—— 树

    之前介绍了一些顺序数据结构,介绍的第一个非顺序数据结构是散列表.本章才会学习另一种非顺序数据结构--树,它对于存储需要快速寻找的数据非常有用. 本章内容 树的相关术语 创建树数据结构 树的遍历 添加和 ...

  7. 为什么我要放弃javaScript数据结构与算法(第七章)—— 字典和散列表

    本章学习使用字典和散列表来存储唯一值(不重复的值)的数据结构. 集合.字典和散列表可以存储不重复的值.在集合中,我们感兴趣的是每个值本身,并把它作为主要元素.而字典和散列表中都是用 [键,值]的形式来 ...

  8. 为什么我要放弃javaScript数据结构与算法(第六章)—— 集合

    前面已经学习了数组(列表).栈.队列和链表等顺序数据结构.这一章,我们要学习集合,这是一种不允许值重复的顺序数据结构. 本章可以学习到,如何添加和移除值,如何搜索值是否存在,也可以学习如何进行并集.交 ...

  9. 为什么我要放弃javaScript数据结构与算法(第四章)—— 队列

    有两种结构类似于数组,但在添加和删除元素时更加可控,它们就是栈和队列. 第四章 队列 队列数据结构 队列是遵循FIFO(First In First Out,先进先出,也称为先来先服务)原则的一组有序 ...

随机推荐

  1. 为每一个应用程序池单独设置aspnet.config配置文件

    ASP.NET2.0之后的版本号就在各Framework的根文件夹下提供了一个aspnet.config文件.这个文件用来配置全局的一些信息,可是一直以来我们都没有怎么用过. ASP.NET4.0之后 ...

  2. 页面找不到js方法的原因,关于EasyUI

    有时EasyUI中datagride写法不正确,会导致无法加载页面上其他的js方法.datagride中的逗号是一个也不能多.一定要注意: 例如以下代码中标红的逗号就会导致后边的js不能正常加载. c ...

  3. HDU 4287 Intelligent IME(map运用)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 Intellig ...

  4. Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.cache.ehcache.EhCacheRegionFactory] as strategy [org.hibernate.cache.spi.RegionFac

    警告: Exception encountered during context initialization - cancelling refresh attempt: org.springfram ...

  5. SVN环境搭建(2)

    原文地址:http://www.penglig.com/post-73.html TortoiseSVN的使用. 首先打开VisualSVN Server Manager,如图: 可以在窗口的右边看到 ...

  6. WPF入门教程系列三

    WPF之Binding的使用(一) 一.  前言 初学WPF经常被Binding搞得苦不堪言,Binding的重用性就不做介绍了,在WPF应用程序开发中Binding是一个非常重要的部分.WPF也是近 ...

  7. 打开eclipse中文件所在文件夹

    在myeclipse中选中文件后能够打开文件所在文件夹,可是eclipse中没有直接打开文件路径的功能.须要我们自己加入. 选择:Run -> External Tools -> Exte ...

  8. FPGA和DSP间基于SRIO的高速通信系统设计

    作者:陈婷,岳强,汪洋 解放军信息工程大学 摘要: 现代信号处理系统通常需要在不同处理器之间实现高速数据通信,SRIO协议由于高效率.低延时的特性被广泛使用.本文研究了在FPGA和DSP两种处理器之间 ...

  9. 【HDU-5246】超级赛亚ACMer(贪心)

    之前用了个nlogn的算法超时了.仅仅能改成n的算法了 大题贪心思路就是 对每一个人的能力值从小到大进行排序,当前能力值为now,那么我们找到一个人的能力使得这个能力值 <= now.now + ...

  10. Django的模型

    Django的模型类相当于数据库的一张表,模型类的实例对象相当于表中的一行内容 Django提供了多种创建对象的方式,常用如下: 通过create()方法 1.Person.objects.creat ...