Skip to content

Commit 901450d

Browse files
author
huangjunjian
committed
feat:增加目录
1 parent 4aebabb commit 901450d

File tree

3 files changed

+314
-0
lines changed

3 files changed

+314
-0
lines changed

2.LinkList/demo1.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
function LinkedList() {
2+
var Node = function (element) {        //新元素构造
3+
this.element = element;
4+
this.next = null;
5+
};
6+
var length = 0;
7+
var head = null;
8+
9+
this.append = function (element) {
10+
var node = new Node(element);        //构造新的元素节点
11+
var current;
12+
if (head === null) {             //头节点为空时 当前结点作为头节点
13+
head = node;
14+
} else {
15+
current = head;              
16+
while (current.next) {          //遍历,直到节点的next为null时停止循环,当前节点为尾节点
17+
current = current.next;
18+
}
19+
current.next = node;            //将尾节点指向新的元素,新元素作为尾节点
20+
}
21+
length++;                    //更新链表长度
22+
};
23+
this.removeAt = function (position) {
24+
if (position > -1 && position < length) {
25+
var current = head;
26+
var index = 0;
27+
var previous;
28+
if (position == 0) {
29+
head = current.next;
30+
} else {
31+
while (index++ < position) {
32+
previous = current;
33+
current = current.next;
34+
}
35+
previous.next = current.next;
36+
}
37+
length--;
38+
return current.element;
39+
} else {
40+
return null;
41+
}
42+
};
43+
this.insert = function (position, element) {
44+
if (position > -1 && position <= length) {        //校验边界
45+
var node = new Node(element);        
46+
current = head;
47+
var index = 0;
48+
var previous;
49+
if (position == 0) {                    //作为头节点,将新节点的next指向原有的头节点。
50+
node.next = current;
51+
head = node;                        //新节点赋值给头节点
52+
} else {
53+
while (index++ < position) {
54+
previous = current;
55+
current = current.next;
56+
}                                //遍历结束得到当前position所在的current节点,和上一个节点
57+
previous.next = node;                    //上一个节点的next指向新节点 新节点指向当前结点,可以参照上图来看
58+
node.next = current;
59+
}
60+
length++;
61+
return true;
62+
} else {
63+
return false;
64+
}
65+
66+
};
67+
this.toString = function () {
68+
var current = head;
69+
var string = '';
70+
while (current) {
71+
// console.log(current)
72+
string += ',' + current.element;
73+
current = current.next;
74+
}
75+
return string;
76+
};
77+
this.indexOf = function (element) {
78+
var current = head;
79+
var index = -1;
80+
while (current) {
81+
if (element === current.element) {            //从头节点开始遍历
82+
return index;
83+
}
84+
index++;
85+
current = current.next;
86+
}
87+
return -1;
88+
};
89+
this.getLength = function () {
90+
return length;
91+
}
92+
93+
}
94+
95+
let linkList = new LinkedList()
96+
for (let index = 0; index < 10; index++){
97+
linkList.append(index)
98+
}
99+
100+
console.time('start')
101+
linkList.insert(1,'呵呵哒')
102+
console.timeEnd('start')
103+
104+
105+
// let arr = []
106+
// for (let index = 0; index < 1000000; index++){
107+
// arr.push(index)
108+
// }
109+
// console.time('start')
110+
// arr.unshift('呵呵哒')
111+
// console.timeEnd('start')

2.LinkList/demo2.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
class Node {
2+
constructor(val){
3+
// console.log('节点构造函数')
4+
this.init(val)
5+
}
6+
init(val){
7+
this.val = val
8+
this.next = null
9+
}
10+
}
11+
12+
class LinkList {
13+
constructor(){
14+
// console.log('链表构造函数')
15+
this._length = 0 //链表长度
16+
this._head = null //链表起点
17+
}
18+
19+
//返回值的索引
20+
//
21+
// 链表最后位置添加
22+
push(val){
23+
let node = new Node(val)
24+
// console.log('节点',node)
25+
let current ;
26+
if(this._head === null){
27+
this._head = node
28+
}else{
29+
// console.log(222)
30+
current = this._head
31+
// console.log(current.next)
32+
while (current.next) {
33+
// console.log(current.nex)
34+
current= current.next
35+
}
36+
// console.log(222)
37+
current.next =node
38+
}
39+
//链表长度递增
40+
this._length ++
41+
42+
}
43+
// 链表任意位置添加
44+
insert(){}
45+
//删除某一个链表根据值删除某一个链表
46+
remove(val){
47+
// 将删除位置之前的 块next 指针指向下一个
48+
let current = this._head
49+
let preNode = null
50+
// console.log('current',this._head)
51+
if(this._head.val ===val) {
52+
// 将head next 指向下一个块
53+
this._head = current.next
54+
}else{
55+
console.log('hehe',current)
56+
while (current.next&&current.val !== val) {
57+
// 如果游标小于要删除的块
58+
preNode = current
59+
current = current.next
60+
61+
}
62+
preNode.next = current.next
63+
this._length --
64+
return current.val
65+
}
66+
}
67+
//查看所有的链表数据
68+
values(){
69+
// 从头开始链表到链表为空为止循环
70+
let current = this._head
71+
console.log(current)
72+
let arr = []
73+
while (current.next) {
74+
arr.push(current.val)
75+
current=current.next
76+
}
77+
arr.push(current.val)
78+
return arr
79+
}
80+
}
81+
let list = new LinkList()
82+
// console.log(list)
83+
list.push(1)
84+
list.push(2)
85+
list.push(3)
86+
list.remove(3)
87+
console.log(list.values())
88+
console.log(list._length)

2.LinkList/demo3.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import Node from './node'
2+
export default class LinkedList {
3+
constructor() {
4+
this._length = 0;
5+
this._head = null;
6+
}
7+
8+
/**
9+
* [在链表结尾插入]
10+
* @param {[*]} val [要插入的节点]
11+
* @return {[type]} [description]
12+
*/
13+
append(val) {
14+
let node = new Node(val);
15+
16+
if (!this._head) {
17+
this._head = node;
18+
}else {
19+
let current = this._head;
20+
while(current.next) {
21+
current = current.next;
22+
}
23+
current.next = node;
24+
}
25+
this._length++;
26+
}
27+
28+
/**
29+
* [在指定位置前插入]
30+
* @param {[Number]} position [插入的位置]
31+
* @param {[*]} val [要插入的节点]
32+
* @return {[type]} [description]
33+
*/
34+
insert(position,val) {
35+
if (position >= 0 && position <=_length) {
36+
let node = new Node(val);
37+
let current = this._head;
38+
let index = 0;
39+
let pre = null;
40+
if(position === 0) {
41+
node.next = this._head;
42+
this._head = node;
43+
}else {
44+
while (index++ < position) {
45+
pre = current;
46+
current = current.next;
47+
}
48+
pre.next = node;
49+
node.next = current;
50+
}
51+
this._length++;
52+
}
53+
}
54+
/**
55+
* [移除第一个找到的值]
56+
* @param {[*]} val [要移除的值]
57+
* @return {[type]} [description]
58+
*/
59+
remove(val) {
60+
let current = this._head;
61+
let pre = null;
62+
if (val === this._head.value) {
63+
this._head = current.next;
64+
}else{
65+
while(current.next && current.value !== val) {
66+
pre = current;
67+
current = current.next;
68+
}
69+
pre.next = current.next;
70+
this._length--;
71+
}
72+
}
73+
/**
74+
* [查找指定值的位置]
75+
* @param {[*]} val [要查找的值]
76+
* @return {[Number]} [位置]
77+
*/
78+
indexOf(val) {
79+
let current = this._head;
80+
let index = 0;
81+
while(current.next && current.value !== val) {
82+
current = current.next;
83+
index++;
84+
}
85+
return index;
86+
}
87+
/**
88+
* [获取链表的值]
89+
* @return {[Array]} [链表值组成的数组]
90+
*/
91+
values() {
92+
let current = this._head;
93+
let values_arr = new Array();
94+
while(current.next) {
95+
values_arr.push(current.value);
96+
current = current.next;
97+
}
98+
values_arr.push(current.value);
99+
return values_arr;
100+
}
101+
/**
102+
* [链表是否为空]
103+
* @return {Boolean} [description]
104+
*/
105+
isEmpty() {
106+
return this._length === 0;
107+
}
108+
/**
109+
* [链表长度]
110+
* @return {[Number]} [链表的长度]
111+
*/
112+
size() {
113+
return this._length;
114+
}
115+
}

0 commit comments

Comments
 (0)