数据结构与算法
初识数据结构与算法
数组结构
一些方法
splice
可以传三个值,
第一个——开始下标
第二个——要删的个数
第三个——要加的元素
sort
借助回调函数
按年龄从小到大排
concat
输出结果:1。2。3.。。9
迭代方法
filter把符合条件得弄出来
reduce累加累成
entries拿到键值对
values 拿到每个值
keys拿到所有键
迭代器对象
只有迭代器对象可以用
array.from转换成数组
晕arguments?
后面可以用表达式
栈结构
后进先出
就像往管子里装东西一样
也可看做受限的数组
如:历史记录;
如
递归函数如果不设置终止条件就会一直压栈,最后溢出
栈结构的封装
数组相关方法
pop的返回值是被删除的元素
加下划线表示私有的
在前面加#也可以
保证不能用其他数组方法?
栈 应用
将十进制转换为n进制(十六进制特殊)
队列
先进先出
队列的封装
应用 击鼓传花
循环队列
队头放到队尾
先进先出
双端队列
链表
不能通过下标找到想要的
单链表1
通过索引删除
<!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 LinkedList {
constructor() {
this.count = 0;
this.head = null;
}
//push
push(element) {
const node = new Node(element);
console.log(node);
//分情况
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){
//确保current可以访问到
let current=this.head;
if(index===0){
//覆盖操作
this.head=this.head.next;
}else{
//设有a,b,c三个顺序节点,
//目标:删除b节点
//那么,让a节点放在别的节点处,
//把c的节点放到原来a的位置
//如此,即可跳过b节点
let previous;
//从链头开始找
for(let i=0;i<index;i++){
previous=current;
current=current.next;
}
previous.next=current.next;
}
this.count--;
return current.element;
}
return
}
}
var list = new LinkedList()
</script>
</body>
</html>
传入索引
<!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 LinkedList {
constructor() {
this.count = 0;
this.head = null;
}
//push
push(element) {
const node = new Node(element);
console.log(node);
//分情况
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){
//确保current可以访问到
let current=this.head;
if(index===0){
//覆盖操作
this.head=this.head.next;
}else{
//简化
let previous=this.getNodeAt(index-1);//找到上一个节点
current=previous.next;//.next就是当前这个
previous.next=current.next;
}
this.count--;
return current.element;
}
return;
}
//封装节点
getNodeAt(index){
if(index>=0&&index<this.count){
let node=this.head;
for(let i=0;i<index;i++){
node=node.next;
}
return node;
}
}
}
var list = new LinkedList()
</script>
</body>
</html>
通过指定数据内容,删除
<!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 LinkedList {
constructor() {
this.count = 0;
this.head = null;
}
//push
push(element) {
const node = new Node(element);
console.log(node);
//分情况
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) {
//确保current可以访问到
let current = this.head;
if (index === 0) {
//覆盖操作
this.head = this.head.next;
} else {
//简化
let previous = this.getNodeAt(index - 1);//找到上一个节点
current = previous.next;//.next就是当前这个
previous.next = current.next;
}
this.count--;
return current.element;
}
return;
}
equalFn(a,b){
// return a===b;
//1.递归函数,深度检查是否相等
//2.转换成json字符串
return Json.stringify(a)===json.stringify(b);
}
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()
return this.removeAt(index)
}
}
var list = new LinkedList()
</script>
</body>
</html>