算法learn
function calc(input) {
if(typeof input!='string') console.warn('请输入字符串')
input = input.split(' ').reverse();
var len = input.length;
var optStack = [];
var valStack = [];
while(len--) {
var item = input[len];
switch(item) {
case "+":
case "-":
case "*":
case "/":
optStack.push(item);
break;
case "(":
break;
case ")":
var a = valStack.pop();
var b = valStack.pop();
var opt = optStack.pop();
valStack.push(eval(a + opt + b));
break;
default:
valStack.push(item);
}
}
return valStack.pop();
}
console.log(calc('( 1 + ( 5 * 2 ) )' ))
//object版本
function Stack(){
//stark
this.arr = {}
//指针
this.pointer = 0
}
Stack.prototype = {
push:function(item){
return this.arr[++this.pointer] = item
},
pop:function(){
let res = this.arr[this.pointer]
delete this.arr[this.pointer]
--this.pointer
return res
},
peek:function(){
return this.arr[this.pointer]
},
size:function(){
return Object.keys(this.arr).length
},
clear:function(){
this.pointer = 0
this.arr = {}
return this.arr
}
}
//测试
// var stack = new Stack()
// stack.push(1)
// console.log(stack.push(1))
// stack.push(1)
// stack.push(2)
// stack.push(3)
// console.log(stack.peek(),stack.size())
// console.log(stack.pop())
// console.log(stack.peek(),stack.size())
//Array版本
function Stack_arr(){
//stark
this.arr = []
}
Stack_arr.prototype = {
push:function(item){
return this.arr.push(item)
},
pop:function(){
return this.arr.pop()
},
peek:function(){
return this.arr[this.arr.length-1]
},
size:function(){
return this.arr.length
},
clear:function(){
this.arr = []
return this.arr
}
}
//测试
// var stack = new Stack_arr()
// stack.push(1)
// console.log(stack.push(1))
// stack.push(1)
// stack.push(2)
// stack.push(3)
// console.log(stack.peek(),stack.size())
// console.log(stack.pop())
// console.log(stack.peek(),stack.size())
//singlelink 单链表
class Node{
constructor(ele){
this.element = ele
this.next = null
this.pre = null
}
}
class Link {
constructor(){
this.head = new Node('head')
this.cur = this.head
this.pointer = 1
}
find(ele){
let cur = this.head;
while(cur.element !==ele){
cur = cur.next
}
return cur
}
findPre(ele){
let cur = this.head;
while(cur.element !== null && cur.next.element !==ele){
cur = cur.next
}
return cur
}
insert(ele,item){
const newNode = new Node(ele)
let cur = this.find(item)
newNode.next = cur.next
cur.next = newNode
this.pointer++;
}
advance(n){
if (n <= 0||n>=this.size()) {
console.warn('n值范围不对')
return
}
let index = 0;
while (index < n && this.cur.next != null) {
this.cur = this.cur.next;
index++;
}
}
remove(ele){
let pre = this.findPre(ele)
while (pre.next != null) {
pre.next = pre.next.next;
this.pointer--;
}
}
isEmpty(){
return this.pointer ==0
}
size(){
return this.pointer
}
show(){
return this.cur.element
}
}
const link = new Link();
link.insert('aaa', 'head');
link.insert('bbb', 'aaa');
link.advance(2);
console.log(link.show());
console.log(link.size())
//doublelink 双向链表
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.