-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA4Stack.java
More file actions
79 lines (60 loc) · 1.3 KB
/
Copy pathA4Stack.java
File metadata and controls
79 lines (60 loc) · 1.3 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
79
// Name: Puneet Grewal
// V#: V00951156
public class A4Stack<T> implements Stack<T> {
private static final int DEFAULT_CAPACITY = 5;
private T[] data;
private int top;
public A4Stack() {
data = (T[]) new Object[DEFAULT_CAPACITY];
top = 0;
}
public A4Stack(int size) {
data = (T[]) new Object[size];
top = 0;
}
public void push(T v) throws FullStackException {
// TODO: implement this
if (isFull()){
throw new FullStackException ("Stack is full.");
} else {
data[top] = v;
top = top + 1;
}
}
public T pop() throws EmptyStackException {
// TODO: implement this
if (isEmpty()){
throw new EmptyStackException ("Stack is empty.");
} else {
T val = top();
top = top - 1;
data[top] = null;
return val;
}
}
public void popAll() {
// TODO: implement this
if (top!=0){
data = (T[]) new Object[DEFAULT_CAPACITY];
top=0;
} else {
return;
}
}
public boolean isEmpty() {
// TODO: implement this
return(top==0);
}
public boolean isFull() {
// TODO: implement this
return (data.length == top);
}
public T top() throws EmptyStackException {
// TODO: implement this
if(isEmpty()){
throw new EmptyStackException();
} else {
return data[top-1];
}
}
}