FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Data-Structures-Algorithms/Structures/Stack.java at master · hound77/Data-Structures-Algorithms · GitHub
hound77
/
Data-Structures-Algorithms
Public
forked from
CodersForLife/Data-Structures-Algorithms
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
Data-Structures-Algorithms
/
Structures
/
Stack.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (59 loc) · 1.37 KB
Breadcrumbs
Data-Structures-Algorithms
/
Structures
/
Stack.java
Copy path
File metadata and controls
73 lines (59 loc) · 1.37 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
import
java
.
lang
.
IndexOutOfBoundsException
;
import
java
.
util
.
Arrays
;
public
class
Stack
<
E
> {
private
int
max
;
private
int
top
;
private
Object
[]
list
;
public
Stack
() {
max
=
512
;
top
=
0
;
list
=
new
Object
[
max
];
}
public
Stack
(
int
size
) {
max
=
size
;
top
=
0
;
list
=
new
Object
[
max
];
}
@
SuppressWarnings
(
"unchecked"
)
public
boolean
isEmpty
() {
for
(
E
t
: (
E
[])
list
) {
if
(
t
!=
null
) {
return
false
;
}
}
return
true
;
}
@
SuppressWarnings
(
"unchecked"
)
public
E
peek
() {
return
(
E
)
list
[
top
];
}
@
SuppressWarnings
(
"unchecked"
)
public
int
size
() {
if
(
top
==
0
) {
return
0
;
}
int
count
=
0
;
for
(
E
t
: (
E
[])
list
) {
if
(
t
!=
null
) {
count
++;
}
}
return
count
;
}
@
SuppressWarnings
(
"unchecked"
)
public
E
pop
() {
if
(
isEmpty
()) {
throw
new
IndexOutOfBoundsException
(
"This stack is empty; nothing to pop."
);
}
E
data
= (
E
)
list
[
top
];
top
= (
top
==
0
?
0
:
top
-
1
);
return
data
;
}
public
void
push
(
E
data
) {
if
(
top
==
max
-
1
) {
list
=
Arrays
.
copyOf
(
list
,
list
.
length
*
2
);
}
top
++;
list
[
top
] =
data
;
}
}
Back
|
FazBrowse Home
|
New Git URL