-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
82 lines (71 loc) · 1.75 KB
/
Stack.java
File metadata and controls
82 lines (71 loc) · 1.75 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
80
81
82
/**
* Universidad del Valle de Guatemala
* Algoritmos y Estructura de Datos
* Sección: 10
* 30/07/2015
* Hoja de Trabajo 2
*
*/
import java.util.Vector;
/**
*
* La clase <Stack> implementa la clase <ADTStack>.
* Utiliza datos tipo Integer. Con esta clase se pretende utilizar
* la clase <Vector> la cual tiene opciones predefinidas que harán
* que se cree un vector de datos que tendrá la forma de una pila
* por los métodos definidos para esta clase. Gracias a esta clase
* se harán operaciones en notación Postfix en <Calculadora>
* La idea de la pila es realizar push y pop de datos, verificar si
* la pila está vacía y mostrarlo en pantalla así como vaciar la pila
* u obtener su último dato.
*
* @author André Rodas
* @author Rudy Garrido
* @author Yosemite Meléndez
*
* @param <Integer> para recibir datos numéricos
*/
public class Stack<Integer> implements ADTStack<Integer> {
private Vector <Integer> myVec;
/**
* Este es el constructor de <Stack> y crea un nuevo vector
* de la Clase <Vector>.
* Crea un Objeto:
* <myvec> que será la nueva pila
* La inicializa y la vacía.
*/
public Stack() {
this.myVec = new Vector <Integer>();
myVec.clear();
}
@Override
public void empty() {
myVec.clear();
}
@Override
public boolean isEmpty() {
return myVec.isEmpty();
}
@Override
public int size() {
return myVec.size();
}
@Override
public Integer pop() throws Exception {
if (myVec.isEmpty())
throw new Exception("Vector vacio!");
else
return myVec.remove(myVec.size()-1);
}
@Override
public Integer peek() throws Exception {
if (myVec.isEmpty())
throw new Exception("Vector vacio!");
else
return myVec.lastElement();
}
@Override
public void push(Integer x) {
myVec.add(x);
}
}