forked from AllAlgorithms/java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseStack.java
More file actions
Latest commit
34 lines (30 loc) · 548 Bytes
/
Copy pathUseStack.java
File metadata and controls
34 lines (30 loc) · 548 Bytes
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
classStack {
intstcksize = 100;
intstck[] = newint[stcksize];
inttos;
Stack() {
tos = -1;
}
voidpush(intitem) {
if (tos == stcksize - 1)
System.out.println("Stack Overflow");
else
stck[++tos] = item;
}
intpop() {
if (tos < 0) {
System.out.println("Stack Underflow.");
return0;
} else
returnstck[tos--];
}
}
publicclassUseStack {
publicstaticvoidmain(Stringargs[]) {
Stacks = newStack();
s.push(1);
s.push(2);
s.push(3);
s.pop(); // return 3 as it was the last inserted element
}
}