View difference between Paste ID: 1FGVjt3z and SsUQ5dab
SHOW: | | - or go back to the newest paste.
1
public class Stack<Generico> {
2
3
    private final static Integer dimencion=10;
4
5
    private Generico contenedor[];
6
    private Integer count;
7
8
    public Stack (){
9
        this(Stack.dimencion);   
10
    }
11
    
12
    public Stack(Integer dimencion){
13
        if (dimencion <=0){
14
            throw new RuntimeException("La cantidad de elementos en la  pila debe ser positiva");
15
        }
16
        this.contenedor = (Generico[]) new Object[dimencion];
17
        this.count = 0;
18
    }
19
20
 
21
    //Metodo de pilas de regiones
22
 
23
    // Prueva si esta pila esta vacia
24
    public boolean empty() {
25
        return this.count <= 0;
26
    }
27
 
28
    //Mira el objeto en la parte superior de esta pila sin quitarlo de la pila.
29
    public Generico peek() {
30
        if (this.empty()) {
31
            throw new RuntimeException("La pila está vacía...");
32
        }
33
        return this.contenedor[this.count - 1];
34
    }
35
 
36
     // Elimina el objeto en la parte superior de esta pila y devuelve ese objeto como el valor de esta función.
37
    public Generico pop() {
38
        if (this.empty()) {
39
            throw new RuntimeException("La pila está vacía...");
40
        }
41
        --this.count;
42
        return this.contenedor[this.count];
43
    }
44
 
45
    // Coloca un elemento en la parte superior de esta pila.    
46
    public Generico push(Generico element) {
47
        if (this.size() >= this.contenedor.length) {
48
49
// lanzar nueva RuntimeException ("La pila está llena ...");
50
 
51
            Generico [] temp = (Generico[]) new Object[this.contenedor.length * 2];
52
            for (int i = 0; i < this.contenedor.length; ++i) {
53
                temp[i] = this.contenedor[i];
54
            }
55
            this.contenedor = temp;
56
        }
57
        this.contenedor[this.count] = element;
58
        ++this.count;
59
        return element;
60
    }
61
 
62
    // Devuelve la posición basada en 1 donde un objeto está en esta pila.
63
    public int search(Object object) {
64
        for (int pos = this.count - 1; pos >= 0; --pos) {
65
            if (this.contenedor[pos].equals(object)) {
66
                return this.count - pos;
67
            }
68
        }
69
        return -1;
70
    }
71
    // Devuelve el número de componentes de este vector.
72
 
73
    public int size() {
74
        return this.count;
75
    }
76
 
77
    // métodos básicos de la región Override Object
78
79
    public String toString() {
80
 
81
        if (this.size() <=0) {
82
            return "";
83
        }
84
 
85
        StringBuilder sb = new StringBuilder();
86
        sb.append("[" + this.contenedor[0].toString());
87
        for (int i = 1; i < this.size(); ++i) {
88
            sb.append(", " + this.contenedor[i].toString());
89
        }
90
        sb.append("]");
91
        return sb.toString();
92
    }
93
    
94
    public  int eliminarElemento(String elemento){
95
        int contador=0;
96
        for (int i=0;i<this.count ;i++){
97
            if (this.contenedor[i].equals(elemento)){
98
                this.contenedor[i]=this.contenedor[i+1];
99
                contador++;
100
            }
101
        }
102
        count=count-contador;
103
        return contador;
104
        
105
        
106
107
        
108
109
    }
110
111
    public static Integer getDimencion() {
112
        return dimencion;
113
    }
114
115
    public Generico[] getContenedor() {
116
        return contenedor;
117
    }
118
119
    public void setContenedor(Generico[] contenedor) {
120
        this.contenedor = contenedor;
121
    }
122
123
    public Integer getCount() {
124
        return count;
125
    }
126
127
    public void setCount(Integer count) {
128
        this.count = count;
129
    }
130
    
131
    
132
}