JS实现单向链表

来源:互联网 发布:c语言玫瑰花代码 编辑:程序博客网 时间:2024/06/18 00:27

目的:

用JS实现一个简单的单向链表,并完成相关的功能

功能说明:

1.push(value):从链表尾部添加一个新的节点
2.insertAfer(value,item):向链表中的item节点之后插入一个 值为value的新节点
3.remove(value):删除链表中值为value的节点
4.removeAt(pos):删除链表中第pos个节点
5.find(value):查找链表中值为value的节点
6.findPrevious(value):查找链表中值为value的节点的前一个节点
7.indexof(vallue):查找链表中值为value的节点的索引值,如果查找不到则返回-1
8.size():获取当前链表的长度
9.getHead():获取当前链表的头节点
10.print():打印当前链表,供测试用

代码实现:

//创建一个Node辅助类,用来生成节点function Node(value) {    this.value = value;    this.next = null;  }//链表类function LinkedList() {    this.head = null;    this.length = 0;    //向链表尾部追加元素    this.push = push;    //从链表中查找某个元素    this.find = find;    //在链表中任意一个元素之后插入一个元素    this.insertAfter = insertAfter;    //从链表中查找任意元素节点的前一个节点    this.findPrevious = findPrevious;    //从链表中删除值为value的元素    this.remove = remove;    //返回当前链表的长度    this.size = size;    //查找某个元素在链表中的索引值    this.indexof = indexof;    //删除链表中第pos个元素    this.removeAt = removeAt;    //获取链表中第一个元素    this.getHead = getHead;    //打印当前的链表,供测试用    this.print = print;  }
1.push(value):从链表尾部添加一个新的节点
function push(value) {    var node = new Node(value);    if (this.head == null) {      this.head = node;    } else {      var current = this.head;      while (current.next != null) {        current = current.next;      }      current.next = node;    }    length++;  }
2.insertAfer(value,item):向链表中的item节点之后插入一个 值为value的新节点
function insertAfter(value, item) {    var node = new Node(value);    var current = this.find(item);    if (current == null) {      return console.log('找不到元素');    }    node.next = current.next;    current.next = node;    length++;  }
3.remove(value):删除链表中值为value的节点
function remove(value) {    var previous = this.findPrevious(value);    var current = this.find(value);    if (previous == null) {      return console.log('链表中找不到被删除的元素');    }    previous.next = current.next;    length--;  }
4.removeAt(pos):删除链表中第pos个节点
function removeAt(pos) {    if (pos > -1 && pos < length) {      var current = this.head;      var index = 0;      if (pos === 0) {        this.head = current.next;      } else {        while (index < pos) {          var previous = current;          current = current.next;          index++;        }        previous.next = current.next;      }      length--;    } else {      return null;    }  }
5.find(value):查找链表中值为value的节点
function find(value) {    var currentNode = this.head;    if (currentNode == null) {      console.log("这是一个空链表!!!");      return null;    }    if (currentNode.value === value) {      return currentNode;    }    while (currentNode.next) {      currentNode = currentNode.next;      if (currentNode.value === value) {        return currentNode      }    }    console.log("没有找到该元素!!!");    return null;  }
6.findPrevious(value):查找链表中值为value的节点的前一个节点
function findPrevious(value) {    var current = this.head;    if (current == null) {      console.log('这是一个空链表');      return null;    }    while (current.next) {      current = current.next;      if (current.next.value === value) {        return current;      }    }    console.log('找不到该元素的前一个元素');    return null;  }
7.indexof(vallue):查找链表中值为value的节点的索引值,如果查找不到则返回-1
function indexof(value) {    var current = this.head;    var index = 0;    if (current == null) {      return null;    } else {      while (current) {        if (current.value === value) {          return index;        }        index++;        current = current.next;      }    }    return -1;  }
8.size():获取当前链表的长度
function size(){    return length;  }
9.getHead():获取当前链表的头节点
function getHead(){    return this.head;  }
10.print():打印当前链表,供测试用
function print() {    var current = this.head;    while (current != null) {      console.log(current.value);      current = current.next;    }  }

功能测试:

var list = new LinkedList();  for (var i = 1; i < 6; i++) {    list.push(i);  }  list.print();
原创粉丝点击