FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/generics/LinkedStack.java at master · CSapprenticeWT/OnJava8-Examples · GitHub
CSapprenticeWT
OnJava8-Examples
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
generics
/
LinkedStack.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (42 loc) · 1.02 KB
Breadcrumbs
OnJava8-Examples
/
generics
/
LinkedStack.java
Copy path
File metadata and controls
43 lines (42 loc) · 1.02 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
// generics/LinkedStack.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// A stack implemented with an internal linked structure
public
class
LinkedStack
<
T
> {
private
static
class
Node
<
U
> {
U
item
;
Node
<
U
>
next
;
Node
() {
item
=
null
;
next
=
null
; }
Node
(
U
item
,
Node
<
U
>
next
) {
this
.
item
=
item
;
this
.
next
=
next
;
}
boolean
end
() {
return
item
==
null
&&
next
==
null
;
}
}
private
Node
<
T
>
top
=
new
Node
<>();
// End sentinel
public
void
push
(
T
item
) {
top
=
new
Node
<>(
item
,
top
);
}
public
T
pop
() {
T
result
=
top
.
item
;
if
(!
top
.
end
())
top
=
top
.
next
;
return
result
;
}
public
static
void
main
(
String
[]
args
) {
LinkedStack
<
String
>
lss
=
new
LinkedStack
<>();
for
(
String
s
:
"Phasers on stun!"
.
split
(
" "
))
lss
.
push
(
s
);
String
s
;
while
((
s
=
lss
.
pop
()) !=
null
)
System
.
out
.
println
(
s
);
}
}
/* Output:
stun!
on
Phasers
*/
Back
|
FazBrowse Home
|
New Git URL