-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArrayStack.java
More file actions
78 lines (67 loc) · 1.41 KB
/
Copy pathMyArrayStack.java
File metadata and controls
78 lines (67 loc) · 1.41 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package Review;
interface Stack {
void push(int data);
int pop();
int peek();
int getStackSize();
}
public class MyArrayStack extends MyArray implements Stack{
private int top;
public int STACK_SIZE;
public MyArrayStack() {
STACK_SIZE = 10;
top = 0;
}
public MyArrayStack(int size) {
super(size);
STACK_SIZE = size;
top = 0;
}
@Override
public void push(int data) {
if(isFull()) {
System.out.println("stack is full");
return;
}
addElement(data);
top++;
}
@Override
public int pop() {
int ret = MyArray.ERROR_NUM;
if(isEmpty()) {
System.out.println("stack is empty");
return ret;
}
return removeElement(--top);
}
@Override
public int peek() {
int ret = MyArray.ERROR_NUM;
if(isEmpty()) {
System.out.println("stack is empty");
return ret;
}
return removeElement(top-1);
}
@Override
public int getStackSize() {
return top;
}
public boolean isEmpty() {
if(top == 0) {
return true;
}
else {
return false;
}
}
public boolean isFull() {
if(top == STACK_SIZE) {
return true;
}
else {
return false;
}
}
}