- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.java
More file actions
Latest commit
42 lines (39 loc) · 1017 Bytes
/
Copy pathStack.java
File metadata and controls
42 lines (39 loc) · 1017 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
packagecom.codinginterview.dsa.stack;
publicclassStack<T> {
privatestaticintMAX_SIZE = 40;
privateElement<T> top;
privateintsize = 0;
publicvoidpush(Tdata) throwsStackOverflowException {
if(size == MAX_SIZE){
thrownewStackOverflowException();
}
Elementelem = newElement(data, top);
top = elem;
size ++;
}
publicTpop() throwsStackUnderflowException {
if (size == 0) {
thrownewStackUnderflowException();
}
Tdata = top.getData();
top = top.getNext();
size --;
returndata;
}
publicTpeek() throwsStackUnderflowException {
if(size == 0){
thrownewStackUnderflowException();
}
Tdata = top.getData();
returndata;
}
publicbooleanisEmpty(){
returnsize == 0;
}
publicbooleanisFull(){
returnsize == MAX_SIZE;
}
publicintgetSize(){
returnsize;
}
}