栈结构
function Stack() {
var items = [];
this.push = function (ele) {
items.push(ele);
};
this.pop = function () {
return items.pop();
};
this.peek = function () {
return items[items.length - 1];
};
this.isEmpty = function () {
return items.length == 0;
};
this.clear = function () {
items = [];
};
this.size = function () {
return items.length;
};
this.print = function () {
console.log(items.toString());
};
}
// 将十进制转为任意进制
function baseConerter(decNumber, base) {
var remStack = new Stack(),
rem,
baseString = '',
digits = '0123456789ABCDEF'
while (decNumber > 0) {
rem = Math.floor(decNumber % base)
remStack.push(rem)
decNumber = Math.floor(decNumber / base)
}
while (!remStack.isEmpty()) {
baseString += digits[remStack.pop()]
}
console.log('baseString:' + baseString)
return baseString
}
baseConerter(223411, 2) // 110110100010110011
baseConerter(223411, 8) // 664263
baseConerter(223411, 16) // 368B3
队列结构(queue)
function Queue() {
this.items = []
this.enqueue = function (ele) {
this.items.push(ele);
};
this.dequeue = function () {
return this.items.shift();
};
this.front = function () {
return this.items[0];
};
this.isEmpty = function () {
return this.items.length == 0;
};
this.size = function () {
return this.items.length;
};
this.print = function () {
console.log(this.items.toString());
};
}
function jgch(arr, n) {
var queue = new Queue()
for (var i = 0; i < arr.length; i++) {
queue.enqueue(arr[i])
}
for (var j = 0; j < n - 1; j++) {
queue.enqueue(queue.dequeue())
}
queue.dequeue()
while (queue.size() > 1) {
for (var j = 0; j < n - 1; j++) {
queue.enqueue(queue.dequeue())
}
queue.dequeue()
}
return queue.front()
}
jgch(['aa', 'bb', 'cc'], 2)
优先级队列
function QueurElement(element, priority) {
this.element = element
this.priority = priority
}
function PriorityQueue() {
this.items = []
this.enqueue = function (element, priority) {
var queurElement = new QueurElement(element, priority)
if (this.items.length == 0) {
this.items.push(queurElement)
} else {
var added = false
for (var i = 0; i < this.items.length; i++) {
if (queurElement.priority < this.items[i].priority) {
this.items.splice(i, 0, queurElement)
added = true
break
}
}
if (!added) {
this.items.push(queurElement)
}
}
};
this.dequeue = function () {
return this.items.shift();
};
this.front = function () {
return this.items[0];
};
this.isEmpty = function () {
return this.items.length == 0;
};
this.size = function () {
return this.items.length;
};
this.print = function () {
console.log(this.items.toString());
};
}
链表
function Node(element) {
this.element = element
this.next = null
}
function LinkedList() {
this.length = 0
this.head = null
// 链表尾部追加元素方法
LinkedList.prototype.append = function (element) {
var newNode = new Node(element)
if (this.head === null) { // 链表尾空
this.head = newNode
} else { // 链表不为空
var current = this.head
while (current.next) {
current = current.next
}
current.next = newNode
}
this.length++
}
LinkedList.prototype.toString = function () {
var current = this.head
var listString = ""
while (current) {
listString += "," + current.element
current = current.next
}
return listString.slice(1)
}
LinkedList.prototype.insert = function (position, element) {
if (position < 0 || position > this.length) return false
var newNode = new Node(element)
var current = this.head
var previous = null
index = 0
if (position == 0) {
newNode.next = current
this.head = newNode
} else {
while (index++ < position) {
previous = current
current = current.next
}
newNode.next = current
previous.next = newNode
}
this.length++
return true
}
LinkedList.prototype.removeAt = function (position) {
if (position < 0 || position >= this.length) return null
var current = this.head
var previous = null
var index = 0
if (position === 0) {
this.head = current.next
} else {
while (index++ < position) {
previous = current
current = current.next
}
previous.next = current.next
}
this.length--
return current.element
}
LinkedList.prototype.indexOf = function (element) {
var current = this.head
index = 0
while (current) {
if (current.element === element) {
return index
}
index++
current = current.next
}
return -1
}
LinkedList.prototype.remove = function (element) {
var index = this.indexOf(element)
return this.removeAt(index)
}
LinkedList.prototype.isEmpty = function () {
return this.length == 0
}
LinkedList.prototype.size = function () {
return this.length
}
LinkedList.prototype.getFirst = function () {
return this.head.element
}
}
双向链表
function Node(element) {
this.element = element
this.next = null
this.prev = null // 新添加的
}
function DoublyLinkedList() {
this.length = 0
this.head = null
this.tail = null // 新添加的
DoublyLinkedList.prototype.append = function (element) {
var newNode = new Node(element)
if (this.head == null) {
this.head = newNode
this.tail = newNode
} else {
this.tail.next = newNode
newNode.prev = this.tail
this.tail = newNode
}
this.length++
}
DoublyLinkedList.prototype.insert = function (position, element) {
if (position < 0 || position > this.length) return false
var newNode = new Node(element)
if (position === 0) { // 在第一个位置插入数据
if (this.head == null) {
this.head = newNode
this.tail = newNode
} else {
this.head.prev = newNode
newNode.next = this.head
this.head = newNode
}
} else if (position === this.length) { // 插入到最后的情况
this.tail.next = newNode
newNode.prev = this.tail
this.tail = newNode
} else { // 在中间位置插入数据
var index = 0
var current = this.head
var previous = null
while (index++ < position) {
previous = current
current = current.next
}
newNode.next = current
newNode.prev = previous
current.prev = newNode
previous.next = newNode
}
this.length++
return true
}
DoublyLinkedList.prototype.removeAt = function (position) {
if (position < 0 || position >= this.length) return null
var current = this.head
if (position === 0) {
if (this.length == 1) {
this.head = null
this.tail = null
} else {
this.head = this.head.next
this.head.prev = null
}
} else if (position === this.length -1) {
current = this.tail
this.tail = this.tail.prev
this.tail.next = null
} else {
var index = 0
var previous = null
while (index++ < position) {
previous = current
current = current.next
}
previous.next = current.next
current.next.prev = previous
}
this.length--
return current.element
}
DoublyLinkedList.prototype.indexOf = function (element) {
var current = this.head
var index = 0
while (current) {
if (current.element === element) {
return index
}
index++
current = current.next
}
return -1
}
DoublyLinkedList.prototype.remove = function (element) {
var index = this.indexOf(element)
return this.removeAt(index)
}
DoublyLinkedList.prototype.isEmpty = function () {
return this.length === 0
}
DoublyLinkedList.prototype.size = function () {
return this.length
}
DoublyLinkedList.prototype.getHead = function () {
return this.head.element
}
DoublyLinkedList.prototype.getTail = function () {
return this.tail.element
}
DoublyLinkedList.prototype.forwardString = function () {
var current = this.head
var forwardStr = ""
while (current) {
forwardStr += "," + current.element
current = current.next
}
return forwardStr.slice(1)
}
DoublyLinkedList.prototype.reverseString = function () {
var current = this.tail
var reverseStr = ""
while (current) {
reverseStr += "," + current.element
current = current.prev
}
return reverseStr.slice(1)
}
DoublyLinkedList.prototype.toString = function () {
return this.forwardString()
}
}
集合结构
function Set() {
this.items = {}
Set.prototype.has = function (value) {
return this.items.hasOwnProperty(value)
}
Set.prototype.add = function (value) {
if (this.has(value)) return false
this.items[value] = value
return true
}
Set.prototype.remove = function (value) {
if (!this.has(value)) return false
delete this.items[value]
return true
}
Set.prototype.clear = function () {
this.items = {}
}
Set.prototype.size = function () {
return Object.keys(this.items).length
}
Set.prototype.values = function () {
return Object.keys(this.items)
}
}
字典类型
哈希表
质数算法简单版本
质数的特点:大于1的自然数中,只能被1和自己整除的数
function isPrime(num){
for(var i = 2;i<num;i++){
if(num % i == 0){
return false
}
}
return true
}
树
1、数是n个结点的有限集合T(Tree),n=0的时候,称为为空树 数的特性 1、任意非空树,有且仅有一个特定的成为root的节点,树形结构是一种具有递归特征的数据结构(任何一颗数又满足树的概念) 2、树形结构中数据元素之间存在这一对多的关系,或者是多对多 数的存储结构 1、顺序存储或者是链表存储,树的结构不能够直接的存储,需要转换为顺序存储或链表存储。 2、双亲表示:顺序存储各个节点的同时,给各个节点添加一个变量,记录其父节点的位置 3、孩子表示法:建立多个指针域,指向它所有子节点的地址,任何一个节点,都会掌握它所有子节点的信息 4、孩子兄弟表示法:从树的根节点开始,依次采用链表去存储各个节点的孩子节点和兄弟节点。可以把一颗普通树,转换为二叉树
二叉树
如果说树中的每个节点最多只能有两个子节点,这样的树我们称之为二叉树,二叉树可以为空
特点:
1、每个节点最多有两棵子树,所以二叉树中不存在度大于二的节点
一棵树中,最大的节点的度称为树的度,节点的度:节点所拥有的子树的个数
2、左子树和右子树是有顺序的,次序不能任意的颠倒
3、即使树中某个节点只有一颗树,也要去区分它是左子树还是右子树
性质:在二叉树中,第i层上最多有2^i - 1次节点(i >= 1)