-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicStack.java
More file actions
40 lines (34 loc) · 953 Bytes
/
BasicStack.java
File metadata and controls
40 lines (34 loc) · 953 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
/**
* Created by yasin_000 on 19.8.2017.
*/
public class BasicStack<T> {
private T [] data;
private int stackPointer;
public BasicStack(int capacity){
data = (T[]) new Object[capacity];
stackPointer = 0;
}
public void push(T newItem){
if (stackPointer == 0)
throw new IllegalStateException("No more Stack item");
// if (size() == data.length)
// throw new IllegalStateException("No more Stack item");
data[stackPointer++] = newItem;
}
public T pop(){
return data[--stackPointer];
}
public boolean contains(T item){
boolean found = false;
for (int i = 0; i < stackPointer; i++) {
if (data[i].equals(item)){
found = true;
break;
}
}
return found;
}
public int size(){
return stackPointer;
}
}