栈
先进后出。
function Stack() {
this.arr = []
this.push = function (value) {
this.arr.push(value)
};
this.pop = function () {
return this.arr.pop()
}
}
let stack = new Stack()
stack.push(1)
stack.push(2)
stack.push(3)
console.log(stack.arr)
stack.pop()
console.log(stack.arr)
队列
先进先出。
function Queue() {
this.arr = []
this.push = function (value) {
this.arr.push(value)
}
this.shift = function () {
this.arr.shift()
}
}
let queue = new Queue()
queue.push(1)
queue.push(2)
queue.push(3)
console.log(queue.arr)
queue.shift()
console.log(queue.arr)
双向链表
无论给出哪一个节点,都可以对整个链表遍历;但是多浪费一个引用的空间。
function Node(value) {
this.value = value
this.next = null
this.pre = null
}
let node1 = new Node(1)
let node2 = new Node(2)
let node3 = new Node(3)
let node4 = new Node(4)
let node5 = new Node(5)
node1.next = node2
node2.pre = node1
node2.next = node3
node3.pre = node2
node3.next = node4
node4.pre = node3
node4.next = node5
node5.pre= node4
二维数据结构
let arr = new Array(4)
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(8)
}
二维拓扑结构(图)
function Node(value) {
this.value = value
this.neighbor = []
}
let a = new Node('a')
let b = new Node('b')
let c = new Node('c')
let d = new Node('d')
let e = new Node('e')
a.neighbor.push(b)
a.neighbor.push(c)
a.neighbor.push(e)
b.neighbor.push(a)
a.neighbor.push(d)
e.neighbor.push(b)
本文含有隐藏内容,请 开通VIP 后查看