//js实现链表
//单向链表
function LinkedList() {
//单链表的节点结构
function Node(ele){
this.element = ele;
this.next = null;
}
this.head = null;
this.length = 0;
//向链表的尾部追加一个元素
this.append = function(ele){
var newNode = new Node(ele),current;
if(!this.head){
this.head = newNode;
}else{
current = this.head;
if(current.next) {
current = current.next;
}
current.next = newNode;
}
this.length++;
}
//根据元素的位置来移除某一个元素,移除成功返回元素的值
this.removeAt = function(pos){
if(pos>=0 && pos<this.length){
var current = this.head,previous,index = 0;
if(pos === 0){
this.head = current.next;
}else{
while(index++ < pos){
previous = current;
current = current.next;
}
previous.next = current.next;
}
this.length--;
return current.element;
}else{
return null;
}
}
//在任意位置插入元素
this.insert = function(pos,ele){
if(pos>-1 && pos<this.length) {
var node = new Node(ele),current = this.head,previous,index = 0;
if(pos === 0){
node.next = current;
this.head = node;
}else{
while(index++ < pos){
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
}
this.length++;
return true;
}else{
return false;
}
}
//输出链表
this.toString = function(){
var current = this.head;
var str = '';
while(current){
str += current.element + (current.next ? ',':'');
current = current.next;
}
return str;
}
//查找元素
this.indexOf = function(ele){
var current = this.head,index=-1;
while(current){
index++;
if(current.element === ele){
return index;
}else{
current = current.next;
}
}
return -1;
}
//根据某一个节点的值来删除节点
this.remove = function(ele){
var index = this.indexOf(ele);
return this.removeAt(index);
}
//判断链表是否为空
this.isEmpty = function(){
return this.length === 0;
}
//获取链表长度
this.size = function(){
return this.length;
}
//获取链表的第一个节点
this.getHead = function () {
return this.head;
}
}
//js实现双向链表
function DoubleLinkedList() {
//双向链表的节点结构
function Node(ele){
this.element = ele;
this.prev = null;
this.next = null;
}
this.head = null;
this.tail = null;
this.length = 0;
//在任意位置插入元素
this.insert = function(pos,ele){
if(pos > -1 && pos < this.length){
var current = this.head,previous,index = 0,node = new Node(ele);
if(pos === 0){
if(!this.head){
this.head = node;
this.tail = node;
}else{
node.next = current;
current.prev = node;
this.head = node;
}
}else if(pos === this.length){
current = this.tail;
current.next = node;
node.prev = current;
this.tail = node;
}else{
while(index++ < pos){
previous = current;
current = current.next;
}
node.next = current;
current.prev = node;
previous.next = node;
node.prev = previous;
}
this.length++;
return true;
}else{
return false;
}
}
//从任意位置移除元素
this.removeAt = function(pos){
if(pos>-1 && pos<this.length){
var current = this.head,index = 0,previous;
if(pos === 0){
this.head = current.next;
if(this.length === 1){
this.tail = null;
}else{
this.head.prev = null;
}
}else if(pos === this.length-1){
current = this.tail;
this.tail = current.prev;
this.tail.next = null;
}else{
while(index++ < pos){
previous = current;
current = current.next;
}
previous.next = current.next;
current.next.prev = previous;
}
this.length--;
return current.element;
}else{
return null;
}
}
}