经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
用JavaScript撸一个静态链表
来源:cnblogs  作者:freephp  时间:2023/6/26 8:50:16  对本文有异议

最近重新开始翻起《大话数据结构》,看到了静态链表部分里面讲C语言是利用数组模拟,觉得十分有趣。但是在JavaScript中,也可以用类似的方式去实现,定义一个数据域和一个结点域,然后实现链表的基础操作。弱类型语言没有指针,所以需要自己区实现。算法的乐趣就在于解决一些思路上的问题,直击问题的本质。
首先可以定义Node类,如下所示:

  1. class Node {
  2. constructor(value) {
  3. this.data = value;
  4. this.next = null;
  5. }
  6. }

然后实现StaticLinkedList类,先定义简单的append和display方法:

  1. class StaticLinkedList {
  2. constructor() {
  3. this.head = null;
  4. this.length = 0;
  5. }
  6. append(value) {
  7. const newNode = new Node(value);
  8. this.length++;
  9. if (this.head === null) {
  10. this.head = newNode;
  11. return;
  12. }
  13. let current = this.head;
  14. while (current.next != null) {
  15. current = current.next;
  16. }
  17. current.next = newNode;
  18. }
  19. display() {
  20. console.log('the static linked list is:\r\n');
  21. let current = this.head;
  22. if (current === null) {
  23. console.log('empty!');
  24. return;
  25. }
  26. while (current !== null) {
  27. console.log(JSON.stringify(current));
  28. console.log(`its value is ${current.data}\r\n`);
  29. current = current.next;
  30. }
  31. }
  32. }

其中append方法是在链表尾部添加新的Node对象,display方法可以打印出Node对象和它的数据。使用这个静态链表类也很简单,比如添加4个结点到这个链表里面:

  1. const staticLinkedList = new StaticLinkedList();
  2. staticLinkedList.append(3);
  3. staticLinkedList.append(7);
  4. staticLinkedList.append(16);
  5. staticLinkedList.append(24);

我们还应该提供更加灵活添加结点的方法,比如我想在第三个结点位置插入一个新的结点,数值为11,那么现有的append方法就不适用了,需要定义一个新的插入结点的方法,代码如下:

  1. /**
  2. * Method to insert an new element at the specific location
  3. *
  4. * @param {*} elementValue the value of the element that to be inserted
  5. * @param {*} index the position of the element, from 1 to maximum of the list
  6. * @returns true/false
  7. */
  8. insertAt(elementValue, index) {
  9. if (index < 1 || index > this.length + 1) {
  10. console.log('index is out of the range!');
  11. return false;
  12. }
  13. const newNode = new Node(elementValue);
  14. let startPos = 1;
  15. let current = this.head;
  16. while (startPos < index - 1) {
  17. current = current.next;
  18. startPos++;
  19. }
  20. newNode.next = current.next;
  21. current.next = newNode;
  22. this.length++;
  23. return true;
  24. }

这段代码需要理解的是新结点如何添加到链表的那两行代码,首先是newNode.next = current.next,这行代码是把新结点的next指向了原来插入前位置的结点的下一个结点。然后current.next = nextNode,把新结点替换掉原来该位置的结点。

为了更好地理解,我画了一张示意图:

要注意的是step1和step2的顺序不能颠倒,否则会导致代码运行错误。

然后我们还需要定义一个移除指定位置结点的方法,如下所示:

  1. removeAt(index) {
  2. if (index < 1 || index > this.length + 1) {
  3. console.log('index is out of the range!');
  4. return;
  5. }
  6. let current = this.head;
  7. let startPos = 1;
  8. let previous = null;
  9. while (startPos < index) {
  10. previous = current;
  11. current = current.next;
  12. startPos++;
  13. }
  14. previous.next = current.next;
  15. this.length--;
  16. }

我对previous.next = current.next也画了一张示意图,删除原来结点,需要把它前面一个结点的next指向该结点的next。

总结:
静态链表的添加和移除略有不同,需要利用Node中的next进行模拟指针操作,让新的结点加入到链表,让需要被删除的结点直接从上一个结点的next指向到原有结点的next。

原文链接:https://www.cnblogs.com/freephp/p/17504086.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号