Skip to content

Commit 09e759b

Browse files
committed
add stack
1 parent fb56328 commit 09e759b

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package chap4_Stacks_Queues;
2+
3+
class StackX{
4+
private int maxSize;
5+
private long[] stackArray;
6+
private int top;
7+
8+
public StackX(int s) {
9+
maxSize = s;
10+
stackArray = new long[maxSize];
11+
top = -1;
12+
}
13+
14+
public void push(long j) {
15+
stackArray[++top] = j;
16+
}
17+
18+
public long pop() {
19+
return stackArray[top--];
20+
}
21+
22+
public long peek() {
23+
return stackArray[top];
24+
}
25+
26+
public boolean isEmpty() {
27+
return (top == -1);
28+
}
29+
30+
public boolean isFull() {
31+
return (top == maxSize);
32+
}
33+
} //end class StackX
34+
35+
public class StackApp {
36+
37+
public static void main(String[] args) {
38+
// TODO Auto-generated method stub
39+
StackX theStack = new StackX(10);
40+
theStack.push(20);
41+
theStack.push(40);
42+
theStack.push(60);
43+
theStack.push(80);
44+
45+
while(!theStack.isEmpty()) {
46+
long value = theStack.pop();
47+
System.out.print(value + ", ");
48+
}
49+
System.out.println("");
50+
} //end main()
51+
52+
}

0 commit comments

Comments
 (0)