Skip to content

Commit

Permalink
Merge pull request wangzheng0822#44 from nameczz/master
Browse files Browse the repository at this point in the history
[Javascript] [08_stack] [09_queue] code
  • Loading branch information
wangzheng0822 authored Oct 12, 2018
2 parents 5c65080 + c2f1a0f commit baa4a21
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 0 deletions.
56 changes: 56 additions & 0 deletions javascript/08_stack/SampleBrowser.js
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')
62 changes: 62 additions & 0 deletions javascript/08_stack/StackBasedOnLinkedList.js
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
71 changes: 71 additions & 0 deletions javascript/09_queue/CircularQueueBasedOnLinkedList.js
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
52 changes: 52 additions & 0 deletions javascript/09_queue/QueueBasedOnLinkedList.js
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)
}

0 comments on commit baa4a21

Please sign in to comment.