-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.java
More file actions
42 lines (39 loc) · 1017 Bytes
/
Stack.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
package com.codinginterview.dsa.stack;
public class Stack<T> {
private static int MAX_SIZE = 40;
private Element<T> top;
private int size = 0;
public void push(T data) throws StackOverflowException {
if(size == MAX_SIZE){
throw new StackOverflowException();
}
Element elem = new Element(data, top);
top = elem;
size ++;
}
public T pop() throws StackUnderflowException {
if (size == 0) {
throw new StackUnderflowException();
}
T data = top.getData();
top = top.getNext();
size --;
return data;
}
public T peek() throws StackUnderflowException {
if(size == 0){
throw new StackUnderflowException();
}
T data = top.getData();
return data;
}
public boolean isEmpty(){
return size == 0;
}
public boolean isFull(){
return size == MAX_SIZE;
}
public int getSize(){
return size;
}
}