<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
class Node{
constructor(element){
this.element = element
this.next = null
}
}
class LinkList {
constructor(){
this.count = 0
this.head = null
}
push(element){
const node = new Node(element)
if(this.head === null) {
this.head = node
}else {
let current = this.head
while(current.next != null){
current = current.next
}
current.next = node
}
this.count++
}
// 指定位置删除
removeAt(index){
if(index >= 0 && index < this.count){
let current = this.head
if(index === 0) {
this.head = this.head.next
} else {
let previous
for(let i = 0; i < index; i++){
previous = current
current = current.next
}
previous.next = current.next
}
this.count--
return current.element
}
}
getNodeAt(index) {
if(index >= 0 && index < this.count){
let node = this.head
for(let i = 0; i <index;i++){
node = node.next
}
return node
}
return
}
// 指定位置删除
removeAt2(index){
if(index >= 0 && index < this.count){
let current = this.head
if(index === 0) {
this.head = this.head.next
} else {
let previous = this.getNodeAt(index - 1)
current = previous.next
previous.next = current.next
}
this.count--
return current.element
}
}
equalFn(a,b){
// return a === b
return JSON.stringify(a) === JSON.stringify(b)
// imumutable 判断两个元素是否相等
}
// 返回指定数据索引
indexOf(element){
let current = this.head
for(let i = 0; i < this.count;i++){
if(this.equalFn(element,current.element)){
return i
}
current = current.next
}
return -1
}
// 指定数据删除
remove(element){
const index = this.indexOf(element)
return this.removeAt(index)
}
//指定位置插入一个元素
insert(element,index){
if(index >= 0 && index <= this.count){
const node = new Node(element)
if(index === 0){
const current = this.head
node.next = current
this.head = node
} else{
// 找到指定位置数据 和 上级的数据
let previous = this.getNodeAt(index - 1)
const current = previous.next
node.next = current
prevent.next = node
}
this.count++
return true
}
return false
}
}
</script>
<script>
class CirularLinkedList extends LinkList {
constructor(){
super()
}
push(element){
const node = new Node(element)
let current
if(this.head === null){
this.head = node
}else {
current = this.getNodeAt(this.count - 1)
current.next = node
}
node.next = this.head
this.count++
}
insert(element,index){
if(index >=0 && index <= this.count) {
const node = new Node(element)
let current = this.head
if(index === 0){
if(this.head === null) {
this.head = node
node.next = this.head
} else {
node.next = current
current = this.getNodeAt(this.count - 1 )
this.head = node
current.next = this.head
}
} else {
const previous = this.getNodeAt(index - 1)
node.next = previous.next
previous.next = node
}
this.count++
return true
}
return false
}
removeAt(index){
if(index >=0 && index < this.count) {
let current = this.head
if(index === 0){
if(this.count === 1){
this.head = undefined
} else {
let last = this.getNodeAt(this.count - 1)
this.head = this.head.next
last.next = this.head
}
} else {
const previous = this.getNodeAt(index - 1)
current = previous.next
previous.next = current.next
}
this.count--
return current.element
}
return
}
}
var list = new CirularLinkedList()
list.push(100)
list.push(200)
list.push(300)
list.push(400)
console.log(list)
</script>
</body>
</html>