|
| 1 | +/** |
| 2 | + * 基于单向链表实现栈结构 |
| 3 | + */ |
| 4 | +export class Stack<T> { |
| 5 | + private node: LinkedNode<T> | null = null |
| 6 | + size: number = 0 |
| 7 | + |
| 8 | + public push(value: T) { |
| 9 | + if (!value) return |
| 10 | + const newNode = new LinkedNode(value) |
| 11 | + if (!this.node) { |
| 12 | + this.node = newNode |
| 13 | + } else { |
| 14 | + newNode.next = this.node |
| 15 | + this.node = newNode |
| 16 | + } |
| 17 | + this.size++ |
| 18 | + } |
| 19 | + |
| 20 | + public pop(): T | null { |
| 21 | + if (!this.node) { |
| 22 | + return null |
| 23 | + } |
| 24 | + const value = this.node.value |
| 25 | + this.node = this.node.next |
| 26 | + this.size-- |
| 27 | + return value |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * 单向链表 |
| 33 | + */ |
| 34 | +class LinkedNode<T> { |
| 35 | + value: T |
| 36 | + next: LinkedNode<T> | null |
| 37 | + |
| 38 | + constructor(value: T, next: LinkedNode<T> | null = null) { |
| 39 | + this.value = value |
| 40 | + this.next = next |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * 使用双栈结构实现浏览器的前进后退 |
| 46 | + */ |
| 47 | +class Browser<T> { |
| 48 | + // 存放后退的所有历史url |
| 49 | + private backStack: Stack<T> |
| 50 | + // 存放前进的所有url |
| 51 | + private forwardStack: Stack<T> |
| 52 | + private current: T |
| 53 | + |
| 54 | + constructor(current: T) { |
| 55 | + this.backStack = new Stack<T>() |
| 56 | + this.forwardStack = new Stack<T>() |
| 57 | + this.current = current |
| 58 | + } |
| 59 | + |
| 60 | + public back(): T | null { |
| 61 | + if (this.backStack.size > 0) { |
| 62 | + this.forwardStack.push(this.current) |
| 63 | + this.current = this.backStack.pop()! |
| 64 | + return this.getCurrentPage() |
| 65 | + } |
| 66 | + return null |
| 67 | + } |
| 68 | + |
| 69 | + public forward(): T | null { |
| 70 | + if (this.forwardStack.size > 0) { |
| 71 | + this.backStack.push(this.current) |
| 72 | + this.current = this.forwardStack.pop()! |
| 73 | + return this.getCurrentPage() |
| 74 | + } |
| 75 | + return null |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * 在网页上点击一个链接 |
| 80 | + * @param value |
| 81 | + */ |
| 82 | + public linkUrl(value: T) { |
| 83 | + this.current && this.backStack.push(this.current) |
| 84 | + this.current = value |
| 85 | + } |
| 86 | + |
| 87 | + public getCurrentPage(): T { |
| 88 | + return this.current |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +const browser = new Browser('www.baidu.com') |
| 93 | +browser.linkUrl('www.yuanzhoucehui.com') |
| 94 | +browser.linkUrl('www.github.com/jsrdxzw') |
| 95 | +// browser.back() |
| 96 | +// www.github.com/jsrdxzw |
| 97 | +console.log(browser.getCurrentPage()) |
| 98 | +browser.back() |
| 99 | +// www.yuanzhucehui.com |
| 100 | +console.log(browser.getCurrentPage()) |
| 101 | +browser.back() |
| 102 | +// www.baidu.com |
| 103 | +console.log(browser.getCurrentPage()) |
| 104 | +browser.back() |
| 105 | +// www.baidu.com |
| 106 | +console.log(browser.getCurrentPage()) |
| 107 | +browser.forward() |
| 108 | +// www.yuanzhucehui.com |
| 109 | +console.log(browser.getCurrentPage()) |
| 110 | +browser.forward() |
| 111 | +// www.github.com/jsrdxzw |
| 112 | +console.log(browser.getCurrentPage()) |
0 commit comments