// 链表
function LinkedList () {
function Node(value) {
this.next = null
this.value = value
}
this.head = null
this.length = 0
//在链表尾部追加元素
LinkedList.prototype.add = function (value) {
if(!value) {
return false
}
var newElement = new Node(value)
if(!this.head) {
this.head = newElement
} else {
var current = this.head
while(current.next) {
current = current.next
}
current.next = newElement
}
this.length ++
return true
}
//把链表元素 拼接成字符串
LinkedList.prototype.toString = function () {
if(!this.head) {
return ''
}
var current = this.head
var= Result ''
the while (Current) {
Result + + = current.value ' '
Current = current.next
}
return Result
}
// selection position in the list to insert
LinkedList.prototype.insertELe = function (value, position) {
/ / bounds Analyzing
IF (position < 0 || position> the this .length) {
return to false
}
var the newElement = new new the Node (value)
var index = 0
var PREV
// when the insertion location list head case
IF (position == 0 ) {
newElement.next = the this .head
the this .head = the newElement
} the else {
var Current = the this .head
the while (index < position) {
PREV = Current
index ++
Current = current.next
}
newElement.next = Current
prev.next= newElement
}
this.length ++
return true
}
//修改某个位置的值
LinkedList.prototype.update = function (value, position) {
//越界判断
if(position < 0 || position > this.length) {
return false
}
var index = 0
var current = this.head
while(index < position) {
index ++
current = current.next
}
current.value = value
return true
}
//获取某个位置的值
LinkedList.prototype.get = function (position) {
//越界判断
if(position > this.length || position < 0) {
return false
}
var index = 0
var current = this.head
while(index < position) {
index ++
current =current.next
}
Return current.value
}
// Get an index worth
LinkedList.prototype.indexOf = function (value) {
var Current = the this .head
var index = 0
the while (Current) {
IF (value == current.value) {
return index
}
index ++
Current = current.next
}
return false
}
// delete the value of a position of
LinkedList.prototype.removeAt = function (position) {
//越界判断
if(position > this.length || position < 0) {
return false
}
var index = 0
var current = this.head
var prev
while(index < position) {
prev = current
index++
current = current.next
}
prev.next = current.next
the this .length --
return to true
}
// delete an element
LinkedList.prototype.remove = function (Element) {
var index = the this .indexOf (Element)
return the this .removeAt (index)
}
// Check the list is empty
LinkedList.prototype.isEmpty = function () {
return the this .length == 0
}
// check the length of the list
LinkedList.prototype.size = function () {
return the this .length
}
}
// list
function
LinkedList () {
function
Node(
value) {
this.next
=
null
this.value
= value
}
this.head
=
null
this.length
=
0
//在链表尾部追加元素
LinkedList.prototype.
add
=
function (
value) {
if(
!value) {
return
false
}
var newElement
=
new
Node(value)
if(
!
this.head) {
this.head
= newElement
}
else {
var current
=
this.head
while(current.next) {
current
= current.next
}
current.next
= newElement
}
this.length
++
return
true
}
//把链表元素 拼接成字符串
LinkedList.prototype.
toString
=
function () {
if(
!
this.head) {
return
''
}
var current
=
this.head
var result
=
''
while(current) {
result
+= current.value
+
' '
current
= current.next
}
return result
}
//在链表中选择位置进行插入
LinkedList.prototype.
insertELe
=
function (
value,
position) {
//越界判断
if(position
<
0
|| position
>
this.length) {
return
false
}
var newElement
=
new
Node(value)
var index
=
0
var prev
//当插入的位置为链表头的情况
if(position
==
0) {
newElement.next
=
this.head
this.head
= newElement
}
else {
var current
=
this.head
while(index
< position) {
prev
= current
index
++
current
= current.next
}
newElement.next
= current
prev.next
= newElement
}
this.length
++
return
true
}
//修改某个位置的值
LinkedList.prototype.
update
=
function (
value,
position) {
//越界判断
if(position
<
0
|| position
>
this.length) {
return
false
}
var index
=
0
var current
=
this.head
while(index
< position) {
index
++
current
= current.next
}
current.value
= value
return
true
}
//获取某个位置的值
LinkedList.prototype.
get
=
function (
position) {
//越界判断
if(position
>
this.length
|| position
<
0) {
return
false
}
var index
=
0
var current
=
this.head
while(index
< position) {
index
++
current
= current.next
}
return current.value
}
//获取某个值得索引
LinkedList.prototype.
indexOf
=
function (
value) {
var current
=
this.head
var index
=
0
while(current) {
if(value
== current.value) {
return index
}
index
++
current
= current.next
}
return
false
}
//删除某个位置的值
LinkedList.prototype.
removeAt
=
function (
position) {
//越界判断
if(position
>
this.length
|| position
<
0) {
return
false
}
var index
=
0
var current
=
this.head
var prev
while(index
< position) {
prev
= current
index
++
current
= current.next
}
prev.next
= current.next
this.length
--
return
true
}
//删除某个元素
LinkedList.prototype.
remove
=
function (
element) {
var index
=
this.
indexOf(element)
return
this.
removeAt(index)
}
//查看链表是否为空
LinkedList.prototype.
isEmpty
=
function () {
return
this.length
==
0
}
//查看链表的长度
LinkedList.prototype.
size
=
function () {
return
this.length
}
}