单链表
function Node(element) {
this.element = element
this.next = null
}
function LList() {
this.head = new Node("head")
}
LList.prototype = {
constructor: LList,
find: function (item) {
var currNode = this.head
if (item === null) return
while (currNode.element != item) {
currNode = currNode.next
}
return currNode
},
Preinsert: function (newEel, item) {
var newNode = new Node(newEel)
var current = this.find(item)
newNode.next = current.next
current.next = newNode
},
nextInsert: function (newEel) {
var currNode = this.head
var newNode = new Node(newEel)
while (currNode.next !== null) {
currNode = currNode.next
}
var endNode = currNode
endNode.next = newNode
endNode = newNode
},
display: function () {
var current = this.head
while (current.next !== null) {
console.log(current.next)
current = current.next
}
},
findPre: function (item) {
var currNode = this.head
while (!(currNode.next === null) && currNode.next.element != item) {
currNode = currNode.next
}
return currNode
},
remove: function (item) {
var preItem = this.find(item)
if (preItem.next !== null) {
preItem.next = preItem.next.next
} else {
console.log(false)
}
},
}
var cities = new LList()
cities.Preinsert("Conway", "head")
cities.nextInsert("God")
cities.nextInsert('Aglet')
cities.display()