forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wangzheng0822#44 from nameczz/master
[Javascript] [08_stack] [09_queue] code
- Loading branch information
Showing
4 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* 使用前后栈实现浏览器的前进后退。 | ||
* | ||
* Author nameczz | ||
*/ | ||
const stack = require('./StackBasedOnLinkedList') | ||
|
||
class SampleBrowser { | ||
constructor() { | ||
this.normalStack = new stack.CreatedStack() | ||
this.backStack = new stack.CreatedStack() | ||
} | ||
// 正常浏览页面 | ||
pushNormal(name) { | ||
this.normalStack.push(name) | ||
this.backStack.clear() | ||
this.displayAllStack() | ||
} | ||
// 后退 | ||
back() { | ||
const value = this.normalStack.pop() | ||
if (value !== -1) { | ||
this.backStack.push(value) | ||
this.displayAllStack() | ||
} else { | ||
console.log('无法后退') | ||
} | ||
} | ||
// 前进 | ||
front() { | ||
const value = this.backStack.pop() | ||
if (value !== -1) { | ||
this.normalStack.push(value) | ||
this.displayAllStack() | ||
} else { | ||
console.log('无法前进') | ||
} | ||
} | ||
// 打印栈内数据 | ||
displayAllStack() { | ||
console.log('---后退页面---') | ||
this.backStack.display() | ||
console.log('---浏览页面---') | ||
this.normalStack.display() | ||
} | ||
} | ||
// Test | ||
const browser = new SampleBrowser() | ||
browser.pushNormal('www.google.com') | ||
browser.pushNormal('www.baidu.com') | ||
browser.pushNormal('www.github.com') | ||
// 后退 | ||
browser.back() | ||
browser.back() | ||
browser.front() | ||
browser.pushNormal('www.new.com') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* 基于链表实现的栈。 | ||
* | ||
* Author: nameczz | ||
*/ | ||
|
||
class Node { | ||
constructor(element) { | ||
this.element = element | ||
this.next = null | ||
} | ||
} | ||
|
||
class StackBasedLinkedList { | ||
constructor() { | ||
this.top = null | ||
} | ||
push(value) { | ||
const node = new Node(value) | ||
if (this.top === null) { | ||
this.top = node | ||
} else { | ||
node.next = this.top | ||
this.top = node | ||
} | ||
} | ||
pop() { | ||
if (this.top === null) { | ||
return -1 | ||
} | ||
const value = this.top.element | ||
this.top = this.top.next | ||
return value | ||
} | ||
// 为了实现浏览器前进后退 | ||
clear() { | ||
this.top = null | ||
} | ||
display() { | ||
if (this.top !== null) { | ||
let temp = this.top | ||
while (temp !== null) { | ||
console.log(temp.element) | ||
temp = temp.next | ||
} | ||
} | ||
} | ||
} | ||
// Test | ||
const newStack = new StackBasedLinkedList() | ||
newStack.push(1) | ||
newStack.push(2) | ||
newStack.push(3) | ||
// 获取元素 | ||
let res = 0 | ||
console.log('-------获取pop元素------') | ||
while (res !== -1) { | ||
res = newStack.pop() | ||
console.log(res) | ||
} | ||
|
||
exports.CreatedStack = StackBasedLinkedList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* 基于链表实现的循环队列。 | ||
* | ||
* Author: nameczz | ||
*/ | ||
|
||
class Node { | ||
constructor(element) { | ||
this.element = element | ||
this.next = null | ||
} | ||
} | ||
|
||
class CircularQueue { | ||
constructor() { | ||
this.head = null | ||
this.tail = null | ||
} | ||
|
||
enqueue(value) { | ||
if (this.head === null) { | ||
this.head = new Node(value) | ||
this.head.next = this.head | ||
this.tail = this.head | ||
} else { | ||
const flag = this.head === this.tail | ||
this.tail.next = new Node(value) | ||
this.tail.next.next = this.head | ||
this.tail = this.tail.next | ||
if (flag) { | ||
this.head.next = this.tail | ||
} | ||
} | ||
} | ||
|
||
dequeue() { | ||
if (this.head === this.tail) { | ||
const value = this.head.element | ||
this.head = null | ||
return value | ||
} else if (this.head !== null) { | ||
const value = this.head.element | ||
this.head = this.head.next | ||
this.tail.next = this.head | ||
return value | ||
} else { | ||
return -1 | ||
} | ||
} | ||
|
||
display() { | ||
let res = 0 | ||
console.log('-------获取dequeue元素------') | ||
while (res !== -1) { | ||
res = this.dequeue() | ||
console.log(res) | ||
} | ||
} | ||
} | ||
// Test | ||
const newCircularQueue = new CircularQueue() | ||
// 插入元素 | ||
newCircularQueue.enqueue(1) | ||
newCircularQueue.enqueue(2) | ||
newCircularQueue.enqueue(3) | ||
// 获取元素 | ||
newCircularQueue.display() | ||
newCircularQueue.enqueue(1) | ||
newCircularQueue.display() | ||
|
||
// exports.CreatedStack = StackBasedLinkedList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* 基于链表实现的队列。 | ||
* | ||
* Author: nameczz | ||
*/ | ||
|
||
class Node { | ||
constructor(element) { | ||
this.element = element | ||
this.next = null | ||
} | ||
} | ||
|
||
class QueueBasedOnLinkedList { | ||
constructor() { | ||
this.head = null | ||
this.tail = null | ||
} | ||
|
||
enqueue(value) { | ||
if (this.head === null) { | ||
this.head = new Node(value) | ||
this.tail = this.head | ||
} else { | ||
this.tail.next = new Node(value) | ||
this.tail = this.tail.next | ||
} | ||
} | ||
|
||
dequeue() { | ||
if (this.head !== null) { | ||
const value = this.head.element | ||
this.head = this.head.next | ||
return value | ||
} else { | ||
return -1 | ||
} | ||
} | ||
} | ||
// Test | ||
const newQueue = new QueueBasedOnLinkedList() | ||
// 插入元素 | ||
newQueue.enqueue(1) | ||
newQueue.enqueue(2) | ||
newQueue.enqueue(3) | ||
// 获取元素 | ||
let res = 0 | ||
console.log('-------获取dequeue元素------') | ||
while (res !== -1) { | ||
res = newQueue.dequeue() | ||
console.log(res) | ||
} |