-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.java
62 lines (48 loc) · 867 Bytes
/
Stack.java
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
public class Stack {
private Object[] elements;
private int top;
public Stack(int capacity) {
elements = new Object[capacity];
top = -1;
}
public boolean isEmpty() {
return (top ==-1);
}
public boolean isFull() {
return (top +1 == elements.length);
}
public void Push(Object data) {
if(isFull()) {
System.out.println("Stack is full.");
}
else {
top++;
elements[top] = data;
}
}
public Object pop() {
if(isEmpty()) {
System.out.println("Stack is empty.");
return null;
}
else {
Object retData = elements[top];
elements[top] = null;
top--;
return retData;
}
}
public Object peek() {
if(isEmpty()) {
System.out.println("Stack is empty.");
return null;
}
else {
Object retData = elements[top];
return retData;
}
}
public int size() {
return top+1;
}
}