|
| 1 | +package algo.lesson08; |
| 2 | + |
| 3 | +/** |
| 4 | + * 使用前后栈实现浏览器的前进后退。 |
| 5 | + * |
| 6 | + * @author chinalwb |
| 7 | + */ |
| 8 | +public class SampleBrowser { |
| 9 | + |
| 10 | + public static void main(String[] args) { |
| 11 | + SampleBrowser browser = new SampleBrowser(); |
| 12 | + browser.open("http://www.baidu.com"); |
| 13 | + browser.open("http://news.baidu.com/"); |
| 14 | + browser.open("http://news.baidu.com/ent"); |
| 15 | + browser.goBack(); |
| 16 | + browser.goBack(); |
| 17 | + browser.goForward(); |
| 18 | + browser.open("http://www.qq.com"); |
| 19 | + browser.goForward(); |
| 20 | + browser.goBack(); |
| 21 | + browser.goForward(); |
| 22 | + browser.goBack(); |
| 23 | + browser.goBack(); |
| 24 | + browser.goBack(); |
| 25 | + browser.goBack(); |
| 26 | + browser.checkCurrentPage(); |
| 27 | + } |
| 28 | + |
| 29 | + private String currentPage; |
| 30 | + private LinkedListBasedStack backStack; |
| 31 | + private LinkedListBasedStack forwardStack; |
| 32 | + |
| 33 | + public SampleBrowser() { |
| 34 | + this.backStack = new LinkedListBasedStack(); |
| 35 | + this.forwardStack = new LinkedListBasedStack(); |
| 36 | + } |
| 37 | + |
| 38 | + public void open(String url) { |
| 39 | + if (this.currentPage != null) { |
| 40 | + this.backStack.push(this.currentPage); |
| 41 | + this.forwardStack.clear(); |
| 42 | + } |
| 43 | + showUrl(url, "Open"); |
| 44 | + } |
| 45 | + |
| 46 | + public boolean canGoBack() { |
| 47 | + return this.backStack.size() > 0; |
| 48 | + } |
| 49 | + |
| 50 | + public boolean canGoForward() { |
| 51 | + return this.forwardStack.size() > 0; |
| 52 | + } |
| 53 | + |
| 54 | + public String goBack() { |
| 55 | + if (this.canGoBack()) { |
| 56 | + this.forwardStack.push(this.currentPage); |
| 57 | + String backUrl = this.backStack.pop(); |
| 58 | + showUrl(backUrl, "Back"); |
| 59 | + return backUrl; |
| 60 | + } |
| 61 | + |
| 62 | + System.out.println("* Cannot go back, no pages behind."); |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + public String goForward() { |
| 67 | + if (this.canGoForward()) { |
| 68 | + this.backStack.push(this.currentPage); |
| 69 | + String forwardUrl = this.forwardStack.pop(); |
| 70 | + showUrl(forwardUrl, "Foward"); |
| 71 | + return forwardUrl; |
| 72 | + } |
| 73 | + |
| 74 | + System.out.println("** Cannot go forward, no pages ahead."); |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + public void showUrl(String url, String prefix) { |
| 79 | + this.currentPage = url; |
| 80 | + System.out.println(prefix + " page == " + url); |
| 81 | + } |
| 82 | + |
| 83 | + public void checkCurrentPage() { |
| 84 | + System.out.println("Current page is: " + this.currentPage); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * A LinkedList based Stack implementation. |
| 89 | + */ |
| 90 | + public static class LinkedListBasedStack { |
| 91 | + |
| 92 | +// public static void main(String[] args) { |
| 93 | +// LinkedListBasedStack stack = new LinkedListBasedStack(); |
| 94 | +// stack.push("A"); |
| 95 | +// stack.push("B"); |
| 96 | +// stack.push("C"); |
| 97 | +// stack.pop(); |
| 98 | +// stack.push("D"); |
| 99 | +// stack.push("E"); |
| 100 | +// stack.pop(); |
| 101 | +// stack.push("F"); |
| 102 | +// stack.print(); |
| 103 | +// |
| 104 | +//// String data = stack.getTopData(); |
| 105 | +//// System.out.println("Top data == " + data); |
| 106 | +// } |
| 107 | + |
| 108 | + private int size; |
| 109 | + private Node top; |
| 110 | + |
| 111 | + static Node createNode(String data, Node next) { |
| 112 | + return new Node(data, next); |
| 113 | + } |
| 114 | + |
| 115 | + public void clear() { |
| 116 | + this.top = null; |
| 117 | + this.size = 0; |
| 118 | + } |
| 119 | + |
| 120 | + public void push(String data) { |
| 121 | + Node node = createNode(data, this.top); |
| 122 | + this.top = node; |
| 123 | + this.size++; |
| 124 | + } |
| 125 | + |
| 126 | + public String pop() { |
| 127 | + Node popNode = this.top; |
| 128 | + if (popNode == null) { |
| 129 | + System.out.println("Stack is empty."); |
| 130 | + return null; |
| 131 | + } |
| 132 | + this.top = popNode.next; |
| 133 | + if (this.size > 0) { |
| 134 | + this.size--; |
| 135 | + } |
| 136 | + return popNode.data; |
| 137 | + } |
| 138 | + |
| 139 | + public String getTopData() { |
| 140 | + if (this.top == null) { |
| 141 | + return null; |
| 142 | + } |
| 143 | + return this.top.data; |
| 144 | + } |
| 145 | + |
| 146 | + public int size() { |
| 147 | + return this.size; |
| 148 | + } |
| 149 | + |
| 150 | + public void print() { |
| 151 | + System.out.println("Print stack:"); |
| 152 | + Node currentNode = this.top; |
| 153 | + while (currentNode != null) { |
| 154 | + String data = currentNode.getData(); |
| 155 | + System.out.print(data + "\t"); |
| 156 | + currentNode = currentNode.next; |
| 157 | + } |
| 158 | + System.out.println(); |
| 159 | + } |
| 160 | + |
| 161 | + public static class Node { |
| 162 | + |
| 163 | + private String data; |
| 164 | + private Node next; |
| 165 | + |
| 166 | + public Node(String data) { |
| 167 | + this(data, null); |
| 168 | + } |
| 169 | + |
| 170 | + public Node(String data, Node next) { |
| 171 | + this.data = data; |
| 172 | + this.next = next; |
| 173 | + } |
| 174 | + |
| 175 | + public void setData(String data) { |
| 176 | + this.data = data; |
| 177 | + } |
| 178 | + |
| 179 | + public String getData() { |
| 180 | + return this.data; |
| 181 | + } |
| 182 | + |
| 183 | + public void setNext(Node next) { |
| 184 | + this.next = next; |
| 185 | + } |
| 186 | + |
| 187 | + public Node getNext() { |
| 188 | + return this.next; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + } |
| 193 | +} |
0 commit comments