-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.java
68 lines (57 loc) · 1.25 KB
/
Stack.java
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
package prg.es1;
import java.util.ArrayList;
import java.lang.IllegalArgumentException;
import java.util.EmptyStackException;
public class Stack<E>{
private ArrayList<E> stackOfString = new ArrayList<>();
public void push(E element){
if (!this.isFull()) {
stackOfString.add(0, element);
} else {
throw new IllegalArgumentException("StackPieno");
}
}
public E pop(){
if(!isEmpty()){
return stackOfString.remove(0);
} else {
throw new EmptyStackException();
}
}
public boolean isEmpty(){
if (stackOfString.isEmpty()) {
return true;
}
return false;
}
public boolean isFull(){
if(stackOfString.size()>=100){
return true;
}
return false;
}
public int size(){
return stackOfString.size();
}
public boolean equals(Stack stack2){
/*if(this.size() == stack2.size()){
for(int i = 0; i < dim; i++){
if(!this.get(i).equals(stack2.get(i))){
return false;
}
}
return true;*/
return this.stackOfString.equals(stack2.stackOfString);
}
public String toString(){
String res = "Pila: ";
if(this.isEmpty()){
res += " - ";
} else {
for (E element : stackOfString) {
res += "\n" + element;
}
}
return res;
}
}