Skip to content

Commit d44645b

Browse files
committed
Contains methods for Stack
1 parent 2df35f4 commit d44645b

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

general/Stack.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
public class Stack {
2+
private int[] stack;
3+
private static int tos;
4+
5+
Stack() {
6+
stack = new int[5];
7+
tos = -1;
8+
}
9+
10+
public boolean isEmpty() {
11+
if (tos < 0) {
12+
return true;
13+
} else {
14+
return false;
15+
}
16+
}
17+
18+
public boolean isFull() {
19+
if (tos >= 5) {
20+
return true;
21+
} else {
22+
return false;
23+
}
24+
}
25+
26+
public boolean push(int data) {
27+
if (!isFull()) {
28+
stack[++tos] = data;
29+
System.out.println("Data pushed : " + data);
30+
return true;
31+
}
32+
return false;
33+
}
34+
35+
public int pop() {
36+
if (!isEmpty()) {
37+
int data = stack[tos--];
38+
System.out.println("Data popped : " + data);
39+
return data;
40+
}
41+
return -1;
42+
}
43+
44+
public void display() {
45+
if(!isEmpty()){
46+
System.out.print("Elements in Stack : [ ");
47+
for (int i = 0; i <= 5; i++) {
48+
System.out.print(stack[tos] + " ");
49+
}
50+
System.out.print("]\n");
51+
}else{
52+
System.out.println("Empty\n");
53+
}
54+
}
55+
}
56+
57+

0 commit comments

Comments
 (0)