1. function LinkedList () {
    2. //Node类表示要加入列表的项
    3. var Node = function (element) {
    4. this.element = element;;//要添加到链表的值
    5. this.next = null;//指向链表中下一个节点项的指针
    6. };
    7. var length = 0;//链表项的数量
    8. var head = null;//存储第一个节点的引用
    9. //向链表尾部追加元素
    10. this.append = function (element) {
    11. var node = new Node(element), //创建Node项
    12. current;
    13. if (head === null) {//若head元素为null,意味着向链表添加第一个元素
    14. head = node;
    15. } else {
    16. current = head; //查询链表中的元素,需要从起点开始迭代
    17. while (current.next) {
    18. //当current.next元素为null时,就知道已经到达链表的尾部了
    19. current = current.next;
    20. }
    21. current.next = node;
    22. }
    23. length++;
    24. };
    25. //从链表中移除元素
    26. this.removeAt = function (position) {
    27. if (position > -1 && position < length) { //验证位置有效
    28. var current = head, //用current变量创建链表中第一个元素引用
    29. previous, index = 0;
    30. if (position === 0) {
    31. head = cuttent.next; //如果想要移除第一个,就让head指向第二个元素
    32. } else {
    33. while (index++ < position) {//迭代直到到达目标位置
    34. previous = current;
    35. current = current.next;
    36. }
    37. //将previous与current的下一项链接起来:跳过current,从而移除它
    38. previous.next = current.next;
    39. }
    40. length--;
    41. return current.element;
    42. } else {
    43. return null;
    44. }
    45. };
    46. //在任意位置插入一个元素
    47. this.insert = function (positon, element) {
    48. if (position >= 0 && position <= length) {//检查是否越界
    49. var node = new Node(element);
    50. var index = 0, previous, current = head;
    51. if (position === 0) {//在第一个位置添加
    52. node.next = current;
    53. head = node;
    54. } else {
    55. while (index++ < position) {
    56. previous = current;
    57. current = current.next;
    58. }
    59. node.next = current;
    60. previous.next = node;
    61. }
    62. length++;//更新链表长度
    63. return true;
    64. } else {
    65. return false;//如果越界返回false
    66. }
    67. };
    68. //把LinkedList对象转换成一个字符串
    69. this.toString = function () {
    70. var current = head, //起点
    71. string = '';
    72. while (current) {//检查元素是否存在
    73. string += ', ' + current.element;//拼接到字符串
    74. current = current.next;
    75. }
    76. return string.slice(1);
    77. };
    78. //indexOf方法
    79. this.indexOf = function (element) {
    80. var current = head,
    81. index = 0;//计算位置数
    82. while (current) {//循环访问元素
    83. if (element === current.element) {//检查当前元素是否是我们要找的
    84. return index;
    85. }
    86. index++;
    87. current = current.next;
    88. }
    89. return -1;
    90. };
    91. //实现了indexOf方法后可以根据元素的值来移除元素
    92. this.remove = function (element) {
    93. var index = this.indexOf(element);
    94. return this.removeAt(element);
    95. };
    96. //isEmpty方法和size方法与栈和队列中实现一样
    97. this.isEmpty = function () {
    98. return length === 0;//如果列表中没有元素,isEmpty方法就返回true,否则返回false
    99. };
    100. this.size = function () {
    101. return length;
    102. };
    103. /**
    104. *head变量是LinkedList类的私有变量
    105. *意味着它不能在LinkedList实例外部访问和更改,只有通过LinkedList实例才可以
    106. */
    107. this.getHead = function () {
    108. return head;
    109. };
    110. }
    1. function doubleLinkedList () {
    2. var Node = function (element) {
    3. this.element = element;
    4. this.next = null;
    5. this.prev = null;//新增的,保存链表最后一项
    6. };
    7. var length = 0;
    8. var head = null;
    9. var tail = null;
    10. //在任意一个位置插入一个新元素
    11. this.insert = function (position, element) {
    12. //检查是否越界
    13. if (position >= 0 && position <= length) {
    14. var node = new Node(element),
    15. current = head,
    16. previous,
    17. index = 0;
    18. if (position === 0) {
    19. if (!head) {//如果链表为空,把head和tail都指向这个新节点
    20. head = node;
    21. tail = node;
    22. } else {
    23. node.next = current;
    24. current.prev = node;
    25. head = node;
    26. }
    27. } else if (position === length) {
    28. current = tail;
    29. current.next = node;
    30. node.prev = current;
    31. tail = node;
    32. } else {
    33. while (index++ < position) {
    34. previous = current;
    35. current = current.next;
    36. }
    37. node.next = current;
    38. previous.next = node;
    39. current.prev = node;
    40. node.prev = previous;
    41. }
    42. length++;
    43. return true;
    44. } else {
    45. return false;
    46. }
    47. };
    48. //从任意位置移除元素
    49. this.removeAt = function (position) {
    50. if (position > -1 && position < length) {
    51. var current = head;
    52. previous, index = 0;
    53. if (position === 0) {//移除第一项
    54. head = current.next;
    55. if(length === 1) {//如果只有一项
    56. tail = null;
    57. } else {
    58. head.prev = null;//更新指向上一个元素的指针
    59. }
    60. } else if (position === length - 1) {//移除最后一项
    61. current = tail;
    62. tail = current.prev;
    63. tail.next = null;
    64. } else {
    65. while (index++ < position) {
    66. previous = current;
    67. current = current.next;
    68. }
    69. //将previous与current的下一项链接起来,跳过currrent
    70. previous.next = current.next;
    71. current.next.prev = previous;
    72. }
    73. length--;
    74. return current.element;
    75. } else {
    76. return null;
    77. }
    78. };
    79. }

    链表的实例

    1. /*JS实现一个基于对象的链表*/
    2. function Node(element){
    3. this.element = element;//节点存储的元素
    4. this.next = null;//节点指向的下一个节点,这里先设置为空
    5. }
    6. function LList(){
    7. this.head = new Node("head");//生成一个头节点
    8. this.find = find;//在链表中找到某个节点
    9. this.insert = insert;//在链表中某个元素后面插入某个节点元素
    10. this.display = display;//在将链表中的节点元素显示出来
    11. this.findPrevious = findPrevious;//找到某个节点的上一个节点
    12. this.remove = remove;//删除某个节点
    13. }
    14. function remove(item) {
    15. var prevNode = this.findPrevious(item);
    16. if (!(prevNode.next == null)) {
    17. prevNode.next = prevNode.next.next;
    18. }
    19. }
    20. function findPrevious(item) {
    21. var currNode = this.head;
    22. while (!(currNode.next == null) &&
    23. (currNode.next.element != item)) {
    24. currNode = currNode.next;
    25. }
    26. return currNode;
    27. }
    28. function display() {
    29. var currNode = this.head;
    30. var nodestr = "";
    31. while (!(currNode.next == null)) {
    32. nodestr +=" "+currNode.next.element;
    33. currNode = currNode.next;
    34. }
    35. console.log(nodestr);
    36. }
    37. function find(item) {
    38. var currNode = this.head;
    39. while (currNode.element != item) {
    40. currNode = currNode.next;
    41. }
    42. return currNode;
    43. }
    44. function insert(newElement, item) {
    45. var newNode = new Node(newElement);
    46. var current = this.find(item);
    47. newNode.next = current.next;
    48. current.next = newNode;
    49. }
    50. /*测试例子*/
    51. var num = new LList();
    52. num.insert("a1","head");
    53. num.insert("b1","a1");
    54. num.insert("c1","b1");
    55. num.display();// a1 b1 c1
    56. num.remove("b1");
    57. num.display();// a1 c1