-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArrayImplementation.java
More file actions
55 lines (42 loc) · 989 Bytes
/
StackArrayImplementation.java
File metadata and controls
55 lines (42 loc) · 989 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package DSImplementations;
public class StackArrayImplementation {
private int[] stackArray;
private int top; // index of the top element
private int size;
public StackArrayImplementation(int size) {
this.size = size;
stackArray = new int[size];
top = -1;
}
public boolean isFull() {
return (top == size - 1);
}
public boolean isEmpty() {
return (top == -1);
}
public void push(int element) {
if (isFull()) {
System.out.println("Stack is full");
} else {
top++;
stackArray[top] = element;
}
}
public int pop() {
int element = 0;
if (isEmpty()) {
System.out.println("Stack is empty");
} else {
element = stackArray[top];
top--;
}
return element;
}
public int peek() {
if (isEmpty()) {
System.out.println("Stack is empty");
return -1;
}
return stackArray[top];
}
}