<!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
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
}
}
// 案例一 判断回文 dad 假似真时真似假
function test(str){
const lowstr = str.toLocaleLowerCase().split(' ').join('')
let dequeue = new LinkList()
for(let i=0;i<lowstr.length;i++){
dequeue.push(lowstr[i])
}
let isEqual = true
while(dequeue.count > 1){
if(dequeue.removeAt(0) !== dequeue.removeAt(dequeue.count - 1)){
isEqual = false
break
}
}
return isEqual
}
// let str = 'd a A d t d a a D'
// let str = '假似真时真似假'
// console.log( test(str))
// 案例二 击鼓传花
// console.log(game(['kerwin','xiaoming','tiechui','gangdan','guladunzi'],7))
// function game(list,num){
// let queue = new LinkList()
// for(let i=0;i<list.length;i++){
// queue.push(list[i])
// }
// console.log('queue',queue,num)
// while(queue.count > 1){
// for(let i=0;i<num;i++) {
// queue.push(queue.removeAt(0))
// }
// console.log(queue.removeAt(0),'淘汰了')
// }
// return queue.removeAt(0)
// }
// 案例三 进制转换
function convert(decNumber,base){
let remStack = new LinkList()
let number = decNumber
let string = ''
let baseString = '0123456789ABCDEF'
while(number > 0){
remStack.push(number%base)
number = Math.floor(number/base)
}
while(!(remStack.count === 0)){
string += baseString[remStack.removeAt(remStack.count-1)]
}
return string
}
let res = convert(50,2)
console.log(res)
let res2 = convert(50,8)
console.log(res2)
let res3 = convert(500,16)
console.log(res3)
</script>
</body>
</html>