-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackLengthTest.java
More file actions
49 lines (42 loc) · 1.32 KB
/
Copy pathStackLengthTest.java
File metadata and controls
49 lines (42 loc) · 1.32 KB
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
public class StackLengthTest {
private int stck[];
private int tos;
StackLengthTest(int size) {
stck = new int[size];
tos = -1;
}
void push(int item) {
// Stack length is items + 1.
// If tos is 5 then 5th element already there because stck[++tos].
if (tos == stck.length-1) {
System.out.println("Stack is full.");
} else {
// If tos -1 then it will be the first item stck[0].
stck[++tos] = item;
}
}
int pop() {
if (tos < 0) {
System.out.println("Stack is empty.");
return 0;
} else {
// First return stck[tos == 4] then decrement.
return stck[tos--];
}
}
}
class TestStack {
public static void main(String args[]) {
StackLengthTest mystack1;
mystack1 = new StackLengthTest(5);
StackLengthTest mystack2 = new StackLengthTest(10);
for(int i = 0; i < 5; i++) mystack1.push(i);
for(int i = 0; i < 10; i++) mystack2.push(i);
System.out.println("Stack in mystack1: ");
for(int i = 0; i < 5; i++)
System.out.print(mystack1.pop() + " ");
System.out.println("\nStack in mystack2: ");
for(int i = 0; i < 10; i++)
System.out.print(mystack2.pop() + " ");
}
}