Skip to content

Commit ffcd001

Browse files
committed
浏览器页面亲近后退原理代码详细剖析
1 parent 952e42a commit ffcd001

File tree

1 file changed

+245
-0
lines changed

1 file changed

+245
-0
lines changed

Stack/JAVA/SampleBrower.md

+245
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
```java
2+
package easy_test;
3+
4+
/**
5+
* 功能:使用前后栈实现浏览器的前进后退。
6+
* @author :小鹿
7+
*
8+
*/
9+
10+
public class SampleBrowser {
11+
//存储当前页面变量
12+
private String currentPage;
13+
//backStack 栈(存储浏览器前进时浏览过的页面)
14+
private LinkedListBasedStack backStack;
15+
//forwardStack 栈(存储浏览器后退时浏览过的页面)
16+
private LinkedListBasedStack forwardStack;
17+
18+
//构造函数
19+
public SampleBrowser() {
20+
//新建一个浏览器栈(backStack)
21+
this.backStack = new LinkedListBasedStack();
22+
//新建一个浏览器栈(forwardStack)
23+
this.forwardStack = new LinkedListBasedStack();
24+
}
25+
26+
//主函数
27+
public static void main(String[] args) {
28+
SampleBrowser browser = new SampleBrowser();
29+
//打开新网页
30+
browser.open("http://www.baidu.com");
31+
//打开新网页
32+
browser.open("http://news.baidu.com/");
33+
//打开新网页
34+
browser.open("http://news.baidu.com/ent");
35+
//后退网页
36+
browser.goBack();
37+
//后退网页
38+
browser.goBack();
39+
//前进页面
40+
browser.goForward();
41+
//打开新页面
42+
browser.open("http://www.qq.com");
43+
//前进页面
44+
browser.goForward();
45+
//后退网页
46+
browser.goBack();
47+
//前进页面
48+
browser.goForward();
49+
//后退网页
50+
browser.goBack();
51+
//后退网页
52+
browser.goBack();
53+
//后退网页
54+
browser.goBack();
55+
//后退网页
56+
browser.goBack();
57+
//查看当前页面
58+
browser.checkCurrentPage();
59+
}
60+
61+
//打开一个网页
62+
public void open(String url) {
63+
//判断是不是第一个入栈页面(除第一次调用外,将前一个页面入栈,让当前的页面显示到浏览器)
64+
if (this.currentPage != null) {
65+
//入栈操作
66+
this.backStack.push(this.currentPage);
67+
this.forwardStack.clear();
68+
}
69+
//第一个入栈网页,显示到界面
70+
showUrl(url, "Open");
71+
}
72+
73+
//判断栈大小是否大于0
74+
public boolean canGoBack() {
75+
return this.backStack.size() > 0;
76+
}
77+
78+
//判断第二个占是否存在页面
79+
public boolean canGoForward() {
80+
return this.forwardStack.size() > 0;
81+
}
82+
83+
//浏览器退回
84+
public String goBack() {
85+
//如果栈不为空
86+
if (this.canGoBack()) {
87+
//将当前页放到另一个栈中
88+
this.forwardStack.push(this.currentPage);
89+
//原来的栈数据出栈
90+
String backUrl = this.backStack.pop();
91+
//将出栈的页面显示到浏览器界面上
92+
showUrl(backUrl, "Back");
93+
//返回该页面
94+
return backUrl;
95+
}
96+
//否则提示不能进行浏览器倒退
97+
System.out.println("* Cannot go back, no pages behind.");
98+
return null;
99+
}
100+
101+
//浏览器前进
102+
public String goForward() {
103+
//判断第二个栈中是否有页面
104+
if (this.canGoForward()) {
105+
//当前页面入栈
106+
this.backStack.push(this.currentPage);
107+
//前进的页面出栈
108+
String forwardUrl = this.forwardStack.pop();
109+
//将出栈的页面显示到浏览器中
110+
showUrl(forwardUrl, "Foward");
111+
//返回当前页面
112+
return forwardUrl;
113+
}
114+
//否则,提示浏览器不能进行前进
115+
System.out.println("** Cannot go forward, no pages ahead.");
116+
return null;
117+
}
118+
119+
//显示网页,并标记为当前网页
120+
public void showUrl(String url, String prefix) {
121+
this.currentPage = url;
122+
System.out.println(prefix + " page == " + url);
123+
}
124+
125+
//当前页面是哪一个页面
126+
public void checkCurrentPage() {
127+
System.out.println("Current page is: " + this.currentPage);
128+
}
129+
130+
/**
131+
* A LinkedList based Stack implementation.
132+
*/
133+
public static class LinkedListBasedStack {
134+
135+
// 测试栈代码
136+
// public static void main(String[] args) {
137+
// LinkedListBasedStack stack = new LinkedListBasedStack();
138+
// stack.push("A");
139+
// stack.push("B");
140+
// stack.push("C");
141+
// stack.pop();
142+
// stack.push("D");
143+
// stack.push("E");
144+
// stack.pop();
145+
// stack.push("F");
146+
// stack.print();
147+
//
148+
//// String data = stack.getTopData();
149+
//// System.out.println("Top data == " + data);
150+
// }
151+
152+
//栈的大小
153+
private int size;
154+
//栈顶部
155+
private Node top;
156+
157+
//该定义一个结点类
158+
public static class Node {
159+
160+
//数据域
161+
private String data;
162+
//指针域
163+
private Node next;
164+
165+
public Node(String data) {
166+
this(data, null);
167+
}
168+
169+
public Node(String data, Node next) {
170+
this.data = data;
171+
this.next = next;
172+
}
173+
174+
public void setData(String data) {
175+
this.data = data;
176+
}
177+
178+
public String getData() {
179+
return this.data;
180+
}
181+
182+
public void setNext(Node next) {
183+
this.next = next;
184+
}
185+
186+
public Node getNext() {
187+
return this.next;
188+
}
189+
}
190+
191+
//创建一个结点
192+
static Node createNode(String data, Node next) {
193+
return new Node(data, next);
194+
}
195+
//清空栈
196+
public void clear() {
197+
this.top = null;
198+
this.size = 0;
199+
}
200+
//入栈
201+
public void push(String data) {
202+
Node node = createNode(data, this.top);
203+
this.top = node;
204+
this.size++;
205+
}
206+
//出栈
207+
public String pop() {
208+
Node popNode = this.top;
209+
if (popNode == null) {
210+
System.out.println("Stack is empty.");
211+
return null;
212+
}
213+
this.top = popNode.next;
214+
if (this.size > 0) {
215+
this.size--;
216+
}
217+
return popNode.data;
218+
}
219+
//获取栈顶元素
220+
public String getTopData() {
221+
if (this.top == null) {
222+
return null;
223+
}
224+
return this.top.data;
225+
}
226+
//栈的大小
227+
public int size() {
228+
return this.size;
229+
}
230+
//输出栈中的所有元素
231+
public void print() {
232+
System.out.println("Print stack:");
233+
Node currentNode = this.top;
234+
while (currentNode != null) {
235+
String data = currentNode.getData();
236+
System.out.print(data + "\t");
237+
currentNode = currentNode.next;
238+
}
239+
System.out.println();
240+
}
241+
}
242+
}
243+
244+
```
245+

0 commit comments

Comments
 (0)