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